eslint-plugin-perfectionist
Advanced tools
Comparing version 4.0.3 to 4.1.0
@@ -33,4 +33,4 @@ 'use strict' | ||
create: context => ({ | ||
TSEnumDeclaration: node => { | ||
let members = getEnumMembers.getEnumMembers(node) | ||
TSEnumDeclaration: enumDeclaration => { | ||
let members = getEnumMembers.getEnumMembers(enumDeclaration) | ||
if ( | ||
@@ -86,3 +86,6 @@ !isSortable.isSortable(members) || | ||
if (member.initializer) { | ||
dependencies = extractDependencies(member.initializer, node.id.name) | ||
dependencies = extractDependencies( | ||
member.initializer, | ||
enumDeclaration.id.name, | ||
) | ||
} | ||
@@ -92,2 +95,5 @@ let lastSortingNode = | ||
let sortingNode = { | ||
numericValue: member.initializer | ||
? getExpressionNumberValue(member.initializer) | ||
: null, | ||
name: | ||
@@ -126,9 +132,8 @@ member.id.type === 'Literal' | ||
) | ||
let isNumericEnum = members.every(member => { | ||
var _a | ||
return ( | ||
((_a = member.initializer) == null ? void 0 : _a.type) === | ||
'Literal' && typeof member.initializer.value === 'number' | ||
) | ||
}) | ||
let sortingNodes = formattedMembers.flat() | ||
let isNumericEnum = sortingNodes.every( | ||
sortingNode => | ||
sortingNode.numericValue !== null && | ||
!Number.isNaN(sortingNode.numericValue), | ||
) | ||
let compareOptions = { | ||
@@ -140,4 +145,6 @@ // Get the enum value rather than the name if needed | ||
var _a, _b | ||
if (isNumericEnum) { | ||
return sortingNode.numericValue.toString() | ||
} | ||
if ( | ||
sortingNode.node.type === 'TSEnumMember' && | ||
((_a = sortingNode.node.initializer) == null | ||
@@ -168,4 +175,4 @@ ? void 0 | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
formattedMembers.flatMap(nodes2 => | ||
sortNodes.sortNodes(nodes2, compareOptions, { | ||
formattedMembers.flatMap(nodes => | ||
sortNodes.sortNodes(nodes, compareOptions, { | ||
ignoreEslintDisabledNodes, | ||
@@ -181,4 +188,3 @@ }), | ||
sortNodesIgnoringEslintDisabledNodes(true) | ||
let nodes = formattedMembers.flat() | ||
pairwise.pairwise(nodes, (left, right) => { | ||
pairwise.pairwise(sortingNodes, (left, right) => { | ||
let indexOfLeft = sortedNodes.indexOf(left) | ||
@@ -195,3 +201,6 @@ let indexOfRight = sortedNodes.indexOf(right) | ||
let firstUnorderedNodeDependentOnRight = | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn(right, nodes) | ||
sortNodesByDependencies.getFirstUnorderedNodeDependentOn( | ||
right, | ||
sortingNodes, | ||
) | ||
context.report({ | ||
@@ -201,6 +210,6 @@ fix: fixer => | ||
sortedNodes: sortedNodesExcludingEslintDisabled, | ||
nodes: sortingNodes, | ||
sourceCode, | ||
options, | ||
fixer, | ||
nodes, | ||
}), | ||
@@ -268,2 +277,70 @@ data: { | ||
}) | ||
let getExpressionNumberValue = expression => { | ||
switch (expression.type) { | ||
case 'BinaryExpression': | ||
return getBinaryExpressionNumberValue( | ||
expression.left, | ||
expression.right, | ||
expression.operator, | ||
) | ||
case 'UnaryExpression': | ||
return getUnaryExpressionNumberValue( | ||
expression.argument, | ||
expression.operator, | ||
) | ||
case 'Literal': | ||
return typeof expression.value === 'number' | ||
? expression.value | ||
: Number.NaN | ||
default: | ||
return Number.NaN | ||
} | ||
} | ||
let getUnaryExpressionNumberValue = (argumentExpression, operator) => { | ||
let argument = getExpressionNumberValue(argumentExpression) | ||
switch (operator) { | ||
case '+': | ||
return argument | ||
case '-': | ||
return -argument | ||
case '~': | ||
return ~argument | ||
default: | ||
return Number.NaN | ||
} | ||
} | ||
let getBinaryExpressionNumberValue = ( | ||
leftExpression, | ||
rightExpression, | ||
operator, | ||
) => { | ||
let left = getExpressionNumberValue(leftExpression) | ||
let right = getExpressionNumberValue(rightExpression) | ||
switch (operator) { | ||
case '**': | ||
return left ** right | ||
case '>>': | ||
return left >> right | ||
case '<<': | ||
return left << right | ||
case '+': | ||
return left + right | ||
case '-': | ||
return left - right | ||
case '*': | ||
return left * right | ||
case '/': | ||
return left / right | ||
case '%': | ||
return left % right | ||
case '|': | ||
return left | right | ||
case '&': | ||
return left & right | ||
case '^': | ||
return left ^ right | ||
default: | ||
return Number.NaN | ||
} | ||
} | ||
module.exports = sortEnums |
@@ -23,2 +23,3 @@ 'use strict' | ||
const makeFixes = require('../utils/make-fixes.js') | ||
const sortNodes = require('../utils/sort-nodes.js') | ||
const complete = require('../utils/complete.js') | ||
@@ -32,2 +33,4 @@ const pairwise = require('../utils/pairwise.js') | ||
specialCharacters: 'keep', | ||
destructuredObjects: true, | ||
objectDeclarations: true, | ||
styledComponents: true, | ||
@@ -45,3 +48,6 @@ destructureOnly: false, | ||
create: context => { | ||
let sortObject = node => { | ||
let sortObject = nodeObject => { | ||
if (!isSortable.isSortable(nodeObject.properties)) { | ||
return | ||
} | ||
let settings = getSettings.getSettings(context.settings) | ||
@@ -61,8 +67,12 @@ let options = complete.complete( | ||
) | ||
let shouldIgnore = false | ||
if (options.destructureOnly) { | ||
shouldIgnore = node.type !== 'ObjectPattern' | ||
let isDestructuredObject = nodeObject.type === 'ObjectPattern' | ||
if (isDestructuredObject) { | ||
if (!options.destructuredObjects) { | ||
return | ||
} | ||
} else if (options.destructureOnly || !options.objectDeclarations) { | ||
return | ||
} | ||
if (!shouldIgnore && options.ignorePattern.length) { | ||
let variableParent = getNodeParent.getNodeParent(node, [ | ||
if (options.ignorePattern.length) { | ||
let variableParent = getNodeParent.getNodeParent(nodeObject, [ | ||
'VariableDeclarator', | ||
@@ -90,5 +100,7 @@ 'Property', | ||
) { | ||
shouldIgnore = true | ||
return | ||
} | ||
let callParent = getNodeParent.getNodeParent(node, ['CallExpression']) | ||
let callParent = getNodeParent.getNodeParent(nodeObject, [ | ||
'CallExpression', | ||
]) | ||
let callIdentifier = | ||
@@ -100,8 +112,5 @@ (callParent == null ? void 0 : callParent.type) === | ||
if (callIdentifier && checkMatch(callIdentifier)) { | ||
shouldIgnore = true | ||
return | ||
} | ||
} | ||
if (shouldIgnore || !isSortable.isSortable(node.properties)) { | ||
return | ||
} | ||
let isStyledCallExpression = identifier => | ||
@@ -124,5 +133,5 @@ identifier.type === 'Identifier' && identifier.name === 'styled' | ||
!options.styledComponents && | ||
(isStyledComponents(node.parent) || | ||
(node.parent.type === 'ArrowFunctionExpression' && | ||
isStyledComponents(node.parent.parent))) | ||
(isStyledComponents(nodeObject.parent) || | ||
(nodeObject.parent.type === 'ArrowFunctionExpression' && | ||
isStyledComponents(nodeObject.parent.parent))) | ||
) { | ||
@@ -281,7 +290,13 @@ return | ||
) | ||
let formattedMembers = formatProperties(node.properties) | ||
let formattedMembers = formatProperties(nodeObject.properties) | ||
let nodesSortingFunction = | ||
isDestructuredObject && | ||
typeof options.destructuredObjects === 'object' && | ||
!options.destructuredObjects.groups | ||
? sortNodes.sortNodes | ||
: sortNodesByGroups.sortNodesByGroups | ||
let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes => | ||
sortNodesByDependencies.sortNodesByDependencies( | ||
formattedMembers.flatMap(nodes2 => | ||
sortNodesByGroups.sortNodesByGroups(nodes2, options, { | ||
nodesSortingFunction(nodes2, options, { | ||
ignoreEslintDisabledNodes, | ||
@@ -382,2 +397,21 @@ }), | ||
properties: { | ||
destructuredObjects: { | ||
oneOf: [ | ||
{ | ||
type: 'boolean', | ||
}, | ||
{ | ||
properties: { | ||
groups: { | ||
description: | ||
'Controls whether to use groups to sort destructured objects.', | ||
type: 'boolean', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
type: 'object', | ||
}, | ||
], | ||
description: 'Controls whether to sort destructured objects.', | ||
}, | ||
ignorePattern: { | ||
@@ -400,2 +434,6 @@ description: | ||
}, | ||
objectDeclarations: { | ||
description: 'Controls whether to sort object declarations.', | ||
type: 'boolean', | ||
}, | ||
styledComponents: { | ||
@@ -402,0 +440,0 @@ description: 'Controls whether to sort styled components.', |
{ | ||
"name": "eslint-plugin-perfectionist", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.", | ||
@@ -26,4 +26,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@typescript-eslint/types": "^8.15.0", | ||
"@typescript-eslint/utils": "^8.15.0", | ||
"@typescript-eslint/types": "^8.16.0", | ||
"@typescript-eslint/utils": "^8.16.0", | ||
"natural-orderby": "^5.0.0" | ||
@@ -30,0 +30,0 @@ }, |
274334
7575
3
9