Skip to content

Commit c050e55

Browse files
committed
add additional tests to no-keywords
1 parent 6988e25 commit c050e55

File tree

2 files changed

+129
-22
lines changed

2 files changed

+129
-22
lines changed

scripts/eslint/rules/no-keywords.ts

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,83 @@ export = createRule({
3636
"any",
3737
];
3838

39-
const hasKeyword = (name: string) => keywords.includes(name);
39+
const isKeyword = (name: string) => keywords.includes(name);
4040

41-
const shouldReport = (node: TSESTree.Identifier) => {
42-
if (!node || !node.parent || !hasKeyword(node.name)) {
43-
return false;
44-
}
45-
46-
const parent = node.parent;
47-
if (parent.type === AST_NODE_TYPES.FunctionDeclaration || parent.type === AST_NODE_TYPES.FunctionExpression) {
48-
return !(parent.id && hasKeyword(parent.id.name));
49-
}
41+
const report = (node: TSESTree.Identifier) => {
42+
context.report({ messageId: "noKeywordsError", data: { name: node.name }, node });
43+
};
5044

51-
if (parent.type === AST_NODE_TYPES.TSMethodSignature && parent.key.type === AST_NODE_TYPES.Identifier) {
52-
return !(parent.key && hasKeyword(parent.key.name));
53-
}
45+
const checkProperties = (node: TSESTree.ObjectPattern): void => {
46+
node.properties.forEach(property => {
47+
if (
48+
property &&
49+
property.type === AST_NODE_TYPES.Property &&
50+
property.key.type === AST_NODE_TYPES.Identifier &&
51+
isKeyword(property.key.name)
52+
) {
53+
report(property.key);
54+
}
55+
});
56+
};
5457

55-
return [
56-
AST_NODE_TYPES.ArrowFunctionExpression,
57-
AST_NODE_TYPES.VariableDeclarator,
58-
AST_NODE_TYPES.TSFunctionType,
59-
].includes(node.parent.type);
58+
const checkElements = (node: TSESTree.ArrayPattern): void => {
59+
node.elements.forEach(element => {
60+
if (
61+
element &&
62+
element.type === AST_NODE_TYPES.Identifier &&
63+
isKeyword(element.name)
64+
) {
65+
report(element);
66+
}
67+
});
6068
};
6169

62-
const check = (node: TSESTree.Identifier) => {
63-
if (shouldReport(node)) {
64-
context.report({ messageId: "noKeywordsError", data: { name: node.name }, node });
70+
const checkParams = (
71+
node:
72+
| TSESTree.ArrowFunctionExpression
73+
| TSESTree.FunctionDeclaration
74+
| TSESTree.FunctionExpression
75+
| TSESTree.TSMethodSignature
76+
| TSESTree.TSFunctionType
77+
): void => {
78+
if (!node || !node.params || !node.params.length) {
79+
return;
6580
}
81+
82+
node.params.forEach(param => {
83+
if (
84+
param &&
85+
param.type === AST_NODE_TYPES.Identifier &&
86+
isKeyword(param.name)
87+
) {
88+
report(param);
89+
}
90+
});
6691
};
6792

68-
return { Identifier: check };
93+
return {
94+
VariableDeclarator(node) {
95+
if (node.id.type === AST_NODE_TYPES.ObjectPattern) {
96+
checkProperties(node.id);
97+
}
98+
99+
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
100+
checkElements(node.id);
101+
}
102+
103+
if (
104+
node.id.type === AST_NODE_TYPES.Identifier &&
105+
isKeyword(node.id.name)
106+
) {
107+
report(node.id);
108+
}
109+
},
110+
111+
ArrowFunctionExpression: checkParams,
112+
FunctionDeclaration: checkParams,
113+
FunctionExpression: checkParams,
114+
TSMethodSignature: checkParams,
115+
TSFunctionType: checkParams,
116+
};
69117
},
70118
});

scripts/eslint/tests/no-keywords.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@ const obj = {
5252
};
5353
`,
5454
},
55+
{
56+
code: `() => undefined`,
57+
},
58+
{
59+
code: `let a = 2;`,
60+
},
61+
{
62+
code: `let b: any;`,
63+
},
64+
{
65+
code: `function foo(a: any) { }`,
66+
},
67+
{
68+
code: `let [a] = [5];`,
69+
},
70+
{
71+
code: `const { a } = { a: 5 };`,
72+
},
73+
{
74+
code: `
75+
interface Foo {
76+
number: string;
77+
}
78+
`,
79+
}
5580
],
5681

5782
invalid: [
@@ -95,5 +120,39 @@ interface A {
95120
{ messageId: "noKeywordsError" },
96121
],
97122
},
123+
{
124+
code: `let undefined = 8;`,
125+
errors: [{ messageId: "noKeywordsError" }],
126+
},
127+
{
128+
code: `let boolean: boolean;`,
129+
errors: [{ messageId: "noKeywordsError" }],
130+
},
131+
{
132+
code: `function foo(any: any) { }`,
133+
errors: [{ messageId: "noKeywordsError" }],
134+
},
135+
{
136+
code: `let [number] = [3];`,
137+
errors: [{ messageId: "noKeywordsError" }],
138+
},
139+
{
140+
code: `let { String } = { String: 1 };`,
141+
errors: [{ messageId: "noKeywordsError" }],
142+
},
143+
{
144+
code: `let [number, string] = [3, ''];`,
145+
errors: [
146+
{ messageId: "noKeywordsError" },
147+
{ messageId: "noKeywordsError" }
148+
],
149+
},
150+
{
151+
code: `let { String, Boolean } = { String: 1, Boolean: false };`,
152+
errors: [
153+
{ messageId: "noKeywordsError" },
154+
{ messageId: "noKeywordsError" }
155+
],
156+
},
98157
],
99158
});

0 commit comments

Comments
 (0)