@flint.fyi/rule-tester 0.13.1 → 0.14.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.
- package/CHANGELOG.md +19 -0
- package/lib/RuleTester.d.ts +9 -5
- package/lib/RuleTester.js +44 -12
- package/lib/createReportSnapshot.d.ts +2 -2
- package/lib/createReportSnapshot.js +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/normalizeTestCase.d.ts +5 -0
- package/lib/normalizeTestCase.js +6 -0
- package/lib/predicates.d.ts +2 -0
- package/lib/predicates.js +3 -0
- package/lib/resolveReportedSuggestions.d.ts +14 -0
- package/lib/resolveReportedSuggestions.js +46 -0
- package/lib/resolveReportedSuggestions.test.d.ts +1 -0
- package/lib/resolveReportedSuggestions.test.js +172 -0
- package/lib/runTestCaseRule.d.ts +2 -5
- package/lib/runTestCaseRule.js +2 -2
- package/lib/types.d.ts +37 -0
- package/lib/types.js +1 -0
- package/package.json +2 -1
- package/src/RuleTester.ts +75 -19
- package/src/createReportSnapshot.ts +4 -7
- package/src/index.ts +1 -0
- package/src/normalizeTestCase.ts +14 -0
- package/src/predicates.ts +7 -0
- package/src/resolveReportedSuggestions.test.ts +201 -0
- package/src/resolveReportedSuggestions.ts +85 -0
- package/src/runTestCaseRule.ts +3 -6
- package/src/types.ts +54 -0
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { formatReportPrimary,
|
|
1
|
+
import { formatReportPrimary, NormalizedReport } from "@flint.fyi/core";
|
|
2
2
|
|
|
3
3
|
export function createReportSnapshot(
|
|
4
4
|
sourceText: string,
|
|
5
|
-
reports:
|
|
5
|
+
reports: NormalizedReport[],
|
|
6
6
|
) {
|
|
7
7
|
let result = sourceText;
|
|
8
8
|
|
|
@@ -13,10 +13,7 @@ export function createReportSnapshot(
|
|
|
13
13
|
return result;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
function createReportSnapshotAt(
|
|
17
|
-
sourceText: string,
|
|
18
|
-
report: NormalizedRuleReport,
|
|
19
|
-
) {
|
|
16
|
+
function createReportSnapshotAt(sourceText: string, report: NormalizedReport) {
|
|
20
17
|
const range = report.range;
|
|
21
18
|
|
|
22
19
|
const lineEndIndex = ifNegative(
|
|
@@ -28,7 +25,7 @@ function createReportSnapshotAt(
|
|
|
28
25
|
0,
|
|
29
26
|
);
|
|
30
27
|
|
|
31
|
-
const column = ifNegative(range.begin.raw - lineStartIndex -
|
|
28
|
+
const column = ifNegative(range.begin.raw - lineStartIndex - 1, 0);
|
|
32
29
|
const width = range.end.raw - range.begin.raw;
|
|
33
30
|
|
|
34
31
|
const injectionPrefix = " ".repeat(column);
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TestCase } from "./types.js";
|
|
2
|
+
|
|
3
|
+
export interface TestCaseNormalized extends TestCase {
|
|
4
|
+
fileName: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function normalizeTestCase<T extends TestCase>(
|
|
8
|
+
testCase: T,
|
|
9
|
+
): T & TestCaseNormalized {
|
|
10
|
+
return {
|
|
11
|
+
fileName: "file.ts",
|
|
12
|
+
...testCase,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { resolveReportedSuggestions } from "./resolveReportedSuggestions.js";
|
|
4
|
+
|
|
5
|
+
const mockReport = {
|
|
6
|
+
message: { primary: "", secondary: [], suggestions: [] },
|
|
7
|
+
range: {
|
|
8
|
+
begin: { column: 0, line: 1, raw: 0 },
|
|
9
|
+
end: { column: 3, line: 1, raw: 3 },
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const mockTestCaseNormalized = {
|
|
14
|
+
code: "xyz",
|
|
15
|
+
fileName: "file.ts",
|
|
16
|
+
snapshot: "",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe("resolveReportedSuggestions", () => {
|
|
20
|
+
it("returns undefined when reports is empty", () => {
|
|
21
|
+
const result = resolveReportedSuggestions([], mockTestCaseNormalized);
|
|
22
|
+
|
|
23
|
+
expect(result).toEqual(undefined);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("returns undefined when given one report with no suggestions", () => {
|
|
27
|
+
const report = {
|
|
28
|
+
...mockReport,
|
|
29
|
+
suggestions: [],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const result = resolveReportedSuggestions([report], mockTestCaseNormalized);
|
|
33
|
+
|
|
34
|
+
expect(result).toEqual(undefined);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("returns id and updated text when given a single file suggestion", () => {
|
|
38
|
+
const suggestion = {
|
|
39
|
+
id: "suggestion",
|
|
40
|
+
range: { begin: 0, end: 3 },
|
|
41
|
+
text: "abc",
|
|
42
|
+
};
|
|
43
|
+
const report = {
|
|
44
|
+
...mockReport,
|
|
45
|
+
suggestions: [suggestion],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const result = resolveReportedSuggestions([report], mockTestCaseNormalized);
|
|
49
|
+
|
|
50
|
+
expect(result).toEqual([
|
|
51
|
+
{
|
|
52
|
+
id: suggestion.id,
|
|
53
|
+
updated: suggestion.text,
|
|
54
|
+
},
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("throws when given a test case with no suggestions", () => {
|
|
59
|
+
const report = {
|
|
60
|
+
...mockReport,
|
|
61
|
+
suggestions: [
|
|
62
|
+
{
|
|
63
|
+
files: {
|
|
64
|
+
"file.ts": () => [{ range: { begin: 0, end: 3 }, text: "def" }],
|
|
65
|
+
},
|
|
66
|
+
id: "suggestion-report",
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
expect(() =>
|
|
72
|
+
resolveReportedSuggestions([report], {
|
|
73
|
+
...mockTestCaseNormalized,
|
|
74
|
+
suggestions: [
|
|
75
|
+
{
|
|
76
|
+
id: "suggestion-result",
|
|
77
|
+
updated: "...",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
}),
|
|
81
|
+
).toThrowErrorMatchingInlineSnapshot(
|
|
82
|
+
`[Error: This test case describes suggestions across files, but the rule is only reporting changes to its own file.]`,
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("throws when given a test case that doesn't have cross-file suggestions", () => {
|
|
87
|
+
const report = {
|
|
88
|
+
...mockReport,
|
|
89
|
+
suggestions: [
|
|
90
|
+
{
|
|
91
|
+
files: {
|
|
92
|
+
"file.ts": () => [{ range: { begin: 0, end: 3 }, text: "def" }],
|
|
93
|
+
},
|
|
94
|
+
id: "suggestion-report",
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
expect(() =>
|
|
100
|
+
resolveReportedSuggestions([report], {
|
|
101
|
+
...mockTestCaseNormalized,
|
|
102
|
+
suggestions: [
|
|
103
|
+
{
|
|
104
|
+
id: "suggestion-result",
|
|
105
|
+
updated: "...",
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
}),
|
|
109
|
+
).toThrowErrorMatchingInlineSnapshot(
|
|
110
|
+
`[Error: This test case describes suggestions across files, but the rule is only reporting changes to its own file.]`,
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("returns id and a files object when given multi-file suggestions with a single file", () => {
|
|
115
|
+
const report = {
|
|
116
|
+
...mockReport,
|
|
117
|
+
suggestions: [
|
|
118
|
+
{
|
|
119
|
+
files: {
|
|
120
|
+
"file.ts": () => [{ range: { begin: 0, end: 3 }, text: "def" }],
|
|
121
|
+
},
|
|
122
|
+
id: "suggestion-report",
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const result = resolveReportedSuggestions([report], {
|
|
128
|
+
...mockTestCaseNormalized,
|
|
129
|
+
suggestions: [
|
|
130
|
+
{
|
|
131
|
+
files: {
|
|
132
|
+
"file.ts": [{ original: "abc", updated: "def" }],
|
|
133
|
+
},
|
|
134
|
+
id: "suggestion-result",
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(result).toEqual([
|
|
140
|
+
{
|
|
141
|
+
files: {
|
|
142
|
+
"file.ts": [
|
|
143
|
+
{
|
|
144
|
+
original: "abc",
|
|
145
|
+
updated: "def",
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
id: "suggestion-report",
|
|
150
|
+
},
|
|
151
|
+
]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("returns id and a files object when given multi-file suggestions with multiple file", () => {
|
|
155
|
+
const report = {
|
|
156
|
+
...mockReport,
|
|
157
|
+
suggestions: [
|
|
158
|
+
{
|
|
159
|
+
files: {
|
|
160
|
+
"fileA.ts": () => [{ range: { begin: 0, end: 5 }, text: "def-A" }],
|
|
161
|
+
"fileB.ts": () => [{ range: { begin: 0, end: 5 }, text: "def-B" }],
|
|
162
|
+
},
|
|
163
|
+
id: "suggestion-report",
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const result = resolveReportedSuggestions([report], {
|
|
169
|
+
...mockTestCaseNormalized,
|
|
170
|
+
suggestions: [
|
|
171
|
+
{
|
|
172
|
+
files: {
|
|
173
|
+
"fileA.ts": [{ original: "abc-A", updated: "def-A" }],
|
|
174
|
+
"fileB.ts": [{ original: "abc-B", updated: "def-B" }],
|
|
175
|
+
},
|
|
176
|
+
id: "suggestion-result",
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(result).toEqual([
|
|
182
|
+
{
|
|
183
|
+
files: {
|
|
184
|
+
"fileA.ts": [
|
|
185
|
+
{
|
|
186
|
+
original: "abc-A",
|
|
187
|
+
updated: "def-A",
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
"fileB.ts": [
|
|
191
|
+
{
|
|
192
|
+
original: "abc-B",
|
|
193
|
+
updated: "def-B",
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
id: "suggestion-report",
|
|
198
|
+
},
|
|
199
|
+
]);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyChangesToText,
|
|
3
|
+
isSuggestionForFiles,
|
|
4
|
+
NormalizedReport,
|
|
5
|
+
} from "@flint.fyi/core";
|
|
6
|
+
import { SuggestionForFiles } from "@flint.fyi/core/src/types/changes.js";
|
|
7
|
+
import { isTruthy } from "@flint.fyi/utils";
|
|
8
|
+
|
|
9
|
+
import { TestCaseNormalized } from "./normalizeTestCase.js";
|
|
10
|
+
import { isTestSuggestionForFiles } from "./predicates.js";
|
|
11
|
+
import { InvalidTestCase, TestSuggestionFileCase } from "./types.js";
|
|
12
|
+
|
|
13
|
+
export function resolveReportedSuggestions(
|
|
14
|
+
reports: NormalizedReport[],
|
|
15
|
+
testCaseNormalized: InvalidTestCase & TestCaseNormalized,
|
|
16
|
+
) {
|
|
17
|
+
const suggestionsReported = reports
|
|
18
|
+
.flatMap((report) => report.suggestions)
|
|
19
|
+
.filter(isTruthy);
|
|
20
|
+
|
|
21
|
+
if (!suggestionsReported.length) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return suggestionsReported.map((suggestionReported) =>
|
|
26
|
+
isSuggestionForFiles(suggestionReported)
|
|
27
|
+
? {
|
|
28
|
+
files: resolveReportedSuggestionForFiles(
|
|
29
|
+
suggestionReported,
|
|
30
|
+
testCaseNormalized,
|
|
31
|
+
),
|
|
32
|
+
id: suggestionReported.id,
|
|
33
|
+
}
|
|
34
|
+
: {
|
|
35
|
+
id: suggestionReported.id,
|
|
36
|
+
updated: applyChangesToText(
|
|
37
|
+
[suggestionReported],
|
|
38
|
+
testCaseNormalized.code,
|
|
39
|
+
),
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function resolveReportedSuggestionForFiles(
|
|
45
|
+
suggestionReported: SuggestionForFiles,
|
|
46
|
+
testCaseNormalized: InvalidTestCase & TestCaseNormalized,
|
|
47
|
+
) {
|
|
48
|
+
if (!testCaseNormalized.suggestions) {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!testCaseNormalized.suggestions.every(isTestSuggestionForFiles)) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"This test case describes suggestions across files, but the rule is only reporting changes to its own file.",
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return Object.fromEntries(
|
|
59
|
+
testCaseNormalized.suggestions
|
|
60
|
+
.map((suggestionExpected): [string, TestSuggestionFileCase[]][] => {
|
|
61
|
+
return Object.entries(suggestionExpected.files).map(
|
|
62
|
+
([filePath, suggestionCasesExpected]) => {
|
|
63
|
+
return [
|
|
64
|
+
filePath,
|
|
65
|
+
suggestionCasesExpected.map((suggestionCaseExpected) => {
|
|
66
|
+
const changes = suggestionReported.files[filePath]?.(
|
|
67
|
+
suggestionCaseExpected.original,
|
|
68
|
+
);
|
|
69
|
+
return {
|
|
70
|
+
original: suggestionCaseExpected.original,
|
|
71
|
+
updated: changes
|
|
72
|
+
? applyChangesToText(
|
|
73
|
+
changes,
|
|
74
|
+
suggestionCaseExpected.original,
|
|
75
|
+
)
|
|
76
|
+
: suggestionCaseExpected.original,
|
|
77
|
+
};
|
|
78
|
+
}),
|
|
79
|
+
];
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
})
|
|
83
|
+
.flat(),
|
|
84
|
+
);
|
|
85
|
+
}
|
package/src/runTestCaseRule.ts
CHANGED
|
@@ -8,10 +8,7 @@ import {
|
|
|
8
8
|
} from "@flint.fyi/core";
|
|
9
9
|
import { CachedFactory } from "cached-factory";
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
code: string;
|
|
13
|
-
fileName?: string;
|
|
14
|
-
}
|
|
11
|
+
import { TestCaseNormalized } from "./normalizeTestCase.js";
|
|
15
12
|
|
|
16
13
|
export interface TestCaseRuleConfiguration<
|
|
17
14
|
OptionsSchema extends AnyOptionalSchema | undefined,
|
|
@@ -25,14 +22,14 @@ export function runTestCaseRule<
|
|
|
25
22
|
>(
|
|
26
23
|
fileFactories: CachedFactory<AnyLanguage, LanguageFileFactory>,
|
|
27
24
|
{ options, rule }: Required<TestCaseRuleConfiguration<OptionsSchema>>,
|
|
28
|
-
{ code, fileName
|
|
25
|
+
{ code, fileName }: TestCaseNormalized,
|
|
29
26
|
) {
|
|
30
27
|
using file = fileFactories
|
|
31
28
|
// TODO: How to make types more permissive around assignability?
|
|
32
29
|
// See AnyRule's any
|
|
33
30
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
34
31
|
.get(rule.language)
|
|
35
|
-
.
|
|
32
|
+
.prepareFromVirtual(fileName, code).file;
|
|
36
33
|
|
|
37
34
|
return file.runRule(rule, options as InferredObject<OptionsSchema>);
|
|
38
35
|
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface InvalidTestCase<
|
|
2
|
+
Options extends object | undefined = object | undefined,
|
|
3
|
+
> extends TestCase<Options> {
|
|
4
|
+
output?: string;
|
|
5
|
+
snapshot: string;
|
|
6
|
+
suggestions?: TestSuggestion[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface TestCase<
|
|
10
|
+
Options extends object | undefined = object | undefined,
|
|
11
|
+
> {
|
|
12
|
+
code: string;
|
|
13
|
+
fileName?: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Run only this test case. Useful for debugging.
|
|
17
|
+
*
|
|
18
|
+
* Do not commit code with this flag set.
|
|
19
|
+
*/
|
|
20
|
+
only?: boolean;
|
|
21
|
+
|
|
22
|
+
options?: Options;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Skip running this test case. Useful for work-in-progress tests.
|
|
26
|
+
*
|
|
27
|
+
* Do not commit code with this flag set.
|
|
28
|
+
*/
|
|
29
|
+
skip?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type TestSuggestion = TestSuggestionForFile | TestSuggestionForFiles;
|
|
33
|
+
|
|
34
|
+
export interface TestSuggestionFileCase {
|
|
35
|
+
original: string;
|
|
36
|
+
updated: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface TestSuggestionForFile {
|
|
40
|
+
id: string;
|
|
41
|
+
updated: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface TestSuggestionForFiles {
|
|
45
|
+
files: Record<string, TestSuggestionFileCase[]>;
|
|
46
|
+
id: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type ValidTestCase<Options extends object | undefined> =
|
|
50
|
+
| string
|
|
51
|
+
| ValidTestCaseObject<Options>;
|
|
52
|
+
|
|
53
|
+
export type ValidTestCaseObject<Options extends object | undefined> =
|
|
54
|
+
TestCase<Options>;
|
package/tsconfig.json
CHANGED