Skip to content

Commit

Permalink
refactor: uses getNodeName
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Jan 3, 2025
1 parent fc73e4b commit 09f6849
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
})
let matchedContextOptions = getMatchingContextOptions({
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,
}).find(options => {
if (!options.useConfigurationIf?.callingFunctionNamePattern) {
Expand Down Expand Up @@ -309,22 +313,13 @@ export default createEslintRule<Options, MESSAGE_ID>({

let lastProperty = accumulator.at(-1)?.at(-1)

let name: string
let dependencies: string[] = []

let { setCustomGroups, defineGroup, getGroup } = useGroups(options)

let selectors: Selector[] = []
let modifiers: Modifier[] = []

if (property.key.type === 'Identifier') {
;({ name } = property.key)
} else if (property.key.type === 'Literal') {
name = `${property.key.value}`
} else {
name = sourceCode.getText(property.key)
}

if (property.value.type === 'AssignmentPattern') {
dependencies = extractDependencies(property.value)
}
Expand Down Expand Up @@ -355,6 +350,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup(predefinedGroup)
}

let name = getNodeName({ sourceCode, property })
if (Array.isArray(options.customGroups)) {
for (let customGroup of options.customGroups) {
if (
Expand Down Expand Up @@ -631,15 +627,9 @@ let getNodeName = ({
sourceCode,
property,
}: {
property:
| TSESTree.ObjectLiteralElement
| TSESTree.RestElement
| TSESTree.Property
sourceCode: ReturnType<typeof getSourceCode>
}): string | null => {
if (property.type === 'SpreadElement' || property.type === 'RestElement') {
return null
}
property: TSESTree.Property
}): string => {
if (property.key.type === 'Identifier') {
return property.key.name
} else if (property.key.type === 'Literal') {
Expand Down

0 comments on commit 09f6849

Please sign in to comment.