@flint.fyi/plugin-flint 0.0.2 → 0.2.0
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/CHANGELOG.md +17 -0
- package/lib/findProperty.d.ts +2 -0
- package/lib/findProperty.js +7 -0
- package/lib/flint.d.ts +6 -1
- package/lib/flint.js +3 -2
- package/lib/getRuleTesterDescribedCases.d.ts +5 -0
- package/lib/getRuleTesterDescribedCases.js +31 -0
- package/lib/parseTestCases.d.ts +4 -0
- package/lib/parseTestCases.js +62 -0
- package/lib/rules/invalidCodeLines.d.ts +6 -0
- package/lib/rules/invalidCodeLines.js +71 -0
- package/lib/rules/invalidCodeLines.test.js +197 -0
- package/lib/rules/{duplicateTestCases.d.ts → testCaseDuplicates.d.ts} +2 -1
- package/lib/rules/{duplicateTestCases.js → testCaseDuplicates.js} +8 -4
- package/lib/rules/testCaseDuplicates.test.d.ts +1 -0
- package/lib/rules/{duplicateTestCases.test.js → testCaseDuplicates.test.js} +13 -13
- package/lib/types.d.ts +17 -0
- package/lib/types.js +1 -0
- package/package.json +1 -1
- package/src/findProperty.ts +15 -0
- package/src/flint.ts +3 -2
- package/src/getRuleTesterDescribedCases.ts +48 -0
- package/src/parseTestCases.ts +96 -0
- package/src/rules/invalidCodeLines.test.ts +198 -0
- package/src/rules/invalidCodeLines.ts +84 -0
- package/src/rules/{duplicateTestCases.test.ts → testCaseDuplicates.test.ts} +13 -13
- package/src/rules/{duplicateTestCases.ts → testCaseDuplicates.ts} +11 -7
- package/src/types.ts +22 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/rules/getRuleTesterDescribedCases.d.ts +0 -9
- package/lib/rules/getRuleTesterDescribedCases.js +0 -59
- package/src/rules/getRuleTesterDescribedCases.ts +0 -102
- /package/lib/rules/{duplicateTestCases.test.d.ts → invalidCodeLines.test.d.ts} +0 -0
- /package/lib/{rules/tsAstToLiteral.d.ts → tsAstToLiteral.d.ts} +0 -0
- /package/lib/{rules/tsAstToLiteral.js → tsAstToLiteral.js} +0 -0
- /package/src/{rules/tsAstToLiteral.ts → tsAstToLiteral.ts} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @flint.fyi/plugin-flint
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 950e84c: feat(rule-tester): [invalidCodeLines] add rule
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- d89c480: feat(comparisons): add eslint-plugin-eslint-plugin comparisons
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [738fe36]
|
|
18
|
+
- @flint.fyi/rule-tester@0.14.3
|
|
19
|
+
|
|
3
20
|
## 0.0.2
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
export function findProperty(properties, name, predicate) {
|
|
3
|
+
return properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
4
|
+
ts.isIdentifier(property.name) &&
|
|
5
|
+
property.name.text === name &&
|
|
6
|
+
predicate(property.initializer))?.initializer;
|
|
7
|
+
}
|
package/lib/flint.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export declare const flint: import("@flint.fyi/core").Plugin<import("@flint.fyi/core").RuleAbout, string | undefined, [import("@flint.fyi/core").Rule<{
|
|
2
|
-
readonly
|
|
2
|
+
readonly description: "Reports cases for invalid code that isn't formatted across lines.";
|
|
3
|
+
readonly id: "invalidCodeLines";
|
|
4
|
+
readonly preset: "logical";
|
|
5
|
+
}, import("@flint.fyi/ts/lib/nodes.js").TSNodesByName, import("@flint.fyi/ts").TypeScriptServices, "singleLineTest", undefined>, import("@flint.fyi/core").Rule<{
|
|
6
|
+
readonly description: "Reports test cases that are identical to previous test cases.";
|
|
7
|
+
readonly id: "testCaseDuplicates";
|
|
3
8
|
readonly preset: "logical";
|
|
4
9
|
}, import("@flint.fyi/ts/lib/nodes.js").TSNodesByName, import("@flint.fyi/ts").TypeScriptServices, "duplicateTest", undefined>]>;
|
package/lib/flint.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createPlugin } from "@flint.fyi/core";
|
|
2
|
-
import
|
|
2
|
+
import invalidCodeLines from "./rules/invalidCodeLines.js";
|
|
3
|
+
import testCaseDuplicates from "./rules/testCaseDuplicates.js";
|
|
3
4
|
export const flint = createPlugin({
|
|
4
5
|
name: "flint",
|
|
5
|
-
rules: [
|
|
6
|
+
rules: [invalidCodeLines, testCaseDuplicates],
|
|
6
7
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isTruthy } from "@flint.fyi/utils";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import { findProperty } from "./findProperty.js";
|
|
4
|
+
import { parseTestCase, parseTestCaseInvalid } from "./parseTestCases.js";
|
|
5
|
+
export function getRuleTesterDescribedCases(node) {
|
|
6
|
+
if (!ts.isPropertyAccessExpression(node.expression) ||
|
|
7
|
+
!ts.isIdentifier(node.expression.expression) ||
|
|
8
|
+
!ts.isIdentifier(node.expression.name) ||
|
|
9
|
+
node.expression.name.text !== "describe" ||
|
|
10
|
+
node.arguments.length !== 2) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
// TODO: Check node.expression.expression's type for being a RuleTester
|
|
14
|
+
// https://github.com/JoshuaKGoldberg/flint/issues/152
|
|
15
|
+
const argument = node.arguments[1];
|
|
16
|
+
if (!ts.isObjectLiteralExpression(argument)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const invalid = findProperty(argument.properties, "invalid", ts.isArrayLiteralExpression);
|
|
20
|
+
if (!invalid) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const valid = findProperty(argument.properties, "valid", ts.isArrayLiteralExpression);
|
|
24
|
+
if (!valid) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
invalid: invalid.elements.map(parseTestCaseInvalid).filter(isTruthy),
|
|
29
|
+
valid: valid.elements.map(parseTestCase).filter(isTruthy),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import type { ParsedTestCase, ParsedTestCaseInvalid } from "./types.js";
|
|
3
|
+
export declare function parseTestCase(node: ts.Expression): ParsedTestCase | undefined;
|
|
4
|
+
export declare function parseTestCaseInvalid(node: ts.Expression): ParsedTestCaseInvalid | undefined;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import { findProperty } from "./findProperty.js";
|
|
3
|
+
import { tsAstToLiteral } from "./tsAstToLiteral.js";
|
|
4
|
+
export function parseTestCase(node) {
|
|
5
|
+
if (ts.isStringLiteralLike(node)) {
|
|
6
|
+
return {
|
|
7
|
+
code: node.text,
|
|
8
|
+
nodes: {
|
|
9
|
+
case: node,
|
|
10
|
+
code: node,
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (!ts.isObjectLiteralExpression(node)) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
|
|
18
|
+
if (!code) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const fileName = findProperty(node.properties, "fileName", ts.isStringLiteralLike);
|
|
22
|
+
const options = findProperty(node.properties, "options", ts.isObjectLiteralExpression);
|
|
23
|
+
return {
|
|
24
|
+
code: code.text,
|
|
25
|
+
fileName: fileName?.text,
|
|
26
|
+
nodes: {
|
|
27
|
+
case: node,
|
|
28
|
+
code,
|
|
29
|
+
fileName,
|
|
30
|
+
options,
|
|
31
|
+
},
|
|
32
|
+
options: options && tsAstToLiteral(options),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function parseTestCaseInvalid(node) {
|
|
36
|
+
if (!ts.isObjectLiteralExpression(node)) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
|
|
40
|
+
if (!code) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const fileName = findProperty(node.properties, "fileName", ts.isStringLiteralLike);
|
|
44
|
+
const options = findProperty(node.properties, "options", ts.isObjectLiteralExpression);
|
|
45
|
+
const snapshot = findProperty(node.properties, "snapshot", ts.isStringLiteralLike);
|
|
46
|
+
if (!snapshot) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
code: code.text,
|
|
51
|
+
fileName: fileName?.text,
|
|
52
|
+
nodes: {
|
|
53
|
+
case: node,
|
|
54
|
+
code,
|
|
55
|
+
fileName,
|
|
56
|
+
options,
|
|
57
|
+
snapshot,
|
|
58
|
+
},
|
|
59
|
+
options: options && tsAstToLiteral(options),
|
|
60
|
+
snapshot: snapshot.text,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
declare const _default: import("@flint.fyi/core").Rule<{
|
|
2
|
+
readonly description: "Reports cases for invalid code that isn't formatted across lines.";
|
|
3
|
+
readonly id: "invalidCodeLines";
|
|
4
|
+
readonly preset: "logical";
|
|
5
|
+
}, import("@flint.fyi/ts/lib/nodes.js").TSNodesByName, import("@flint.fyi/ts").TypeScriptServices, "singleLineTest", undefined>;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { getTSNodeRange, typescriptLanguage } from "@flint.fyi/ts";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import { getRuleTesterDescribedCases } from "../getRuleTesterDescribedCases.js";
|
|
4
|
+
export default typescriptLanguage.createRule({
|
|
5
|
+
about: {
|
|
6
|
+
description: "Reports cases for invalid code that isn't formatted across lines.",
|
|
7
|
+
id: "invalidCodeLines",
|
|
8
|
+
preset: "logical",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
singleLineTest: {
|
|
12
|
+
primary: "This code block should be formatted across multiple lines for more readable reports.",
|
|
13
|
+
secondary: [
|
|
14
|
+
"When writing `invalid` case code blocks, it's better to start code on a new line in a template literal string.",
|
|
15
|
+
"Doing so allows the case's `snapshot` to visualize the `~` characters visually underneath reported ranges.",
|
|
16
|
+
],
|
|
17
|
+
suggestions: [
|
|
18
|
+
"Delete this redundant test case.",
|
|
19
|
+
"Change a property to make the test case unique.",
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
setup(context) {
|
|
24
|
+
function checkTestCase(testCase) {
|
|
25
|
+
if (!testCase.code.startsWith("\n")) {
|
|
26
|
+
context.report({
|
|
27
|
+
fix: [createFixForCode(testCase), createFixForSnapshot(testCase)],
|
|
28
|
+
message: "singleLineTest",
|
|
29
|
+
range: getTSNodeRange(testCase.nodes.code, context.sourceFile),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function createFixForCode(testCase) {
|
|
34
|
+
if (ts.isStringLiteral(testCase.nodes.code)) {
|
|
35
|
+
return {
|
|
36
|
+
range: getTSNodeRange(testCase.nodes.code, context.sourceFile),
|
|
37
|
+
text: `\`\n${testCase.code}\n\``,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const begin = testCase.nodes.code.getStart(context.sourceFile) + 1;
|
|
41
|
+
return {
|
|
42
|
+
range: {
|
|
43
|
+
begin,
|
|
44
|
+
end: begin,
|
|
45
|
+
},
|
|
46
|
+
text: "\n",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function createFixForSnapshot(testCase) {
|
|
50
|
+
const begin = testCase.nodes.snapshot.getStart(context.sourceFile) + 1;
|
|
51
|
+
return {
|
|
52
|
+
range: {
|
|
53
|
+
begin,
|
|
54
|
+
end: begin,
|
|
55
|
+
},
|
|
56
|
+
text: "\n",
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
visitors: {
|
|
61
|
+
CallExpression(node) {
|
|
62
|
+
const describedCases = getRuleTesterDescribedCases(node);
|
|
63
|
+
if (!describedCases) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
describedCases.invalid.forEach(checkTestCase);
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import rule from "./invalidCodeLines.js";
|
|
2
|
+
import { ruleTester } from "./ruleTester.js";
|
|
3
|
+
ruleTester.describe(rule, {
|
|
4
|
+
invalid: [
|
|
5
|
+
{
|
|
6
|
+
code: `
|
|
7
|
+
ruleTester.describe(rule, {
|
|
8
|
+
valid: ['a', 'a'],
|
|
9
|
+
invalid: [
|
|
10
|
+
{
|
|
11
|
+
code: "",
|
|
12
|
+
snapshot: \`
|
|
13
|
+
~
|
|
14
|
+
Rule report message.
|
|
15
|
+
\`,
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
`,
|
|
20
|
+
output: `
|
|
21
|
+
ruleTester.describe(rule, {
|
|
22
|
+
valid: ['a', 'a'],
|
|
23
|
+
invalid: [
|
|
24
|
+
{
|
|
25
|
+
code: \`
|
|
26
|
+
|
|
27
|
+
\`,
|
|
28
|
+
snapshot: \`
|
|
29
|
+
~
|
|
30
|
+
Rule report message.
|
|
31
|
+
\`,
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
`,
|
|
36
|
+
snapshot: `
|
|
37
|
+
ruleTester.describe(rule, {
|
|
38
|
+
valid: ['a', 'a'],
|
|
39
|
+
invalid: [
|
|
40
|
+
{
|
|
41
|
+
code: "",
|
|
42
|
+
~~
|
|
43
|
+
This code block should be formatted across multiple lines for more readable reports.
|
|
44
|
+
snapshot: \`
|
|
45
|
+
~
|
|
46
|
+
Rule report message.
|
|
47
|
+
\`,
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
`,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
code: `
|
|
55
|
+
ruleTester.describe(rule, {
|
|
56
|
+
valid: ['a', 'a'],
|
|
57
|
+
invalid: [
|
|
58
|
+
{
|
|
59
|
+
code: \`console.log(
|
|
60
|
+
);\`,
|
|
61
|
+
snapshot: \`console.log(
|
|
62
|
+
);
|
|
63
|
+
~
|
|
64
|
+
Rule report message.
|
|
65
|
+
\`,
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
`,
|
|
70
|
+
output: `
|
|
71
|
+
ruleTester.describe(rule, {
|
|
72
|
+
valid: ['a', 'a'],
|
|
73
|
+
invalid: [
|
|
74
|
+
{
|
|
75
|
+
code: \`
|
|
76
|
+
console.log(
|
|
77
|
+
);
|
|
78
|
+
\`,
|
|
79
|
+
snapshot: \`
|
|
80
|
+
console.log(
|
|
81
|
+
);
|
|
82
|
+
~
|
|
83
|
+
Rule report message.
|
|
84
|
+
\`,
|
|
85
|
+
}
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
`,
|
|
89
|
+
snapshot: `
|
|
90
|
+
ruleTester.describe(rule, {
|
|
91
|
+
valid: ['a', 'a'],
|
|
92
|
+
invalid: [
|
|
93
|
+
{
|
|
94
|
+
code: \`console.log(
|
|
95
|
+
~~~~~~~~~~~~~
|
|
96
|
+
This code block should be formatted across multiple lines for more readable reports.
|
|
97
|
+
);\`,
|
|
98
|
+
~~~
|
|
99
|
+
snapshot: \`console.log(
|
|
100
|
+
);
|
|
101
|
+
~
|
|
102
|
+
Rule report message.
|
|
103
|
+
\`,
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
`,
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
code: `
|
|
111
|
+
ruleTester.describe(rule, {
|
|
112
|
+
valid: ['a', 'a'],
|
|
113
|
+
invalid: [
|
|
114
|
+
{
|
|
115
|
+
code: \`console.log();\`,
|
|
116
|
+
snapshot: \`console.log();
|
|
117
|
+
~~~~~~~~~~~~
|
|
118
|
+
Rule report message.
|
|
119
|
+
\`,
|
|
120
|
+
}
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
`,
|
|
124
|
+
output: `
|
|
125
|
+
ruleTester.describe(rule, {
|
|
126
|
+
valid: ['a', 'a'],
|
|
127
|
+
invalid: [
|
|
128
|
+
{
|
|
129
|
+
code: \`
|
|
130
|
+
console.log();
|
|
131
|
+
\`,
|
|
132
|
+
snapshot: \`
|
|
133
|
+
console.log();
|
|
134
|
+
~~~~~~~~~~~~~
|
|
135
|
+
Rule report message.
|
|
136
|
+
\`,
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
`,
|
|
141
|
+
snapshot: `
|
|
142
|
+
ruleTester.describe(rule, {
|
|
143
|
+
valid: ['a', 'a'],
|
|
144
|
+
invalid: [
|
|
145
|
+
{
|
|
146
|
+
code: \`console.log();\`,
|
|
147
|
+
~~~~~~~~~~~~~~~~
|
|
148
|
+
This code block should be formatted across multiple lines for more readable reports.
|
|
149
|
+
snapshot: \`console.log();
|
|
150
|
+
~~~~~~~~~~~~
|
|
151
|
+
Rule report message.
|
|
152
|
+
\`,
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
});
|
|
156
|
+
`,
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
valid: [
|
|
160
|
+
`
|
|
161
|
+
ruleTester.describe(rule, {
|
|
162
|
+
valid: [],
|
|
163
|
+
invalid: []
|
|
164
|
+
});
|
|
165
|
+
`,
|
|
166
|
+
`
|
|
167
|
+
ruleTester.describe(rule, {
|
|
168
|
+
valid: [],
|
|
169
|
+
invalid: [
|
|
170
|
+
{
|
|
171
|
+
code: \`
|
|
172
|
+
\`,
|
|
173
|
+
snapshot: \`
|
|
174
|
+
~
|
|
175
|
+
\`,
|
|
176
|
+
}
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
`,
|
|
180
|
+
`
|
|
181
|
+
ruleTester.describe(rule, {
|
|
182
|
+
valid: [],
|
|
183
|
+
invalid: [
|
|
184
|
+
{
|
|
185
|
+
code: \`
|
|
186
|
+
console.log();
|
|
187
|
+
\`,
|
|
188
|
+
snapshot: \`
|
|
189
|
+
~~~~~~~~~~~~
|
|
190
|
+
Rule report message.
|
|
191
|
+
\`,
|
|
192
|
+
}
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
`,
|
|
196
|
+
],
|
|
197
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare const _default: import("@flint.fyi/core").Rule<{
|
|
2
|
-
readonly
|
|
2
|
+
readonly description: "Reports test cases that are identical to previous test cases.";
|
|
3
|
+
readonly id: "testCaseDuplicates";
|
|
3
4
|
readonly preset: "logical";
|
|
4
5
|
}, import("@flint.fyi/ts/lib/nodes.js").TSNodesByName, import("@flint.fyi/ts").TypeScriptServices, "duplicateTest", undefined>;
|
|
5
6
|
export default _default;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { getTSNodeRange, typescriptLanguage } from "@flint.fyi/ts";
|
|
2
|
-
import { getRuleTesterDescribedCases
|
|
2
|
+
import { getRuleTesterDescribedCases } from "../getRuleTesterDescribedCases.js";
|
|
3
3
|
export default typescriptLanguage.createRule({
|
|
4
4
|
about: {
|
|
5
|
-
|
|
5
|
+
description: "Reports test cases that are identical to previous test cases.",
|
|
6
|
+
id: "testCaseDuplicates",
|
|
6
7
|
preset: "logical",
|
|
7
8
|
},
|
|
8
9
|
messages: {
|
|
9
10
|
duplicateTest: {
|
|
10
11
|
primary: "This test code already appeared in a previous test.",
|
|
11
|
-
secondary: [
|
|
12
|
+
secondary: [
|
|
13
|
+
"When writing tests for lint rules, it's possible to accidentally create deeply identical test cases.",
|
|
14
|
+
"Doing so provides no added benefit for testing and is unnecessary.",
|
|
15
|
+
],
|
|
12
16
|
suggestions: [
|
|
13
17
|
"Delete this redundant test case.",
|
|
14
18
|
"Change a property to make the test case unique.",
|
|
@@ -27,7 +31,7 @@ export default typescriptLanguage.createRule({
|
|
|
27
31
|
if (seen.has(key)) {
|
|
28
32
|
context.report({
|
|
29
33
|
message: "duplicateTest",
|
|
30
|
-
range: getTSNodeRange(testCase.
|
|
34
|
+
range: getTSNodeRange(testCase.nodes.case, context.sourceFile),
|
|
31
35
|
});
|
|
32
36
|
}
|
|
33
37
|
else {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import rule from "./duplicateTestCases.js";
|
|
2
1
|
import { ruleTester } from "./ruleTester.js";
|
|
2
|
+
import rule from "./testCaseDuplicates.js";
|
|
3
3
|
ruleTester.describe(rule, {
|
|
4
4
|
invalid: [
|
|
5
5
|
{
|
|
@@ -12,8 +12,8 @@ ruleTester.describe(rule, {
|
|
|
12
12
|
snapshot: `
|
|
13
13
|
ruleTester.describe(rule, {
|
|
14
14
|
valid: ['a', 'a'],
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
~~~
|
|
16
|
+
This test code already appeared in a previous test.
|
|
17
17
|
invalid: []
|
|
18
18
|
});
|
|
19
19
|
`,
|
|
@@ -33,8 +33,8 @@ ruleTester.describe(rule, {
|
|
|
33
33
|
valid: [
|
|
34
34
|
'a',
|
|
35
35
|
"a",
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
~~~
|
|
37
|
+
This test code already appeared in a previous test.
|
|
38
38
|
],
|
|
39
39
|
invalid: []
|
|
40
40
|
});
|
|
@@ -55,8 +55,8 @@ ruleTester.describe(rule, {
|
|
|
55
55
|
valid: [
|
|
56
56
|
\`a\`,
|
|
57
57
|
\`a\`,
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
~~~
|
|
59
|
+
This test code already appeared in a previous test.
|
|
60
60
|
],
|
|
61
61
|
invalid: []
|
|
62
62
|
});
|
|
@@ -77,8 +77,8 @@ ruleTester.describe(rule, {
|
|
|
77
77
|
valid: [
|
|
78
78
|
{ code: "a" },
|
|
79
79
|
{ code: "a" },
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
~~~~~~~~~~~~~
|
|
81
|
+
This test code already appeared in a previous test.
|
|
82
82
|
],
|
|
83
83
|
invalid: []
|
|
84
84
|
});
|
|
@@ -99,8 +99,8 @@ ruleTester.describe(rule, {
|
|
|
99
99
|
valid: [
|
|
100
100
|
{ code: "a", fileName: "b.ts" },
|
|
101
101
|
{ code: "a", fileName: "b.ts" },
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
103
|
+
This test code already appeared in a previous test.
|
|
104
104
|
],
|
|
105
105
|
invalid: []
|
|
106
106
|
});
|
|
@@ -121,8 +121,8 @@ ruleTester.describe(rule, {
|
|
|
121
121
|
valid: [
|
|
122
122
|
{ code: "a", fileName: "b.ts", options: { c: "d" } },
|
|
123
123
|
{ code: "a", fileName: "b.ts", options: { c: "d" } },
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
125
|
+
This test code already appeared in a previous test.
|
|
126
126
|
],
|
|
127
127
|
invalid: []
|
|
128
128
|
});
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { InvalidTestCase, TestCase } from "@flint.fyi/rule-tester";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
export interface ParsedTestCase extends TestCase {
|
|
4
|
+
nodes: ParsedTestCaseNodes;
|
|
5
|
+
}
|
|
6
|
+
export interface ParsedTestCaseInvalid extends InvalidTestCase {
|
|
7
|
+
nodes: ParsedTestCaseNodesInvalid;
|
|
8
|
+
}
|
|
9
|
+
export interface ParsedTestCaseNodes {
|
|
10
|
+
case: ts.Node;
|
|
11
|
+
code: ts.StringLiteralLike;
|
|
12
|
+
fileName?: ts.StringLiteralLike;
|
|
13
|
+
options?: ts.ObjectLiteralExpression;
|
|
14
|
+
}
|
|
15
|
+
export interface ParsedTestCaseNodesInvalid extends ParsedTestCaseNodes {
|
|
16
|
+
snapshot: ts.Node;
|
|
17
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export function findProperty<Node extends ts.Node>(
|
|
4
|
+
properties: ts.NodeArray<ts.ObjectLiteralElementLike>,
|
|
5
|
+
name: string,
|
|
6
|
+
predicate: (node: ts.Node) => node is Node,
|
|
7
|
+
) {
|
|
8
|
+
return properties.find(
|
|
9
|
+
(property): property is ts.PropertyAssignment & { initializer: Node } =>
|
|
10
|
+
ts.isPropertyAssignment(property) &&
|
|
11
|
+
ts.isIdentifier(property.name) &&
|
|
12
|
+
property.name.text === name &&
|
|
13
|
+
predicate(property.initializer),
|
|
14
|
+
)?.initializer;
|
|
15
|
+
}
|
package/src/flint.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createPlugin } from "@flint.fyi/core";
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import invalidCodeLines from "./rules/invalidCodeLines.js";
|
|
4
|
+
import testCaseDuplicates from "./rules/testCaseDuplicates.js";
|
|
4
5
|
|
|
5
6
|
export const flint = createPlugin({
|
|
6
7
|
name: "flint",
|
|
7
|
-
rules: [
|
|
8
|
+
rules: [invalidCodeLines, testCaseDuplicates],
|
|
8
9
|
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isTruthy } from "@flint.fyi/utils";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { findProperty } from "./findProperty.js";
|
|
5
|
+
import { parseTestCase, parseTestCaseInvalid } from "./parseTestCases.js";
|
|
6
|
+
|
|
7
|
+
export function getRuleTesterDescribedCases(node: ts.CallExpression) {
|
|
8
|
+
if (
|
|
9
|
+
!ts.isPropertyAccessExpression(node.expression) ||
|
|
10
|
+
!ts.isIdentifier(node.expression.expression) ||
|
|
11
|
+
!ts.isIdentifier(node.expression.name) ||
|
|
12
|
+
node.expression.name.text !== "describe" ||
|
|
13
|
+
node.arguments.length !== 2
|
|
14
|
+
) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// TODO: Check node.expression.expression's type for being a RuleTester
|
|
19
|
+
// https://github.com/JoshuaKGoldberg/flint/issues/152
|
|
20
|
+
|
|
21
|
+
const argument = node.arguments[1];
|
|
22
|
+
if (!ts.isObjectLiteralExpression(argument)) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const invalid = findProperty(
|
|
27
|
+
argument.properties,
|
|
28
|
+
"invalid",
|
|
29
|
+
ts.isArrayLiteralExpression,
|
|
30
|
+
);
|
|
31
|
+
if (!invalid) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const valid = findProperty(
|
|
36
|
+
argument.properties,
|
|
37
|
+
"valid",
|
|
38
|
+
ts.isArrayLiteralExpression,
|
|
39
|
+
);
|
|
40
|
+
if (!valid) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
invalid: invalid.elements.map(parseTestCaseInvalid).filter(isTruthy),
|
|
46
|
+
valid: valid.elements.map(parseTestCase).filter(isTruthy),
|
|
47
|
+
};
|
|
48
|
+
}
|