Skip to content

Commit c8cc23d

Browse files
refactor: make getNodeIdentifierTexts avaliable to all rules
1 parent 9fedd2b commit c8cc23d

File tree

2 files changed

+91
-87
lines changed

2 files changed

+91
-87
lines changed

src/common/ignore-options.ts

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,15 @@ import escapeRegExp from "escape-string-regexp";
33
import type { JSONSchema4 } from "json-schema";
44
import type { ReadonlyDeep } from "type-fest";
55

6+
import { getNodeIdentifierTexts } from "~/util/misc";
67
import type { BaseOptions } from "~/util/rule";
8+
import { inClass, inFunctionBody, inInterface } from "~/util/tree";
79
import {
8-
getKeyOfValueInObjectExpression,
9-
inClass,
10-
inFunctionBody,
11-
inInterface,
12-
} from "~/util/tree";
13-
import {
14-
hasID,
15-
hasKey,
1610
isAssignmentExpression,
17-
isDefined,
1811
isPropertyDefinition,
19-
isExpressionStatement,
20-
isIdentifier,
2112
isMemberExpression,
2213
isReadonlyArray,
2314
isThisExpression,
24-
isTSArrayType,
25-
isTSIndexSignature,
26-
isTSTupleType,
27-
isTSTypeAnnotation,
28-
isTSTypeLiteral,
29-
isTSTypeReference,
30-
isUnaryExpression,
31-
isVariableDeclaration,
3215
} from "~/util/typeguard";
3316

3417
/**
@@ -125,73 +108,6 @@ export const ignoreInterfaceOptionSchema: JSONSchema4["properties"] = {
125108
},
126109
};
127110

128-
/**
129-
* Get the identifier text of the given node.
130-
*/
131-
function getNodeIdentifierText(
132-
node: ReadonlyDeep<TSESTree.Node> | null | undefined,
133-
context: ReadonlyDeep<TSESLint.RuleContext<string, BaseOptions>>
134-
): string | undefined {
135-
if (!isDefined(node)) {
136-
return undefined;
137-
}
138-
139-
const identifierText = isIdentifier(node)
140-
? node.name
141-
: hasID(node) && isDefined(node.id)
142-
? getNodeIdentifierText(node.id, context)
143-
: hasKey(node) && isDefined(node.key)
144-
? getNodeIdentifierText(node.key, context)
145-
: isAssignmentExpression(node)
146-
? getNodeIdentifierText(node.left, context)
147-
: isMemberExpression(node)
148-
? `${getNodeIdentifierText(node.object, context)}.${getNodeIdentifierText(
149-
node.property,
150-
context
151-
)}`
152-
: isThisExpression(node)
153-
? "this"
154-
: isUnaryExpression(node)
155-
? getNodeIdentifierText(node.argument, context)
156-
: isExpressionStatement(node)
157-
? context.getSourceCode().getText(node as TSESTree.Node)
158-
: isTSArrayType(node) ||
159-
isTSIndexSignature(node) ||
160-
isTSTupleType(node) ||
161-
isTSTypeAnnotation(node) ||
162-
isTSTypeLiteral(node) ||
163-
isTSTypeReference(node)
164-
? getNodeIdentifierText(node.parent, context)
165-
: null;
166-
167-
if (identifierText !== null) {
168-
return identifierText;
169-
}
170-
171-
const keyInObjectExpression = getKeyOfValueInObjectExpression(node);
172-
if (keyInObjectExpression !== null) {
173-
return keyInObjectExpression;
174-
}
175-
176-
return undefined;
177-
}
178-
179-
/**
180-
* Get all the identifier texts of the given node.
181-
*/
182-
function getNodeIdentifierTexts(
183-
node: ReadonlyDeep<TSESTree.Node>,
184-
context: ReadonlyDeep<TSESLint.RuleContext<string, BaseOptions>>
185-
): ReadonlyArray<string> {
186-
return (
187-
isVariableDeclaration(node)
188-
? node.declarations.flatMap((declarator) =>
189-
getNodeIdentifierText(declarator, context)
190-
)
191-
: [getNodeIdentifierText(node, context)]
192-
).filter<string>((text): text is string => text !== undefined);
193-
}
194-
195111
/**
196112
* Should the given text be allowed?
197113
*

src/util/misc.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
import type { TSESTree } from "@typescript-eslint/utils";
1+
import type { TSESLint, TSESTree } from "@typescript-eslint/utils";
22
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
33
import type { ReadonlyDeep } from "type-fest";
44

5+
import type { BaseOptions } from "~/util/rule";
6+
import { getKeyOfValueInObjectExpression } from "~/util/tree";
7+
import {
8+
hasID,
9+
hasKey,
10+
isAssignmentExpression,
11+
isDefined,
12+
isExpressionStatement,
13+
isIdentifier,
14+
isMemberExpression,
15+
isThisExpression,
16+
isTSArrayType,
17+
isTSIndexSignature,
18+
isTSTupleType,
19+
isTSTypeAnnotation,
20+
isTSTypeLiteral,
21+
isTSTypeReference,
22+
isUnaryExpression,
23+
isVariableDeclaration,
24+
} from "~/util/typeguard";
25+
526
/**
627
* Higher order function to check if the two given values are the same.
728
*/
@@ -21,3 +42,70 @@ export function isDirectivePrologue(
2142
node.expression.value.startsWith("use ")
2243
);
2344
}
45+
46+
/**
47+
* Get the identifier text of the given node.
48+
*/
49+
function getNodeIdentifierText(
50+
node: ReadonlyDeep<TSESTree.Node> | null | undefined,
51+
context: ReadonlyDeep<TSESLint.RuleContext<string, BaseOptions>>
52+
): string | undefined {
53+
if (!isDefined(node)) {
54+
return undefined;
55+
}
56+
57+
const identifierText = isIdentifier(node)
58+
? node.name
59+
: hasID(node) && isDefined(node.id)
60+
? getNodeIdentifierText(node.id, context)
61+
: hasKey(node) && isDefined(node.key)
62+
? getNodeIdentifierText(node.key, context)
63+
: isAssignmentExpression(node)
64+
? getNodeIdentifierText(node.left, context)
65+
: isMemberExpression(node)
66+
? `${getNodeIdentifierText(node.object, context)}.${getNodeIdentifierText(
67+
node.property,
68+
context
69+
)}`
70+
: isThisExpression(node)
71+
? "this"
72+
: isUnaryExpression(node)
73+
? getNodeIdentifierText(node.argument, context)
74+
: isExpressionStatement(node)
75+
? context.getSourceCode().getText(node as TSESTree.Node)
76+
: isTSArrayType(node) ||
77+
isTSIndexSignature(node) ||
78+
isTSTupleType(node) ||
79+
isTSTypeAnnotation(node) ||
80+
isTSTypeLiteral(node) ||
81+
isTSTypeReference(node)
82+
? getNodeIdentifierText(node.parent, context)
83+
: null;
84+
85+
if (identifierText !== null) {
86+
return identifierText;
87+
}
88+
89+
const keyInObjectExpression = getKeyOfValueInObjectExpression(node);
90+
if (keyInObjectExpression !== null) {
91+
return keyInObjectExpression;
92+
}
93+
94+
return undefined;
95+
}
96+
97+
/**
98+
* Get all the identifier texts of the given node.
99+
*/
100+
export function getNodeIdentifierTexts(
101+
node: ReadonlyDeep<TSESTree.Node>,
102+
context: ReadonlyDeep<TSESLint.RuleContext<string, BaseOptions>>
103+
): ReadonlyArray<string> {
104+
return (
105+
isVariableDeclaration(node)
106+
? node.declarations.flatMap((declarator) =>
107+
getNodeIdentifierText(declarator, context)
108+
)
109+
: [getNodeIdentifierText(node, context)]
110+
).filter<string>((text): text is string => text !== undefined);
111+
}

0 commit comments

Comments
 (0)