Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort-objects ignore-pattern add Property type #127

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading