Skip to content

Commit

Permalink
fix: sort-objects ignore-pattern add property type
Browse files Browse the repository at this point in the history
  • Loading branch information
KID-joker committed Apr 13, 2024
1 parent 3ad40ff commit 3de399f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
37 changes: 22 additions & 15 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCommentBefore } from '../utils/get-comment-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
import { getNodeParent } from '../utils/get-node-parent'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
Expand Down Expand Up @@ -132,21 +133,27 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [],
})

let variableIdentifier =
node.parent.type === 'VariableDeclarator' &&
node.parent.id.type === 'Identifier'
? node.parent.id.name
: null

let shouldIgnore =
options['ignore-pattern'].length &&
typeof variableIdentifier === 'string'
? options['ignore-pattern'].some(pattern =>
minimatch(variableIdentifier!, pattern, {
nocomment: true,
}),
)
: false
let shouldIgnore = false
if (options['ignore-pattern'].length) {
let parent = getNodeParent(node, ['VariableDeclarator', 'Property'])
let parentId =
parent?.type === 'VariableDeclarator'
? parent.id
: (parent as TSESTree.Property | null)?.key
let variableIdentifier =
parentId?.type === 'Identifier' ? parentId.name : null

if (
typeof variableIdentifier === 'string' &&
options['ignore-pattern'].some(pattern =>
minimatch(variableIdentifier!, pattern, {
nocomment: true,
}),
)
) {
shouldIgnore = true
}
}

if (!shouldIgnore && node.properties.length > 1) {
let isStyledCallExpression = (identifier: TSESTree.Expression) =>
Expand Down
55 changes: 54 additions & 1 deletion test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,60 @@ describe(RULE_NAME, () => {
],
},
],
invalid: [],
invalid: [
{
code: dedent`
export default {
methods: {
foo() {},
bar() {},
baz() {},
},
data() {
return {
background: "palevioletred",
display: 'flex',
flexDirection: 'column',
width: "50px",
height: "50px",
}
},
}
`,
output: dedent`
export default {
data() {
return {
background: "palevioletred",
display: 'flex',
flexDirection: 'column',
width: "50px",
height: "50px",
}
},
methods: {
foo() {},
bar() {},
baz() {},
},
}
`,
options: [
{
'ignore-pattern': ['data', 'methods'],
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'methods',
right: 'data',
},
},
],
},
],
})
})
})
17 changes: 17 additions & 0 deletions utils/get-node-parent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { TSESTree } from '@typescript-eslint/types'

export let getNodeParent = (
node: TSESTree.Node,
type: string[] | string,
): TSESTree.Node | null => {
let types = Array.isArray(type) ? type : [type]
let { parent } = node as { parent: TSESTree.Node | null }
while (parent) {
if (types.includes(parent.type)) {
return parent
}

;({ parent } = parent as { parent: TSESTree.Node | null })
}
return null
}

0 comments on commit 3de399f

Please sign in to comment.