From 3de399fa719feb03939b4932f7798d071720e679 Mon Sep 17 00:00:00 2001 From: Sergio Date: Sat, 13 Apr 2024 18:55:01 +0800 Subject: [PATCH] fix: sort-objects ignore-pattern add property type --- rules/sort-objects.ts | 37 +++++++++++++++----------- test/sort-objects.test.ts | 55 ++++++++++++++++++++++++++++++++++++++- utils/get-node-parent.ts | 17 ++++++++++++ 3 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 utils/get-node-parent.ts diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index 68f49348..ee31b83f 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -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' @@ -132,21 +133,27 @@ export default createEslintRule({ 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) => diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index 26b9df5d..dd533fd8 100644 --- a/test/sort-objects.test.ts +++ b/test/sort-objects.test.ts @@ -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', + }, + }, + ], + }, + ], }) }) }) diff --git a/utils/get-node-parent.ts b/utils/get-node-parent.ts new file mode 100644 index 00000000..b55d94e3 --- /dev/null +++ b/utils/get-node-parent.ts @@ -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 +}