eslint-plugin-perfectionist
Advanced tools
Comparing version 4.6.0 to 4.7.0
@@ -32,3 +32,3 @@ 'use strict' | ||
'sort-object-types': sortObjectTypes.default, | ||
'sort-union-types': sortUnionTypes, | ||
'sort-union-types': sortUnionTypes.default, | ||
'sort-switch-case': sortSwitchCase, | ||
@@ -35,0 +35,0 @@ 'sort-decorators': sortDecorators, |
@@ -7,3 +7,5 @@ 'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const types = require('./sort-array-includes/types.js') | ||
@@ -16,18 +18,12 @@ const getCustomGroupsCompareOptions = require('../utils/get-custom-groups-compare-options.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const isSortable = require('../utils/is-sortable.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
@@ -39,2 +35,3 @@ let defaultOptions = { | ||
partitionByNewLine: false, | ||
newlinesBetween: 'ignore', | ||
useConfigurationIf: {}, | ||
@@ -52,7 +49,3 @@ type: 'alphabetical', | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the array members into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
groupKind: { | ||
@@ -68,9 +61,6 @@ enum: ['mixed', 'literals-first', 'spreads-first'], | ||
type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -98,2 +88,6 @@ additionalProperties: false, | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: | ||
'missedSpacingBetweenArrayIncludesMembers', | ||
extraSpacingBetweenMembers: | ||
'extraSpacingBetweenArrayIncludesMembers', | ||
unexpectedGroupOrder: 'unexpectedArrayIncludesGroupOrder', | ||
@@ -112,2 +106,6 @@ unexpectedOrder: 'unexpectedArrayIncludesOrder', | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
missedSpacingBetweenArrayIncludesMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" members.', | ||
extraSpacingBetweenArrayIncludesMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" members.', | ||
unexpectedArrayIncludesOrder: | ||
@@ -154,2 +152,3 @@ 'Expected "{{right}}" to come before "{{left}}".', | ||
} | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration({ | ||
@@ -161,2 +160,5 @@ customGroups: options.customGroups, | ||
}) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
@@ -219,16 +221,8 @@ ruleName: context.id, | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: element, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -257,3 +251,3 @@ accumulator.push([]) | ||
) | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
filteredGroupKindNodes.flatMap(groupedNodes => | ||
@@ -269,40 +263,9 @@ sortNodesByGroups.sortNodesByGroups(groupedNodes, options, { | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
messageId: | ||
leftNumber === rightNumber | ||
? availableMessageIds.unexpectedOrder | ||
: availableMessageIds.unexpectedGroupOrder, | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
sortNodesExcludingEslintDisabled, | ||
availableMessageIds, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -309,0 +272,0 @@ } |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
@@ -11,15 +10,12 @@ const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const doesCustomGroupMatch = require('./sort-classes/does-custom-group-match.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -29,5 +25,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const matches = require('../utils/matches.js') | ||
@@ -238,3 +232,3 @@ let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
(accumulator, member) => { | ||
var _a2, _b, _c, _d, _e | ||
var _a2, _b, _c | ||
let name | ||
@@ -278,123 +272,141 @@ let dependencies = [] | ||
let addSafetySemicolonWhenInline = true | ||
if ( | ||
member.type === 'MethodDefinition' || | ||
member.type === 'TSAbstractMethodDefinition' | ||
) { | ||
if (member.static) { | ||
modifiers.push('static') | ||
switch (member.type) { | ||
case 'TSAbstractMethodDefinition': | ||
case 'MethodDefinition': { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.type === 'TSAbstractMethodDefinition') { | ||
modifiers.push('abstract') | ||
} else if (!node.parent.declare) { | ||
addSafetySemicolonWhenInline = false | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
} | ||
if (member.override) { | ||
modifiers.push('override') | ||
} | ||
if (member.accessibility === 'protected') { | ||
modifiers.push('protected') | ||
} else if (member.accessibility === 'private' || isPrivateHash) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
if (member.optional) { | ||
modifiers.push('optional') | ||
} | ||
if (member.value.async) { | ||
modifiers.push('async') | ||
} | ||
if (member.kind === 'constructor') { | ||
selectors.push('constructor') | ||
} | ||
if (member.kind === 'get') { | ||
selectors.push('get-method') | ||
} | ||
if (member.kind === 'set') { | ||
selectors.push('set-method') | ||
} | ||
selectors.push('method') | ||
break | ||
} | ||
if (member.type === 'TSAbstractMethodDefinition') { | ||
modifiers.push('abstract') | ||
} else if (!node.parent.declare) { | ||
addSafetySemicolonWhenInline = false | ||
case 'TSAbstractAccessorProperty': | ||
case 'AccessorProperty': { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.type === 'TSAbstractAccessorProperty') { | ||
modifiers.push('abstract') | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
} | ||
if (member.override) { | ||
modifiers.push('override') | ||
} | ||
if (member.accessibility === 'protected') { | ||
modifiers.push('protected') | ||
} else if (member.accessibility === 'private' || isPrivateHash) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
selectors.push('accessor-property') | ||
break | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
case 'TSIndexSignature': { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.readonly) { | ||
modifiers.push('readonly') | ||
} | ||
selectors.push('index-signature') | ||
break | ||
} | ||
if (member.override) { | ||
modifiers.push('override') | ||
case 'StaticBlock': { | ||
addSafetySemicolonWhenInline = false | ||
selectors.push('static-block') | ||
dependencies = extractDependencies(member, true) | ||
break | ||
} | ||
if (member.accessibility === 'protected') { | ||
modifiers.push('protected') | ||
} else if (member.accessibility === 'private' || isPrivateHash) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
if (member.optional) { | ||
modifiers.push('optional') | ||
} | ||
if (member.value.async) { | ||
modifiers.push('async') | ||
} | ||
if (member.kind === 'constructor') { | ||
selectors.push('constructor') | ||
} | ||
if (member.kind === 'get') { | ||
selectors.push('get-method') | ||
} | ||
if (member.kind === 'set') { | ||
selectors.push('set-method') | ||
} | ||
selectors.push('method') | ||
} else if (member.type === 'TSIndexSignature') { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.readonly) { | ||
modifiers.push('readonly') | ||
} | ||
selectors.push('index-signature') | ||
} else if (member.type === 'StaticBlock') { | ||
addSafetySemicolonWhenInline = false | ||
selectors.push('static-block') | ||
dependencies = extractDependencies(member, true) | ||
} else if ( | ||
member.type === 'AccessorProperty' || | ||
member.type === 'TSAbstractAccessorProperty' | ||
) { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.type === 'TSAbstractAccessorProperty') { | ||
modifiers.push('abstract') | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
} | ||
if (member.override) { | ||
modifiers.push('override') | ||
} | ||
if (member.accessibility === 'protected') { | ||
modifiers.push('protected') | ||
} else if (member.accessibility === 'private' || isPrivateHash) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
selectors.push('accessor-property') | ||
} else { | ||
if (member.static) { | ||
modifiers.push('static') | ||
} | ||
if (member.declare) { | ||
modifiers.push('declare') | ||
} | ||
if (member.type === 'TSAbstractPropertyDefinition') { | ||
modifiers.push('abstract') | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
} | ||
if (member.override) { | ||
modifiers.push('override') | ||
} | ||
if (member.readonly) { | ||
modifiers.push('readonly') | ||
} | ||
if (member.accessibility === 'protected') { | ||
modifiers.push('protected') | ||
} else if (member.accessibility === 'private' || isPrivateHash) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
if (member.optional) { | ||
modifiers.push('optional') | ||
} | ||
if ( | ||
((_b = member.value) == null ? void 0 : _b.type) === | ||
'ArrowFunctionExpression' || | ||
((_c = member.value) == null ? void 0 : _c.type) === | ||
'FunctionExpression' | ||
) { | ||
if (member.value.async) { | ||
modifiers.push('async') | ||
default: { | ||
if ('static' in member && member.static) { | ||
modifiers.push('static') | ||
} | ||
selectors.push('function-property') | ||
} else if (member.value) { | ||
memberValue = sourceCode.getText(member.value) | ||
dependencies = extractDependencies(member.value, member.static) | ||
if ('declare' in member && member.declare) { | ||
modifiers.push('declare') | ||
} | ||
if (member.type === 'TSAbstractPropertyDefinition') { | ||
modifiers.push('abstract') | ||
} | ||
if (decorated) { | ||
modifiers.push('decorated') | ||
} | ||
if ('override' in member && member.override) { | ||
modifiers.push('override') | ||
} | ||
if ('readonly' in member && member.readonly) { | ||
modifiers.push('readonly') | ||
} | ||
if ( | ||
'accessibility' in member && | ||
member.accessibility === 'protected' | ||
) { | ||
modifiers.push('protected') | ||
} else if ( | ||
('accessibility' in member && | ||
member.accessibility === 'private') || | ||
isPrivateHash | ||
) { | ||
modifiers.push('private') | ||
} else { | ||
modifiers.push('public') | ||
} | ||
if ('optional' in member && member.optional) { | ||
modifiers.push('optional') | ||
} | ||
if ('value' in member && member.value) { | ||
if ( | ||
member.value.type === 'ArrowFunctionExpression' || | ||
member.value.type === 'FunctionExpression' | ||
) { | ||
if (member.value.async) { | ||
modifiers.push('async') | ||
} | ||
selectors.push('function-property') | ||
} else { | ||
memberValue = sourceCode.getText(member.value) | ||
} | ||
if (member.value.type !== 'TSEmptyBodyFunctionExpression') { | ||
dependencies = extractDependencies( | ||
member.value, | ||
member.static, | ||
) | ||
} | ||
} | ||
selectors.push('property') | ||
} | ||
selectors.push('property') | ||
} | ||
@@ -428,7 +440,7 @@ let predefinedGroups = | ||
let overloadSignatureGroupMember = | ||
(_d = overloadSignatureGroups.find(overloadSignatures => | ||
(_b = overloadSignatureGroups.find(overloadSignatures => | ||
overloadSignatures.includes(member), | ||
)) == null | ||
? void 0 | ||
: _d.at(-1) | ||
: _b.at(-1) | ||
let sortingNode = { | ||
@@ -458,18 +470,10 @@ dependencyName: getDependencyName({ | ||
} | ||
let lastMember = | ||
(_e = accumulator.at(-1)) == null ? void 0 : _e.at(-1) | ||
let lastSortingNode = | ||
(_c = accumulator.at(-1)) == null ? void 0 : _c.at(-1) | ||
if ( | ||
(options.partitionByNewLine && | ||
lastMember && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastMember, | ||
sortingNode, | ||
)) || | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: member, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
@@ -484,3 +488,3 @@ ) { | ||
) | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
@@ -504,76 +508,16 @@ formattedNodes.flatMap(nodes2 => | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodes = formattedNodes.flat() | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let indexOfLeft = sortedNodes.indexOf(left) | ||
let indexOfRight = sortedNodes.indexOf(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
let firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn(right, nodes) | ||
if ( | ||
firstUnorderedNodeDependentOnRight || | ||
indexOfLeft > indexOfRight || | ||
indexOfLeft >= indexOfRightExcludingEslintDisabled | ||
) { | ||
if (firstUnorderedNodeDependentOnRight) { | ||
messageIds.push('unexpectedClassesDependencyOrder') | ||
} else { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedClassesOrder' | ||
: 'unexpectedClassesGroupOrder', | ||
) | ||
} | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenClassMembers', | ||
extraSpacingError: 'extraSpacingBetweenClassMembers', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
nodeDependentOnRight: | ||
firstUnorderedNodeDependentOnRight == null | ||
? void 0 | ||
: firstUnorderedNodeDependentOnRight.name, | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenClassMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenClassMembers', | ||
unexpectedDependencyOrder: 'unexpectedClassesDependencyOrder', | ||
unexpectedGroupOrder: 'unexpectedClassesGroupOrder', | ||
unexpectedOrder: 'unexpectedClassesOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -586,2 +530,3 @@ }, | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
ignoreCallbackDependenciesPatterns: { | ||
@@ -595,19 +540,10 @@ description: | ||
}, | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows to use comments to separate the class members into logical groups.', | ||
}, | ||
customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: types.singleCustomGroupJsonSchema, | ||
}), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -614,0 +550,0 @@ additionalProperties: false, |
@@ -29,6 +29,2 @@ 'use strict' | ||
let singleCustomGroupJsonSchema = { | ||
elementValuePattern: { | ||
description: 'Element value pattern filter for properties.', | ||
type: 'string', | ||
}, | ||
decoratorNamePattern: { | ||
@@ -41,2 +37,3 @@ description: 'Decorator name pattern filter.', | ||
selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors), | ||
elementValuePattern: commonJsonSchemas.elementValuePatternJsonSchema, | ||
elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema, | ||
@@ -43,0 +40,0 @@ } |
@@ -8,11 +8,8 @@ 'use strict' | ||
const getDecoratorName = require('./sort-decorators/get-decorator-name.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const getNodeDecorators = require('../utils/get-node-decorators.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -22,5 +19,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -47,7 +42,3 @@ specialCharacters: 'keep', | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the decorators into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
sortOnParameters: { | ||
@@ -78,10 +69,6 @@ description: | ||
}, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -184,13 +171,3 @@ additionalProperties: false, | ||
(accumulator, decorator) => { | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: decorator, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) | ||
) { | ||
accumulator.push([]) | ||
} | ||
var _a | ||
let { setCustomGroups, getGroup } = useGroups.useGroups(options) | ||
@@ -209,2 +186,14 @@ let name = getDecoratorName.getDecoratorName(decorator) | ||
} | ||
let lastSortingNode = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
if ( | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
accumulator.push([]) | ||
} | ||
accumulator.at(-1).push(sortingNode) | ||
@@ -221,44 +210,16 @@ return accumulator | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodes = formattedMembers.flat() | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
ignoreFirstNodeHighestBlockComment: true, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
messageId: | ||
leftNumber === rightNumber | ||
? 'unexpectedDecoratorsOrder' | ||
: 'unexpectedDecoratorsGroupOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedGroupOrder: 'unexpectedDecoratorsGroupOrder', | ||
unexpectedOrder: 'unexpectedDecoratorsOrder', | ||
}, | ||
ignoreFirstNodeHighestBlockComment: true, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
} | ||
module.exports = sortDecorators |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getEnumMembers = require('../utils/get-enum-members.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -19,5 +16,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -60,3 +55,3 @@ partitionByComment: false, | ||
let stack = [expression] | ||
while (stack.length) { | ||
while (stack.length > 0) { | ||
let node = stack.pop() | ||
@@ -114,16 +109,8 @@ if ( | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: member, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -137,4 +124,4 @@ accumulator.push([]) | ||
) | ||
let sortingNodes = formattedMembers.flat() | ||
let isNumericEnum = sortingNodes.every( | ||
let nodes = formattedMembers.flat() | ||
let isNumericEnum = nodes.every( | ||
sortingNode => | ||
@@ -181,6 +168,6 @@ sortingNode.numericValue !== null && | ||
} | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
formattedMembers.flatMap(nodes => | ||
sortNodes.sortNodes(nodes, compareOptions, { | ||
formattedMembers.flatMap(sortingNodes => | ||
sortNodes.sortNodes(sortingNodes, compareOptions, { | ||
ignoreEslintDisabledNodes, | ||
@@ -193,44 +180,12 @@ }), | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(sortingNodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
let firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn( | ||
right, | ||
sortingNodes, | ||
) | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
nodes: sortingNodes, | ||
sourceCode, | ||
options, | ||
fixer, | ||
}), | ||
data: { | ||
nodeDependentOnRight: | ||
firstUnorderedNodeDependentOnRight == null | ||
? void 0 | ||
: firstUnorderedNodeDependentOnRight.name, | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
}, | ||
messageId: firstUnorderedNodeDependentOnRight | ||
? 'unexpectedEnumsDependencyOrder' | ||
: 'unexpectedEnumsOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedDependencyOrder: 'unexpectedEnumsDependencyOrder', | ||
unexpectedOrder: 'unexpectedEnumsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -243,7 +198,3 @@ }, | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the members of enums into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
forceNumericSort: { | ||
@@ -258,9 +209,5 @@ description: | ||
}, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -267,0 +214,0 @@ additionalProperties: false, |
@@ -6,7 +6,5 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -16,5 +14,3 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -41,3 +37,2 @@ specialCharacters: 'keep', | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let partitionComment = options.partitionByComment | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
@@ -63,13 +58,8 @@ ruleName: context.id, | ||
if ( | ||
(partitionComment && | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
sourceCode, | ||
node, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
})) || | ||
(options.partitionByNewLine && | ||
lastNode && | ||
getLinesBetween.getLinesBetween(sourceCode, lastNode, sortingNode)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode: lastNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -103,33 +93,11 @@ parts.push([]) | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
messageId: 'unexpectedExportsOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedOrder: 'unexpectedExportsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -150,7 +118,3 @@ } | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the exports into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
groupKind: { | ||
@@ -161,9 +125,5 @@ enum: ['mixed', 'values-first', 'types-first'], | ||
}, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -170,0 +130,0 @@ additionalProperties: false, |
@@ -7,8 +7,6 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -18,5 +16,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -37,10 +33,6 @@ specialCharacters: 'keep', | ||
properties: { | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -116,39 +108,12 @@ additionalProperties: false, | ||
}) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
context.report({ | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
fixer, | ||
nodes, | ||
}), | ||
messageId: | ||
leftNumber === rightNumber | ||
? 'unexpectedHeritageClausesOrder' | ||
: 'unexpectedHeritageClausesGroupOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedGroupOrder: 'unexpectedHeritageClausesGroupOrder', | ||
unexpectedOrder: 'unexpectedHeritageClausesOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -155,0 +120,0 @@ } |
@@ -12,11 +12,6 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -27,5 +22,3 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const matches = require('../utils/matches.js') | ||
@@ -132,3 +125,3 @@ const sortImports = createEslintRule.createEslintRule({ | ||
}) | ||
let nodes = [] | ||
let sortingNodes = [] | ||
let isSideEffectImport = node => | ||
@@ -187,3 +180,2 @@ node.type === 'ImportDeclaration' && | ||
let matchesInternalPattern = value => | ||
options.internalPattern.length && | ||
options.internalPattern.some(pattern => matches.matches(value, pattern)) | ||
@@ -324,3 +316,3 @@ let isCoreModule = value => { | ||
} | ||
nodes.push({ | ||
sortingNodes.push({ | ||
isIgnored: | ||
@@ -351,24 +343,16 @@ !options.sortSideEffects && | ||
let hasContentBetweenNodes = (left, right) => | ||
!!sourceCode.getTokensBetween(left.node, right.node, { | ||
sourceCode.getTokensBetween(left.node, right.node, { | ||
includeComments: false, | ||
}).length | ||
}).length > 0 | ||
let formattedMembers = [[]] | ||
for (let sortingNode of nodes) { | ||
for (let sortingNode of sortingNodes) { | ||
let lastGroup = formattedMembers.at(-1) | ||
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1) | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: sortingNode.node, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) || | ||
(lastSortingNode && | ||
@@ -382,5 +366,5 @@ hasContentBetweenNodes(lastSortingNode, sortingNode)) | ||
} | ||
for (let nodeList of formattedMembers) { | ||
for (let nodes of formattedMembers) { | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByGroups.sortNodesByGroups(nodeList, options, { | ||
sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
getGroupCompareOptions: groupNumber => { | ||
@@ -396,74 +380,17 @@ if (options.sortSideEffects) { | ||
}) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodeList, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber( | ||
options.groups, | ||
right, | ||
) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
if ( | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedImportsOrder' | ||
: 'unexpectedImportsGroupOrder', | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
options: { | ||
...options, | ||
customGroups: [], | ||
}, | ||
missedSpacingError: 'missedSpacingBetweenImports', | ||
extraSpacingError: 'extraSpacingBetweenImports', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
nodes: nodeList, | ||
sourceCode, | ||
options, | ||
fixer, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
options: { | ||
...options, | ||
customGroups: [], | ||
}, | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
nodes: nodeList, | ||
sourceCode, | ||
fixer, | ||
}), | ||
], | ||
data: { | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenImports', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenImports', | ||
unexpectedGroupOrder: 'unexpectedImportsGroupOrder', | ||
unexpectedOrder: 'unexpectedImportsOrder', | ||
}, | ||
options: { | ||
...options, | ||
customGroups: [], | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
context, | ||
nodes, | ||
}) | ||
@@ -494,2 +421,3 @@ } | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
customGroups: { | ||
@@ -510,7 +438,2 @@ properties: { | ||
}, | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the interface properties into logical groups.', | ||
}, | ||
internalPattern: { | ||
@@ -543,11 +466,7 @@ description: 'Specifies the pattern for internal modules.', | ||
}, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -554,0 +473,0 @@ definitions: { |
@@ -85,7 +85,7 @@ 'use strict' | ||
typescriptImport.convertCompilerOptionsFromJson( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
// eslint-disable-next-line typescript/no-unsafe-member-access | ||
parsedContent.raw.config.compilerOptions, | ||
path__namespace.dirname(filePath), | ||
) | ||
if (compilerOptionsConverted.errors.length) { | ||
if (compilerOptionsConverted.errors.length > 0) { | ||
throw new Error( | ||
@@ -92,0 +92,0 @@ `Error getting compiler options: ${JSON.stringify( |
@@ -26,5 +26,5 @@ 'use strict' | ||
missedSpacingBetweenInterfaceMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" interfaces.', | ||
'Missed spacing between "{{left}}" and "{{right}}" properties.', | ||
extraSpacingBetweenInterfaceMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" interfaces.', | ||
'Extra spacing between "{{left}}" and "{{right}}" properties.', | ||
unexpectedInterfacePropertiesOrder: | ||
@@ -31,0 +31,0 @@ 'Expected "{{right}}" to come before "{{left}}".', |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const sortUnionTypes = require('./sort-union-types.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -38,230 +17,3 @@ specialCharacters: 'keep', | ||
const sortIntersectionTypes = createEslintRule.createEslintRule({ | ||
create: context => ({ | ||
TSIntersectionType: node => { | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
[ | ||
'intersection', | ||
'conditional', | ||
'function', | ||
'operator', | ||
'keyword', | ||
'literal', | ||
'nullish', | ||
'unknown', | ||
'import', | ||
'object', | ||
'named', | ||
'tuple', | ||
'union', | ||
], | ||
[], | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = node.types.reduce( | ||
(accumulator, type) => { | ||
let { defineGroup, getGroup } = useGroups.useGroups(options) | ||
switch (type.type) { | ||
case 'TSTemplateLiteralType': | ||
case 'TSLiteralType': | ||
defineGroup('literal') | ||
break | ||
case 'TSIndexedAccessType': | ||
case 'TSTypeReference': | ||
case 'TSQualifiedName': | ||
case 'TSArrayType': | ||
case 'TSInferType': | ||
defineGroup('named') | ||
break | ||
case 'TSIntersectionType': | ||
defineGroup('intersection') | ||
break | ||
case 'TSUndefinedKeyword': | ||
case 'TSNullKeyword': | ||
case 'TSVoidKeyword': | ||
defineGroup('nullish') | ||
break | ||
case 'TSConditionalType': | ||
defineGroup('conditional') | ||
break | ||
case 'TSConstructorType': | ||
case 'TSFunctionType': | ||
defineGroup('function') | ||
break | ||
case 'TSBooleanKeyword': | ||
case 'TSUnknownKeyword': | ||
case 'TSBigIntKeyword': | ||
case 'TSNumberKeyword': | ||
case 'TSObjectKeyword': | ||
case 'TSStringKeyword': | ||
case 'TSSymbolKeyword': | ||
case 'TSNeverKeyword': | ||
case 'TSAnyKeyword': | ||
case 'TSThisType': | ||
defineGroup('keyword') | ||
break | ||
case 'TSTypeOperator': | ||
case 'TSTypeQuery': | ||
defineGroup('operator') | ||
break | ||
case 'TSTypeLiteral': | ||
case 'TSMappedType': | ||
defineGroup('object') | ||
break | ||
case 'TSImportType': | ||
defineGroup('import') | ||
break | ||
case 'TSTupleType': | ||
defineGroup('tuple') | ||
break | ||
case 'TSUnionType': | ||
defineGroup('union') | ||
break | ||
} | ||
let lastGroup = accumulator.at(-1) | ||
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1) | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
type, | ||
eslintDisabledLines, | ||
), | ||
size: rangeToDiff.rangeToDiff(type, sourceCode), | ||
name: sourceCode.getText(type), | ||
group: getGroup(), | ||
node: type, | ||
} | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
tokenValueToIgnoreBefore: '&', | ||
node: type, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
) { | ||
lastGroup = [] | ||
accumulator.push(lastGroup) | ||
} | ||
lastGroup == null ? void 0 : lastGroup.push(sortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
for (let nodes of formattedMembers) { | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
ignoreEslintDisabledNodes, | ||
}) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
if ( | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedIntersectionTypesOrder' | ||
: 'unexpectedIntersectionTypesGroupOrder', | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenIntersectionTypes', | ||
extraSpacingError: 'extraSpacingBetweenIntersectionTypes', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
}) | ||
} | ||
}, | ||
}), | ||
meta: { | ||
schema: [ | ||
{ | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the intersection types members into logical groups.', | ||
}, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
messages: { | ||
@@ -282,5 +34,21 @@ unexpectedIntersectionTypesGroupOrder: | ||
}, | ||
schema: [sortUnionTypes.jsonSchema], | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
TSIntersectionType: node => { | ||
sortUnionTypes.sortUnionOrIntersectionTypes({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenIntersectionTypes', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenIntersectionTypes', | ||
unexpectedGroupOrder: 'unexpectedIntersectionTypesGroupOrder', | ||
unexpectedOrder: 'unexpectedIntersectionTypesOrder', | ||
}, | ||
tokenValueToIgnoreBefore: '&', | ||
context, | ||
node, | ||
}) | ||
}, | ||
}), | ||
defaultOptions: [defaultOptions], | ||
@@ -287,0 +55,0 @@ name: 'sort-intersection-types', |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
@@ -7,6 +8,6 @@ const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -17,4 +18,2 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const complete = require('../utils/complete.js') | ||
@@ -24,2 +23,4 @@ const matches = require('../utils/matches.js') | ||
specialCharacters: 'keep', | ||
newlinesBetween: 'ignore', | ||
partitionByNewLine: false, | ||
type: 'alphabetical', | ||
@@ -52,5 +53,8 @@ ignorePattern: [], | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let shouldIgnore = false | ||
if (options.ignorePattern.length) { | ||
if (options.ignorePattern.length > 0) { | ||
let tagName = sourceCode.getText(node.openingElement.name) | ||
@@ -71,4 +75,5 @@ shouldIgnore = options.ignorePattern.some(pattern => | ||
}) | ||
let parts = node.openingElement.attributes.reduce( | ||
let formattedMembers = node.openingElement.attributes.reduce( | ||
(accumulator, attribute) => { | ||
var _a | ||
if (attribute.type === 'JSXSpreadAttribute') { | ||
@@ -90,3 +95,3 @@ accumulator.push([]) | ||
} | ||
let jsxNode = { | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
@@ -101,3 +106,15 @@ attribute, | ||
} | ||
accumulator.at(-1).push(jsxNode) | ||
let lastSortingNode = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
if ( | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
accumulator.push([]) | ||
} | ||
accumulator.at(-1).push(sortingNode) | ||
return accumulator | ||
@@ -107,3 +124,3 @@ }, | ||
) | ||
for (let nodes of parts) { | ||
for (let nodes of formattedMembers) { | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
@@ -113,39 +130,14 @@ sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
}) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
messageId: | ||
leftNumber === rightNumber | ||
? 'unexpectedJSXPropsOrder' | ||
: 'unexpectedJSXPropsGroupOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenJSXPropsMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenJSXPropsMembers', | ||
unexpectedGroupOrder: 'unexpectedJSXPropsGroupOrder', | ||
unexpectedOrder: 'unexpectedJSXPropsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -159,2 +151,3 @@ } | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
ignorePattern: { | ||
@@ -168,10 +161,7 @@ description: | ||
}, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -185,2 +175,6 @@ additionalProperties: false, | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
missedSpacingBetweenJSXPropsMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" props.', | ||
extraSpacingBetweenJSXPropsMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" props.', | ||
unexpectedJSXPropsOrder: | ||
@@ -187,0 +181,0 @@ 'Expected "{{right}}" to come before "{{left}}".', |
'use strict' | ||
const types = require('@typescript-eslint/types') | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const getCustomGroupsCompareOptions = require('../utils/get-custom-groups-compare-options.js') | ||
const getMatchingContextOptions = require('../utils/get-matching-context-options.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const doesCustomGroupMatch = require('./sort-maps/does-custom-group-match.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const types$1 = require('./sort-maps/types.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const isSortable = require('../utils/is-sortable.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -24,7 +26,11 @@ specialCharacters: 'keep', | ||
partitionByNewLine: false, | ||
newlinesBetween: 'ignore', | ||
useConfigurationIf: {}, | ||
type: 'alphabetical', | ||
ignoreCase: true, | ||
customGroups: [], | ||
locales: 'en-US', | ||
alphabet: '', | ||
order: 'asc', | ||
groups: [], | ||
} | ||
@@ -38,3 +44,3 @@ const sortMaps = createEslintRule.createEslintRule({ | ||
node.callee.name !== 'Map' || | ||
!node.arguments.length || | ||
node.arguments.length === 0 || | ||
((_a = node.arguments[0]) == null ? void 0 : _a.type) !== | ||
@@ -49,5 +55,17 @@ 'ArrayExpression' | ||
} | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let settings = getSettings.getSettings(context.settings) | ||
let matchedContextOptions = | ||
getMatchingContextOptions.getMatchingContextOptions({ | ||
nodeNames: elements | ||
.filter( | ||
element => | ||
element !== null && | ||
element.type !== types.TSESTree.AST_NODE_TYPES.SpreadElement, | ||
) | ||
.map(element => getNodeName({ sourceCode, element })), | ||
contextOptions: context.options, | ||
}) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
matchedContextOptions[0], | ||
settings, | ||
@@ -57,3 +75,10 @@ defaultOptions, | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration( | ||
{ | ||
customGroups: options.customGroups, | ||
groups: options.groups, | ||
selectors: [], | ||
modifiers: [], | ||
}, | ||
) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
@@ -77,17 +102,22 @@ ruleName: context.id, | ||
for (let element of part) { | ||
let name | ||
if (element.type === 'ArrayExpression') { | ||
let [left] = element.elements | ||
if (!left) { | ||
name = `${left}` | ||
} else if (left.type === 'Literal') { | ||
name = left.raw | ||
} else { | ||
name = sourceCode.getText(left) | ||
let name = getNodeName({ | ||
sourceCode, | ||
element, | ||
}) | ||
let lastSortingNode = | ||
(_b = formattedMembers.at(-1)) == null ? void 0 : _b.at(-1) | ||
let { defineGroup, getGroup } = useGroups.useGroups(options) | ||
for (let customGroup of options.customGroups) { | ||
if ( | ||
doesCustomGroupMatch.doesCustomGroupMatch({ | ||
elementName: name, | ||
customGroup, | ||
}) | ||
) { | ||
defineGroup(customGroup.groupName, true) | ||
if (getGroup() === customGroup.groupName) { | ||
break | ||
} | ||
} | ||
} else { | ||
name = sourceCode.getText(element) | ||
} | ||
let lastSortingNode = | ||
(_b = formattedMembers.at(-1)) == null ? void 0 : _b.at(-1) | ||
let sortingNode = { | ||
@@ -99,2 +129,3 @@ isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
size: rangeToDiff.rangeToDiff(element, sourceCode), | ||
group: getGroup(), | ||
node: element, | ||
@@ -104,16 +135,8 @@ name, | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: element, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -126,34 +149,24 @@ formattedMembers.push([]) | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodes.sortNodes(nodes, options, { ignoreEslintDisabledNodes }) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
getGroupCompareOptions: groupNumber => | ||
getCustomGroupsCompareOptions.getCustomGroupsCompareOptions( | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
}, | ||
messageId: 'unexpectedMapElementsOrder', | ||
node: right.node, | ||
groupNumber, | ||
), | ||
ignoreEslintDisabledNodes, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: | ||
'missedSpacingBetweenMapElementsMembers', | ||
extraSpacingBetweenMembers: | ||
'extraSpacingBetweenMapElementsMembers', | ||
unexpectedGroupOrder: 'unexpectedMapElementsGroupOrder', | ||
unexpectedOrder: 'unexpectedMapElementsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -165,17 +178,16 @@ } | ||
meta: { | ||
schema: [ | ||
{ | ||
schema: { | ||
items: { | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the maps members into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: types$1.singleCustomGroupJsonSchema, | ||
}), | ||
useConfigurationIf: | ||
commonJsonSchemas.buildUseConfigurationIfJsonSchema(), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
}, | ||
@@ -185,3 +197,15 @@ additionalProperties: false, | ||
}, | ||
], | ||
uniqueItems: true, | ||
type: 'array', | ||
}, | ||
messages: { | ||
unexpectedMapElementsGroupOrder: | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
missedSpacingBetweenMapElementsMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" members.', | ||
extraSpacingBetweenMapElementsMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" members.', | ||
unexpectedMapElementsOrder: | ||
'Expected "{{right}}" to come before "{{left}}".', | ||
}, | ||
docs: { | ||
@@ -192,6 +216,2 @@ url: 'https://perfectionist.dev/rules/sort-maps', | ||
}, | ||
messages: { | ||
unexpectedMapElementsOrder: | ||
'Expected "{{right}}" to come before "{{left}}".', | ||
}, | ||
type: 'suggestion', | ||
@@ -203,2 +223,14 @@ fixable: 'code', | ||
}) | ||
let getNodeName = ({ sourceCode, element }) => { | ||
if (element.type === 'ArrayExpression') { | ||
let [left] = element.elements | ||
if (!left) { | ||
return `${left}` | ||
} else if (left.type === 'Literal') { | ||
return left.raw | ||
} | ||
return sourceCode.getText(left) | ||
} | ||
return sourceCode.getText(element) | ||
} | ||
module.exports = sortMaps |
'use strict' | ||
const utils = require('@typescript-eslint/utils') | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
@@ -11,18 +10,14 @@ const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const doesCustomGroupMatch = require('./sort-modules/does-custom-group-match.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const getNodeDecorators = require('../utils/get-node-decorators.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const getEnumMembers = require('../utils/get-enum-members.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -32,5 +27,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
@@ -65,22 +58,25 @@ let defaultOptions = { | ||
meta: { | ||
messages: { | ||
unexpectedModulesGroupOrder: | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
unexpectedModulesDependencyOrder: | ||
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', | ||
missedSpacingBetweenModulesMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" objects.', | ||
extraSpacingBetweenModulesMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" objects.', | ||
unexpectedModulesOrder: 'Expected "{{right}}" to come before "{{left}}".', | ||
}, | ||
schema: [ | ||
{ | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows to use comments to separate the modules members into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: types.singleCustomGroupJsonSchema, | ||
}), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -91,13 +87,2 @@ additionalProperties: false, | ||
], | ||
messages: { | ||
unexpectedModulesGroupOrder: | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
unexpectedModulesDependencyOrder: | ||
'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".', | ||
missedSpacingBetweenModulesMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" objects.', | ||
extraSpacingBetweenModulesMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" objects.', | ||
unexpectedModulesOrder: 'Expected "{{right}}" to come before "{{left}}".', | ||
}, | ||
docs: { | ||
@@ -308,15 +293,7 @@ url: 'https://perfectionist.dev/rules/sort-modules', | ||
if ( | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) || | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
sourceCode, | ||
node, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
@@ -328,3 +305,3 @@ ) { | ||
} | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
@@ -348,77 +325,16 @@ formattedNodes.flatMap(nodes2 => | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodes = formattedNodes.flat() | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
let firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn(right, nodes) | ||
if ( | ||
firstUnorderedNodeDependentOnRight || | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
if (firstUnorderedNodeDependentOnRight) { | ||
messageIds.push('unexpectedModulesDependencyOrder') | ||
} else { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedModulesOrder' | ||
: 'unexpectedModulesGroupOrder', | ||
) | ||
} | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenModulesMembers', | ||
extraSpacingError: 'extraSpacingBetweenModulesMembers', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
nodeDependentOnRight: | ||
firstUnorderedNodeDependentOnRight == null | ||
? void 0 | ||
: firstUnorderedNodeDependentOnRight.name, | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenModulesMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenModulesMembers', | ||
unexpectedDependencyOrder: 'unexpectedModulesDependencyOrder', | ||
unexpectedGroupOrder: 'unexpectedModulesGroupOrder', | ||
unexpectedOrder: 'unexpectedModulesOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -425,0 +341,0 @@ } |
@@ -6,7 +6,5 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -17,5 +15,3 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -73,16 +69,8 @@ specialCharacters: 'keep', | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: specifier, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -114,33 +102,11 @@ formattedMembers.push([]) | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
messageId: 'unexpectedNamedExportsOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedOrder: 'unexpectedNamedExportsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -154,7 +120,3 @@ } | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the named exports members into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
groupKind: { | ||
@@ -165,9 +127,5 @@ enum: ['mixed', 'values-first', 'types-first'], | ||
}, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -174,0 +132,0 @@ additionalProperties: false, |
@@ -6,7 +6,5 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -17,5 +15,3 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -82,16 +78,8 @@ specialCharacters: 'keep', | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: specifier, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -123,33 +111,11 @@ formattedMembers.push([]) | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
messageId: 'unexpectedNamedImportsOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedOrder: 'unexpectedNamedImportsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -163,7 +129,3 @@ } | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the named imports members into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
groupKind: { | ||
@@ -178,9 +140,5 @@ enum: ['mixed', 'values-first', 'types-first'], | ||
}, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -187,0 +145,0 @@ additionalProperties: false, |
@@ -18,21 +18,13 @@ 'use strict' | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const isNodeFunctionType = require('../utils/is-node-function-type.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const isSortable = require('../utils/is-sortable.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const matches = require('../utils/matches.js') | ||
@@ -59,2 +51,3 @@ let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
ignorePattern: { | ||
@@ -75,7 +68,2 @@ description: | ||
}), | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate members into logical groups.', | ||
}, | ||
customGroups: { | ||
@@ -95,10 +83,6 @@ oneOf: [ | ||
type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -117,5 +101,5 @@ additionalProperties: false, | ||
missedSpacingBetweenObjectTypeMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" types.', | ||
'Missed spacing between "{{left}}" and "{{right}}" properties.', | ||
extraSpacingBetweenObjectTypeMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" types.', | ||
'Extra spacing between "{{left}}" and "{{right}}" properties.', | ||
unexpectedObjectTypesOrder: | ||
@@ -305,17 +289,8 @@ 'Expected "{{right}}" to come before "{{left}}".', | ||
if ( | ||
(options.partitionByComment && | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: typeElement, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
})) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -356,65 +331,9 @@ lastGroup = [] | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
if ( | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? availableMessageIds.unexpectedOrder | ||
: availableMessageIds.unexpectedGroupOrder, | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: availableMessageIds.missedSpacingBetweenMembers, | ||
extraSpacingError: availableMessageIds.extraSpacingBetweenMembers, | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
reportAllErrors.reportAllErrors({ | ||
sortNodesExcludingEslintDisabled, | ||
availableMessageIds, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -421,0 +340,0 @@ } |
'use strict' | ||
const types = require('@typescript-eslint/types') | ||
const types$1 = require('@typescript-eslint/types') | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const getFirstNodeParentWithType = require('./sort-objects/get-first-node-parent-with-type.js') | ||
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js') | ||
const getCustomGroupsCompareOptions = require('../utils/get-custom-groups-compare-options.js') | ||
const getMatchingContextOptions = require('../utils/get-matching-context-options.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const doesCustomGroupMatch = require('./sort-objects/does-custom-group-match.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const types = require('./sort-objects/types.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -26,7 +25,6 @@ const rangeToDiff = require('../utils/range-to-diff.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const matches = require('../utils/matches.js') | ||
let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
let defaultOptions = { | ||
@@ -66,4 +64,8 @@ partitionByNewLine: false, | ||
nodeNames: nodeObject.properties | ||
.map(property => getNodeName({ sourceCode, property })) | ||
.filter(nodeName => nodeName !== null), | ||
.filter( | ||
property => | ||
property.type !== 'SpreadElement' && | ||
property.type !== 'RestElement', | ||
) | ||
.map(property => getNodeName({ sourceCode, property })), | ||
contextOptions: context.options, | ||
@@ -106,6 +108,9 @@ }) | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
['multiline', 'method', 'unknown'], | ||
Object.keys(options.customGroups), | ||
validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration( | ||
{ | ||
customGroups: options.customGroups, | ||
selectors: types.allSelectors, | ||
modifiers: types.allModifiers, | ||
groups: options.groups, | ||
}, | ||
) | ||
@@ -247,19 +252,12 @@ validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
} | ||
let lastProperty = | ||
let lastSortingNode = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
let name | ||
let dependencies = [] | ||
let { setCustomGroups, defineGroup, getGroup } = | ||
useGroups.useGroups(options) | ||
if (property.key.type === 'Identifier') { | ||
;({ name } = property.key) | ||
} else if (property.key.type === 'Literal') { | ||
name = `${property.key.value}` | ||
} else { | ||
name = sourceCode.getText(property.key) | ||
} | ||
let selectors = [] | ||
let modifiers = [] | ||
if (property.value.type === 'AssignmentPattern') { | ||
dependencies = extractDependencies(property.value) | ||
} | ||
setCustomGroups(options.customGroups, name) | ||
if ( | ||
@@ -269,8 +267,47 @@ property.value.type === 'ArrowFunctionExpression' || | ||
) { | ||
defineGroup('method') | ||
selectors.push('method') | ||
} else { | ||
selectors.push('property') | ||
} | ||
selectors.push('member') | ||
if (property.loc.start.line !== property.loc.end.line) { | ||
defineGroup('multiline') | ||
modifiers.push('multiline') | ||
selectors.push('multiline') | ||
} | ||
let propertySortingNode = { | ||
let predefinedGroups = | ||
generatePredefinedGroups.generatePredefinedGroups({ | ||
cache: cachedGroupsByModifiersAndSelectors, | ||
selectors, | ||
modifiers, | ||
}) | ||
for (let predefinedGroup of predefinedGroups) { | ||
defineGroup(predefinedGroup) | ||
} | ||
let name = getNodeName({ sourceCode, property }) | ||
if (Array.isArray(options.customGroups)) { | ||
for (let customGroup of options.customGroups) { | ||
if ( | ||
doesCustomGroupMatch.doesCustomGroupMatch({ | ||
elementValue: getNodeValue({ | ||
sourceCode, | ||
property, | ||
}), | ||
elementName: name, | ||
customGroup, | ||
selectors, | ||
modifiers, | ||
}) | ||
) { | ||
defineGroup(customGroup.groupName, true) | ||
if (getGroup() === customGroup.groupName) { | ||
break | ||
} | ||
} | ||
} | ||
} else { | ||
setCustomGroups(options.customGroups, name, { | ||
override: true, | ||
}) | ||
} | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
@@ -287,15 +324,7 @@ property, | ||
if ( | ||
(options.partitionByNewLine && | ||
lastProperty && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastProperty, | ||
propertySortingNode, | ||
)) || | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: property, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
@@ -305,3 +334,3 @@ ) { | ||
} | ||
accumulator.at(-1).push(propertySortingNode) | ||
accumulator.at(-1).push(sortingNode) | ||
return accumulator | ||
@@ -318,6 +347,11 @@ }, | ||
: sortNodesByGroups.sortNodesByGroups | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
formattedMembers.flatMap(nodes2 => | ||
nodesSortingFunction(nodes2, options, { | ||
getGroupCompareOptions: groupNumber => | ||
getCustomGroupsCompareOptions.getCustomGroupsCompareOptions( | ||
options, | ||
groupNumber, | ||
), | ||
ignoreEslintDisabledNodes, | ||
@@ -330,80 +364,16 @@ }), | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodes = formattedMembers.flat() | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
let firstUnorderedNodeDependentOnRight | ||
if ( | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn( | ||
right, | ||
nodes, | ||
) | ||
if (firstUnorderedNodeDependentOnRight) { | ||
messageIds.push('unexpectedObjectsDependencyOrder') | ||
} else { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedObjectsOrder' | ||
: 'unexpectedObjectsGroupOrder', | ||
) | ||
} | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenObjectMembers', | ||
extraSpacingError: 'extraSpacingBetweenObjectMembers', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
nodeDependentOnRight: | ||
firstUnorderedNodeDependentOnRight == null | ||
? void 0 | ||
: firstUnorderedNodeDependentOnRight.name, | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenObjectMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenObjectMembers', | ||
unexpectedDependencyOrder: 'unexpectedObjectsDependencyOrder', | ||
unexpectedGroupOrder: 'unexpectedObjectsGroupOrder', | ||
unexpectedOrder: 'unexpectedObjectsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -420,2 +390,3 @@ } | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
destructuredObjects: { | ||
@@ -456,6 +427,9 @@ oneOf: [ | ||
}), | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the keys of objects into logical groups.', | ||
customGroups: { | ||
oneOf: [ | ||
commonJsonSchemas.customGroupsJsonSchema, | ||
commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: types.singleCustomGroupJsonSchema, | ||
}), | ||
], | ||
}, | ||
@@ -475,11 +449,6 @@ destructureOnly: { | ||
type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }), | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -515,5 +484,2 @@ additionalProperties: false, | ||
let getNodeName = ({ sourceCode, property }) => { | ||
if (property.type === 'SpreadElement' || property.type === 'RestElement') { | ||
return null | ||
} | ||
if (property.key.type === 'Identifier') { | ||
@@ -526,2 +492,11 @@ return property.key.name | ||
} | ||
let getNodeValue = ({ sourceCode, property }) => { | ||
if ( | ||
property.value.type === 'ArrowFunctionExpression' || | ||
property.value.type === 'FunctionExpression' | ||
) { | ||
return null | ||
} | ||
return sourceCode.getText(property.value) | ||
} | ||
let getObjectParent = ({ onlyFirstParent, node }) => { | ||
@@ -550,4 +525,4 @@ let variableParentName = getVariableParentName({ onlyFirstParent, node }) | ||
allowedTypes: [ | ||
types.TSESTree.AST_NODE_TYPES.VariableDeclarator, | ||
types.TSESTree.AST_NODE_TYPES.Property, | ||
types$1.TSESTree.AST_NODE_TYPES.VariableDeclarator, | ||
types$1.TSESTree.AST_NODE_TYPES.Property, | ||
], | ||
@@ -572,3 +547,3 @@ onlyFirstParent, | ||
let callParent = getFirstNodeParentWithType.getFirstNodeParentWithType({ | ||
allowedTypes: [types.TSESTree.AST_NODE_TYPES.CallExpression], | ||
allowedTypes: [types$1.TSESTree.AST_NODE_TYPES.CallExpression], | ||
onlyFirstParent, | ||
@@ -575,0 +550,0 @@ node, |
@@ -11,3 +11,3 @@ 'use strict' | ||
node.callee.name === 'Set' && | ||
node.arguments.length && | ||
node.arguments.length > 0 && | ||
(((_a = node.arguments[0]) == null ? void 0 : _a.type) === | ||
@@ -26,2 +26,4 @@ 'ArrayExpression' || | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenSetsMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenSetsMembers', | ||
unexpectedGroupOrder: 'unexpectedSetsGroupOrder', | ||
@@ -40,2 +42,6 @@ unexpectedOrder: 'unexpectedSetsOrder', | ||
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).', | ||
missedSpacingBetweenSetsMembers: | ||
'Missed spacing between "{{left}}" and "{{right}}" members.', | ||
extraSpacingBetweenSetsMembers: | ||
'Extra spacing between "{{left}}" and "{{right}}" members.', | ||
unexpectedSetsOrder: 'Expected "{{right}}" to come before "{{left}}".', | ||
@@ -42,0 +48,0 @@ }, |
'use strict' | ||
const makeSingleNodeCommentAfterFixes = require('../utils/make-single-node-comment-after-fixes.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const makeCommentAfterFixes = require('../utils/make-comment-after-fixes.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const reportErrors = require('../utils/report-errors.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -55,3 +56,3 @@ const getSettings = require('../utils/get-settings.js') | ||
if ( | ||
caseNode.consequent.length && | ||
caseNode.consequent.length > 0 && | ||
index !== switchNode.cases.length - 1 | ||
@@ -84,16 +85,10 @@ ) { | ||
} | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedCaseNameSortingNodes, | ||
nodes: caseNodesSortingNodeGroup, | ||
sourceCode, | ||
fixer, | ||
}), | ||
data: { | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
messageId: 'unexpectedSwitchCaseOrder', | ||
node: right.node, | ||
reportErrors.reportErrors({ | ||
messageIds: ['unexpectedSwitchCaseOrder'], | ||
sortedNodes: sortedCaseNameSortingNodes, | ||
nodes: caseNodesSortingNodeGroup, | ||
sourceCode, | ||
context, | ||
right, | ||
left, | ||
}) | ||
@@ -114,3 +109,3 @@ }) | ||
sortingNodes, | ||
caseNode => !!caseNode.node.consequent.length, | ||
caseNode => caseNode.node.consequent.length > 0, | ||
) | ||
@@ -146,14 +141,18 @@ let sortingNodesGroupWithDefault = sortingNodeGroupsForDefaultSort.find( | ||
), | ||
...makeCommentAfterFixes.makeCommentAfterFixes({ | ||
sortedNode: punctuatorAfterLastCase, | ||
node: defaultCase.node, | ||
sourceCode, | ||
fixer, | ||
}), | ||
...makeCommentAfterFixes.makeCommentAfterFixes({ | ||
node: punctuatorAfterLastCase, | ||
sortedNode: defaultCase.node, | ||
sourceCode, | ||
fixer, | ||
}), | ||
...makeSingleNodeCommentAfterFixes.makeSingleNodeCommentAfterFixes( | ||
{ | ||
sortedNode: punctuatorAfterLastCase, | ||
node: defaultCase.node, | ||
sourceCode, | ||
fixer, | ||
}, | ||
), | ||
...makeSingleNodeCommentAfterFixes.makeSingleNodeCommentAfterFixes( | ||
{ | ||
node: punctuatorAfterLastCase, | ||
sortedNode: defaultCase.node, | ||
sourceCode, | ||
fixer, | ||
}, | ||
), | ||
] | ||
@@ -230,8 +229,4 @@ }, | ||
properties: { | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -238,0 +233,0 @@ additionalProperties: false, |
'use strict' | ||
Object.defineProperties(exports, { | ||
__esModule: { value: true }, | ||
[Symbol.toStringTag]: { value: 'Module' }, | ||
}) | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
@@ -8,19 +12,11 @@ const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const makeNewlinesFixes = require('../utils/make-newlines-fixes.js') | ||
const getNewlinesErrors = require('../utils/get-newlines-errors.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const getGroupNumber = require('../utils/get-group-number.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const useGroups = require('../utils/use-groups.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -38,231 +34,16 @@ specialCharacters: 'keep', | ||
} | ||
let jsonSchema = { | ||
properties: { | ||
...commonJsonSchemas.commonJsonSchemas, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
} | ||
const sortUnionTypes = createEslintRule.createEslintRule({ | ||
create: context => ({ | ||
TSUnionType: node => { | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
[ | ||
'intersection', | ||
'conditional', | ||
'function', | ||
'operator', | ||
'keyword', | ||
'literal', | ||
'nullish', | ||
'unknown', | ||
'import', | ||
'object', | ||
'named', | ||
'tuple', | ||
'union', | ||
], | ||
[], | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = node.types.reduce( | ||
(accumulator, type) => { | ||
let { defineGroup, getGroup } = useGroups.useGroups(options) | ||
switch (type.type) { | ||
case 'TSTemplateLiteralType': | ||
case 'TSLiteralType': | ||
defineGroup('literal') | ||
break | ||
case 'TSIndexedAccessType': | ||
case 'TSTypeReference': | ||
case 'TSQualifiedName': | ||
case 'TSArrayType': | ||
case 'TSInferType': | ||
defineGroup('named') | ||
break | ||
case 'TSIntersectionType': | ||
defineGroup('intersection') | ||
break | ||
case 'TSUndefinedKeyword': | ||
case 'TSNullKeyword': | ||
case 'TSVoidKeyword': | ||
defineGroup('nullish') | ||
break | ||
case 'TSConditionalType': | ||
defineGroup('conditional') | ||
break | ||
case 'TSConstructorType': | ||
case 'TSFunctionType': | ||
defineGroup('function') | ||
break | ||
case 'TSBooleanKeyword': | ||
case 'TSUnknownKeyword': | ||
case 'TSBigIntKeyword': | ||
case 'TSNumberKeyword': | ||
case 'TSObjectKeyword': | ||
case 'TSStringKeyword': | ||
case 'TSSymbolKeyword': | ||
case 'TSNeverKeyword': | ||
case 'TSAnyKeyword': | ||
case 'TSThisType': | ||
defineGroup('keyword') | ||
break | ||
case 'TSTypeOperator': | ||
case 'TSTypeQuery': | ||
defineGroup('operator') | ||
break | ||
case 'TSTypeLiteral': | ||
case 'TSMappedType': | ||
defineGroup('object') | ||
break | ||
case 'TSImportType': | ||
defineGroup('import') | ||
break | ||
case 'TSTupleType': | ||
defineGroup('tuple') | ||
break | ||
case 'TSUnionType': | ||
defineGroup('union') | ||
break | ||
} | ||
let lastGroup = accumulator.at(-1) | ||
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1) | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
type, | ||
eslintDisabledLines, | ||
), | ||
size: rangeToDiff.rangeToDiff(type, sourceCode), | ||
name: sourceCode.getText(type), | ||
group: getGroup(), | ||
node: type, | ||
} | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
tokenValueToIgnoreBefore: '|', | ||
node: type, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
) { | ||
lastGroup = [] | ||
accumulator.push(lastGroup) | ||
} | ||
lastGroup == null ? void 0 : lastGroup.push(sortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
for (let nodes of formattedMembers) { | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
ignoreEslintDisabledNodes, | ||
}) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftNumber = getGroupNumber.getGroupNumber(options.groups, left) | ||
let rightNumber = getGroupNumber.getGroupNumber(options.groups, right) | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
let messageIds = [] | ||
if ( | ||
leftIndex > rightIndex || | ||
leftIndex >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedUnionTypesOrder' | ||
: 'unexpectedUnionTypesGroupOrder', | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenUnionTypes', | ||
extraSpacingError: 'extraSpacingBetweenUnionTypes', | ||
rightNum: rightNumber, | ||
leftNum: leftNumber, | ||
sourceCode, | ||
options, | ||
right, | ||
left, | ||
}), | ||
] | ||
for (let messageId of messageIds) { | ||
context.report({ | ||
fix: fixer => [ | ||
...makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
...makeNewlinesFixes.makeNewlinesFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
], | ||
data: { | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
}) | ||
} | ||
}, | ||
}), | ||
meta: { | ||
schema: [ | ||
{ | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the union types into logical groups.', | ||
}, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
messages: { | ||
@@ -283,8 +64,170 @@ unexpectedUnionTypesGroupOrder: | ||
}, | ||
schema: [jsonSchema], | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
TSUnionType: node => { | ||
sortUnionOrIntersectionTypes({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenUnionTypes', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenUnionTypes', | ||
unexpectedGroupOrder: 'unexpectedUnionTypesGroupOrder', | ||
unexpectedOrder: 'unexpectedUnionTypesOrder', | ||
}, | ||
tokenValueToIgnoreBefore: '|', | ||
context, | ||
node, | ||
}) | ||
}, | ||
}), | ||
defaultOptions: [defaultOptions], | ||
name: 'sort-union-types', | ||
}) | ||
module.exports = sortUnionTypes | ||
let sortUnionOrIntersectionTypes = ({ | ||
tokenValueToIgnoreBefore, | ||
availableMessageIds, | ||
context, | ||
node, | ||
}) => { | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateCustomSortConfiguration.validateCustomSortConfiguration(options) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
[ | ||
'intersection', | ||
'conditional', | ||
'function', | ||
'operator', | ||
'keyword', | ||
'literal', | ||
'nullish', | ||
'unknown', | ||
'import', | ||
'object', | ||
'named', | ||
'tuple', | ||
'union', | ||
], | ||
[], | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = node.types.reduce( | ||
(accumulator, type) => { | ||
let { defineGroup, getGroup } = useGroups.useGroups(options) | ||
switch (type.type) { | ||
case 'TSTemplateLiteralType': | ||
case 'TSLiteralType': | ||
defineGroup('literal') | ||
break | ||
case 'TSIndexedAccessType': | ||
case 'TSTypeReference': | ||
case 'TSQualifiedName': | ||
case 'TSArrayType': | ||
case 'TSInferType': | ||
defineGroup('named') | ||
break | ||
case 'TSIntersectionType': | ||
defineGroup('intersection') | ||
break | ||
case 'TSUndefinedKeyword': | ||
case 'TSNullKeyword': | ||
case 'TSVoidKeyword': | ||
defineGroup('nullish') | ||
break | ||
case 'TSConditionalType': | ||
defineGroup('conditional') | ||
break | ||
case 'TSConstructorType': | ||
case 'TSFunctionType': | ||
defineGroup('function') | ||
break | ||
case 'TSBooleanKeyword': | ||
case 'TSUnknownKeyword': | ||
case 'TSBigIntKeyword': | ||
case 'TSNumberKeyword': | ||
case 'TSObjectKeyword': | ||
case 'TSStringKeyword': | ||
case 'TSSymbolKeyword': | ||
case 'TSNeverKeyword': | ||
case 'TSAnyKeyword': | ||
case 'TSThisType': | ||
defineGroup('keyword') | ||
break | ||
case 'TSTypeOperator': | ||
case 'TSTypeQuery': | ||
defineGroup('operator') | ||
break | ||
case 'TSTypeLiteral': | ||
case 'TSMappedType': | ||
defineGroup('object') | ||
break | ||
case 'TSImportType': | ||
defineGroup('import') | ||
break | ||
case 'TSTupleType': | ||
defineGroup('tuple') | ||
break | ||
case 'TSUnionType': | ||
defineGroup('union') | ||
break | ||
} | ||
let lastGroup = accumulator.at(-1) | ||
let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1) | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
type, | ||
eslintDisabledLines, | ||
), | ||
size: rangeToDiff.rangeToDiff(type, sourceCode), | ||
name: sourceCode.getText(type), | ||
group: getGroup(), | ||
node: type, | ||
} | ||
if ( | ||
shouldPartition.shouldPartition({ | ||
tokenValueToIgnoreBefore, | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
lastGroup = [] | ||
accumulator.push(lastGroup) | ||
} | ||
lastGroup == null ? void 0 : lastGroup.push(sortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
for (let nodes of formattedMembers) { | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByGroups.sortNodesByGroups(nodes, options, { | ||
ignoreEslintDisabledNodes, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
sortNodesExcludingEslintDisabled, | ||
availableMessageIds, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
} | ||
} | ||
exports.default = sortUnionTypes | ||
exports.jsonSchema = jsonSchema | ||
exports.sortUnionOrIntersectionTypes = sortUnionOrIntersectionTypes |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const hasPartitionComment = require('../utils/has-partition-comment.js') | ||
const createNodeIndexMap = require('../utils/create-node-index-map.js') | ||
const getCommentsBefore = require('../utils/get-comments-before.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
const reportAllErrors = require('../utils/report-all-errors.js') | ||
const shouldPartition = require('../utils/should-partition.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
const toSingleLine = require('../utils/to-single-line.js') | ||
const rangeToDiff = require('../utils/range-to-diff.js') | ||
@@ -18,5 +15,3 @@ const getSettings = require('../utils/get-settings.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const complete = require('../utils/complete.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
let defaultOptions = { | ||
@@ -151,16 +146,8 @@ specialCharacters: 'keep', | ||
if ( | ||
hasPartitionComment.hasPartitionComment({ | ||
comments: getCommentsBefore.getCommentsBefore({ | ||
node: declaration, | ||
sourceCode, | ||
}), | ||
partitionByComment: options.partitionByComment, | ||
}) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
shouldPartition.shouldPartition({ | ||
lastSortingNode, | ||
sortingNode, | ||
sourceCode, | ||
options, | ||
}) | ||
) { | ||
@@ -174,3 +161,3 @@ accumulator.push([]) | ||
) | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
@@ -186,42 +173,14 @@ formattedMembers.flatMap(nodes2 => | ||
) | ||
let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodes = formattedMembers.flat() | ||
let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes) | ||
pairwise.pairwise(nodes, (left, right) => { | ||
let leftIndex = nodeIndexMap.get(left) | ||
let rightIndex = nodeIndexMap.get(right) | ||
let indexOfRightExcludingEslintDisabled = | ||
sortedNodesExcludingEslintDisabled.indexOf(right) | ||
if ( | ||
leftIndex < rightIndex && | ||
leftIndex < indexOfRightExcludingEslintDisabled | ||
) { | ||
return | ||
} | ||
let firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn(right, nodes) | ||
context.report({ | ||
fix: fixer => | ||
makeFixes.makeFixes({ | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
data: { | ||
nodeDependentOnRight: | ||
firstUnorderedNodeDependentOnRight == null | ||
? void 0 | ||
: firstUnorderedNodeDependentOnRight.name, | ||
right: toSingleLine.toSingleLine(right.name), | ||
left: toSingleLine.toSingleLine(left.name), | ||
}, | ||
messageId: firstUnorderedNodeDependentOnRight | ||
? 'unexpectedVariableDeclarationsDependencyOrder' | ||
: 'unexpectedVariableDeclarationsOrder', | ||
node: right.node, | ||
}) | ||
reportAllErrors.reportAllErrors({ | ||
availableMessageIds: { | ||
unexpectedDependencyOrder: | ||
'unexpectedVariableDeclarationsDependencyOrder', | ||
unexpectedOrder: 'unexpectedVariableDeclarationsOrder', | ||
}, | ||
sortNodesExcludingEslintDisabled, | ||
sourceCode, | ||
options, | ||
context, | ||
nodes, | ||
}) | ||
@@ -234,14 +193,6 @@ }, | ||
properties: { | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the variable declarations into logical groups.', | ||
}, | ||
...commonJsonSchemas.commonJsonSchemas, | ||
partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
alphabet: commonJsonSchemas.alphabetJsonSchema, | ||
type: commonJsonSchemas.buildTypeJsonSchema(), | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
}, | ||
@@ -248,0 +199,0 @@ additionalProperties: false, |
@@ -48,2 +48,9 @@ 'use strict' | ||
} | ||
let commonJsonSchemas = { | ||
specialCharacters: specialCharactersJsonSchema, | ||
ignoreCase: ignoreCaseJsonSchema, | ||
alphabet: alphabetJsonSchema, | ||
locales: localesJsonSchema, | ||
order: orderJsonSchema, | ||
} | ||
let newlinesBetweenJsonSchema = { | ||
@@ -123,2 +130,4 @@ description: 'Specifies how new lines should be handled between groups.', | ||
], | ||
description: | ||
'Allows to use comments to separate members into logical groups.', | ||
} | ||
@@ -223,3 +232,6 @@ let partitionByNewLineJsonSchema = { | ||
} | ||
exports.alphabetJsonSchema = alphabetJsonSchema | ||
let elementValuePatternJsonSchema = { | ||
description: 'Element value pattern filter.', | ||
type: 'string', | ||
} | ||
exports.buildCustomGroupModifiersJsonSchema = | ||
@@ -231,11 +243,9 @@ buildCustomGroupModifiersJsonSchema | ||
exports.buildUseConfigurationIfJsonSchema = buildUseConfigurationIfJsonSchema | ||
exports.commonJsonSchemas = commonJsonSchemas | ||
exports.customGroupsJsonSchema = customGroupsJsonSchema | ||
exports.elementNamePatternJsonSchema = elementNamePatternJsonSchema | ||
exports.elementValuePatternJsonSchema = elementValuePatternJsonSchema | ||
exports.groupsJsonSchema = groupsJsonSchema | ||
exports.ignoreCaseJsonSchema = ignoreCaseJsonSchema | ||
exports.localesJsonSchema = localesJsonSchema | ||
exports.newlinesBetweenJsonSchema = newlinesBetweenJsonSchema | ||
exports.orderJsonSchema = orderJsonSchema | ||
exports.partitionByCommentJsonSchema = partitionByCommentJsonSchema | ||
exports.partitionByNewLineJsonSchema = partitionByNewLineJsonSchema | ||
exports.specialCharactersJsonSchema = specialCharactersJsonSchema |
@@ -7,3 +7,3 @@ 'use strict' | ||
if ( | ||
commentsBefore.length || | ||
commentsBefore.length > 0 || | ||
!tokenValueToIgnoreBefore || | ||
@@ -10,0 +10,0 @@ (tokenBeforeNode == null ? void 0 : tokenBeforeNode.value) !== |
@@ -8,4 +8,4 @@ 'use strict' | ||
) | ||
return linesBetween.filter(line => !line.trim().length).length | ||
return linesBetween.filter(line => line.trim().length === 0).length | ||
} | ||
exports.getLinesBetween = getLinesBetween |
@@ -23,3 +23,3 @@ 'use strict' | ||
let invalidOptions = getInvalidOptions(perfectionistSettings) | ||
if (invalidOptions.length) { | ||
if (invalidOptions.length > 0) { | ||
throw new Error( | ||
@@ -26,0 +26,0 @@ `Invalid Perfectionist setting(s): ${invalidOptions.join(', ')}`, |
@@ -5,4 +5,4 @@ 'use strict' | ||
/* v8 ignore next 2 */ | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
// eslint-disable-next-line typescript/no-unnecessary-condition | ||
context.sourceCode ?? context.getSourceCode() | ||
exports.getSourceCode = getSourceCode |
'use strict' | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) | ||
const getCommentAfter = require('./get-comment-after.js') | ||
let makeCommentAfterFixes = ({ sortedNode, sourceCode, fixer, node }) => { | ||
let commentAfter = getCommentAfter.getCommentAfter(sortedNode, sourceCode) | ||
let areNodesOnSameLine = node.loc.start.line === sortedNode.loc.end.line | ||
if (!commentAfter || areNodesOnSameLine) { | ||
return [] | ||
const makeSingleNodeCommentAfterFixes = require('./make-single-node-comment-after-fixes.js') | ||
let makeCommentAfterFixes = ({ sortedNodes, sourceCode, fixer, nodes }) => { | ||
let fixes = [] | ||
for (let max = nodes.length, i = 0; i < max; i++) { | ||
let sortingNode = nodes.at(i) | ||
let sortedSortingNode = sortedNodes.at(i) | ||
let { node } = sortingNode | ||
let { node: sortedNode } = sortedSortingNode | ||
if (node === sortedNode) { | ||
continue | ||
} | ||
fixes = [ | ||
...fixes, | ||
...makeSingleNodeCommentAfterFixes.makeSingleNodeCommentAfterFixes({ | ||
sortedNode, | ||
sourceCode, | ||
fixer, | ||
node, | ||
}), | ||
] | ||
} | ||
let fixes = [] | ||
let tokenBefore = sourceCode.getTokenBefore(commentAfter) | ||
let range = [tokenBefore.range.at(1), commentAfter.range.at(1)] | ||
fixes.push(fixer.replaceTextRange(range, '')) | ||
let tokenAfterNode = sourceCode.getTokenAfter(node) | ||
fixes.push( | ||
fixer.insertTextAfter( | ||
(tokenAfterNode == null ? void 0 : tokenAfterNode.loc.end.line) === | ||
node.loc.end.line | ||
? tokenAfterNode | ||
: node, | ||
sourceCode.text.slice(...range), | ||
), | ||
) | ||
return fixes | ||
} | ||
exports.makeCommentAfterFixes = makeCommentAfterFixes |
'use strict' | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) | ||
const makeCommentAfterFixes = require('./make-comment-after-fixes.js') | ||
const getNodeRange = require('./get-node-range.js') | ||
const makeNewlinesFixes = require('./make-newlines-fixes.js') | ||
const makeOrderFixes = require('./make-order-fixes.js') | ||
let makeFixes = ({ | ||
@@ -13,68 +14,36 @@ ignoreFirstNodeHighestBlockComment, | ||
}) => { | ||
let fixes = [] | ||
for (let max = nodes.length, i = 0; i < max; i++) { | ||
let sortingNode = nodes.at(i) | ||
let sortedSortingNode = sortedNodes.at(i) | ||
let { node } = sortingNode | ||
let { addSafetySemicolonWhenInline, node: sortedNode } = sortedSortingNode | ||
let isNodeFirstNode = node === nodes.at(0).node | ||
let isSortedNodeFirstNode = sortedNode === nodes.at(0).node | ||
if (node === sortedNode) { | ||
continue | ||
} | ||
let sortedNodeCode = sourceCode.text.slice( | ||
...getNodeRange.getNodeRange({ | ||
ignoreHighestBlockComment: | ||
ignoreFirstNodeHighestBlockComment && isSortedNodeFirstNode, | ||
node: sortedNode, | ||
sourceCode, | ||
options, | ||
}), | ||
) | ||
let sortedNodeText = sourceCode.getText(sortedNode) | ||
let tokensAfter = sourceCode.getTokensAfter(node, { | ||
includeComments: false, | ||
count: 1, | ||
}) | ||
let nextToken = tokensAfter.at(0) | ||
let sortedNextNodeEndsWithSafeCharacter = | ||
sortedNodeText.endsWith(';') || sortedNodeText.endsWith(',') | ||
let isNextTokenOnSameLineAsNode = | ||
(nextToken == null ? void 0 : nextToken.loc.start.line) === | ||
node.loc.end.line | ||
let isNextTokenSafeCharacter = | ||
(nextToken == null ? void 0 : nextToken.value) === ';' || | ||
(nextToken == null ? void 0 : nextToken.value) === ',' | ||
if ( | ||
addSafetySemicolonWhenInline && | ||
isNextTokenOnSameLineAsNode && | ||
!sortedNextNodeEndsWithSafeCharacter && | ||
!isNextTokenSafeCharacter | ||
) { | ||
sortedNodeCode += ';' | ||
} | ||
fixes.push( | ||
fixer.replaceTextRange( | ||
getNodeRange.getNodeRange({ | ||
ignoreHighestBlockComment: | ||
ignoreFirstNodeHighestBlockComment && isNodeFirstNode, | ||
sourceCode, | ||
options, | ||
node, | ||
}), | ||
sortedNodeCode, | ||
), | ||
) | ||
fixes = [ | ||
...fixes, | ||
...makeCommentAfterFixes.makeCommentAfterFixes({ | ||
sortedNode, | ||
sourceCode, | ||
fixer, | ||
node, | ||
}), | ||
] | ||
let orderFixes = makeOrderFixes.makeOrderFixes({ | ||
ignoreFirstNodeHighestBlockComment, | ||
sortedNodes, | ||
sourceCode, | ||
options, | ||
nodes, | ||
fixer, | ||
}) | ||
let commentAfterFixes = makeCommentAfterFixes.makeCommentAfterFixes({ | ||
sortedNodes, | ||
sourceCode, | ||
nodes, | ||
fixer, | ||
}) | ||
if ( | ||
commentAfterFixes.length > 0 || | ||
!(options == null ? void 0 : options.groups) || | ||
!options.newlinesBetween | ||
) { | ||
return [...orderFixes, ...commentAfterFixes] | ||
} | ||
return fixes | ||
let newlinesFixes = makeNewlinesFixes.makeNewlinesFixes({ | ||
options: { | ||
...options, | ||
newlinesBetween: options.newlinesBetween, | ||
groups: options.groups, | ||
}, | ||
sortedNodes, | ||
sourceCode, | ||
fixer, | ||
nodes, | ||
}) | ||
return [...orderFixes, ...newlinesFixes] | ||
} | ||
exports.makeFixes = makeFixes |
@@ -7,3 +7,3 @@ 'use strict' | ||
} | ||
if (!alphabet.length) { | ||
if (alphabet.length === 0) { | ||
throw new Error('`alphabet` option must not be empty') | ||
@@ -10,0 +10,0 @@ } |
@@ -23,3 +23,3 @@ 'use strict' | ||
) | ||
if (invalidGroups.length) { | ||
if (invalidGroups.length > 0) { | ||
throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`) | ||
@@ -26,0 +26,0 @@ } |
@@ -32,3 +32,3 @@ 'use strict' | ||
} | ||
if (invalidGroups.length) { | ||
if (invalidGroups.length > 0) { | ||
throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`) | ||
@@ -35,0 +35,0 @@ } |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.", | ||
@@ -30,4 +30,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@typescript-eslint/types": "^8.19.0", | ||
"@typescript-eslint/utils": "^8.19.0", | ||
"@typescript-eslint/types": "^8.21.0", | ||
"@typescript-eslint/utils": "^8.21.0", | ||
"natural-orderby": "^5.0.0" | ||
@@ -34,0 +34,0 @@ }, |
94
286715
8182
+ Added@eslint/config-array@0.19.1(transitive)
+ Added@eslint/object-schema@2.1.5(transitive)
+ Added@typescript-eslint/scope-manager@8.22.0(transitive)
+ Added@typescript-eslint/types@8.22.0(transitive)
+ Added@typescript-eslint/typescript-estree@8.22.0(transitive)
+ Added@typescript-eslint/utils@8.22.0(transitive)
+ Added@typescript-eslint/visitor-keys@8.22.0(transitive)
+ Addedfastq@1.18.0(transitive)
+ Addedimport-fresh@3.3.0(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedts-api-utils@2.0.0(transitive)
- Removed@eslint/config-array@0.19.2(transitive)
- Removed@eslint/object-schema@2.1.6(transitive)
- Removed@typescript-eslint/scope-manager@8.23.0(transitive)
- Removed@typescript-eslint/types@8.23.0(transitive)
- Removed@typescript-eslint/typescript-estree@8.23.0(transitive)
- Removed@typescript-eslint/utils@8.23.0(transitive)
- Removed@typescript-eslint/visitor-keys@8.23.0(transitive)
- Removedfastq@1.19.0(transitive)
- Removedimport-fresh@3.3.1(transitive)
- Removedsemver@7.7.1(transitive)
- Removedts-api-utils@2.0.1(transitive)