@flint.fyi/plugin-flint 0.1.0 → 0.2.1

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.
Files changed (37) hide show
  1. package/lib/findProperty.d.ts +3 -0
  2. package/lib/findProperty.js +8 -0
  3. package/lib/flint.d.ts +6 -1
  4. package/lib/flint.js +4 -2
  5. package/lib/getRuleTesterDescribedCases.d.ts +6 -0
  6. package/lib/getRuleTesterDescribedCases.js +32 -0
  7. package/lib/index.d.ts +1 -0
  8. package/lib/index.js +1 -0
  9. package/lib/parseTestCases.d.ts +5 -0
  10. package/lib/parseTestCases.js +63 -0
  11. package/lib/rules/invalidCodeLines.d.ts +7 -0
  12. package/lib/rules/invalidCodeLines.js +77 -0
  13. package/lib/rules/invalidCodeLines.test.d.ts +2 -0
  14. package/lib/rules/invalidCodeLines.test.js +198 -0
  15. package/lib/rules/ruleTester.d.ts +1 -0
  16. package/lib/rules/ruleTester.js +6 -1
  17. package/lib/rules/testCaseDuplicates.d.ts +2 -1
  18. package/lib/rules/testCaseDuplicates.js +7 -6
  19. package/lib/rules/testCaseDuplicates.test.d.ts +1 -0
  20. package/lib/rules/testCaseDuplicates.test.js +1 -0
  21. package/lib/{rules/tsAstToLiteral.d.ts → tsAstToLiteral.d.ts} +1 -0
  22. package/lib/{rules/tsAstToLiteral.js → tsAstToLiteral.js} +1 -0
  23. package/lib/types.d.ts +18 -0
  24. package/lib/types.js +2 -0
  25. package/package.json +17 -7
  26. package/CHANGELOG.md +0 -30
  27. package/lib/rules/getRuleTesterDescribedCases.d.ts +0 -9
  28. package/lib/rules/getRuleTesterDescribedCases.js +0 -59
  29. package/src/flint.ts +0 -8
  30. package/src/index.ts +0 -1
  31. package/src/rules/getRuleTesterDescribedCases.ts +0 -102
  32. package/src/rules/ruleTester.ts +0 -4
  33. package/src/rules/testCaseDuplicates.test.ts +0 -174
  34. package/src/rules/testCaseDuplicates.ts +0 -64
  35. package/src/rules/tsAstToLiteral.ts +0 -55
  36. package/tsconfig.json +0 -14
  37. package/tsconfig.tsbuildinfo +0 -1
@@ -1,59 +0,0 @@
1
- import { isTruthy } from "@flint.fyi/utils";
2
- import * as ts from "typescript";
3
- import { tsAstToLiteral } from "./tsAstToLiteral.js";
4
- export function getRuleTesterDescribedCases(node) {
5
- if (!ts.isPropertyAccessExpression(node.expression) ||
6
- !ts.isIdentifier(node.expression.expression) ||
7
- !ts.isIdentifier(node.expression.name) ||
8
- node.expression.name.text !== "describe" ||
9
- node.arguments.length !== 2) {
10
- return undefined;
11
- }
12
- // TODO: Check node.expression.expression's type for being a RuleTester
13
- // https://github.com/JoshuaKGoldberg/flint/issues/152
14
- const argument = node.arguments[1];
15
- if (!ts.isObjectLiteralExpression(argument)) {
16
- return undefined;
17
- }
18
- const invalid = findProperty(argument.properties, "invalid", ts.isArrayLiteralExpression);
19
- if (!invalid) {
20
- return undefined;
21
- }
22
- const valid = findProperty(argument.properties, "valid", ts.isArrayLiteralExpression);
23
- if (!valid) {
24
- return undefined;
25
- }
26
- return {
27
- invalid: invalid.elements.map(parseTestCase).filter(isTruthy),
28
- valid: valid.elements.map(parseTestCase).filter(isTruthy),
29
- };
30
- }
31
- function findProperty(properties, name, predicate) {
32
- return properties.find((property) => ts.isPropertyAssignment(property) &&
33
- ts.isIdentifier(property.name) &&
34
- property.name.text === name &&
35
- predicate(property.initializer))?.initializer;
36
- }
37
- function parseTestCase(node) {
38
- if (ts.isStringLiteralLike(node)) {
39
- return {
40
- code: node.text,
41
- node,
42
- };
43
- }
44
- if (!ts.isObjectLiteralExpression(node)) {
45
- return undefined;
46
- }
47
- const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
48
- if (!code) {
49
- return undefined;
50
- }
51
- const fileName = findProperty(node.properties, "fileName", ts.isStringLiteralLike);
52
- const options = findProperty(node.properties, "options", ts.isObjectLiteralExpression);
53
- return {
54
- code: code.text,
55
- fileName: fileName?.text,
56
- node,
57
- options: options && tsAstToLiteral(options),
58
- };
59
- }
package/src/flint.ts DELETED
@@ -1,8 +0,0 @@
1
- import { createPlugin } from "@flint.fyi/core";
2
-
3
- import testCaseDuplicates from "./rules/testCaseDuplicates.js";
4
-
5
- export const flint = createPlugin({
6
- name: "flint",
7
- rules: [testCaseDuplicates],
8
- });
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./flint.js";
@@ -1,102 +0,0 @@
1
- import { TestCase } from "@flint.fyi/rule-tester";
2
- import { isTruthy } from "@flint.fyi/utils";
3
- import * as ts from "typescript";
4
-
5
- import { tsAstToLiteral } from "./tsAstToLiteral.js";
6
-
7
- export interface ParsedTestCase extends TestCase {
8
- node: ts.Node;
9
- }
10
-
11
- export function getRuleTesterDescribedCases(node: ts.CallExpression) {
12
- if (
13
- !ts.isPropertyAccessExpression(node.expression) ||
14
- !ts.isIdentifier(node.expression.expression) ||
15
- !ts.isIdentifier(node.expression.name) ||
16
- node.expression.name.text !== "describe" ||
17
- node.arguments.length !== 2
18
- ) {
19
- return undefined;
20
- }
21
-
22
- // TODO: Check node.expression.expression's type for being a RuleTester
23
- // https://github.com/JoshuaKGoldberg/flint/issues/152
24
-
25
- const argument = node.arguments[1];
26
- if (!ts.isObjectLiteralExpression(argument)) {
27
- return undefined;
28
- }
29
-
30
- const invalid = findProperty(
31
- argument.properties,
32
- "invalid",
33
- ts.isArrayLiteralExpression,
34
- );
35
- if (!invalid) {
36
- return undefined;
37
- }
38
-
39
- const valid = findProperty(
40
- argument.properties,
41
- "valid",
42
- ts.isArrayLiteralExpression,
43
- );
44
- if (!valid) {
45
- return undefined;
46
- }
47
-
48
- return {
49
- invalid: invalid.elements.map(parseTestCase).filter(isTruthy),
50
- valid: valid.elements.map(parseTestCase).filter(isTruthy),
51
- };
52
- }
53
-
54
- function findProperty<Node extends ts.Node>(
55
- properties: ts.NodeArray<ts.ObjectLiteralElementLike>,
56
- name: string,
57
- predicate: (node: ts.Node) => node is Node,
58
- ) {
59
- return properties.find(
60
- (property): property is ts.PropertyAssignment & { initializer: Node } =>
61
- ts.isPropertyAssignment(property) &&
62
- ts.isIdentifier(property.name) &&
63
- property.name.text === name &&
64
- predicate(property.initializer),
65
- )?.initializer;
66
- }
67
-
68
- function parseTestCase(node: ts.Expression): ParsedTestCase | undefined {
69
- if (ts.isStringLiteralLike(node)) {
70
- return {
71
- code: node.text,
72
- node,
73
- };
74
- }
75
-
76
- if (!ts.isObjectLiteralExpression(node)) {
77
- return undefined;
78
- }
79
-
80
- const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
81
- if (!code) {
82
- return undefined;
83
- }
84
-
85
- const fileName = findProperty(
86
- node.properties,
87
- "fileName",
88
- ts.isStringLiteralLike,
89
- );
90
- const options = findProperty(
91
- node.properties,
92
- "options",
93
- ts.isObjectLiteralExpression,
94
- );
95
-
96
- return {
97
- code: code.text,
98
- fileName: fileName?.text,
99
- node,
100
- options: options && tsAstToLiteral(options),
101
- };
102
- }
@@ -1,4 +0,0 @@
1
- import { RuleTester } from "@flint.fyi/rule-tester";
2
- import { describe, it } from "vitest";
3
-
4
- export const ruleTester = new RuleTester({ describe, it });
@@ -1,174 +0,0 @@
1
- import { ruleTester } from "./ruleTester.js";
2
- import rule from "./testCaseDuplicates.js";
3
-
4
- ruleTester.describe(rule, {
5
- invalid: [
6
- {
7
- code: `
8
- ruleTester.describe(rule, {
9
- valid: ['a', 'a'],
10
- invalid: []
11
- });
12
- `,
13
- snapshot: `
14
- ruleTester.describe(rule, {
15
- valid: ['a', 'a'],
16
- ~~~
17
- This test code already appeared in a previous test.
18
- invalid: []
19
- });
20
- `,
21
- },
22
- {
23
- code: `
24
- ruleTester.describe(rule, {
25
- valid: [
26
- 'a',
27
- "a",
28
- ],
29
- invalid: []
30
- });
31
- `,
32
- snapshot: `
33
- ruleTester.describe(rule, {
34
- valid: [
35
- 'a',
36
- "a",
37
- ~~~
38
- This test code already appeared in a previous test.
39
- ],
40
- invalid: []
41
- });
42
- `,
43
- },
44
- {
45
- code: `
46
- ruleTester.describe(rule, {
47
- valid: [
48
- \`a\`,
49
- \`a\`,
50
- ],
51
- invalid: []
52
- });
53
- `,
54
- snapshot: `
55
- ruleTester.describe(rule, {
56
- valid: [
57
- \`a\`,
58
- \`a\`,
59
- ~~~
60
- This test code already appeared in a previous test.
61
- ],
62
- invalid: []
63
- });
64
- `,
65
- },
66
- {
67
- code: `
68
- ruleTester.describe(rule, {
69
- valid: [
70
- { code: "a" },
71
- { code: "a" },
72
- ],
73
- invalid: []
74
- });
75
- `,
76
- snapshot: `
77
- ruleTester.describe(rule, {
78
- valid: [
79
- { code: "a" },
80
- { code: "a" },
81
- ~~~~~~~~~~~~~
82
- This test code already appeared in a previous test.
83
- ],
84
- invalid: []
85
- });
86
- `,
87
- },
88
- {
89
- code: `
90
- ruleTester.describe(rule, {
91
- valid: [
92
- { code: "a", fileName: "b.ts" },
93
- { code: "a", fileName: "b.ts" },
94
- ],
95
- invalid: []
96
- });
97
- `,
98
- snapshot: `
99
- ruleTester.describe(rule, {
100
- valid: [
101
- { code: "a", fileName: "b.ts" },
102
- { code: "a", fileName: "b.ts" },
103
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104
- This test code already appeared in a previous test.
105
- ],
106
- invalid: []
107
- });
108
- `,
109
- },
110
- {
111
- code: `
112
- ruleTester.describe(rule, {
113
- valid: [
114
- { code: "a", fileName: "b.ts", options: { c: "d" } },
115
- { code: "a", fileName: "b.ts", options: { c: "d" } },
116
- ],
117
- invalid: []
118
- });
119
- `,
120
- snapshot: `
121
- ruleTester.describe(rule, {
122
- valid: [
123
- { code: "a", fileName: "b.ts", options: { c: "d" } },
124
- { code: "a", fileName: "b.ts", options: { c: "d" } },
125
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126
- This test code already appeared in a previous test.
127
- ],
128
- invalid: []
129
- });
130
- `,
131
- },
132
- ],
133
- valid: [
134
- `
135
- describe(rule, {
136
- valid: ['a', 'a'],
137
- invalid: []
138
- });
139
- `,
140
- `
141
- ruleTester.describe(rule, {
142
- valid: ['a', 'b'],
143
- invalid: []
144
- });
145
- `,
146
- `
147
- ruleTester.describe(rule, {
148
- valid: [
149
- { code: \`a\`, fileName: "b.ts" },
150
- { code: \`a\`, fileName: "c.ts" },
151
- ],
152
- invalid: []
153
- });
154
- `,
155
- `
156
- ruleTester.describe(rule, {
157
- valid: [
158
- { code: \`a\`, fileName: "b.ts", options: { c: "d" } },
159
- { code: \`a\`, fileName: "b.ts", options: { c: "e" } },
160
- ],
161
- invalid: []
162
- });
163
- `,
164
- `
165
- ruleTester.describe(rule, {
166
- valid: [
167
- { code: \`a\`, fileName: "b.ts", options: { c: "d" } },
168
- { code: \`a\`, fileName: "c.ts", options: { c: "d" } },
169
- ],
170
- invalid: []
171
- });
172
- `,
173
- ],
174
- });
@@ -1,64 +0,0 @@
1
- import { getTSNodeRange, typescriptLanguage } from "@flint.fyi/ts";
2
-
3
- import {
4
- getRuleTesterDescribedCases,
5
- ParsedTestCase,
6
- } from "./getRuleTesterDescribedCases.js";
7
-
8
- export default typescriptLanguage.createRule({
9
- about: {
10
- description:
11
- "Reports test cases that are identical to previous test cases.",
12
- id: "testCaseDuplicates",
13
- preset: "logical",
14
- },
15
- messages: {
16
- duplicateTest: {
17
- primary: "This test code already appeared in a previous test.",
18
- secondary: [
19
- "When writing tests for lint rules, it's possible to accidentally create deeply identical test cases.",
20
- "Doing so provides no added benefit for testing and is unnecessary.",
21
- ],
22
- suggestions: [
23
- "Delete this redundant test case.",
24
- "Change a property to make the test case unique.",
25
- ],
26
- },
27
- },
28
- setup(context) {
29
- function checkTestCases(testCases: ParsedTestCase[]) {
30
- const seen = new Set<string>();
31
-
32
- for (const testCase of testCases) {
33
- const key = JSON.stringify({
34
- code: testCase.code,
35
- fileName: testCase.fileName,
36
- options: testCase.options,
37
- });
38
-
39
- if (seen.has(key)) {
40
- context.report({
41
- message: "duplicateTest",
42
- range: getTSNodeRange(testCase.node, context.sourceFile),
43
- });
44
- } else {
45
- seen.add(key);
46
- }
47
- }
48
- }
49
-
50
- return {
51
- visitors: {
52
- CallExpression(node) {
53
- const describedCases = getRuleTesterDescribedCases(node);
54
- if (!describedCases) {
55
- return;
56
- }
57
-
58
- checkTestCases(describedCases.invalid);
59
- checkTestCases(describedCases.valid);
60
- },
61
- },
62
- };
63
- },
64
- });
@@ -1,55 +0,0 @@
1
- // Fork of ts-ast-to-literal pending:
2
- // https://github.com/dword-design/ts-ast-to-literal/issues/89
3
-
4
- // Changing from the switch to manual ifs is due to:
5
- // https://github.com/Microsoft/TypeScript/issues/56275
6
-
7
- import ts from "typescript";
8
-
9
- export function tsAstToLiteral(node: ts.ArrayLiteralExpression): unknown[];
10
- export function tsAstToLiteral(node: ts.ObjectLiteralExpression): object;
11
- export function tsAstToLiteral(node: ts.Node): unknown;
12
- export function tsAstToLiteral(node: ts.Node): unknown {
13
- switch (node.kind) {
14
- case ts.SyntaxKind.FalseKeyword:
15
- return false;
16
- case ts.SyntaxKind.NullKeyword:
17
- return null;
18
- case ts.SyntaxKind.TrueKeyword:
19
- return true;
20
- }
21
-
22
- if (ts.isArrayLiteralExpression(node)) {
23
- return node.elements
24
- .filter((element) => element.kind !== ts.SyntaxKind.SpreadElement)
25
- .map((element) => tsAstToLiteral(element));
26
- }
27
-
28
- if (ts.isNumericLiteral(node)) {
29
- return parseFloat(node.text);
30
- }
31
-
32
- if (ts.isObjectLiteralExpression(node)) {
33
- return Object.fromEntries(
34
- node.properties
35
- .filter(
36
- (
37
- property,
38
- ): property is ts.PropertyAssignment & { name: ts.Identifier } =>
39
- ts.isPropertyAssignment(property) &&
40
- (property.name.kind === ts.SyntaxKind.Identifier ||
41
- property.name.kind === ts.SyntaxKind.StringLiteral),
42
- )
43
- .map((property) => [
44
- property.name.escapedText || property.name.text,
45
- tsAstToLiteral(property.initializer),
46
- ]),
47
- );
48
- }
49
-
50
- if (ts.isStringLiteral(node)) {
51
- return node.text;
52
- }
53
-
54
- return undefined;
55
- }
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "lib",
4
- "rootDir": "src"
5
- },
6
- "extends": "../../tsconfig.base.json",
7
- "include": ["src"],
8
- "references": [
9
- { "path": "../core" },
10
- { "path": "../rule-tester" },
11
- { "path": "../ts" },
12
- { "path": "../utils" }
13
- ]
14
- }