@tanstack/eslint-plugin-query 5.59.1 → 5.59.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/rules/exhaustive-deps/exhaustive-deps.rule.cjs +1 -1
- package/dist/cjs/rules/exhaustive-deps/exhaustive-deps.rule.cjs.map +1 -1
- package/dist/cjs/rules/infinite-query-property-order/infinite-query-property-order.rule.cjs +5 -5
- package/dist/cjs/rules/infinite-query-property-order/infinite-query-property-order.rule.cjs.map +1 -1
- package/dist/cjs/utils/ast-utils.cjs +14 -0
- package/dist/cjs/utils/ast-utils.cjs.map +1 -1
- package/dist/esm/rules/exhaustive-deps/exhaustive-deps.rule.js +1 -1
- package/dist/esm/rules/exhaustive-deps/exhaustive-deps.rule.js.map +1 -1
- package/dist/esm/rules/infinite-query-property-order/infinite-query-property-order.rule.js +5 -5
- package/dist/esm/rules/infinite-query-property-order/infinite-query-property-order.rule.js.map +1 -1
- package/dist/esm/utils/ast-utils.js +14 -0
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/exhaustive-deps.test.ts +47 -0
- package/src/__tests__/test-utils.ts +2 -2
- package/src/rules/exhaustive-deps/exhaustive-deps.rule.ts +1 -1
- package/src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts +7 -5
- package/src/utils/ast-utils.ts +18 -0
|
@@ -82,7 +82,7 @@ const rule = createRule({
|
|
|
82
82
|
ref,
|
|
83
83
|
text: astUtils.ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode)
|
|
84
84
|
})).filter(({ ref, text }) => {
|
|
85
|
-
return !ref.isTypeReference && !astUtils.ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(
|
|
85
|
+
return !ref.isTypeReference && !astUtils.ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(/[?.]/)[0] ?? "");
|
|
86
86
|
}).map(({ ref, text }) => ({
|
|
87
87
|
identifier: ref.identifier,
|
|
88
88
|
text
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exhaustive-deps.rule.cjs","sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n Property: (node) => {\n if (\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.sourceCode.scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n !ASTUtils.isNodeOfOneOf(queryFn.value, [\n AST_NODE_TYPES.ArrowFunctionExpression,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ConditionalExpression,\n ])\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode: context.sourceCode,\n node: getQueryFnRelevantNode(queryFn),\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n sourceCode: context.sourceCode,\n reference,\n scopeManager,\n node: getQueryFnRelevantNode(queryFn),\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyNode).map(\n (identifier) =>\n ASTUtils.mapKeyNodeToText(identifier, context.sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split(
|
|
1
|
+
{"version":3,"file":"exhaustive-deps.rule.cjs","sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n Property: (node) => {\n if (\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.sourceCode.scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n !ASTUtils.isNodeOfOneOf(queryFn.value, [\n AST_NODE_TYPES.ArrowFunctionExpression,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ConditionalExpression,\n ])\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode: context.sourceCode,\n node: getQueryFnRelevantNode(queryFn),\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n sourceCode: context.sourceCode,\n reference,\n scopeManager,\n node: getQueryFnRelevantNode(queryFn),\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyNode).map(\n (identifier) =>\n ASTUtils.mapKeyNodeToText(identifier, context.sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split(/[?.]/)[0] ?? '')\n )\n })\n .map(({ ref, text }) => ({\n identifier: ref.identifier,\n text: text,\n }))\n\n const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text)\n\n if (uniqueMissingRefs.length > 0) {\n const missingAsText = uniqueMissingRefs\n .map((ref) =>\n ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n )\n .join(', ')\n\n const queryKeyValue = context.sourceCode.getText(queryKeyNode)\n\n const existingWithMissing =\n queryKeyValue === '[]'\n ? `[${missingAsText}]`\n : queryKeyValue.replace(/\\]$/, `, ${missingAsText}]`)\n\n const suggestions: TSESLint.ReportSuggestionArray<string> = []\n\n if (queryKeyNode.type === AST_NODE_TYPES.ArrayExpression) {\n suggestions.push({\n messageId: 'fixTo',\n data: { result: existingWithMissing },\n fix(fixer) {\n return fixer.replaceText(queryKeyNode, existingWithMissing)\n },\n })\n }\n\n context.report({\n node: node,\n messageId: 'missingDeps',\n data: {\n deps: uniqueMissingRefs.map((ref) => ref.text).join(', '),\n },\n suggest: suggestions,\n })\n }\n },\n }\n }),\n})\n\nfunction getQueryFnRelevantNode(queryFn: TSESTree.Property) {\n if (queryFn.value.type !== AST_NODE_TYPES.ConditionalExpression) {\n return queryFn.value\n }\n\n if (\n queryFn.value.consequent.type === AST_NODE_TYPES.Identifier &&\n queryFn.value.consequent.name === 'skipToken'\n ) {\n return queryFn.value.alternate\n }\n\n return queryFn.value.consequent\n}\n"],"names":["ESLintUtils","getDocsUrl","detectTanstackQueryImports","ASTUtils","AST_NODE_TYPES","ExhaustiveDepsUtils","uniqueBy"],"mappings":";;;;;;;;AASA,MAAM,YAAY;AAClB,MAAM,WAAW;AAEV,MAAM,OAAO;AAEpB,MAAM,aAAaA,MAAY,YAAA,YAA2BC,WAAAA,UAAU;AAE7D,MAAM,OAAO,WAAW;AAAA,EAC7B;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,aAAa;AAAA,MACb,aAAa;AAAA,IACf;AAAA,IACA,UAAU;AAAA,MACR,aAAa;AAAA,MACb,OAAO;AAAA,IACT;AAAA,IACA,gBAAgB;AAAA,IAChB,SAAS;AAAA,IACT,QAAQ,CAAC;AAAA,EACX;AAAA,EACA,gBAAgB,CAAC;AAAA,EAEjB,QAAQC,wBAAAA,2BAA2B,CAAC,YAAY;AACvC,WAAA;AAAA,MACL,UAAU,CAAC,SAAS;AAClB,YACE,CAACC,SAAA,SAAS,mBAAmB,KAAK,MAAM,KACxC,CAACA,SAAA,SAAS,qBAAqB,KAAK,KAAK,SAAS,GAClD;AACA;AAAA,QACF;AAEM,cAAA,eAAe,QAAQ,WAAW;AACxC,cAAM,WAAWA,SAAAA,SAAS;AAAA,UACxB,KAAK,OAAO;AAAA,UACZ;AAAA,QAAA;AAEF,cAAM,UAAUA,SAAAA,SAAS;AAAA,UACvB,KAAK,OAAO;AAAA,UACZ;AAAA,QAAA;AAIA,YAAA,iBAAiB,QACjB,aAAa,UACb,YAAY,UACZ,CAACA,SAAA,SAAS,cAAc,QAAQ,OAAO;AAAA,UACrCC,MAAAA,eAAe;AAAA,UACfA,MAAAA,eAAe;AAAA,UACfA,MAAAA,eA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
messageId: "invalidOrder",
|
|
71
71
|
fix(fixer) {
|
|
72
72
|
const sourceCode = context.sourceCode;
|
|
73
|
-
const
|
|
73
|
+
const reorderedText = sortedProperties.reduce(
|
|
74
74
|
(sourceText, specifier, index) => {
|
|
75
|
-
let
|
|
75
|
+
let textBetweenProperties = "";
|
|
76
76
|
if (index < allProperties.length - 1) {
|
|
77
|
-
|
|
77
|
+
textBetweenProperties = sourceCode.getText().slice(
|
|
78
78
|
allProperties[index].range[1],
|
|
79
79
|
allProperties[index + 1].range[0]
|
|
80
80
|
);
|
|
81
81
|
}
|
|
82
|
-
return sourceText + sourceCode.getText(specifier.property) +
|
|
82
|
+
return sourceText + sourceCode.getText(specifier.property) + textBetweenProperties;
|
|
83
83
|
},
|
|
84
84
|
""
|
|
85
85
|
);
|
|
86
86
|
return fixer.replaceTextRange(
|
|
87
87
|
[allProperties[0].range[0], allProperties.at(-1).range[1]],
|
|
88
|
-
|
|
88
|
+
reorderedText
|
|
89
89
|
);
|
|
90
90
|
}
|
|
91
91
|
});
|
package/dist/cjs/rules/infinite-query-property-order/infinite-query-property-order.rule.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infinite-query-property-order.rule.cjs","sources":["../../../../src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\n\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { sortDataByOrder } from './infinite-query-property-order.utils'\nimport { infiniteQueryFunctions, sortRules } from './constants'\nimport type { InfiniteQueryFunctions } from './constants'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nconst infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions)\nfunction isInfiniteQueryFunction(node: any): node is InfiniteQueryFunctions {\n return infiniteQueryFunctionsSet.has(node)\n}\n\nexport const name = 'infinite-query-property-order'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description:\n 'Ensure correct order of inference sensitive properties for infinite queries',\n recommended: 'error',\n },\n messages: {\n invalidOrder: 'Invalid order of properties for `{{function}}`.',\n },\n schema: [],\n hasSuggestions: true,\n fixable: 'code',\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n CallExpression(node) {\n if (node.callee.type !== AST_NODE_TYPES.Identifier) {\n return\n }\n const infiniteQueryFunction = node.callee.name\n if (!isInfiniteQueryFunction(infiniteQueryFunction)) {\n return\n }\n const argument = node.arguments[0]\n if (argument === undefined || argument.type !== 'ObjectExpression') {\n return\n }\n\n const allProperties = argument.properties\n\n // no need to sort if there is at max 1 property\n if (allProperties.length < 2) {\n return\n }\n\n const properties = allProperties.flatMap((p) => {\n if (\n p.type === AST_NODE_TYPES.Property &&\n p.key.type === AST_NODE_TYPES.Identifier\n ) {\n return { name: p.key.name, property: p }\n } else if (p.type === AST_NODE_TYPES.SpreadElement) {\n if (p.argument.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.name, property: p }\n } else if (p.argument.type === AST_NODE_TYPES.CallExpression) {\n if (p.argument.callee.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.callee.name, property: p }\n }\n }\n throw new Error('Unsupported spread element')\n }\n return []\n })\n\n const sortedProperties = sortDataByOrder(properties, sortRules, 'name')\n if (sortedProperties === null) {\n return\n }\n context.report({\n node: argument,\n data: { function: node.callee.name },\n messageId: 'invalidOrder',\n fix(fixer) {\n const sourceCode = context.sourceCode\n\n const
|
|
1
|
+
{"version":3,"file":"infinite-query-property-order.rule.cjs","sources":["../../../../src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\n\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { sortDataByOrder } from './infinite-query-property-order.utils'\nimport { infiniteQueryFunctions, sortRules } from './constants'\nimport type { InfiniteQueryFunctions } from './constants'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nconst infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions)\nfunction isInfiniteQueryFunction(node: any): node is InfiniteQueryFunctions {\n return infiniteQueryFunctionsSet.has(node)\n}\n\nexport const name = 'infinite-query-property-order'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description:\n 'Ensure correct order of inference sensitive properties for infinite queries',\n recommended: 'error',\n },\n messages: {\n invalidOrder: 'Invalid order of properties for `{{function}}`.',\n },\n schema: [],\n hasSuggestions: true,\n fixable: 'code',\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n CallExpression(node) {\n if (node.callee.type !== AST_NODE_TYPES.Identifier) {\n return\n }\n const infiniteQueryFunction = node.callee.name\n if (!isInfiniteQueryFunction(infiniteQueryFunction)) {\n return\n }\n const argument = node.arguments[0]\n if (argument === undefined || argument.type !== 'ObjectExpression') {\n return\n }\n\n const allProperties = argument.properties\n\n // no need to sort if there is at max 1 property\n if (allProperties.length < 2) {\n return\n }\n\n const properties = allProperties.flatMap((p) => {\n if (\n p.type === AST_NODE_TYPES.Property &&\n p.key.type === AST_NODE_TYPES.Identifier\n ) {\n return { name: p.key.name, property: p }\n } else if (p.type === AST_NODE_TYPES.SpreadElement) {\n if (p.argument.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.name, property: p }\n } else if (p.argument.type === AST_NODE_TYPES.CallExpression) {\n if (p.argument.callee.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.callee.name, property: p }\n }\n }\n throw new Error('Unsupported spread element')\n }\n return []\n })\n\n const sortedProperties = sortDataByOrder(properties, sortRules, 'name')\n if (sortedProperties === null) {\n return\n }\n context.report({\n node: argument,\n data: { function: node.callee.name },\n messageId: 'invalidOrder',\n fix(fixer) {\n const sourceCode = context.sourceCode\n\n const reorderedText = sortedProperties.reduce(\n (sourceText, specifier, index) => {\n let textBetweenProperties = ''\n if (index < allProperties.length - 1) {\n textBetweenProperties = sourceCode\n .getText()\n .slice(\n allProperties[index]!.range[1],\n allProperties[index + 1]!.range[0],\n )\n }\n return (\n sourceText +\n sourceCode.getText(specifier.property) +\n textBetweenProperties\n )\n },\n '',\n )\n return fixer.replaceTextRange(\n [allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],\n reorderedText,\n )\n },\n })\n },\n }\n }),\n})\n"],"names":["ESLintUtils","getDocsUrl","infiniteQueryFunctions","detectTanstackQueryImports","AST_NODE_TYPES","sortDataByOrder","sortRules"],"mappings":";;;;;;;AASA,MAAM,aAAaA,MAAY,YAAA,YAA2BC,WAAAA,UAAU;AAEpE,MAAM,4BAA4B,IAAI,IAAIC,UAAAA,sBAAsB;AAChE,SAAS,wBAAwB,MAA2C;AACnE,SAAA,0BAA0B,IAAI,IAAI;AAC3C;AAEO,MAAM,OAAO;AAEb,MAAM,OAAO,WAAW;AAAA,EAC7B;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,aACE;AAAA,MACF,aAAa;AAAA,IACf;AAAA,IACA,UAAU;AAAA,MACR,cAAc;AAAA,IAChB;AAAA,IACA,QAAQ,CAAC;AAAA,IACT,gBAAgB;AAAA,IAChB,SAAS;AAAA,EACX;AAAA,EACA,gBAAgB,CAAC;AAAA,EAEjB,QAAQC,wBAAAA,2BAA2B,CAAC,YAAY;AACvC,WAAA;AAAA,MACL,eAAe,MAAM;AACnB,YAAI,KAAK,OAAO,SAASC,MAAAA,eAAe,YAAY;AAClD;AAAA,QACF;AACM,cAAA,wBAAwB,KAAK,OAAO;AACtC,YAAA,CAAC,wBAAwB,qBAAqB,GAAG;AACnD;AAAA,QACF;AACM,cAAA,WAAW,KAAK,UAAU,CAAC;AACjC,YAAI,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,WAAW,QAAQ,UAAU,QAAQ,IACrC;AAAA,cAEJ;AAAA,cACA;AAAA,YAAA;AAEF,mBAAO,MAAM;AAAA,cACX,CAAC,cAAc,CAAC,EAAG,MAAM,CAAC,GAAG,cAAc,GAAG,EAAE,EAAG,MAAM,CAAC,CAAC;AAAA,cAC3D;AAAA,YAAA;AAAA,UAEJ;AAAA,QAAA,CACD;AAAA,MACH;AAAA,IAAA;AAAA,EACF,CACD;AACH,CAAC;;;"}
|
|
@@ -80,6 +80,20 @@ const ASTUtils = {
|
|
|
80
80
|
if (node.type === utils.AST_NODE_TYPES.TSNonNullExpression) {
|
|
81
81
|
identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
|
|
82
82
|
}
|
|
83
|
+
if (node.type === utils.AST_NODE_TYPES.ArrowFunctionExpression) {
|
|
84
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
|
|
85
|
+
}
|
|
86
|
+
if (node.type === utils.AST_NODE_TYPES.FunctionExpression) {
|
|
87
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
|
|
88
|
+
}
|
|
89
|
+
if (node.type === utils.AST_NODE_TYPES.BlockStatement) {
|
|
90
|
+
identifiers.push(
|
|
91
|
+
...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat()
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (node.type === utils.AST_NODE_TYPES.ReturnStatement && node.argument) {
|
|
95
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
|
|
96
|
+
}
|
|
83
97
|
return identifiers;
|
|
84
98
|
},
|
|
85
99
|
isAncestorIsCallee(identifier) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.cjs","sources":["../../../src/utils/ast-utils.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { uniqueBy } from './unique-by'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\n\nexport const ASTUtils = {\n isNodeOfOneOf<T extends AST_NODE_TYPES>(\n node: TSESTree.Node,\n types: ReadonlyArray<T>,\n ): node is TSESTree.Node & { type: T } {\n return types.includes(node.type as T)\n },\n isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {\n return node.type === AST_NODE_TYPES.Identifier\n },\n isIdentifierWithName(\n node: TSESTree.Node,\n name: string,\n ): node is TSESTree.Identifier {\n return ASTUtils.isIdentifier(node) && node.name === name\n },\n isIdentifierWithOneOfNames<T extends Array<string>>(\n node: TSESTree.Node,\n name: T,\n ): node is TSESTree.Identifier & { name: T[number] } {\n return ASTUtils.isIdentifier(node) && name.includes(node.name)\n },\n isProperty(node: TSESTree.Node): node is TSESTree.Property {\n return node.type === AST_NODE_TYPES.Property\n },\n isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {\n return node.type === AST_NODE_TYPES.ObjectExpression\n },\n isPropertyWithIdentifierKey(\n node: TSESTree.Node,\n key: string,\n ): node is TSESTree.Property {\n return (\n ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)\n )\n },\n findPropertyWithIdentifierKey(\n properties: Array<TSESTree.ObjectLiteralElement>,\n key: string,\n ): TSESTree.Property | undefined {\n return properties.find((x) =>\n ASTUtils.isPropertyWithIdentifierKey(x, key),\n ) as TSESTree.Property | undefined\n },\n getNestedIdentifiers(node: TSESTree.Node): Array<TSESTree.Identifier> {\n const identifiers: Array<TSESTree.Identifier> = []\n\n if (ASTUtils.isIdentifier(node)) {\n identifiers.push(node)\n }\n\n if ('arguments' in node) {\n node.arguments.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('elements' in node) {\n node.elements.forEach((x) => {\n if (x !== null) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n }\n })\n }\n\n if ('properties' in node) {\n node.properties.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('expressions' in node) {\n node.expressions.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('left' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.left))\n }\n\n if ('right' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.right))\n }\n\n if (node.type === AST_NODE_TYPES.Property) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.value))\n }\n\n if (node.type === AST_NODE_TYPES.SpreadElement) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.MemberExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.object))\n }\n\n if (node.type === AST_NODE_TYPES.UnaryExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.ChainExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.TSNonNullExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n return identifiers\n },\n isAncestorIsCallee(identifier: TSESTree.Node) {\n let previousNode = identifier\n let currentNode = identifier.parent\n\n while (currentNode !== undefined) {\n if (\n currentNode.type === AST_NODE_TYPES.CallExpression &&\n currentNode.callee === previousNode\n ) {\n return true\n }\n\n if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {\n return false\n }\n\n previousNode = currentNode\n currentNode = currentNode.parent\n }\n\n return false\n },\n traverseUpOnly(\n identifier: TSESTree.Node,\n allowedNodeTypes: Array<AST_NODE_TYPES>,\n ): TSESTree.Node {\n const parent = identifier.parent\n\n if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {\n return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)\n }\n\n return identifier\n },\n isDeclaredInNode(params: {\n functionNode: TSESTree.Node\n reference: TSESLint.Scope.Reference\n scopeManager: TSESLint.Scope.ScopeManager\n }) {\n const { functionNode, reference, scopeManager } = params\n const scope = scopeManager.acquire(functionNode)\n\n if (scope === null) {\n return false\n }\n\n return scope.set.has(reference.identifier.name)\n },\n getExternalRefs(params: {\n scopeManager: TSESLint.Scope.ScopeManager\n sourceCode: Readonly<TSESLint.SourceCode>\n node: TSESTree.Node\n }): Array<TSESLint.Scope.Reference> {\n const { scopeManager, sourceCode, node } = params\n const scope = scopeManager.acquire(node)\n\n if (scope === null) {\n return []\n }\n\n const references = scope.references\n .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))\n .map((x) => {\n const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ])\n\n return {\n variable: x,\n node: referenceNode,\n text: sourceCode.getText(referenceNode),\n }\n })\n\n const localRefIds = new Set(\n [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),\n )\n\n const externalRefs = references.filter(\n (x) => x.variable.resolved === null || !localRefIds.has(x.text),\n )\n\n return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)\n },\n mapKeyNodeToText(\n node: TSESTree.Node,\n sourceCode: Readonly<TSESLint.SourceCode>,\n ) {\n return sourceCode.getText(\n ASTUtils.traverseUpOnly(node, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ]),\n )\n },\n isValidReactComponentOrHookName(\n identifier: TSESTree.Identifier | null | undefined,\n ) {\n return (\n identifier !== null &&\n identifier !== undefined &&\n /^(use|[A-Z])/.test(identifier.name)\n )\n },\n getFunctionAncestor(\n sourceCode: Readonly<TSESLint.SourceCode>,\n node: TSESTree.Node,\n ) {\n for (const ancestor of sourceCode.getAncestors(node)) {\n if (ancestor.type === AST_NODE_TYPES.FunctionDeclaration) {\n return ancestor\n }\n\n if (\n ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator &&\n ancestor.parent.id.type === AST_NODE_TYPES.Identifier &&\n ASTUtils.isNodeOfOneOf(ancestor, [\n AST_NODE_TYPES.FunctionDeclaration,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ArrowFunctionExpression,\n ])\n ) {\n return ancestor\n }\n }\n\n return undefined\n },\n getReferencedExpressionByIdentifier(params: {\n node: TSESTree.Node\n context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>\n }) {\n const { node, context } = params\n\n // we need the fallbacks for backwards compat with eslint < 8.37.0\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const sourceCode = context.sourceCode ?? context.getSourceCode()\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const scope = context.sourceCode.getScope(node)\n ? sourceCode.getScope(node)\n : context.getScope()\n\n const resolvedNode = scope.references.find((ref) => ref.identifier === node)\n ?.resolved?.defs[0]?.node\n\n if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {\n return null\n }\n\n return resolvedNode.init\n },\n getClosestVariableDeclarator(node: TSESTree.Node) {\n let currentNode: TSESTree.Node | undefined = node\n\n while (currentNode.type !== AST_NODE_TYPES.Program) {\n if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {\n return currentNode\n }\n\n currentNode = currentNode.parent\n }\n\n return undefined\n },\n getNestedReturnStatements(\n node: TSESTree.Node,\n ): Array<TSESTree.ReturnStatement> {\n const returnStatements: Array<TSESTree.ReturnStatement> = []\n\n if (node.type === AST_NODE_TYPES.ReturnStatement) {\n returnStatements.push(node)\n }\n\n if ('body' in node && node.body !== undefined && node.body !== null) {\n Array.isArray(node.body)\n ? node.body.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.body),\n )\n }\n\n if ('consequent' in node) {\n Array.isArray(node.consequent)\n ? node.consequent.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.consequent),\n )\n }\n\n if ('alternate' in node && node.alternate !== null) {\n Array.isArray(node.alternate)\n ? node.alternate.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.alternate),\n )\n }\n\n if ('cases' in node) {\n node.cases.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n }\n\n if ('block' in node) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block))\n }\n\n if ('handler' in node && node.handler !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler))\n }\n\n if ('finalizer' in node && node.finalizer !== null) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.finalizer),\n )\n }\n\n if (\n 'expression' in node &&\n node.expression !== true &&\n node.expression !== false\n ) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.expression),\n )\n }\n\n if ('test' in node && node.test !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test))\n }\n\n return returnStatements\n },\n}\n"],"names":["AST_NODE_TYPES","uniqueBy"],"mappings":";;;;AAIO,MAAM,WAAW;AAAA,EACtB,cACE,MACA,OACqC;AAC9B,WAAA,MAAM,SAAS,KAAK,IAAS;AAAA,EACtC;AAAA,EACA,aAAa,MAAkD;AACtD,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,qBACE,MACA,MAC6B;AAC7B,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS;AAAA,EACtD;AAAA,EACA,2BACE,MACA,MACmD;AACnD,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS,KAAK,IAAI;AAAA,EAC/D;AAAA,EACA,WAAW,MAAgD;AAClD,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,mBAAmB,MAAwD;AAClE,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,4BACE,MACA,KAC2B;AAEzB,WAAA,SAAS,WAAW,IAAI,KAAK,SAAS,qBAAqB,KAAK,KAAK,GAAG;AAAA,EAE5E;AAAA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A0B,KAAK,UAAU;AAAA,MAAA;AAAA,IAEzD;AAEA,QAAI,UAAU,QAAQ,KAAK,SAAS,MAAM;AACxC,uBAAiB,KAAK,GAAG,SAAS,0BAA0B,KAAK,IAAI,CAAC;AAAA,IACxE;AAEO,WAAA;AAAA,EACT;AACF;;"}
|
|
1
|
+
{"version":3,"file":"ast-utils.cjs","sources":["../../../src/utils/ast-utils.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { uniqueBy } from './unique-by'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\n\nexport const ASTUtils = {\n isNodeOfOneOf<T extends AST_NODE_TYPES>(\n node: TSESTree.Node,\n types: ReadonlyArray<T>,\n ): node is TSESTree.Node & { type: T } {\n return types.includes(node.type as T)\n },\n isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {\n return node.type === AST_NODE_TYPES.Identifier\n },\n isIdentifierWithName(\n node: TSESTree.Node,\n name: string,\n ): node is TSESTree.Identifier {\n return ASTUtils.isIdentifier(node) && node.name === name\n },\n isIdentifierWithOneOfNames<T extends Array<string>>(\n node: TSESTree.Node,\n name: T,\n ): node is TSESTree.Identifier & { name: T[number] } {\n return ASTUtils.isIdentifier(node) && name.includes(node.name)\n },\n isProperty(node: TSESTree.Node): node is TSESTree.Property {\n return node.type === AST_NODE_TYPES.Property\n },\n isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {\n return node.type === AST_NODE_TYPES.ObjectExpression\n },\n isPropertyWithIdentifierKey(\n node: TSESTree.Node,\n key: string,\n ): node is TSESTree.Property {\n return (\n ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)\n )\n },\n findPropertyWithIdentifierKey(\n properties: Array<TSESTree.ObjectLiteralElement>,\n key: string,\n ): TSESTree.Property | undefined {\n return properties.find((x) =>\n ASTUtils.isPropertyWithIdentifierKey(x, key),\n ) as TSESTree.Property | undefined\n },\n getNestedIdentifiers(node: TSESTree.Node): Array<TSESTree.Identifier> {\n const identifiers: Array<TSESTree.Identifier> = []\n\n if (ASTUtils.isIdentifier(node)) {\n identifiers.push(node)\n }\n\n if ('arguments' in node) {\n node.arguments.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('elements' in node) {\n node.elements.forEach((x) => {\n if (x !== null) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n }\n })\n }\n\n if ('properties' in node) {\n node.properties.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('expressions' in node) {\n node.expressions.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('left' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.left))\n }\n\n if ('right' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.right))\n }\n\n if (node.type === AST_NODE_TYPES.Property) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.value))\n }\n\n if (node.type === AST_NODE_TYPES.SpreadElement) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.MemberExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.object))\n }\n\n if (node.type === AST_NODE_TYPES.UnaryExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.ChainExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.TSNonNullExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))\n }\n\n if (node.type === AST_NODE_TYPES.FunctionExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))\n }\n\n if (node.type === AST_NODE_TYPES.BlockStatement) {\n identifiers.push(\n ...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat(),\n )\n }\n\n if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n return identifiers\n },\n isAncestorIsCallee(identifier: TSESTree.Node) {\n let previousNode = identifier\n let currentNode = identifier.parent\n\n while (currentNode !== undefined) {\n if (\n currentNode.type === AST_NODE_TYPES.CallExpression &&\n currentNode.callee === previousNode\n ) {\n return true\n }\n\n if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {\n return false\n }\n\n previousNode = currentNode\n currentNode = currentNode.parent\n }\n\n return false\n },\n traverseUpOnly(\n identifier: TSESTree.Node,\n allowedNodeTypes: Array<AST_NODE_TYPES>,\n ): TSESTree.Node {\n const parent = identifier.parent\n\n if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {\n return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)\n }\n\n return identifier\n },\n isDeclaredInNode(params: {\n functionNode: TSESTree.Node\n reference: TSESLint.Scope.Reference\n scopeManager: TSESLint.Scope.ScopeManager\n }) {\n const { functionNode, reference, scopeManager } = params\n const scope = scopeManager.acquire(functionNode)\n\n if (scope === null) {\n return false\n }\n\n return scope.set.has(reference.identifier.name)\n },\n getExternalRefs(params: {\n scopeManager: TSESLint.Scope.ScopeManager\n sourceCode: Readonly<TSESLint.SourceCode>\n node: TSESTree.Node\n }): Array<TSESLint.Scope.Reference> {\n const { scopeManager, sourceCode, node } = params\n const scope = scopeManager.acquire(node)\n\n if (scope === null) {\n return []\n }\n\n const references = scope.references\n .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))\n .map((x) => {\n const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ])\n\n return {\n variable: x,\n node: referenceNode,\n text: sourceCode.getText(referenceNode),\n }\n })\n\n const localRefIds = new Set(\n [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),\n )\n\n const externalRefs = references.filter(\n (x) => x.variable.resolved === null || !localRefIds.has(x.text),\n )\n\n return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)\n },\n mapKeyNodeToText(\n node: TSESTree.Node,\n sourceCode: Readonly<TSESLint.SourceCode>,\n ) {\n return sourceCode.getText(\n ASTUtils.traverseUpOnly(node, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ]),\n )\n },\n isValidReactComponentOrHookName(\n identifier: TSESTree.Identifier | null | undefined,\n ) {\n return (\n identifier !== null &&\n identifier !== undefined &&\n /^(use|[A-Z])/.test(identifier.name)\n )\n },\n getFunctionAncestor(\n sourceCode: Readonly<TSESLint.SourceCode>,\n node: TSESTree.Node,\n ) {\n for (const ancestor of sourceCode.getAncestors(node)) {\n if (ancestor.type === AST_NODE_TYPES.FunctionDeclaration) {\n return ancestor\n }\n\n if (\n ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator &&\n ancestor.parent.id.type === AST_NODE_TYPES.Identifier &&\n ASTUtils.isNodeOfOneOf(ancestor, [\n AST_NODE_TYPES.FunctionDeclaration,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ArrowFunctionExpression,\n ])\n ) {\n return ancestor\n }\n }\n\n return undefined\n },\n getReferencedExpressionByIdentifier(params: {\n node: TSESTree.Node\n context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>\n }) {\n const { node, context } = params\n\n // we need the fallbacks for backwards compat with eslint < 8.37.0\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const sourceCode = context.sourceCode ?? context.getSourceCode()\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const scope = context.sourceCode.getScope(node)\n ? sourceCode.getScope(node)\n : context.getScope()\n\n const resolvedNode = scope.references.find((ref) => ref.identifier === node)\n ?.resolved?.defs[0]?.node\n\n if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {\n return null\n }\n\n return resolvedNode.init\n },\n getClosestVariableDeclarator(node: TSESTree.Node) {\n let currentNode: TSESTree.Node | undefined = node\n\n while (currentNode.type !== AST_NODE_TYPES.Program) {\n if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {\n return currentNode\n }\n\n currentNode = currentNode.parent\n }\n\n return undefined\n },\n getNestedReturnStatements(\n node: TSESTree.Node,\n ): Array<TSESTree.ReturnStatement> {\n const returnStatements: Array<TSESTree.ReturnStatement> = []\n\n if (node.type === AST_NODE_TYPES.ReturnStatement) {\n returnStatements.push(node)\n }\n\n if ('body' in node && node.body !== undefined && node.body !== null) {\n Array.isArray(node.body)\n ? node.body.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.body),\n )\n }\n\n if ('consequent' in node) {\n Array.isArray(node.consequent)\n ? node.consequent.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.consequent),\n )\n }\n\n if ('alternate' in node && node.alternate !== null) {\n Array.isArray(node.alternate)\n ? node.alternate.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.alternate),\n )\n }\n\n if ('cases' in node) {\n node.cases.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n }\n\n if ('block' in node) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block))\n }\n\n if ('handler' in node && node.handler !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler))\n }\n\n if ('finalizer' in node && node.finalizer !== null) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.finalizer),\n )\n }\n\n if (\n 'expression' in node &&\n node.expression !== true &&\n node.expression !== false\n ) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.expression),\n )\n }\n\n if ('test' in node && node.test !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test))\n }\n\n return returnStatements\n },\n}\n"],"names":["AST_NODE_TYPES","uniqueBy"],"mappings":";;;;AAIO,MAAM,WAAW;AAAA,EACtB,cACE,MACA,OACqC;AAC9B,WAAA,MAAM,SAAS,KAAK,IAAS;AAAA,EACtC;AAAA,EACA,aAAa,MAAkD;AACtD,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,qBACE,MACA,MAC6B;AAC7B,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS;AAAA,EACtD;AAAA,EACA,2BACE,MACA,MACmD;AACnD,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS,KAAK,IAAI;AAAA,EAC/D;AAAA,EACA,WAAW,MAAgD;AAClD,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,mBAAmB,MAAwD;AAClE,WAAA,KAAK,SAASA,MAAe,eAAA;AAAA,EACtC;AAAA,EACA,4BACE,MACA,KAC2B;AAEzB,WAAA,SAAS,WAAW,IAAI,KAAK,SAAS,qBAAqB,KAAK,KAAK,GAAG;AAAA,EAE5E;AAAA,EACA,8BACE,YACA,KAC+B;AAC/B,WAAO,WAAW;AAAA,MAAK,CAAC,MACtB,SAAS,4BAA4B,GAAG,GAAG;AAAA,IAAA;AAAA,EAE/C;AAAA,EACA,qBAAqB,MAAiD;AACpE,UAAM,cAA0C,CAAA;AAE5C,QAAA,SAAS,aAAa,IAAI,GAAG;AAC/B,kBAAY,KAAK,IAAI;AAAA,IACvB;AAEA,QAAI,eAAe,MAAM;AAClB,WAAA,UAAU,QAAQ,CAAC,MAAM;AAC5B,oBAAY,KAAK,GAAG,SAAS,qBAAqB,CAAC,CAAC;AAAA,MAAA,CACrD;AAAA,IACH;AAEA,QAAI,cAAc,MAAM;AACjB,WAAA,SAAS,QAAQ,CAAC,MAAM;AAC3B,YAAI,MAAM,MAAM;AACd,sBAAY,KAAK,GAAG,SAA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AA,QACf,GAAG,SAAS,0BAA0B,KAAK,UAAU;AAAA,MAAA;AAAA,IAEzD;AAEA,QAAI,UAAU,QAAQ,KAAK,SAAS,MAAM;AACxC,uBAAiB,KAAK,GAAG,SAAS,0BAA0B,KAAK,IAAI,CAAC;AAAA,IACxE;AAEO,WAAA;AAAA,EACT;AACF;;"}
|
|
@@ -80,7 +80,7 @@ const rule = createRule({
|
|
|
80
80
|
ref,
|
|
81
81
|
text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode)
|
|
82
82
|
})).filter(({ ref, text }) => {
|
|
83
|
-
return !ref.isTypeReference && !ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(
|
|
83
|
+
return !ref.isTypeReference && !ASTUtils.isAncestorIsCallee(ref.identifier) && !existingKeys.some((existingKey) => existingKey === text) && !existingKeys.includes(text.split(/[?.]/)[0] ?? "");
|
|
84
84
|
}).map(({ ref, text }) => ({
|
|
85
85
|
identifier: ref.identifier,
|
|
86
86
|
text
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exhaustive-deps.rule.js","sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n Property: (node) => {\n if (\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.sourceCode.scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n !ASTUtils.isNodeOfOneOf(queryFn.value, [\n AST_NODE_TYPES.ArrowFunctionExpression,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ConditionalExpression,\n ])\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode: context.sourceCode,\n node: getQueryFnRelevantNode(queryFn),\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n sourceCode: context.sourceCode,\n reference,\n scopeManager,\n node: getQueryFnRelevantNode(queryFn),\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyNode).map(\n (identifier) =>\n ASTUtils.mapKeyNodeToText(identifier, context.sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split(
|
|
1
|
+
{"version":3,"file":"exhaustive-deps.rule.js","sources":["../../../../src/rules/exhaustive-deps/exhaustive-deps.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\nimport { ASTUtils } from '../../utils/ast-utils'\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { uniqueBy } from '../../utils/unique-by'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { ExhaustiveDepsUtils } from './exhaustive-deps.utils'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst QUERY_KEY = 'queryKey'\nconst QUERY_FN = 'queryFn'\n\nexport const name = 'exhaustive-deps'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description: 'Exhaustive deps rule for useQuery',\n recommended: 'error',\n },\n messages: {\n missingDeps: `The following dependencies are missing in your queryKey: {{deps}}`,\n fixTo: 'Fix to {{result}}',\n },\n hasSuggestions: true,\n fixable: 'code',\n schema: [],\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n Property: (node) => {\n if (\n !ASTUtils.isObjectExpression(node.parent) ||\n !ASTUtils.isIdentifierWithName(node.key, QUERY_KEY)\n ) {\n return\n }\n\n const scopeManager = context.sourceCode.scopeManager\n const queryKey = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_KEY,\n )\n const queryFn = ASTUtils.findPropertyWithIdentifierKey(\n node.parent.properties,\n QUERY_FN,\n )\n\n if (\n scopeManager === null ||\n queryKey === undefined ||\n queryFn === undefined ||\n !ASTUtils.isNodeOfOneOf(queryFn.value, [\n AST_NODE_TYPES.ArrowFunctionExpression,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ConditionalExpression,\n ])\n ) {\n return\n }\n\n let queryKeyNode = queryKey.value\n\n if (\n queryKeyNode.type === AST_NODE_TYPES.TSAsExpression &&\n queryKeyNode.expression.type === AST_NODE_TYPES.ArrayExpression\n ) {\n queryKeyNode = queryKeyNode.expression\n }\n\n if (queryKeyNode.type === AST_NODE_TYPES.Identifier) {\n const expression = ASTUtils.getReferencedExpressionByIdentifier({\n context,\n node: queryKeyNode,\n })\n\n if (expression?.type === AST_NODE_TYPES.ArrayExpression) {\n queryKeyNode = expression\n }\n }\n\n const externalRefs = ASTUtils.getExternalRefs({\n scopeManager,\n sourceCode: context.sourceCode,\n node: getQueryFnRelevantNode(queryFn),\n })\n\n const relevantRefs = externalRefs.filter((reference) =>\n ExhaustiveDepsUtils.isRelevantReference({\n sourceCode: context.sourceCode,\n reference,\n scopeManager,\n node: getQueryFnRelevantNode(queryFn),\n }),\n )\n\n const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyNode).map(\n (identifier) =>\n ASTUtils.mapKeyNodeToText(identifier, context.sourceCode),\n )\n\n const missingRefs = relevantRefs\n .map((ref) => ({\n ref: ref,\n text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n }))\n .filter(({ ref, text }) => {\n return (\n !ref.isTypeReference &&\n !ASTUtils.isAncestorIsCallee(ref.identifier) &&\n !existingKeys.some((existingKey) => existingKey === text) &&\n !existingKeys.includes(text.split(/[?.]/)[0] ?? '')\n )\n })\n .map(({ ref, text }) => ({\n identifier: ref.identifier,\n text: text,\n }))\n\n const uniqueMissingRefs = uniqueBy(missingRefs, (x) => x.text)\n\n if (uniqueMissingRefs.length > 0) {\n const missingAsText = uniqueMissingRefs\n .map((ref) =>\n ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),\n )\n .join(', ')\n\n const queryKeyValue = context.sourceCode.getText(queryKeyNode)\n\n const existingWithMissing =\n queryKeyValue === '[]'\n ? `[${missingAsText}]`\n : queryKeyValue.replace(/\\]$/, `, ${missingAsText}]`)\n\n const suggestions: TSESLint.ReportSuggestionArray<string> = []\n\n if (queryKeyNode.type === AST_NODE_TYPES.ArrayExpression) {\n suggestions.push({\n messageId: 'fixTo',\n data: { result: existingWithMissing },\n fix(fixer) {\n return fixer.replaceText(queryKeyNode, existingWithMissing)\n },\n })\n }\n\n context.report({\n node: node,\n messageId: 'missingDeps',\n data: {\n deps: uniqueMissingRefs.map((ref) => ref.text).join(', '),\n },\n suggest: suggestions,\n })\n }\n },\n }\n }),\n})\n\nfunction getQueryFnRelevantNode(queryFn: TSESTree.Property) {\n if (queryFn.value.type !== AST_NODE_TYPES.ConditionalExpression) {\n return queryFn.value\n }\n\n if (\n queryFn.value.consequent.type === AST_NODE_TYPES.Identifier &&\n queryFn.value.consequent.name === 'skipToken'\n ) {\n return queryFn.value.alternate\n }\n\n return queryFn.value.consequent\n}\n"],"names":[],"mappings":";;;;;;AASA,MAAM,YAAY;AAClB,MAAM,WAAW;AAEV,MAAM,OAAO;AAEpB,MAAM,aAAa,YAAY,YAA2B,UAAU;AAE7D,MAAM,OAAO,WAAW;AAAA,EAC7B;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,aAAa;AAAA,MACb,aAAa;AAAA,IACf;AAAA,IACA,UAAU;AAAA,MACR,aAAa;AAAA,MACb,OAAO;AAAA,IACT;AAAA,IACA,gBAAgB;AAAA,IAChB,SAAS;AAAA,IACT,QAAQ,CAAC;AAAA,EACX;AAAA,EACA,gBAAgB,CAAC;AAAA,EAEjB,QAAQ,2BAA2B,CAAC,YAAY;AACvC,WAAA;AAAA,MACL,UAAU,CAAC,SAAS;AAClB,YACE,CAAC,SAAS,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,uBAAuB,SAA4B;AAC1D,MAAI,QAAQ,MAAM,SAAS,eAAe,uBAAuB;AAC/D,WAAO,QAAQ;AAAA,EACjB;AAGE,MAAA,QAAQ,MAAM,WAAW,SAAS,eAAe,cACjD,QAAQ,MAAM,WAAW,SAAS,aAClC;AACA,WAAO,QAAQ,MAAM;AAAA,EACvB;AAEA,SAAO,QAAQ,MAAM;AACvB;"}
|
|
@@ -68,22 +68,22 @@ const rule = createRule({
|
|
|
68
68
|
messageId: "invalidOrder",
|
|
69
69
|
fix(fixer) {
|
|
70
70
|
const sourceCode = context.sourceCode;
|
|
71
|
-
const
|
|
71
|
+
const reorderedText = sortedProperties.reduce(
|
|
72
72
|
(sourceText, specifier, index) => {
|
|
73
|
-
let
|
|
73
|
+
let textBetweenProperties = "";
|
|
74
74
|
if (index < allProperties.length - 1) {
|
|
75
|
-
|
|
75
|
+
textBetweenProperties = sourceCode.getText().slice(
|
|
76
76
|
allProperties[index].range[1],
|
|
77
77
|
allProperties[index + 1].range[0]
|
|
78
78
|
);
|
|
79
79
|
}
|
|
80
|
-
return sourceText + sourceCode.getText(specifier.property) +
|
|
80
|
+
return sourceText + sourceCode.getText(specifier.property) + textBetweenProperties;
|
|
81
81
|
},
|
|
82
82
|
""
|
|
83
83
|
);
|
|
84
84
|
return fixer.replaceTextRange(
|
|
85
85
|
[allProperties[0].range[0], allProperties.at(-1).range[1]],
|
|
86
|
-
|
|
86
|
+
reorderedText
|
|
87
87
|
);
|
|
88
88
|
}
|
|
89
89
|
});
|
package/dist/esm/rules/infinite-query-property-order/infinite-query-property-order.rule.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infinite-query-property-order.rule.js","sources":["../../../../src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\n\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { sortDataByOrder } from './infinite-query-property-order.utils'\nimport { infiniteQueryFunctions, sortRules } from './constants'\nimport type { InfiniteQueryFunctions } from './constants'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nconst infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions)\nfunction isInfiniteQueryFunction(node: any): node is InfiniteQueryFunctions {\n return infiniteQueryFunctionsSet.has(node)\n}\n\nexport const name = 'infinite-query-property-order'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description:\n 'Ensure correct order of inference sensitive properties for infinite queries',\n recommended: 'error',\n },\n messages: {\n invalidOrder: 'Invalid order of properties for `{{function}}`.',\n },\n schema: [],\n hasSuggestions: true,\n fixable: 'code',\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n CallExpression(node) {\n if (node.callee.type !== AST_NODE_TYPES.Identifier) {\n return\n }\n const infiniteQueryFunction = node.callee.name\n if (!isInfiniteQueryFunction(infiniteQueryFunction)) {\n return\n }\n const argument = node.arguments[0]\n if (argument === undefined || argument.type !== 'ObjectExpression') {\n return\n }\n\n const allProperties = argument.properties\n\n // no need to sort if there is at max 1 property\n if (allProperties.length < 2) {\n return\n }\n\n const properties = allProperties.flatMap((p) => {\n if (\n p.type === AST_NODE_TYPES.Property &&\n p.key.type === AST_NODE_TYPES.Identifier\n ) {\n return { name: p.key.name, property: p }\n } else if (p.type === AST_NODE_TYPES.SpreadElement) {\n if (p.argument.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.name, property: p }\n } else if (p.argument.type === AST_NODE_TYPES.CallExpression) {\n if (p.argument.callee.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.callee.name, property: p }\n }\n }\n throw new Error('Unsupported spread element')\n }\n return []\n })\n\n const sortedProperties = sortDataByOrder(properties, sortRules, 'name')\n if (sortedProperties === null) {\n return\n }\n context.report({\n node: argument,\n data: { function: node.callee.name },\n messageId: 'invalidOrder',\n fix(fixer) {\n const sourceCode = context.sourceCode\n\n const
|
|
1
|
+
{"version":3,"file":"infinite-query-property-order.rule.js","sources":["../../../../src/rules/infinite-query-property-order/infinite-query-property-order.rule.ts"],"sourcesContent":["import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'\n\nimport { getDocsUrl } from '../../utils/get-docs-url'\nimport { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'\nimport { sortDataByOrder } from './infinite-query-property-order.utils'\nimport { infiniteQueryFunctions, sortRules } from './constants'\nimport type { InfiniteQueryFunctions } from './constants'\nimport type { ExtraRuleDocs } from '../../types'\n\nconst createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)\n\nconst infiniteQueryFunctionsSet = new Set(infiniteQueryFunctions)\nfunction isInfiniteQueryFunction(node: any): node is InfiniteQueryFunctions {\n return infiniteQueryFunctionsSet.has(node)\n}\n\nexport const name = 'infinite-query-property-order'\n\nexport const rule = createRule({\n name,\n meta: {\n type: 'problem',\n docs: {\n description:\n 'Ensure correct order of inference sensitive properties for infinite queries',\n recommended: 'error',\n },\n messages: {\n invalidOrder: 'Invalid order of properties for `{{function}}`.',\n },\n schema: [],\n hasSuggestions: true,\n fixable: 'code',\n },\n defaultOptions: [],\n\n create: detectTanstackQueryImports((context) => {\n return {\n CallExpression(node) {\n if (node.callee.type !== AST_NODE_TYPES.Identifier) {\n return\n }\n const infiniteQueryFunction = node.callee.name\n if (!isInfiniteQueryFunction(infiniteQueryFunction)) {\n return\n }\n const argument = node.arguments[0]\n if (argument === undefined || argument.type !== 'ObjectExpression') {\n return\n }\n\n const allProperties = argument.properties\n\n // no need to sort if there is at max 1 property\n if (allProperties.length < 2) {\n return\n }\n\n const properties = allProperties.flatMap((p) => {\n if (\n p.type === AST_NODE_TYPES.Property &&\n p.key.type === AST_NODE_TYPES.Identifier\n ) {\n return { name: p.key.name, property: p }\n } else if (p.type === AST_NODE_TYPES.SpreadElement) {\n if (p.argument.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.name, property: p }\n } else if (p.argument.type === AST_NODE_TYPES.CallExpression) {\n if (p.argument.callee.type === AST_NODE_TYPES.Identifier) {\n return { name: p.argument.callee.name, property: p }\n }\n }\n throw new Error('Unsupported spread element')\n }\n return []\n })\n\n const sortedProperties = sortDataByOrder(properties, sortRules, 'name')\n if (sortedProperties === null) {\n return\n }\n context.report({\n node: argument,\n data: { function: node.callee.name },\n messageId: 'invalidOrder',\n fix(fixer) {\n const sourceCode = context.sourceCode\n\n const reorderedText = sortedProperties.reduce(\n (sourceText, specifier, index) => {\n let textBetweenProperties = ''\n if (index < allProperties.length - 1) {\n textBetweenProperties = sourceCode\n .getText()\n .slice(\n allProperties[index]!.range[1],\n allProperties[index + 1]!.range[0],\n )\n }\n return (\n sourceText +\n sourceCode.getText(specifier.property) +\n textBetweenProperties\n )\n },\n '',\n )\n return fixer.replaceTextRange(\n [allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],\n reorderedText,\n )\n },\n })\n },\n }\n }),\n})\n"],"names":[],"mappings":";;;;;AASA,MAAM,aAAa,YAAY,YAA2B,UAAU;AAEpE,MAAM,4BAA4B,IAAI,IAAI,sBAAsB;AAChE,SAAS,wBAAwB,MAA2C;AACnE,SAAA,0BAA0B,IAAI,IAAI;AAC3C;AAEO,MAAM,OAAO;AAEb,MAAM,OAAO,WAAW;AAAA,EAC7B;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,MAAM;AAAA,MACJ,aACE;AAAA,MACF,aAAa;AAAA,IACf;AAAA,IACA,UAAU;AAAA,MACR,cAAc;AAAA,IAChB;AAAA,IACA,QAAQ,CAAC;AAAA,IACT,gBAAgB;AAAA,IAChB,SAAS;AAAA,EACX;AAAA,EACA,gBAAgB,CAAC;AAAA,EAEjB,QAAQ,2BAA2B,CAAC,YAAY;AACvC,WAAA;AAAA,MACL,eAAe,MAAM;AACnB,YAAI,KAAK,OAAO,SAAS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,EAAG,MAAM,CAAC;AAAA,oBAC7B,cAAc,QAAQ,CAAC,EAAG,MAAM,CAAC;AAAA,kBAAA;AAAA,gBAEvC;AACA,uBACE,aACA,WAAW,QAAQ,UAAU,QAAQ,IACrC;AAAA,cAEJ;AAAA,cACA;AAAA,YAAA;AAEF,mBAAO,MAAM;AAAA,cACX,CAAC,cAAc,CAAC,EAAG,MAAM,CAAC,GAAG,cAAc,GAAG,EAAE,EAAG,MAAM,CAAC,CAAC;AAAA,cAC3D;AAAA,YAAA;AAAA,UAEJ;AAAA,QAAA,CACD;AAAA,MACH;AAAA,IAAA;AAAA,EACF,CACD;AACH,CAAC;"}
|
|
@@ -78,6 +78,20 @@ const ASTUtils = {
|
|
|
78
78
|
if (node.type === AST_NODE_TYPES.TSNonNullExpression) {
|
|
79
79
|
identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression));
|
|
80
80
|
}
|
|
81
|
+
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
|
|
82
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
|
|
83
|
+
}
|
|
84
|
+
if (node.type === AST_NODE_TYPES.FunctionExpression) {
|
|
85
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body));
|
|
86
|
+
}
|
|
87
|
+
if (node.type === AST_NODE_TYPES.BlockStatement) {
|
|
88
|
+
identifiers.push(
|
|
89
|
+
...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat()
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {
|
|
93
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument));
|
|
94
|
+
}
|
|
81
95
|
return identifiers;
|
|
82
96
|
},
|
|
83
97
|
isAncestorIsCallee(identifier) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.js","sources":["../../../src/utils/ast-utils.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { uniqueBy } from './unique-by'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\n\nexport const ASTUtils = {\n isNodeOfOneOf<T extends AST_NODE_TYPES>(\n node: TSESTree.Node,\n types: ReadonlyArray<T>,\n ): node is TSESTree.Node & { type: T } {\n return types.includes(node.type as T)\n },\n isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {\n return node.type === AST_NODE_TYPES.Identifier\n },\n isIdentifierWithName(\n node: TSESTree.Node,\n name: string,\n ): node is TSESTree.Identifier {\n return ASTUtils.isIdentifier(node) && node.name === name\n },\n isIdentifierWithOneOfNames<T extends Array<string>>(\n node: TSESTree.Node,\n name: T,\n ): node is TSESTree.Identifier & { name: T[number] } {\n return ASTUtils.isIdentifier(node) && name.includes(node.name)\n },\n isProperty(node: TSESTree.Node): node is TSESTree.Property {\n return node.type === AST_NODE_TYPES.Property\n },\n isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {\n return node.type === AST_NODE_TYPES.ObjectExpression\n },\n isPropertyWithIdentifierKey(\n node: TSESTree.Node,\n key: string,\n ): node is TSESTree.Property {\n return (\n ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)\n )\n },\n findPropertyWithIdentifierKey(\n properties: Array<TSESTree.ObjectLiteralElement>,\n key: string,\n ): TSESTree.Property | undefined {\n return properties.find((x) =>\n ASTUtils.isPropertyWithIdentifierKey(x, key),\n ) as TSESTree.Property | undefined\n },\n getNestedIdentifiers(node: TSESTree.Node): Array<TSESTree.Identifier> {\n const identifiers: Array<TSESTree.Identifier> = []\n\n if (ASTUtils.isIdentifier(node)) {\n identifiers.push(node)\n }\n\n if ('arguments' in node) {\n node.arguments.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('elements' in node) {\n node.elements.forEach((x) => {\n if (x !== null) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n }\n })\n }\n\n if ('properties' in node) {\n node.properties.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('expressions' in node) {\n node.expressions.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('left' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.left))\n }\n\n if ('right' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.right))\n }\n\n if (node.type === AST_NODE_TYPES.Property) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.value))\n }\n\n if (node.type === AST_NODE_TYPES.SpreadElement) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.MemberExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.object))\n }\n\n if (node.type === AST_NODE_TYPES.UnaryExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.ChainExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.TSNonNullExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n return identifiers\n },\n isAncestorIsCallee(identifier: TSESTree.Node) {\n let previousNode = identifier\n let currentNode = identifier.parent\n\n while (currentNode !== undefined) {\n if (\n currentNode.type === AST_NODE_TYPES.CallExpression &&\n currentNode.callee === previousNode\n ) {\n return true\n }\n\n if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {\n return false\n }\n\n previousNode = currentNode\n currentNode = currentNode.parent\n }\n\n return false\n },\n traverseUpOnly(\n identifier: TSESTree.Node,\n allowedNodeTypes: Array<AST_NODE_TYPES>,\n ): TSESTree.Node {\n const parent = identifier.parent\n\n if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {\n return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)\n }\n\n return identifier\n },\n isDeclaredInNode(params: {\n functionNode: TSESTree.Node\n reference: TSESLint.Scope.Reference\n scopeManager: TSESLint.Scope.ScopeManager\n }) {\n const { functionNode, reference, scopeManager } = params\n const scope = scopeManager.acquire(functionNode)\n\n if (scope === null) {\n return false\n }\n\n return scope.set.has(reference.identifier.name)\n },\n getExternalRefs(params: {\n scopeManager: TSESLint.Scope.ScopeManager\n sourceCode: Readonly<TSESLint.SourceCode>\n node: TSESTree.Node\n }): Array<TSESLint.Scope.Reference> {\n const { scopeManager, sourceCode, node } = params\n const scope = scopeManager.acquire(node)\n\n if (scope === null) {\n return []\n }\n\n const references = scope.references\n .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))\n .map((x) => {\n const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ])\n\n return {\n variable: x,\n node: referenceNode,\n text: sourceCode.getText(referenceNode),\n }\n })\n\n const localRefIds = new Set(\n [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),\n )\n\n const externalRefs = references.filter(\n (x) => x.variable.resolved === null || !localRefIds.has(x.text),\n )\n\n return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)\n },\n mapKeyNodeToText(\n node: TSESTree.Node,\n sourceCode: Readonly<TSESLint.SourceCode>,\n ) {\n return sourceCode.getText(\n ASTUtils.traverseUpOnly(node, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ]),\n )\n },\n isValidReactComponentOrHookName(\n identifier: TSESTree.Identifier | null | undefined,\n ) {\n return (\n identifier !== null &&\n identifier !== undefined &&\n /^(use|[A-Z])/.test(identifier.name)\n )\n },\n getFunctionAncestor(\n sourceCode: Readonly<TSESLint.SourceCode>,\n node: TSESTree.Node,\n ) {\n for (const ancestor of sourceCode.getAncestors(node)) {\n if (ancestor.type === AST_NODE_TYPES.FunctionDeclaration) {\n return ancestor\n }\n\n if (\n ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator &&\n ancestor.parent.id.type === AST_NODE_TYPES.Identifier &&\n ASTUtils.isNodeOfOneOf(ancestor, [\n AST_NODE_TYPES.FunctionDeclaration,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ArrowFunctionExpression,\n ])\n ) {\n return ancestor\n }\n }\n\n return undefined\n },\n getReferencedExpressionByIdentifier(params: {\n node: TSESTree.Node\n context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>\n }) {\n const { node, context } = params\n\n // we need the fallbacks for backwards compat with eslint < 8.37.0\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const sourceCode = context.sourceCode ?? context.getSourceCode()\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const scope = context.sourceCode.getScope(node)\n ? sourceCode.getScope(node)\n : context.getScope()\n\n const resolvedNode = scope.references.find((ref) => ref.identifier === node)\n ?.resolved?.defs[0]?.node\n\n if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {\n return null\n }\n\n return resolvedNode.init\n },\n getClosestVariableDeclarator(node: TSESTree.Node) {\n let currentNode: TSESTree.Node | undefined = node\n\n while (currentNode.type !== AST_NODE_TYPES.Program) {\n if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {\n return currentNode\n }\n\n currentNode = currentNode.parent\n }\n\n return undefined\n },\n getNestedReturnStatements(\n node: TSESTree.Node,\n ): Array<TSESTree.ReturnStatement> {\n const returnStatements: Array<TSESTree.ReturnStatement> = []\n\n if (node.type === AST_NODE_TYPES.ReturnStatement) {\n returnStatements.push(node)\n }\n\n if ('body' in node && node.body !== undefined && node.body !== null) {\n Array.isArray(node.body)\n ? node.body.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.body),\n )\n }\n\n if ('consequent' in node) {\n Array.isArray(node.consequent)\n ? node.consequent.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.consequent),\n )\n }\n\n if ('alternate' in node && node.alternate !== null) {\n Array.isArray(node.alternate)\n ? node.alternate.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.alternate),\n )\n }\n\n if ('cases' in node) {\n node.cases.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n }\n\n if ('block' in node) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block))\n }\n\n if ('handler' in node && node.handler !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler))\n }\n\n if ('finalizer' in node && node.finalizer !== null) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.finalizer),\n )\n }\n\n if (\n 'expression' in node &&\n node.expression !== true &&\n node.expression !== false\n ) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.expression),\n )\n }\n\n if ('test' in node && node.test !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test))\n }\n\n return returnStatements\n },\n}\n"],"names":[],"mappings":";;AAIO,MAAM,WAAW;AAAA,EACtB,cACE,MACA,OACqC;AAC9B,WAAA,MAAM,SAAS,KAAK,IAAS;AAAA,EACtC;AAAA,EACA,aAAa,MAAkD;AACtD,WAAA,KAAK,SAAS,eAAe;AAAA,EACtC;AAAA,EACA,qBACE,MACA,MAC6B;AAC7B,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS;AAAA,EACtD;AAAA,EACA,2BACE,MACA,MACmD;AACnD,WAAO,SAAS,aAAa,IAAI,KAAK,KAAK,SAAS,KAAK,IAAI;AAAA,EAC/D;AAAA,EACA,WAAW,MAAgD;AAClD,WAAA,KAAK,SAAS,eAAe;AAAA,EACtC;AAAA,EACA,mBAAmB,MAAwD;AAClE,WAAA,KAAK,SAAS,eAAe;AAAA,EACtC;AAAA,EACA,4BACE,MACA,KAC2B;AAEzB,WAAA,SAAS,WAAW,IAAI,KAAK,SAA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AAG,SAAS,0BAA0B,KAAK,IAAI,CAAC;AAAA,IACxE;AAEO,WAAA;AAAA,EACT;AACF;"}
|
|
1
|
+
{"version":3,"file":"ast-utils.js","sources":["../../../src/utils/ast-utils.ts"],"sourcesContent":["import { AST_NODE_TYPES } from '@typescript-eslint/utils'\nimport { uniqueBy } from './unique-by'\nimport type { TSESLint, TSESTree } from '@typescript-eslint/utils'\n\nexport const ASTUtils = {\n isNodeOfOneOf<T extends AST_NODE_TYPES>(\n node: TSESTree.Node,\n types: ReadonlyArray<T>,\n ): node is TSESTree.Node & { type: T } {\n return types.includes(node.type as T)\n },\n isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {\n return node.type === AST_NODE_TYPES.Identifier\n },\n isIdentifierWithName(\n node: TSESTree.Node,\n name: string,\n ): node is TSESTree.Identifier {\n return ASTUtils.isIdentifier(node) && node.name === name\n },\n isIdentifierWithOneOfNames<T extends Array<string>>(\n node: TSESTree.Node,\n name: T,\n ): node is TSESTree.Identifier & { name: T[number] } {\n return ASTUtils.isIdentifier(node) && name.includes(node.name)\n },\n isProperty(node: TSESTree.Node): node is TSESTree.Property {\n return node.type === AST_NODE_TYPES.Property\n },\n isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {\n return node.type === AST_NODE_TYPES.ObjectExpression\n },\n isPropertyWithIdentifierKey(\n node: TSESTree.Node,\n key: string,\n ): node is TSESTree.Property {\n return (\n ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)\n )\n },\n findPropertyWithIdentifierKey(\n properties: Array<TSESTree.ObjectLiteralElement>,\n key: string,\n ): TSESTree.Property | undefined {\n return properties.find((x) =>\n ASTUtils.isPropertyWithIdentifierKey(x, key),\n ) as TSESTree.Property | undefined\n },\n getNestedIdentifiers(node: TSESTree.Node): Array<TSESTree.Identifier> {\n const identifiers: Array<TSESTree.Identifier> = []\n\n if (ASTUtils.isIdentifier(node)) {\n identifiers.push(node)\n }\n\n if ('arguments' in node) {\n node.arguments.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('elements' in node) {\n node.elements.forEach((x) => {\n if (x !== null) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n }\n })\n }\n\n if ('properties' in node) {\n node.properties.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('expressions' in node) {\n node.expressions.forEach((x) => {\n identifiers.push(...ASTUtils.getNestedIdentifiers(x))\n })\n }\n\n if ('left' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.left))\n }\n\n if ('right' in node) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.right))\n }\n\n if (node.type === AST_NODE_TYPES.Property) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.value))\n }\n\n if (node.type === AST_NODE_TYPES.SpreadElement) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.MemberExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.object))\n }\n\n if (node.type === AST_NODE_TYPES.UnaryExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n if (node.type === AST_NODE_TYPES.ChainExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.TSNonNullExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))\n }\n\n if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))\n }\n\n if (node.type === AST_NODE_TYPES.FunctionExpression) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))\n }\n\n if (node.type === AST_NODE_TYPES.BlockStatement) {\n identifiers.push(\n ...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat(),\n )\n }\n\n if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {\n identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))\n }\n\n return identifiers\n },\n isAncestorIsCallee(identifier: TSESTree.Node) {\n let previousNode = identifier\n let currentNode = identifier.parent\n\n while (currentNode !== undefined) {\n if (\n currentNode.type === AST_NODE_TYPES.CallExpression &&\n currentNode.callee === previousNode\n ) {\n return true\n }\n\n if (currentNode.type !== AST_NODE_TYPES.MemberExpression) {\n return false\n }\n\n previousNode = currentNode\n currentNode = currentNode.parent\n }\n\n return false\n },\n traverseUpOnly(\n identifier: TSESTree.Node,\n allowedNodeTypes: Array<AST_NODE_TYPES>,\n ): TSESTree.Node {\n const parent = identifier.parent\n\n if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {\n return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)\n }\n\n return identifier\n },\n isDeclaredInNode(params: {\n functionNode: TSESTree.Node\n reference: TSESLint.Scope.Reference\n scopeManager: TSESLint.Scope.ScopeManager\n }) {\n const { functionNode, reference, scopeManager } = params\n const scope = scopeManager.acquire(functionNode)\n\n if (scope === null) {\n return false\n }\n\n return scope.set.has(reference.identifier.name)\n },\n getExternalRefs(params: {\n scopeManager: TSESLint.Scope.ScopeManager\n sourceCode: Readonly<TSESLint.SourceCode>\n node: TSESTree.Node\n }): Array<TSESLint.Scope.Reference> {\n const { scopeManager, sourceCode, node } = params\n const scope = scopeManager.acquire(node)\n\n if (scope === null) {\n return []\n }\n\n const references = scope.references\n .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))\n .map((x) => {\n const referenceNode = ASTUtils.traverseUpOnly(x.identifier, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ])\n\n return {\n variable: x,\n node: referenceNode,\n text: sourceCode.getText(referenceNode),\n }\n })\n\n const localRefIds = new Set(\n [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),\n )\n\n const externalRefs = references.filter(\n (x) => x.variable.resolved === null || !localRefIds.has(x.text),\n )\n\n return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)\n },\n mapKeyNodeToText(\n node: TSESTree.Node,\n sourceCode: Readonly<TSESLint.SourceCode>,\n ) {\n return sourceCode.getText(\n ASTUtils.traverseUpOnly(node, [\n AST_NODE_TYPES.MemberExpression,\n AST_NODE_TYPES.Identifier,\n ]),\n )\n },\n isValidReactComponentOrHookName(\n identifier: TSESTree.Identifier | null | undefined,\n ) {\n return (\n identifier !== null &&\n identifier !== undefined &&\n /^(use|[A-Z])/.test(identifier.name)\n )\n },\n getFunctionAncestor(\n sourceCode: Readonly<TSESLint.SourceCode>,\n node: TSESTree.Node,\n ) {\n for (const ancestor of sourceCode.getAncestors(node)) {\n if (ancestor.type === AST_NODE_TYPES.FunctionDeclaration) {\n return ancestor\n }\n\n if (\n ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator &&\n ancestor.parent.id.type === AST_NODE_TYPES.Identifier &&\n ASTUtils.isNodeOfOneOf(ancestor, [\n AST_NODE_TYPES.FunctionDeclaration,\n AST_NODE_TYPES.FunctionExpression,\n AST_NODE_TYPES.ArrowFunctionExpression,\n ])\n ) {\n return ancestor\n }\n }\n\n return undefined\n },\n getReferencedExpressionByIdentifier(params: {\n node: TSESTree.Node\n context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>\n }) {\n const { node, context } = params\n\n // we need the fallbacks for backwards compat with eslint < 8.37.0\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const sourceCode = context.sourceCode ?? context.getSourceCode()\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const scope = context.sourceCode.getScope(node)\n ? sourceCode.getScope(node)\n : context.getScope()\n\n const resolvedNode = scope.references.find((ref) => ref.identifier === node)\n ?.resolved?.defs[0]?.node\n\n if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {\n return null\n }\n\n return resolvedNode.init\n },\n getClosestVariableDeclarator(node: TSESTree.Node) {\n let currentNode: TSESTree.Node | undefined = node\n\n while (currentNode.type !== AST_NODE_TYPES.Program) {\n if (currentNode.type === AST_NODE_TYPES.VariableDeclarator) {\n return currentNode\n }\n\n currentNode = currentNode.parent\n }\n\n return undefined\n },\n getNestedReturnStatements(\n node: TSESTree.Node,\n ): Array<TSESTree.ReturnStatement> {\n const returnStatements: Array<TSESTree.ReturnStatement> = []\n\n if (node.type === AST_NODE_TYPES.ReturnStatement) {\n returnStatements.push(node)\n }\n\n if ('body' in node && node.body !== undefined && node.body !== null) {\n Array.isArray(node.body)\n ? node.body.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.body),\n )\n }\n\n if ('consequent' in node) {\n Array.isArray(node.consequent)\n ? node.consequent.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.consequent),\n )\n }\n\n if ('alternate' in node && node.alternate !== null) {\n Array.isArray(node.alternate)\n ? node.alternate.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n : returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.alternate),\n )\n }\n\n if ('cases' in node) {\n node.cases.forEach((x) => {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(x))\n })\n }\n\n if ('block' in node) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.block))\n }\n\n if ('handler' in node && node.handler !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.handler))\n }\n\n if ('finalizer' in node && node.finalizer !== null) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.finalizer),\n )\n }\n\n if (\n 'expression' in node &&\n node.expression !== true &&\n node.expression !== false\n ) {\n returnStatements.push(\n ...ASTUtils.getNestedReturnStatements(node.expression),\n )\n }\n\n if ('test' in node && node.test !== null) {\n returnStatements.push(...ASTUtils.getNestedReturnStatements(node.test))\n }\n\n return returnStatements\n },\n}\n"],"names":[],"mappings":";;AAIO,MAAM,WAAW;AAAA,EACtB,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+
{
|
|
417
|
+
name: 'should not fail when queryFn uses nullish coalescing operator',
|
|
418
|
+
code: normalizeIndent`
|
|
419
|
+
useQuery({
|
|
420
|
+
queryKey: ["foo", options],
|
|
421
|
+
queryFn: () => options?.params ?? options
|
|
422
|
+
});
|
|
423
|
+
`,
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
name: 'should not fail when queryKey uses arrow function to produce a key',
|
|
427
|
+
code: normalizeIndent`
|
|
428
|
+
const obj = reactive<{ boo?: string }>({});
|
|
429
|
+
|
|
430
|
+
const query = useQuery({
|
|
431
|
+
queryKey: ['foo', () => obj.boo],
|
|
432
|
+
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
433
|
+
enable: () => !!obj.boo,
|
|
434
|
+
});
|
|
435
|
+
`,
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
name: 'should not fail when queryKey uses arrow function to produce a key as the body return',
|
|
439
|
+
code: normalizeIndent`
|
|
440
|
+
const obj = reactive<{ boo?: string }>({});
|
|
441
|
+
|
|
442
|
+
const query = useQuery({
|
|
443
|
+
queryKey: ['foo', () => { return obj.boo }],
|
|
444
|
+
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
445
|
+
enable: () => !!obj.boo,
|
|
446
|
+
});
|
|
447
|
+
`,
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
name: 'should not fail when queryKey uses function expression to produce a key as the body return',
|
|
451
|
+
code: normalizeIndent`
|
|
452
|
+
const obj = reactive<{ boo?: string }>({});
|
|
453
|
+
|
|
454
|
+
const query = useQuery({
|
|
455
|
+
queryKey: ['foo', function() {
|
|
456
|
+
return obj.boo
|
|
457
|
+
}],
|
|
458
|
+
queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`),
|
|
459
|
+
enable: () => !!obj.boo,
|
|
460
|
+
});
|
|
461
|
+
`,
|
|
462
|
+
},
|
|
416
463
|
],
|
|
417
464
|
invalid: [
|
|
418
465
|
{
|
|
@@ -67,7 +67,7 @@ export function generateInterleavedCombinations<
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
function insertAtPositions(
|
|
70
|
-
|
|
70
|
+
baseData: Array<TResult>,
|
|
71
71
|
subset: Array<TResult>,
|
|
72
72
|
): Array<Array<TResult>> {
|
|
73
73
|
const combinations: Array<Array<TResult>> = []
|
|
@@ -92,7 +92,7 @@ export function generateInterleavedCombinations<
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
recurse(
|
|
95
|
+
recurse(baseData, subset, 0)
|
|
96
96
|
return combinations
|
|
97
97
|
}
|
|
98
98
|
|
|
@@ -115,7 +115,7 @@ export const rule = createRule({
|
|
|
115
115
|
!ref.isTypeReference &&
|
|
116
116
|
!ASTUtils.isAncestorIsCallee(ref.identifier) &&
|
|
117
117
|
!existingKeys.some((existingKey) => existingKey === text) &&
|
|
118
|
-
!existingKeys.includes(text.split(
|
|
118
|
+
!existingKeys.includes(text.split(/[?.]/)[0] ?? '')
|
|
119
119
|
)
|
|
120
120
|
})
|
|
121
121
|
.map(({ ref, text }) => ({
|
|
@@ -86,11 +86,11 @@ export const rule = createRule({
|
|
|
86
86
|
fix(fixer) {
|
|
87
87
|
const sourceCode = context.sourceCode
|
|
88
88
|
|
|
89
|
-
const
|
|
89
|
+
const reorderedText = sortedProperties.reduce(
|
|
90
90
|
(sourceText, specifier, index) => {
|
|
91
|
-
let
|
|
91
|
+
let textBetweenProperties = ''
|
|
92
92
|
if (index < allProperties.length - 1) {
|
|
93
|
-
|
|
93
|
+
textBetweenProperties = sourceCode
|
|
94
94
|
.getText()
|
|
95
95
|
.slice(
|
|
96
96
|
allProperties[index]!.range[1],
|
|
@@ -98,14 +98,16 @@ export const rule = createRule({
|
|
|
98
98
|
)
|
|
99
99
|
}
|
|
100
100
|
return (
|
|
101
|
-
sourceText +
|
|
101
|
+
sourceText +
|
|
102
|
+
sourceCode.getText(specifier.property) +
|
|
103
|
+
textBetweenProperties
|
|
102
104
|
)
|
|
103
105
|
},
|
|
104
106
|
'',
|
|
105
107
|
)
|
|
106
108
|
return fixer.replaceTextRange(
|
|
107
109
|
[allProperties[0]!.range[0], allProperties.at(-1)!.range[1]],
|
|
108
|
-
|
|
110
|
+
reorderedText,
|
|
109
111
|
)
|
|
110
112
|
},
|
|
111
113
|
})
|
package/src/utils/ast-utils.ts
CHANGED
|
@@ -111,6 +111,24 @@ export const ASTUtils = {
|
|
|
111
111
|
identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression))
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
|
|
115
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (node.type === AST_NODE_TYPES.FunctionExpression) {
|
|
119
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.body))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (node.type === AST_NODE_TYPES.BlockStatement) {
|
|
123
|
+
identifiers.push(
|
|
124
|
+
...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat(),
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) {
|
|
129
|
+
identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument))
|
|
130
|
+
}
|
|
131
|
+
|
|
114
132
|
return identifiers
|
|
115
133
|
},
|
|
116
134
|
isAncestorIsCallee(identifier: TSESTree.Node) {
|