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

feat: add custom ignore to enable customizable ignore rule #131

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(sort-objects): add ignore-function to enable customizable ignore…
… rule
KID-joker authored and azat-io committed Jul 17, 2024
commit 0aa760740e5569f3aa5315c7fb7a60d56d5b544f
4 changes: 3 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
@@ -97,6 +97,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
type: 'array',
},
'ignore-function': {
type: 'object',
},
groups: {
type: 'array',
},
@@ -136,7 +139,6 @@ export default createEslintRule<Options, MESSAGE_ID>({

let shouldIgnore = false

let ignoreFunctions = Object.values(options.customIgnore)
if (
ignoreFunctions.length &&
ignoreFunctions.some(fn => fn(node, context.filename))
64 changes: 64 additions & 0 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { TSESTree } from '@typescript-eslint/types'

import { RuleTester } from '@typescript-eslint/rule-tester'
import { afterAll, describe, it } from 'vitest'
import { dedent } from 'ts-dedent'
@@ -2866,6 +2868,32 @@ describe(RULE_NAME, () => {
},
],
},
{
code: dedent`
const buttonStyles = {
background: "palevioletred",
display: 'flex',
flexDirection: 'column',
width: "50px",
height: "50px",
}
`,
options: [
{
'ignore-function': {
ignoreButtonStyles: (node: TSESTree.ObjectExpression) => {
if (
node.parent.type === 'VariableDeclarator' &&
node.parent.id.type === 'Identifier'
) {
return node.parent.id.name === 'buttonStyles'
}
return false
},
},
},
],
},
],
invalid: [
{
@@ -2920,6 +2948,42 @@ describe(RULE_NAME, () => {
},
],
},
{
code: dedent`
const buttonStyles = {
background: "palevioletred",
display: 'flex',
flexDirection: 'column',
width: "50px",
height: "50px",
}
`,
output: dedent`
const buttonStyles = {
background: "palevioletred",
display: 'flex',
flexDirection: 'column',
height: "50px",
width: "50px",
}
`,
options: [
{
'ignore-function': {
ignoreButtonStyles: () => false,
},
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'width',
right: 'height',
},
},
],
},
],
})
})