@flint.fyi/plugin-flint 0.1.0 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
@@ -0,0 +1,2 @@
1
+ import * as ts from "typescript";
2
+ export declare function findProperty<Node extends ts.Node>(properties: ts.NodeArray<ts.ObjectLiteralElementLike>, name: string, predicate: (node: ts.Node) => node is Node): (ts.Expression & Node) | undefined;
@@ -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,8 @@
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 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<{
2
6
  readonly description: "Reports test cases that are identical to previous test cases.";
3
7
  readonly id: "testCaseDuplicates";
4
8
  readonly preset: "logical";
package/lib/flint.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createPlugin } from "@flint.fyi/core";
2
+ import invalidCodeLines from "./rules/invalidCodeLines.js";
2
3
  import testCaseDuplicates from "./rules/testCaseDuplicates.js";
3
4
  export const flint = createPlugin({
4
5
  name: "flint",
5
- rules: [testCaseDuplicates],
6
+ rules: [invalidCodeLines, testCaseDuplicates],
6
7
  });
@@ -0,0 +1,5 @@
1
+ import * as ts from "typescript";
2
+ export declare function getRuleTesterDescribedCases(node: ts.CallExpression): {
3
+ invalid: import("./types.js").ParsedTestCaseInvalid[];
4
+ valid: import("./types.js").ParsedTestCase[];
5
+ } | undefined;
@@ -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 @@
1
+ export {};
@@ -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,5 @@
1
1
  import { getTSNodeRange, typescriptLanguage } from "@flint.fyi/ts";
2
- import { getRuleTesterDescribedCases, } from "./getRuleTesterDescribedCases.js";
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.",
@@ -31,7 +31,7 @@ export default typescriptLanguage.createRule({
31
31
  if (seen.has(key)) {
32
32
  context.report({
33
33
  message: "duplicateTest",
34
- range: getTSNodeRange(testCase.node, context.sourceFile),
34
+ range: getTSNodeRange(testCase.nodes.case, context.sourceFile),
35
35
  });
36
36
  }
37
37
  else {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flint.fyi/plugin-flint",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "[Experimental] A Flint plugin for linting Flint plugins.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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 invalidCodeLines from "./rules/invalidCodeLines.js";
3
4
  import testCaseDuplicates from "./rules/testCaseDuplicates.js";
4
5
 
5
6
  export const flint = createPlugin({
6
7
  name: "flint",
7
- rules: [testCaseDuplicates],
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
+ }
@@ -0,0 +1,96 @@
1
+ import * as ts from "typescript";
2
+
3
+ import type { ParsedTestCase, ParsedTestCaseInvalid } from "./types.js";
4
+
5
+ import { findProperty } from "./findProperty.js";
6
+ import { tsAstToLiteral } from "./tsAstToLiteral.js";
7
+
8
+ export function parseTestCase(node: ts.Expression): ParsedTestCase | undefined {
9
+ if (ts.isStringLiteralLike(node)) {
10
+ return {
11
+ code: node.text,
12
+ nodes: {
13
+ case: node,
14
+ code: node,
15
+ },
16
+ };
17
+ }
18
+
19
+ if (!ts.isObjectLiteralExpression(node)) {
20
+ return undefined;
21
+ }
22
+
23
+ const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
24
+ if (!code) {
25
+ return undefined;
26
+ }
27
+
28
+ const fileName = findProperty(
29
+ node.properties,
30
+ "fileName",
31
+ ts.isStringLiteralLike,
32
+ );
33
+ const options = findProperty(
34
+ node.properties,
35
+ "options",
36
+ ts.isObjectLiteralExpression,
37
+ );
38
+
39
+ return {
40
+ code: code.text,
41
+ fileName: fileName?.text,
42
+ nodes: {
43
+ case: node,
44
+ code,
45
+ fileName,
46
+ options,
47
+ },
48
+ options: options && tsAstToLiteral(options),
49
+ };
50
+ }
51
+
52
+ export function parseTestCaseInvalid(
53
+ node: ts.Expression,
54
+ ): ParsedTestCaseInvalid | undefined {
55
+ if (!ts.isObjectLiteralExpression(node)) {
56
+ return undefined;
57
+ }
58
+
59
+ const code = findProperty(node.properties, "code", ts.isStringLiteralLike);
60
+ if (!code) {
61
+ return undefined;
62
+ }
63
+
64
+ const fileName = findProperty(
65
+ node.properties,
66
+ "fileName",
67
+ ts.isStringLiteralLike,
68
+ );
69
+ const options = findProperty(
70
+ node.properties,
71
+ "options",
72
+ ts.isObjectLiteralExpression,
73
+ );
74
+ const snapshot = findProperty(
75
+ node.properties,
76
+ "snapshot",
77
+ ts.isStringLiteralLike,
78
+ );
79
+ if (!snapshot) {
80
+ return undefined;
81
+ }
82
+
83
+ return {
84
+ code: code.text,
85
+ fileName: fileName?.text,
86
+ nodes: {
87
+ case: node,
88
+ code,
89
+ fileName,
90
+ options,
91
+ snapshot,
92
+ },
93
+ options: options && tsAstToLiteral(options),
94
+ snapshot: snapshot.text,
95
+ };
96
+ }