eslint-plugin-perfectionist
Advanced tools
Comparing version 4.1.2 to 4.2.0
@@ -31,3 +31,3 @@ 'use strict' | ||
'sort-named-exports': sortNamedExports, | ||
'sort-object-types': sortObjectTypes, | ||
'sort-object-types': sortObjectTypes.default, | ||
'sort-union-types': sortUnionTypes, | ||
@@ -34,0 +34,0 @@ 'sort-switch-case': sortSwitchCase, |
@@ -132,3 +132,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(element, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: element, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -135,0 +138,0 @@ (options.partitionByNewLine && |
@@ -92,26 +92,3 @@ 'use strict' | ||
} | ||
let getCompareOptions = (options, groupNumber) => { | ||
let group = options.groups[groupNumber] | ||
let customGroup = | ||
typeof group === 'string' | ||
? options.customGroups.find( | ||
currentGroup => group === currentGroup.groupName, | ||
) | ||
: null | ||
if ((customGroup == null ? void 0 : customGroup.type) === 'unsorted') { | ||
return null | ||
} | ||
return { | ||
order: | ||
customGroup && 'order' in customGroup && customGroup.order | ||
? customGroup.order | ||
: options.order, | ||
specialCharacters: options.specialCharacters, | ||
type: (customGroup == null ? void 0 : customGroup.type) ?? options.type, | ||
ignoreCase: options.ignoreCase, | ||
locales: options.locales, | ||
} | ||
} | ||
exports.customGroupMatches = customGroupMatches | ||
exports.getCompareOptions = getCompareOptions | ||
exports.getOverloadSignatureGroups = getOverloadSignatureGroups |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const sortClasses_types = require('./sort-classes.types.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const sortClasses_types = require('./sort-classes.types.js') | ||
const validateGeneratedGroupsConfiguration = require('./validate-generated-groups-configuration.js') | ||
const sortClassesUtils = require('./sort-classes-utils.js') | ||
const validateGeneratedGroupsConfiguration = require('./validate-generated-groups-configuration.js') | ||
const getCustomGroupsCompareOptions = require('./get-custom-groups-compare-options.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
@@ -449,3 +450,2 @@ const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
} | ||
let comments = getCommentsBefore.getCommentsBefore(member, sourceCode) | ||
let lastMember = | ||
@@ -464,3 +464,6 @@ (_e = accumulator.at(-1)) == null ? void 0 : _e.at(-1) | ||
options.partitionByComment, | ||
comments, | ||
getCommentsBefore.getCommentsBefore({ | ||
node: member, | ||
sourceCode, | ||
}), | ||
)) | ||
@@ -483,3 +486,6 @@ ) { | ||
getGroupCompareOptions: groupNumber => | ||
sortClassesUtils.getCompareOptions(options, groupNumber), | ||
getCustomGroupsCompareOptions.getCustomGroupsCompareOptions( | ||
options, | ||
groupNumber, | ||
), | ||
ignoreEslintDisabledNodes, | ||
@@ -573,40 +579,2 @@ }), | ||
properties: { | ||
customGroups: { | ||
items: { | ||
oneOf: [ | ||
{ | ||
properties: { | ||
...sortClasses_types.customGroupNameJsonSchema, | ||
...sortClasses_types.customGroupSortJsonSchema, | ||
anyOf: { | ||
items: { | ||
properties: { | ||
...sortClasses_types.singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
type: 'array', | ||
}, | ||
}, | ||
description: 'Custom group block.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
{ | ||
properties: { | ||
...sortClasses_types.customGroupNameJsonSchema, | ||
...sortClasses_types.customGroupSortJsonSchema, | ||
...sortClasses_types.singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
description: 'Specifies custom groups.', | ||
type: 'array', | ||
}, | ||
ignoreCallbackDependenciesPatterns: { | ||
@@ -625,2 +593,6 @@ description: | ||
}, | ||
customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: | ||
sortClasses_types.singleCustomGroupJsonSchema, | ||
}), | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
@@ -627,0 +599,0 @@ specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, |
'use strict' | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
let allSelectors = [ | ||
@@ -27,29 +28,3 @@ 'accessor-property', | ||
] | ||
let customGroupSortJsonSchema = { | ||
type: { | ||
enum: ['alphabetical', 'line-length', 'natural', 'unsorted'], | ||
description: 'Custom group sort type.', | ||
type: 'string', | ||
}, | ||
order: { | ||
description: 'Custom group sort order.', | ||
enum: ['desc', 'asc'], | ||
type: 'string', | ||
}, | ||
} | ||
let customGroupNameJsonSchema = { | ||
groupName: { | ||
description: 'Custom group name.', | ||
type: 'string', | ||
}, | ||
} | ||
let singleCustomGroupJsonSchema = { | ||
modifiers: { | ||
items: { | ||
enum: allModifiers, | ||
type: 'string', | ||
}, | ||
description: 'Modifier filters.', | ||
type: 'array', | ||
}, | ||
elementValuePattern: { | ||
@@ -63,16 +38,9 @@ description: 'Element value pattern filter for properties.', | ||
}, | ||
selector: { | ||
description: 'Selector filter.', | ||
enum: allSelectors, | ||
type: 'string', | ||
}, | ||
elementNamePattern: { | ||
description: 'Element name pattern filter.', | ||
type: 'string', | ||
}, | ||
modifiers: | ||
commonJsonSchemas.buildCustomGroupModifiersJsonSchema(allModifiers), | ||
selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors), | ||
elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema, | ||
} | ||
exports.allModifiers = allModifiers | ||
exports.allSelectors = allSelectors | ||
exports.customGroupNameJsonSchema = customGroupNameJsonSchema | ||
exports.customGroupSortJsonSchema = customGroupSortJsonSchema | ||
exports.singleCustomGroupJsonSchema = singleCustomGroupJsonSchema |
@@ -179,3 +179,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(decorator, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: decorator, | ||
sourceCode, | ||
}), | ||
) | ||
@@ -230,2 +233,3 @@ ) { | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
ignoreFirstNodeHighestBlockComment: true, | ||
sourceCode, | ||
@@ -232,0 +236,0 @@ options, |
@@ -112,3 +112,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(member, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: member, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -299,2 +302,3 @@ (options.partitionByNewLine && | ||
return ~argument | ||
/* v8 ignore next 2 - Unsure if we can reach it */ | ||
default: | ||
@@ -334,2 +338,3 @@ return Number.NaN | ||
return left ^ right | ||
/* v8 ignore next 2 - Unsure if we can reach it */ | ||
default: | ||
@@ -336,0 +341,0 @@ return Number.NaN |
@@ -59,3 +59,6 @@ 'use strict' | ||
partitionComment, | ||
getCommentsBefore.getCommentsBefore(node, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
sourceCode, | ||
node, | ||
}), | ||
)) || | ||
@@ -62,0 +65,0 @@ (options.partitionByNewLine && |
@@ -343,6 +343,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore( | ||
sortingNode.node, | ||
getCommentsBefore.getCommentsBefore({ | ||
node: sortingNode.node, | ||
sourceCode, | ||
), | ||
}), | ||
)) || | ||
@@ -349,0 +349,0 @@ (options.partitionByNewLine && |
'use strict' | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-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 isPartitionComment = require('../utils/is-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 sortObjectTypes = require('./sort-object-types.js') | ||
const createEslintRule = require('../utils/create-eslint-rule.js') | ||
const isMemberOptional = require('../utils/is-member-optional.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 rangeToDiff = require('../utils/range-to-diff.js') | ||
const getSettings = require('../utils/get-settings.js') | ||
const isSortable = require('../utils/is-sortable.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') | ||
let defaultOptions = { | ||
@@ -40,241 +19,3 @@ partitionByComment: false, | ||
const sortInterfaces = createEslintRule.createEslintRule({ | ||
create: context => ({ | ||
TSInterfaceDeclaration: node => { | ||
if (!isSortable.isSortable(node.body.body)) { | ||
return | ||
} | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
['multiline', 'method', 'unknown'], | ||
Object.keys(options.customGroups), | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
if ( | ||
options.ignorePattern.some(pattern => | ||
matches.matches(node.id.name, pattern), | ||
) | ||
) { | ||
return | ||
} | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = node.body.body.reduce( | ||
(accumulator, element) => { | ||
var _a, _b, _c, _d, _e | ||
if (element.type === 'TSCallSignatureDeclaration') { | ||
accumulator.push([]) | ||
return accumulator | ||
} | ||
let lastElement = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
let name | ||
let { setCustomGroups, defineGroup, getGroup } = | ||
useGroups.useGroups(options) | ||
if (element.type === 'TSPropertySignature') { | ||
if (element.key.type === 'Identifier') { | ||
;({ name } = element.key) | ||
} else if (element.key.type === 'Literal') { | ||
name = `${element.key.value}` | ||
} else { | ||
let end = | ||
((_b = element.typeAnnotation) == null | ||
? void 0 | ||
: _b.range.at(0)) ?? | ||
element.range.at(1) - (element.optional ? '?'.length : 0) | ||
name = sourceCode.text.slice(element.range.at(0), end) | ||
} | ||
} else if (element.type === 'TSIndexSignature') { | ||
let endIndex = | ||
((_c = element.typeAnnotation) == null | ||
? void 0 | ||
: _c.range.at(0)) ?? element.range.at(1) | ||
name = sourceCode.text.slice(element.range.at(0), endIndex) | ||
} else { | ||
let endIndex = | ||
((_d = element.returnType) == null ? void 0 : _d.range.at(0)) ?? | ||
element.range.at(1) | ||
name = sourceCode.text.slice(element.range.at(0), endIndex) | ||
} | ||
setCustomGroups(options.customGroups, name) | ||
if ( | ||
element.type === 'TSMethodSignature' || | ||
(element.type === 'TSPropertySignature' && | ||
((_e = element.typeAnnotation) == null | ||
? void 0 | ||
: _e.typeAnnotation.type) === 'TSFunctionType') | ||
) { | ||
defineGroup('method') | ||
} | ||
if (element.loc.start.line !== element.loc.end.line) { | ||
defineGroup('multiline') | ||
} | ||
let elementSortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
element, | ||
eslintDisabledLines, | ||
), | ||
groupKind: isMemberOptional.isMemberOptional(element) | ||
? 'optional' | ||
: 'required', | ||
size: rangeToDiff.rangeToDiff(element, sourceCode), | ||
addSafetySemicolonWhenInline: true, | ||
group: getGroup(), | ||
node: element, | ||
name, | ||
} | ||
if ( | ||
(options.partitionByComment && | ||
isPartitionComment.hasPartitionComment( | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(element, sourceCode), | ||
)) || | ||
(options.partitionByNewLine && | ||
lastElement && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastElement, | ||
elementSortingNode, | ||
)) | ||
) { | ||
accumulator.push([]) | ||
} | ||
accumulator.at(-1).push(elementSortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
let groupKindOrder | ||
if (options.groupKind === 'required-first') { | ||
groupKindOrder = ['required', 'optional'] | ||
} else if (options.groupKind === 'optional-first') { | ||
groupKindOrder = ['optional', 'required'] | ||
} else { | ||
groupKindOrder = ['any'] | ||
} | ||
for (let nodes of formattedMembers) { | ||
let filteredGroupKindNodes = groupKindOrder.map(groupKind => | ||
nodes.filter( | ||
currentNode => | ||
groupKind === 'any' || currentNode.groupKind === groupKind, | ||
), | ||
) | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
filteredGroupKindNodes.flatMap(groupedNodes => | ||
sortNodesByGroups.sortNodesByGroups(groupedNodes, options, { | ||
ignoreEslintDisabledNodes, | ||
}), | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
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 = [] | ||
if ( | ||
indexOfLeft > indexOfRight || | ||
indexOfLeft >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedInterfacePropertiesOrder' | ||
: 'unexpectedInterfacePropertiesGroupOrder', | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenInterfaceMembers', | ||
extraSpacingError: 'extraSpacingBetweenInterfaceMembers', | ||
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: { | ||
rightGroup: right.group, | ||
leftGroup: left.group, | ||
right: right.name, | ||
left: left.name, | ||
}, | ||
node: right.node, | ||
messageId, | ||
}) | ||
} | ||
}) | ||
} | ||
}, | ||
}), | ||
meta: { | ||
schema: [ | ||
{ | ||
properties: { | ||
ignorePattern: { | ||
description: | ||
'Specifies names or patterns for nodes that should be ignored by rule.', | ||
items: { | ||
type: 'string', | ||
}, | ||
type: 'array', | ||
}, | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate the interface properties into logical groups.', | ||
}, | ||
groupKind: { | ||
description: 'Specifies the order of optional and required nodes.', | ||
enum: ['mixed', 'optional-first', 'required-first'], | ||
type: 'string', | ||
}, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
type: commonJsonSchemas.typeJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
messages: { | ||
@@ -295,5 +36,20 @@ unexpectedInterfacePropertiesGroupOrder: | ||
}, | ||
schema: [sortObjectTypes.jsonSchema], | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
TSInterfaceDeclaration: node => | ||
sortObjectTypes.sortObjectTypeElements({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenInterfaceMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenInterfaceMembers', | ||
unexpectedGroupOrder: 'unexpectedInterfacePropertiesGroupOrder', | ||
unexpectedOrder: 'unexpectedInterfacePropertiesOrder', | ||
}, | ||
parentNodeName: node.id.name, | ||
elements: node.body.body, | ||
context, | ||
}), | ||
}), | ||
defaultOptions: [defaultOptions], | ||
@@ -300,0 +56,0 @@ name: 'sort-interfaces', |
@@ -147,3 +147,7 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(type, sourceCode, '&'), | ||
getCommentsBefore.getCommentsBefore({ | ||
tokenValueToIgnoreBefore: '&', | ||
node: type, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -150,0 +154,0 @@ (options.partitionByNewLine && |
@@ -97,3 +97,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(element, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: element, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -100,0 +103,0 @@ (options.partitionByNewLine && |
@@ -49,25 +49,2 @@ 'use strict' | ||
} | ||
let getCompareOptions = (options, groupNumber) => { | ||
let group = options.groups[groupNumber] | ||
let customGroup = | ||
typeof group === 'string' | ||
? options.customGroups.find( | ||
currentGroup => group === currentGroup.groupName, | ||
) | ||
: null | ||
if ((customGroup == null ? void 0 : customGroup.type) === 'unsorted') { | ||
return null | ||
} | ||
return { | ||
order: | ||
customGroup && 'order' in customGroup && customGroup.order | ||
? customGroup.order | ||
: options.order, | ||
specialCharacters: options.specialCharacters, | ||
type: (customGroup == null ? void 0 : customGroup.type) ?? options.type, | ||
ignoreCase: options.ignoreCase, | ||
locales: options.locales, | ||
} | ||
} | ||
exports.customGroupMatches = customGroupMatches | ||
exports.getCompareOptions = getCompareOptions |
'use strict' | ||
const utils = require('@typescript-eslint/utils') | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const sortModules_types = require('./sort-modules.types.js') | ||
const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const sortModules_types = require('./sort-modules.types.js') | ||
const validateGeneratedGroupsConfiguration = require('./validate-generated-groups-configuration.js') | ||
const getCustomGroupsCompareOptions = require('./get-custom-groups-compare-options.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
const sortModulesUtils = require('./sort-modules-utils.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
@@ -22,2 +22,3 @@ const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
const getEnumMembers = require('../utils/get-enum-members.js') | ||
const sortModulesUtils = require('./sort-modules-utils.js') | ||
const getSourceCode = require('../utils/get-source-code.js') | ||
@@ -63,40 +64,2 @@ const toSingleLine = require('../utils/to-single-line.js') | ||
properties: { | ||
customGroups: { | ||
items: { | ||
oneOf: [ | ||
{ | ||
properties: { | ||
...sortModules_types.customGroupNameJsonSchema, | ||
...sortModules_types.customGroupSortJsonSchema, | ||
anyOf: { | ||
items: { | ||
properties: { | ||
...sortModules_types.singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
type: 'array', | ||
}, | ||
}, | ||
description: 'Custom group block.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
{ | ||
properties: { | ||
...sortModules_types.customGroupNameJsonSchema, | ||
...sortModules_types.customGroupSortJsonSchema, | ||
...sortModules_types.singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
description: 'Specifies custom groups.', | ||
type: 'array', | ||
}, | ||
partitionByComment: { | ||
@@ -107,2 +70,6 @@ ...commonJsonSchemas.partitionByCommentJsonSchema, | ||
}, | ||
customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: | ||
sortModules_types.singleCustomGroupJsonSchema, | ||
}), | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
@@ -333,3 +300,2 @@ specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
} | ||
let comments = getCommentsBefore.getCommentsBefore(node, sourceCode) | ||
let lastSortingNode = | ||
@@ -348,3 +314,6 @@ (_a = formattedNodes.at(-1)) == null ? void 0 : _a.at(-1) | ||
options.partitionByComment, | ||
comments, | ||
getCommentsBefore.getCommentsBefore({ | ||
sourceCode, | ||
node, | ||
}), | ||
)) | ||
@@ -364,3 +333,6 @@ ) { | ||
getGroupCompareOptions: groupNumber => | ||
sortModulesUtils.getCompareOptions(options, groupNumber), | ||
getCustomGroupsCompareOptions.getCustomGroupsCompareOptions( | ||
options, | ||
groupNumber, | ||
), | ||
ignoreEslintDisabledNodes, | ||
@@ -367,0 +339,0 @@ }), |
'use strict' | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
let allSelectors = [ | ||
@@ -13,33 +14,3 @@ 'enum', | ||
let allModifiers = ['async', 'declare', 'decorated', 'default', 'export'] | ||
let customGroupSortJsonSchema = { | ||
type: { | ||
enum: ['alphabetical', 'line-length', 'natural', 'unsorted'], | ||
description: 'Custom group sort type.', | ||
type: 'string', | ||
}, | ||
order: { | ||
description: 'Custom group sort order.', | ||
enum: ['desc', 'asc'], | ||
type: 'string', | ||
}, | ||
} | ||
let customGroupNameJsonSchema = { | ||
groupName: { | ||
description: 'Custom group name.', | ||
type: 'string', | ||
}, | ||
} | ||
let singleCustomGroupJsonSchema = { | ||
modifiers: { | ||
items: { | ||
enum: allModifiers, | ||
type: 'string', | ||
}, | ||
description: 'Modifier filters.', | ||
type: 'array', | ||
}, | ||
elementValuePattern: { | ||
description: 'Element value pattern filter for properties.', | ||
type: 'string', | ||
}, | ||
decoratorNamePattern: { | ||
@@ -49,16 +20,9 @@ description: 'Decorator name pattern filter.', | ||
}, | ||
selector: { | ||
description: 'Selector filter.', | ||
enum: allSelectors, | ||
type: 'string', | ||
}, | ||
elementNamePattern: { | ||
description: 'Element name pattern filter.', | ||
type: 'string', | ||
}, | ||
modifiers: | ||
commonJsonSchemas.buildCustomGroupModifiersJsonSchema(allModifiers), | ||
selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors), | ||
elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema, | ||
} | ||
exports.allModifiers = allModifiers | ||
exports.allSelectors = allSelectors | ||
exports.customGroupNameJsonSchema = customGroupNameJsonSchema | ||
exports.customGroupSortJsonSchema = customGroupSortJsonSchema | ||
exports.singleCustomGroupJsonSchema = singleCustomGroupJsonSchema |
@@ -70,3 +70,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(specifier, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: specifier, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -73,0 +76,0 @@ (options.partitionByNewLine && |
@@ -79,3 +79,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(specifier, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: specifier, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -82,0 +85,0 @@ (options.partitionByNewLine && |
'use strict' | ||
Object.defineProperties(exports, { | ||
__esModule: { value: true }, | ||
[Symbol.toStringTag]: { value: 'Module' }, | ||
}) | ||
const commonJsonSchemas = require('../utils/common-json-schemas.js') | ||
const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js') | ||
const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js') | ||
const validateGeneratedGroupsConfiguration = require('./validate-generated-groups-configuration.js') | ||
const getCustomGroupsCompareOptions = require('./get-custom-groups-compare-options.js') | ||
const generatePredefinedGroups = require('../utils/generate-predefined-groups.js') | ||
const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js') | ||
const sortObjectTypes_types = require('./sort-object-types.types.js') | ||
const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js') | ||
@@ -14,2 +21,3 @@ const isPartitionComment = require('../utils/is-partition-comment.js') | ||
const isMemberOptional = require('../utils/is-member-optional.js') | ||
const sortObjectTypesUtils = require('./sort-object-types-utils.js') | ||
const getLinesBetween = require('../utils/get-lines-between.js') | ||
@@ -26,2 +34,4 @@ const getGroupNumber = require('../utils/get-group-number.js') | ||
const pairwise = require('../utils/pairwise.js') | ||
const matches = require('../utils/matches.js') | ||
let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map() | ||
let defaultOptions = { | ||
@@ -34,2 +44,3 @@ partitionByComment: false, | ||
groupKind: 'mixed', | ||
ignorePattern: [], | ||
ignoreCase: true, | ||
@@ -41,225 +52,45 @@ customGroups: {}, | ||
} | ||
let jsonSchema = { | ||
properties: { | ||
ignorePattern: { | ||
description: | ||
'Specifies names or patterns for nodes that should be ignored by rule.', | ||
items: { | ||
type: 'string', | ||
}, | ||
type: 'array', | ||
}, | ||
partitionByComment: { | ||
...commonJsonSchemas.partitionByCommentJsonSchema, | ||
description: | ||
'Allows you to use comments to separate members into logical groups.', | ||
}, | ||
customGroups: { | ||
oneOf: [ | ||
commonJsonSchemas.customGroupsJsonSchema, | ||
commonJsonSchemas.buildCustomGroupsArrayJsonSchema({ | ||
singleCustomGroupJsonSchema: | ||
sortObjectTypes_types.singleCustomGroupJsonSchema, | ||
}), | ||
], | ||
}, | ||
groupKind: { | ||
enum: ['mixed', 'required-first', 'optional-first'], | ||
description: 'Specifies top-level groups.', | ||
type: 'string', | ||
}, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
type: commonJsonSchemas.typeJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
} | ||
const sortObjectTypes = createEslintRule.createEslintRule({ | ||
create: context => ({ | ||
TSTypeLiteral: node => { | ||
if (!isSortable.isSortable(node.members)) { | ||
return | ||
} | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateGroupsConfiguration.validateGroupsConfiguration( | ||
options.groups, | ||
['multiline', 'method', 'unknown'], | ||
Object.keys(options.customGroups), | ||
) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = node.members.reduce( | ||
(accumulator, member) => { | ||
var _a, _b, _c, _d, _e | ||
let name | ||
let lastSortingNode = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
let { setCustomGroups, defineGroup, getGroup } = | ||
useGroups.useGroups(options) | ||
let formatName = value => value.replace(/[,;]$/u, '') | ||
if (member.type === 'TSPropertySignature') { | ||
if (member.key.type === 'Identifier') { | ||
;({ name } = member.key) | ||
} else if (member.key.type === 'Literal') { | ||
name = `${member.key.value}` | ||
} else { | ||
name = sourceCode.text.slice( | ||
member.range.at(0), | ||
((_b = member.typeAnnotation) == null | ||
? void 0 | ||
: _b.range.at(0)) ?? member.range.at(1), | ||
) | ||
} | ||
} else if (member.type === 'TSIndexSignature') { | ||
let endIndex = | ||
((_c = member.typeAnnotation) == null | ||
? void 0 | ||
: _c.range.at(0)) ?? member.range.at(1) | ||
name = formatName( | ||
sourceCode.text.slice(member.range.at(0), endIndex), | ||
) | ||
} else { | ||
name = formatName( | ||
sourceCode.text.slice(member.range.at(0), member.range.at(1)), | ||
) | ||
} | ||
setCustomGroups(options.customGroups, name) | ||
if ( | ||
member.type === 'TSMethodSignature' || | ||
(member.type === 'TSPropertySignature' && | ||
((_d = member.typeAnnotation) == null | ||
? void 0 | ||
: _d.typeAnnotation.type) === 'TSFunctionType') | ||
) { | ||
defineGroup('method') | ||
} | ||
if (member.loc.start.line !== member.loc.end.line) { | ||
defineGroup('multiline') | ||
} | ||
let sortingNode = { | ||
isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled( | ||
member, | ||
eslintDisabledLines, | ||
), | ||
groupKind: isMemberOptional.isMemberOptional(member) | ||
? 'optional' | ||
: 'required', | ||
size: rangeToDiff.rangeToDiff(member, sourceCode), | ||
addSafetySemicolonWhenInline: true, | ||
group: getGroup(), | ||
node: member, | ||
name, | ||
} | ||
if ( | ||
(options.partitionByComment && | ||
isPartitionComment.hasPartitionComment( | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(member, sourceCode), | ||
)) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
) { | ||
accumulator.push([]) | ||
} | ||
;(_e = accumulator.at(-1)) == null ? void 0 : _e.push(sortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
let groupKindOrder | ||
if (options.groupKind === 'required-first') { | ||
groupKindOrder = ['required', 'optional'] | ||
} else if (options.groupKind === 'optional-first') { | ||
groupKindOrder = ['optional', 'required'] | ||
} else { | ||
groupKindOrder = ['any'] | ||
} | ||
for (let nodes of formattedMembers) { | ||
let filteredGroupKindNodes = groupKindOrder.map(groupKind => | ||
nodes.filter( | ||
currentNode => | ||
groupKind === 'any' || currentNode.groupKind === groupKind, | ||
), | ||
) | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
filteredGroupKindNodes.flatMap(groupedNodes => | ||
sortNodesByGroups.sortNodesByGroups(groupedNodes, options, { | ||
ignoreEslintDisabledNodes, | ||
}), | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
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 = [] | ||
if ( | ||
indexOfLeft > indexOfRight || | ||
indexOfLeft >= indexOfRightExcludingEslintDisabled | ||
) { | ||
messageIds.push( | ||
leftNumber === rightNumber | ||
? 'unexpectedObjectTypesOrder' | ||
: 'unexpectedObjectTypesGroupOrder', | ||
) | ||
} | ||
messageIds = [ | ||
...messageIds, | ||
...getNewlinesErrors.getNewlinesErrors({ | ||
missedSpacingError: 'missedSpacingBetweenObjectTypeMembers', | ||
extraSpacingError: 'extraSpacingBetweenObjectTypeMembers', | ||
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 type members into logical groups.', | ||
}, | ||
groupKind: { | ||
enum: ['mixed', 'required-first', 'optional-first'], | ||
description: 'Specifies top-level groups.', | ||
type: 'string', | ||
}, | ||
partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema, | ||
specialCharacters: commonJsonSchemas.specialCharactersJsonSchema, | ||
newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema, | ||
customGroups: commonJsonSchemas.customGroupsJsonSchema, | ||
ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema, | ||
locales: commonJsonSchemas.localesJsonSchema, | ||
groups: commonJsonSchemas.groupsJsonSchema, | ||
order: commonJsonSchemas.orderJsonSchema, | ||
type: commonJsonSchemas.typeJsonSchema, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
messages: { | ||
@@ -280,8 +111,300 @@ unexpectedObjectTypesGroupOrder: | ||
}, | ||
schema: [jsonSchema], | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
TSTypeLiteral: node => | ||
sortObjectTypeElements({ | ||
availableMessageIds: { | ||
missedSpacingBetweenMembers: 'missedSpacingBetweenObjectTypeMembers', | ||
extraSpacingBetweenMembers: 'extraSpacingBetweenObjectTypeMembers', | ||
unexpectedGroupOrder: 'unexpectedObjectTypesGroupOrder', | ||
unexpectedOrder: 'unexpectedObjectTypesOrder', | ||
}, | ||
parentNodeName: | ||
node.parent.type === 'TSTypeAliasDeclaration' | ||
? node.parent.id.name | ||
: null, | ||
elements: node.members, | ||
context, | ||
}), | ||
}), | ||
defaultOptions: [defaultOptions], | ||
name: 'sort-object-types', | ||
}) | ||
module.exports = sortObjectTypes | ||
let sortObjectTypeElements = ({ | ||
availableMessageIds, | ||
parentNodeName, | ||
elements, | ||
context, | ||
}) => { | ||
if (!isSortable.isSortable(elements)) { | ||
return | ||
} | ||
let settings = getSettings.getSettings(context.settings) | ||
let options = complete.complete( | ||
context.options.at(0), | ||
settings, | ||
defaultOptions, | ||
) | ||
validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration({ | ||
customGroups: options.customGroups, | ||
selectors: sortObjectTypes_types.allSelectors, | ||
modifiers: sortObjectTypes_types.allModifiers, | ||
groups: options.groups, | ||
}) | ||
validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration( | ||
options, | ||
) | ||
if ( | ||
options.ignorePattern.some( | ||
pattern => parentNodeName && matches.matches(parentNodeName, pattern), | ||
) | ||
) { | ||
return | ||
} | ||
let sourceCode = getSourceCode.getSourceCode(context) | ||
let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({ | ||
ruleName: context.id, | ||
sourceCode, | ||
}) | ||
let formattedMembers = elements.reduce( | ||
(accumulator, typeElement) => { | ||
var _a, _b, _c, _d, _e | ||
if (typeElement.type === 'TSCallSignatureDeclaration') { | ||
accumulator.push([]) | ||
return accumulator | ||
} | ||
let name | ||
let lastSortingNode = | ||
(_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
let { setCustomGroups, defineGroup, getGroup } = | ||
useGroups.useGroups(options) | ||
let formatName = value => value.replace(/[,;]$/u, '') | ||
if (typeElement.type === 'TSPropertySignature') { | ||
if (typeElement.key.type === 'Identifier') { | ||
;({ name } = typeElement.key) | ||
} else if (typeElement.key.type === 'Literal') { | ||
name = `${typeElement.key.value}` | ||
} else { | ||
let end = | ||
((_b = typeElement.typeAnnotation) == null | ||
? void 0 | ||
: _b.range.at(0)) ?? | ||
typeElement.range.at(1) - (typeElement.optional ? '?'.length : 0) | ||
name = sourceCode.text.slice(typeElement.range.at(0), end) | ||
} | ||
} else if (typeElement.type === 'TSIndexSignature') { | ||
let endIndex = | ||
((_c = typeElement.typeAnnotation) == null | ||
? void 0 | ||
: _c.range.at(0)) ?? typeElement.range.at(1) | ||
name = formatName( | ||
sourceCode.text.slice(typeElement.range.at(0), endIndex), | ||
) | ||
} else if ( | ||
typeElement.type === 'TSMethodSignature' && | ||
'name' in typeElement.key | ||
) { | ||
;({ name } = typeElement.key) | ||
} else { | ||
name = formatName( | ||
sourceCode.text.slice( | ||
typeElement.range.at(0), | ||
typeElement.range.at(1), | ||
), | ||
) | ||
} | ||
let selectors = [] | ||
let modifiers = [] | ||
if (typeElement.type === 'TSIndexSignature') { | ||
selectors.push('index-signature') | ||
} | ||
if ( | ||
typeElement.type === 'TSMethodSignature' || | ||
(typeElement.type === 'TSPropertySignature' && | ||
((_d = typeElement.typeAnnotation) == null | ||
? void 0 | ||
: _d.typeAnnotation.type) === 'TSFunctionType') | ||
) { | ||
selectors.push('method') | ||
} | ||
if (typeElement.loc.start.line !== typeElement.loc.end.line) { | ||
modifiers.push('multiline') | ||
selectors.push('multiline') | ||
} | ||
if ( | ||
!selectors.includes('index-signature') && | ||
!selectors.includes('method') | ||
) { | ||
selectors.push('property') | ||
} | ||
selectors.push('member') | ||
if (isMemberOptional.isMemberOptional(typeElement)) { | ||
modifiers.push('optional') | ||
} else { | ||
modifiers.push('required') | ||
} | ||
for (let predefinedGroup of generatePredefinedGroups.generatePredefinedGroups( | ||
{ | ||
cache: cachedGroupsByModifiersAndSelectors, | ||
selectors, | ||
modifiers, | ||
}, | ||
)) { | ||
defineGroup(predefinedGroup) | ||
} | ||
if (Array.isArray(options.customGroups)) { | ||
for (let customGroup of options.customGroups) { | ||
if ( | ||
sortObjectTypesUtils.customGroupMatches({ | ||
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( | ||
typeElement, | ||
eslintDisabledLines, | ||
), | ||
groupKind: isMemberOptional.isMemberOptional(typeElement) | ||
? 'optional' | ||
: 'required', | ||
size: rangeToDiff.rangeToDiff(typeElement, sourceCode), | ||
addSafetySemicolonWhenInline: true, | ||
group: getGroup(), | ||
node: typeElement, | ||
name, | ||
} | ||
if ( | ||
(options.partitionByComment && | ||
isPartitionComment.hasPartitionComment( | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore({ | ||
node: typeElement, | ||
sourceCode, | ||
}), | ||
)) || | ||
(options.partitionByNewLine && | ||
lastSortingNode && | ||
getLinesBetween.getLinesBetween( | ||
sourceCode, | ||
lastSortingNode, | ||
sortingNode, | ||
)) | ||
) { | ||
accumulator.push([]) | ||
} | ||
;(_e = accumulator.at(-1)) == null ? void 0 : _e.push(sortingNode) | ||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
let groupKindOrder | ||
if (options.groupKind === 'required-first') { | ||
groupKindOrder = ['required', 'optional'] | ||
} else if (options.groupKind === 'optional-first') { | ||
groupKindOrder = ['optional', 'required'] | ||
} else { | ||
groupKindOrder = ['any'] | ||
} | ||
for (let nodes of formattedMembers) { | ||
let filteredGroupKindNodes = groupKindOrder.map(groupKind => | ||
nodes.filter( | ||
currentNode => | ||
groupKind === 'any' || currentNode.groupKind === groupKind, | ||
), | ||
) | ||
let sortNodesExcludingEslintDisabled = ignoreEslintDisabledNodes => | ||
filteredGroupKindNodes.flatMap(groupedNodes => | ||
sortNodesByGroups.sortNodesByGroups(groupedNodes, options, { | ||
getGroupCompareOptions: groupNumber => | ||
getCustomGroupsCompareOptions.getCustomGroupsCompareOptions( | ||
options, | ||
groupNumber, | ||
), | ||
ignoreEslintDisabledNodes, | ||
}), | ||
) | ||
let sortedNodes = sortNodesExcludingEslintDisabled(false) | ||
let sortedNodesExcludingEslintDisabled = | ||
sortNodesExcludingEslintDisabled(true) | ||
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 = [] | ||
if ( | ||
indexOfLeft > indexOfRight || | ||
indexOfLeft >= 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, | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
exports.default = sortObjectTypes | ||
exports.jsonSchema = jsonSchema | ||
exports.sortObjectTypeElements = sortObjectTypeElements |
@@ -221,6 +221,2 @@ 'use strict' | ||
} | ||
let comments = getCommentsBefore.getCommentsBefore( | ||
property, | ||
sourceCode, | ||
) | ||
let lastProperty = | ||
@@ -274,3 +270,6 @@ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1) | ||
options.partitionByComment, | ||
comments, | ||
getCommentsBefore.getCommentsBefore({ | ||
node: property, | ||
sourceCode, | ||
}), | ||
)) | ||
@@ -277,0 +276,0 @@ ) { |
@@ -147,3 +147,7 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(type, sourceCode, '|'), | ||
getCommentsBefore.getCommentsBefore({ | ||
tokenValueToIgnoreBefore: '|', | ||
node: type, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -150,0 +154,0 @@ (options.partitionByNewLine && |
@@ -149,3 +149,6 @@ 'use strict' | ||
options.partitionByComment, | ||
getCommentsBefore.getCommentsBefore(declaration, sourceCode), | ||
getCommentsBefore.getCommentsBefore({ | ||
node: declaration, | ||
sourceCode, | ||
}), | ||
)) || | ||
@@ -152,0 +155,0 @@ (options.partitionByNewLine && |
@@ -11,3 +11,5 @@ 'use strict' | ||
let availableCustomGroupNames = new Set( | ||
customGroups.map(customGroup => customGroup.groupName), | ||
Array.isArray(customGroups) | ||
? customGroups.map(customGroup => customGroup.groupName) | ||
: Object.keys(customGroups), | ||
) | ||
@@ -14,0 +16,0 @@ let invalidGroups = groups |
@@ -99,3 +99,81 @@ 'use strict' | ||
} | ||
let customGroupSortJsonSchema = { | ||
type: { | ||
enum: ['alphabetical', 'line-length', 'natural', 'unsorted'], | ||
description: 'Custom group sort type.', | ||
type: 'string', | ||
}, | ||
order: { | ||
description: 'Custom group sort order.', | ||
enum: ['desc', 'asc'], | ||
type: 'string', | ||
}, | ||
} | ||
let customGroupNameJsonSchema = { | ||
groupName: { | ||
description: 'Custom group name.', | ||
type: 'string', | ||
}, | ||
} | ||
let buildCustomGroupsArrayJsonSchema = ({ singleCustomGroupJsonSchema }) => ({ | ||
items: { | ||
oneOf: [ | ||
{ | ||
properties: { | ||
...customGroupNameJsonSchema, | ||
...customGroupSortJsonSchema, | ||
anyOf: { | ||
items: { | ||
properties: { | ||
...singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
type: 'array', | ||
}, | ||
}, | ||
description: 'Custom group block.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
{ | ||
properties: { | ||
...customGroupNameJsonSchema, | ||
...customGroupSortJsonSchema, | ||
...singleCustomGroupJsonSchema, | ||
}, | ||
description: 'Custom group.', | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
description: 'Specifies custom groups.', | ||
type: 'array', | ||
}) | ||
let buildCustomGroupModifiersJsonSchema = modifiers => ({ | ||
items: { | ||
enum: modifiers, | ||
type: 'string', | ||
}, | ||
description: 'Modifier filters.', | ||
type: 'array', | ||
}) | ||
let buildCustomGroupSelectorJsonSchema = selectors => ({ | ||
description: 'Selector filter.', | ||
enum: selectors, | ||
type: 'string', | ||
}) | ||
let elementNamePatternJsonSchema = { | ||
description: 'Element name pattern filter.', | ||
type: 'string', | ||
} | ||
exports.buildCustomGroupModifiersJsonSchema = | ||
buildCustomGroupModifiersJsonSchema | ||
exports.buildCustomGroupSelectorJsonSchema = buildCustomGroupSelectorJsonSchema | ||
exports.buildCustomGroupsArrayJsonSchema = buildCustomGroupsArrayJsonSchema | ||
exports.customGroupsJsonSchema = customGroupsJsonSchema | ||
exports.elementNamePatternJsonSchema = elementNamePatternJsonSchema | ||
exports.groupsJsonSchema = groupsJsonSchema | ||
@@ -102,0 +180,0 @@ exports.ignoreCaseJsonSchema = ignoreCaseJsonSchema |
@@ -21,3 +21,2 @@ 'use strict' | ||
locale: options.locales.toString(), | ||
order: options.order, | ||
}) | ||
@@ -24,0 +23,0 @@ let formatString = getFormatStringFunction( |
'use strict' | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }) | ||
let getCommentsBefore = (node, source, tokenValueToIgnoreBefore) => { | ||
let commentsBefore = getCommentsBeforeNodeOrToken(source, node) | ||
let tokenBeforeNode = source.getTokenBefore(node) | ||
let getCommentsBefore = ({ tokenValueToIgnoreBefore, sourceCode, node }) => { | ||
let commentsBefore = getCommentsBeforeNodeOrToken(sourceCode, node) | ||
let tokenBeforeNode = sourceCode.getTokenBefore(node) | ||
if ( | ||
@@ -14,3 +14,3 @@ commentsBefore.length || | ||
} | ||
return getCommentsBeforeNodeOrToken(source, tokenBeforeNode) | ||
return getCommentsBeforeNodeOrToken(sourceCode, tokenBeforeNode) | ||
} | ||
@@ -17,0 +17,0 @@ let getCommentsBeforeNodeOrToken = (source, node) => |
@@ -7,3 +7,8 @@ 'use strict' | ||
const getCommentsBefore = require('./get-comments-before.js') | ||
let getNodeRange = (node, sourceCode, additionalOptions) => { | ||
let getNodeRange = ({ | ||
ignoreHighestBlockComment, | ||
sourceCode, | ||
options, | ||
node, | ||
}) => { | ||
let start = node.range.at(0) | ||
@@ -23,7 +28,7 @@ let end = node.range.at(1) | ||
} | ||
let comments = getCommentsBefore.getCommentsBefore(node, sourceCode) | ||
let partitionComment = | ||
(additionalOptions == null | ||
? void 0 | ||
: additionalOptions.partitionByComment) ?? false | ||
let comments = getCommentsBefore.getCommentsBefore({ | ||
sourceCode, | ||
node, | ||
}) | ||
let highestBlockComment = comments.find(comment => comment.type === 'Block') | ||
let relevantTopComment | ||
@@ -36,3 +41,6 @@ for (let i = comments.length - 1; i >= 0; i--) { | ||
if ( | ||
isPartitionComment.isPartitionComment(partitionComment, comment.value) || | ||
isPartitionComment.isPartitionComment( | ||
(options == null ? void 0 : options.partitionByComment) ?? false, | ||
comment.value, | ||
) || | ||
(eslintDisabledRules == null | ||
@@ -54,2 +62,5 @@ ? void 0 | ||
} | ||
if (ignoreHighestBlockComment && comment === highestBlockComment) { | ||
break | ||
} | ||
relevantTopComment = comment | ||
@@ -56,0 +67,0 @@ } |
@@ -5,3 +5,10 @@ 'use strict' | ||
const getNodeRange = require('./get-node-range.js') | ||
let makeFixes = ({ sortedNodes, sourceCode, options, fixer, nodes }) => { | ||
let makeFixes = ({ | ||
ignoreFirstNodeHighestBlockComment, | ||
sortedNodes, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}) => { | ||
let fixes = [] | ||
@@ -13,2 +20,4 @@ for (let max = nodes.length, i = 0; i < max; i++) { | ||
let { addSafetySemicolonWhenInline, node: sortedNode } = sortedSortingNode | ||
let isNodeFirstNode = node === nodes.at(0).node | ||
let isSortedNodeFirstNode = sortedNode === nodes.at(0).node | ||
if (node === sortedNode) { | ||
@@ -18,3 +27,9 @@ continue | ||
let sortedNodeCode = sourceCode.text.slice( | ||
...getNodeRange.getNodeRange(sortedNode, sourceCode, options), | ||
...getNodeRange.getNodeRange({ | ||
ignoreHighestBlockComment: | ||
ignoreFirstNodeHighestBlockComment && isSortedNodeFirstNode, | ||
node: sortedNode, | ||
sourceCode, | ||
options, | ||
}), | ||
) | ||
@@ -45,3 +60,9 @@ let sortedNodeText = sourceCode.getText(sortedNode) | ||
fixer.replaceTextRange( | ||
getNodeRange.getNodeRange(node, sourceCode, options), | ||
getNodeRange.getNodeRange({ | ||
ignoreHighestBlockComment: | ||
ignoreFirstNodeHighestBlockComment && isNodeFirstNode, | ||
sourceCode, | ||
options, | ||
node, | ||
}), | ||
sortedNodeCode, | ||
@@ -48,0 +69,0 @@ ), |
@@ -28,8 +28,11 @@ 'use strict' | ||
) | ||
let currentNodeRange = getNodeRange.getNodeRange( | ||
nodes.at(i).node, | ||
let currentNodeRange = getNodeRange.getNodeRange({ | ||
node: nodes.at(i).node, | ||
sourceCode, | ||
) | ||
}) | ||
let nextNodeRangeStart = getNodeRange | ||
.getNodeRange(nodes.at(i + 1).node, sourceCode) | ||
.getNodeRange({ | ||
node: nodes.at(i + 1).node, | ||
sourceCode, | ||
}) | ||
.at(0) | ||
@@ -36,0 +39,0 @@ let rangeToReplace = [currentNodeRange.at(1), nextNodeRangeStart] |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.", | ||
@@ -17,4 +17,4 @@ "keywords": [ | ||
".": { | ||
"default": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
@@ -27,4 +27,4 @@ "./package.json": "./package.json" | ||
"dependencies": { | ||
"@typescript-eslint/types": "^8.16.0", | ||
"@typescript-eslint/utils": "^8.16.0", | ||
"@typescript-eslint/types": "^8.17.0", | ||
"@typescript-eslint/utils": "^8.17.0", | ||
"natural-orderby": "^5.0.0" | ||
@@ -31,0 +31,0 @@ }, |
73
269595
7521