@typescript-eslint/rule-tester 8.0.0-alpha.3 → 8.0.0-alpha.30
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/dist/RuleTester.d.ts.map +1 -1
- package/dist/RuleTester.js +398 -291
- package/dist/RuleTester.js.map +1 -1
- package/dist/types/InvalidTestCase.d.ts +1 -1
- package/dist/types/InvalidTestCase.d.ts.map +1 -1
- package/dist/utils/SourceCodeFixer.d.ts +6 -5
- package/dist/utils/SourceCodeFixer.d.ts.map +1 -1
- package/dist/utils/SourceCodeFixer.js.map +1 -1
- package/dist/utils/cloneDeeplyExcludesParent.js +1 -1
- package/dist/utils/cloneDeeplyExcludesParent.js.map +1 -1
- package/dist/utils/dependencyConstraints.d.ts.map +1 -1
- package/dist/utils/dependencyConstraints.js +3 -1
- package/dist/utils/dependencyConstraints.js.map +1 -1
- package/dist/utils/freezeDeeply.js +1 -1
- package/dist/utils/freezeDeeply.js.map +1 -1
- package/dist/utils/hasOwnProperty.js +1 -1
- package/dist/utils/hasOwnProperty.js.map +1 -1
- package/dist/utils/interpolate.d.ts +4 -0
- package/dist/utils/interpolate.d.ts.map +1 -1
- package/dist/utils/interpolate.js +10 -2
- package/dist/utils/interpolate.js.map +1 -1
- package/dist/utils/serialization.d.ts +7 -0
- package/dist/utils/serialization.d.ts.map +1 -0
- package/dist/utils/serialization.js +44 -0
- package/dist/utils/serialization.js.map +1 -0
- package/package.json +12 -6
package/dist/RuleTester.js
CHANGED
|
@@ -24,21 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
__setModuleDefault(result, mod);
|
|
25
25
|
return result;
|
|
26
26
|
};
|
|
27
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
28
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
29
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
30
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
31
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
32
|
-
};
|
|
33
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
34
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
35
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
36
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
37
|
-
};
|
|
38
27
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
28
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
29
|
};
|
|
41
|
-
var _RuleTester_instances, _RuleTester_testerConfig, _RuleTester_rules, _RuleTester_linter, _RuleTester_normalizeTests, _RuleTester_testValidTemplate, _RuleTester_testInvalidTemplate;
|
|
42
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
31
|
exports.RuleTester = void 0;
|
|
44
32
|
const node_assert_1 = __importDefault(require("node:assert"));
|
|
@@ -49,6 +37,7 @@ const ts_eslint_1 = require("@typescript-eslint/utils/ts-eslint");
|
|
|
49
37
|
// we intentionally import from eslint here because we need to use the same class
|
|
50
38
|
// that ESLint uses, not our custom override typed version
|
|
51
39
|
const eslint_1 = require("eslint");
|
|
40
|
+
const json_stable_stringify_without_jsonify_1 = __importDefault(require("json-stable-stringify-without-jsonify"));
|
|
52
41
|
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
53
42
|
const TestFramework_1 = require("./TestFramework");
|
|
54
43
|
const ajv_1 = require("./utils/ajv");
|
|
@@ -60,6 +49,7 @@ const getRuleOptionsSchema_1 = require("./utils/getRuleOptionsSchema");
|
|
|
60
49
|
const hasOwnProperty_1 = require("./utils/hasOwnProperty");
|
|
61
50
|
const interpolate_1 = require("./utils/interpolate");
|
|
62
51
|
const isReadonlyArray_1 = require("./utils/isReadonlyArray");
|
|
52
|
+
const serialization_1 = require("./utils/serialization");
|
|
63
53
|
const SourceCodeFixer = __importStar(require("./utils/SourceCodeFixer"));
|
|
64
54
|
const validationHelpers_1 = require("./utils/validationHelpers");
|
|
65
55
|
const ajv = (0, ajv_1.ajvBuilder)({ strictDefaults: true });
|
|
@@ -75,27 +65,55 @@ const testerDefaultConfig = {
|
|
|
75
65
|
defaultFilenames: { ts: 'file.ts', tsx: 'react.tsx' },
|
|
76
66
|
};
|
|
77
67
|
let defaultConfig = (0, eslint_utils_1.deepMerge)({}, testerDefaultConfig);
|
|
68
|
+
/**
|
|
69
|
+
* Extracts names of {{ placeholders }} from the reported message.
|
|
70
|
+
* @param message Reported message
|
|
71
|
+
* @returns Array of placeholder names
|
|
72
|
+
*/
|
|
73
|
+
function getMessagePlaceholders(message) {
|
|
74
|
+
const matcher = (0, interpolate_1.getPlaceholderMatcher)();
|
|
75
|
+
return Array.from(message.matchAll(matcher), ([, name]) => name.trim());
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns the placeholders in the reported messages but
|
|
79
|
+
* only includes the placeholders available in the raw message and not in the provided data.
|
|
80
|
+
* @param message The reported message
|
|
81
|
+
* @param raw The raw message specified in the rule meta.messages
|
|
82
|
+
* @param data The passed
|
|
83
|
+
* @returns Missing placeholder names
|
|
84
|
+
*/
|
|
85
|
+
function getUnsubstitutedMessagePlaceholders(message, raw, data = {}) {
|
|
86
|
+
const unsubstituted = getMessagePlaceholders(message);
|
|
87
|
+
if (unsubstituted.length === 0) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
// Remove false positives by only counting placeholders in the raw message, which were not provided in the data matcher or added with a data property
|
|
91
|
+
const known = getMessagePlaceholders(raw);
|
|
92
|
+
const provided = Object.keys(data);
|
|
93
|
+
return unsubstituted.filter(name => known.includes(name) && !provided.includes(name));
|
|
94
|
+
}
|
|
78
95
|
class RuleTester extends TestFramework_1.TestFramework {
|
|
96
|
+
#testerConfig;
|
|
97
|
+
#rules = {};
|
|
98
|
+
// `configType` is hardcoded to eslintrc until switch to flat config is complete
|
|
99
|
+
// We should look to support both eslintrc and flat config for consumers of RuleTester.
|
|
100
|
+
#linter = new ts_eslint_1.Linter({ configType: 'eslintrc' });
|
|
79
101
|
/**
|
|
80
102
|
* Creates a new instance of RuleTester.
|
|
81
103
|
*/
|
|
82
104
|
constructor(testerConfig) {
|
|
83
105
|
super();
|
|
84
|
-
_RuleTester_instances.add(this);
|
|
85
|
-
_RuleTester_testerConfig.set(this, void 0);
|
|
86
|
-
_RuleTester_rules.set(this, {});
|
|
87
|
-
_RuleTester_linter.set(this, new ts_eslint_1.Linter());
|
|
88
106
|
/**
|
|
89
107
|
* The configuration to use for this tester. Combination of the tester
|
|
90
108
|
* configuration and the default configuration.
|
|
91
109
|
*/
|
|
92
|
-
|
|
110
|
+
this.#testerConfig = (0, lodash_merge_1.default)({}, defaultConfig, testerConfig, {
|
|
93
111
|
rules: { 'rule-tester/validate-ast': 'error' },
|
|
94
112
|
// as of eslint 6 you have to provide an absolute path to the parser
|
|
95
113
|
// but that's not as clean to type, this saves us trying to manually enforce
|
|
96
114
|
// that contributors require.resolve everything
|
|
97
115
|
parser: require.resolve(testerConfig?.parser ?? defaultConfig.parser),
|
|
98
|
-
})
|
|
116
|
+
});
|
|
99
117
|
// make sure that the parser doesn't hold onto file handles between tests
|
|
100
118
|
// on linux (i.e. our CI env), there can be very a limited number of watch handles available
|
|
101
119
|
const constructor = this.constructor;
|
|
@@ -103,6 +121,7 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
103
121
|
try {
|
|
104
122
|
// instead of creating a hard dependency, just use a soft require
|
|
105
123
|
// a bit weird, but if they're using this tooling, it'll be installed
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
106
125
|
const parser = require(TYPESCRIPT_ESLINT_PARSER);
|
|
107
126
|
parser.clearCaches();
|
|
108
127
|
}
|
|
@@ -146,15 +165,114 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
146
165
|
* Define a rule for one particular run of tests.
|
|
147
166
|
*/
|
|
148
167
|
defineRule(name, rule) {
|
|
149
|
-
|
|
168
|
+
this.#rules[name] = rule;
|
|
169
|
+
}
|
|
170
|
+
#normalizeTests(rawTests) {
|
|
171
|
+
/*
|
|
172
|
+
Automatically add a filename to the tests to enable type-aware tests to "just work".
|
|
173
|
+
This saves users having to verbosely and manually add the filename to every
|
|
174
|
+
single test case.
|
|
175
|
+
Hugely helps with the string-based valid test cases as it means they don't
|
|
176
|
+
need to be made objects!
|
|
177
|
+
*/
|
|
178
|
+
const getFilename = (testOptions) => {
|
|
179
|
+
const resolvedOptions = (0, eslint_utils_1.deepMerge)(this.#testerConfig.parserOptions, testOptions);
|
|
180
|
+
const filename = resolvedOptions.ecmaFeatures?.jsx
|
|
181
|
+
? this.#testerConfig.defaultFilenames.tsx
|
|
182
|
+
: this.#testerConfig.defaultFilenames.ts;
|
|
183
|
+
if (resolvedOptions.project) {
|
|
184
|
+
return node_path_1.default.join(resolvedOptions.tsconfigRootDir ?? process.cwd(), filename);
|
|
185
|
+
}
|
|
186
|
+
return filename;
|
|
187
|
+
};
|
|
188
|
+
const normalizeTest = (test) => {
|
|
189
|
+
if (test.parser === TYPESCRIPT_ESLINT_PARSER) {
|
|
190
|
+
throw new Error(DUPLICATE_PARSER_ERROR_MESSAGE);
|
|
191
|
+
}
|
|
192
|
+
if (!test.filename) {
|
|
193
|
+
return {
|
|
194
|
+
...test,
|
|
195
|
+
filename: getFilename(test.parserOptions),
|
|
196
|
+
parserOptions: {
|
|
197
|
+
// Re-running simulates --fix mode, which implies an isolated program
|
|
198
|
+
// (i.e. parseAndGenerateServicesCalls[test.filename] > 1).
|
|
199
|
+
disallowAutomaticSingleRunInference: true,
|
|
200
|
+
...test.parserOptions,
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return test;
|
|
205
|
+
};
|
|
206
|
+
const normalizedTests = {
|
|
207
|
+
valid: rawTests.valid
|
|
208
|
+
.map(test => {
|
|
209
|
+
if (typeof test === 'string') {
|
|
210
|
+
return { code: test };
|
|
211
|
+
}
|
|
212
|
+
return test;
|
|
213
|
+
})
|
|
214
|
+
.map(normalizeTest),
|
|
215
|
+
invalid: rawTests.invalid.map(normalizeTest),
|
|
216
|
+
};
|
|
217
|
+
// convenience iterator to make it easy to loop all tests without a concat
|
|
218
|
+
const allTestsIterator = {
|
|
219
|
+
*[Symbol.iterator]() {
|
|
220
|
+
for (const testCase of normalizedTests.valid) {
|
|
221
|
+
yield testCase;
|
|
222
|
+
}
|
|
223
|
+
for (const testCase of normalizedTests.invalid) {
|
|
224
|
+
yield testCase;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
const hasOnly = (() => {
|
|
229
|
+
for (const test of allTestsIterator) {
|
|
230
|
+
if (test.only) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
})();
|
|
236
|
+
if (hasOnly) {
|
|
237
|
+
// if there is an `only: true` - don't try apply constraints - assume that
|
|
238
|
+
// we are in "local development" mode rather than "CI validation" mode
|
|
239
|
+
return normalizedTests;
|
|
240
|
+
}
|
|
241
|
+
const hasConstraints = (() => {
|
|
242
|
+
for (const test of allTestsIterator) {
|
|
243
|
+
if (test.dependencyConstraints &&
|
|
244
|
+
Object.keys(test.dependencyConstraints).length > 0) {
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
})();
|
|
250
|
+
if (!hasConstraints) {
|
|
251
|
+
return normalizedTests;
|
|
252
|
+
}
|
|
253
|
+
/*
|
|
254
|
+
Mark all unsatisfactory tests as `skip: true`.
|
|
255
|
+
We do this instead of just omitting the tests entirely because it gives the
|
|
256
|
+
test framework the opportunity to log the test as skipped rather than the test
|
|
257
|
+
just disappearing without a trace.
|
|
258
|
+
*/
|
|
259
|
+
const maybeMarkAsOnly = (test) => {
|
|
260
|
+
return {
|
|
261
|
+
...test,
|
|
262
|
+
skip: !(0, dependencyConstraints_1.satisfiesAllDependencyConstraints)(test.dependencyConstraints),
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
normalizedTests.valid = normalizedTests.valid.map(maybeMarkAsOnly);
|
|
266
|
+
normalizedTests.invalid = normalizedTests.invalid.map(maybeMarkAsOnly);
|
|
267
|
+
return normalizedTests;
|
|
150
268
|
}
|
|
151
269
|
/**
|
|
152
270
|
* Adds a new rule test to execute.
|
|
153
271
|
*/
|
|
154
272
|
run(ruleName, rule, test) {
|
|
155
273
|
const constructor = this.constructor;
|
|
156
|
-
if (
|
|
157
|
-
!(0, dependencyConstraints_1.satisfiesAllDependencyConstraints)(
|
|
274
|
+
if (this.#testerConfig.dependencyConstraints &&
|
|
275
|
+
!(0, dependencyConstraints_1.satisfiesAllDependencyConstraints)(this.#testerConfig.dependencyConstraints)) {
|
|
158
276
|
// for frameworks like mocha or jest that have a "skip" version of their function
|
|
159
277
|
// we can provide a nice skipped test!
|
|
160
278
|
constructor.describeSkip(ruleName, () => {
|
|
@@ -181,10 +299,12 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
181
299
|
...scenarioErrors,
|
|
182
300
|
].join('\n'));
|
|
183
301
|
}
|
|
302
|
+
const seenValidTestCases = new Set();
|
|
303
|
+
const seenInvalidTestCases = new Set();
|
|
184
304
|
if (typeof rule === 'function') {
|
|
185
305
|
(0, validationHelpers_1.emitLegacyRuleAPIWarning)(ruleName);
|
|
186
306
|
}
|
|
187
|
-
|
|
307
|
+
this.#linter.defineRule(ruleName, Object.assign({}, rule, {
|
|
188
308
|
// Create a wrapper rule that freezes the `context` properties.
|
|
189
309
|
create(context) {
|
|
190
310
|
(0, freezeDeeply_1.freezeDeeply)(context.options);
|
|
@@ -193,8 +313,8 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
193
313
|
return (typeof rule === 'function' ? rule : rule.create)(context);
|
|
194
314
|
},
|
|
195
315
|
}));
|
|
196
|
-
|
|
197
|
-
const normalizedTests =
|
|
316
|
+
this.#linter.defineRules(this.#rules);
|
|
317
|
+
const normalizedTests = this.#normalizeTests(test);
|
|
198
318
|
function getTestMethod(test) {
|
|
199
319
|
if (test.skip) {
|
|
200
320
|
return 'itSkip';
|
|
@@ -219,7 +339,7 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
219
339
|
return valid.name;
|
|
220
340
|
})();
|
|
221
341
|
constructor[getTestMethod(valid)]((0, validationHelpers_1.sanitize)(testName), () => {
|
|
222
|
-
|
|
342
|
+
this.#testValidTemplate(ruleName, rule, valid, seenValidTestCases);
|
|
223
343
|
});
|
|
224
344
|
});
|
|
225
345
|
});
|
|
@@ -234,7 +354,7 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
234
354
|
return invalid.name;
|
|
235
355
|
})();
|
|
236
356
|
constructor[getTestMethod(invalid)]((0, validationHelpers_1.sanitize)(name), () => {
|
|
237
|
-
|
|
357
|
+
this.#testInvalidTemplate(ruleName, rule, invalid, seenInvalidTestCases);
|
|
238
358
|
});
|
|
239
359
|
});
|
|
240
360
|
});
|
|
@@ -247,10 +367,9 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
247
367
|
* Use @private instead of #private to expose it for testing purposes
|
|
248
368
|
*/
|
|
249
369
|
runRuleForItem(ruleName, rule, item) {
|
|
250
|
-
let config = (0, lodash_merge_1.default)({},
|
|
370
|
+
let config = (0, lodash_merge_1.default)({}, this.#testerConfig);
|
|
251
371
|
let code;
|
|
252
372
|
let filename;
|
|
253
|
-
let output;
|
|
254
373
|
let beforeAST;
|
|
255
374
|
let afterAST;
|
|
256
375
|
if (typeof item === 'string') {
|
|
@@ -294,7 +413,7 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
294
413
|
* The goal is to check whether or not AST was modified when
|
|
295
414
|
* running the rule under test.
|
|
296
415
|
*/
|
|
297
|
-
|
|
416
|
+
this.#linter.defineRule('rule-tester/validate-ast', {
|
|
298
417
|
create() {
|
|
299
418
|
return {
|
|
300
419
|
Program(node) {
|
|
@@ -312,7 +431,9 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
312
431
|
else {
|
|
313
432
|
config.parser = require.resolve(TYPESCRIPT_ESLINT_PARSER);
|
|
314
433
|
}
|
|
315
|
-
|
|
434
|
+
this.#linter.defineParser(config.parser,
|
|
435
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
436
|
+
(0, validationHelpers_1.wrapParser)(require(config.parser)));
|
|
316
437
|
if (schema) {
|
|
317
438
|
ajv.validateSchema(schema);
|
|
318
439
|
if (ajv.errors) {
|
|
@@ -345,37 +466,52 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
345
466
|
// Verify the code.
|
|
346
467
|
// @ts-expect-error -- we don't define deprecated members on our types
|
|
347
468
|
const { getComments } = eslint_1.SourceCode.prototype;
|
|
348
|
-
let
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
469
|
+
let initialMessages = null;
|
|
470
|
+
let messages = null;
|
|
471
|
+
let fixedResult = null;
|
|
472
|
+
let passNumber = 0;
|
|
473
|
+
const outputs = [];
|
|
474
|
+
do {
|
|
475
|
+
passNumber++;
|
|
476
|
+
try {
|
|
477
|
+
// @ts-expect-error -- we don't define deprecated members on our types
|
|
478
|
+
eslint_1.SourceCode.prototype.getComments = validationHelpers_1.getCommentsDeprecation;
|
|
479
|
+
messages = this.#linter.verify(code, config, filename);
|
|
480
|
+
if (!initialMessages) {
|
|
481
|
+
initialMessages = messages;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
finally {
|
|
485
|
+
// @ts-expect-error -- we don't define deprecated members on our types
|
|
486
|
+
eslint_1.SourceCode.prototype.getComments = getComments;
|
|
487
|
+
}
|
|
488
|
+
if (messages.length === 0) {
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
const fatalErrorMessage = messages.find(m => m.fatal);
|
|
492
|
+
(0, node_assert_1.default)(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage?.message}`);
|
|
493
|
+
fixedResult = SourceCodeFixer.applyFixes(code, messages);
|
|
494
|
+
if (fixedResult.output === code) {
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
code = fixedResult.output;
|
|
498
|
+
outputs.push(code);
|
|
499
|
+
// Verify if autofix makes a syntax error or not.
|
|
500
|
+
const errorMessageInFix = this.#linter
|
|
501
|
+
.verify(fixedResult.output, config, filename)
|
|
365
502
|
.find(m => m.fatal);
|
|
366
503
|
(0, node_assert_1.default)(!errorMessageInFix, [
|
|
367
504
|
'A fatal parsing error occurred in autofix.',
|
|
368
505
|
`Error: ${errorMessageInFix?.message}`,
|
|
369
506
|
'Autofix output:',
|
|
370
|
-
output,
|
|
507
|
+
fixedResult.output,
|
|
371
508
|
].join('\n'));
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
output = code;
|
|
375
|
-
}
|
|
509
|
+
} while (fixedResult.fixed && passNumber < 10);
|
|
376
510
|
return {
|
|
377
|
-
|
|
378
|
-
|
|
511
|
+
config,
|
|
512
|
+
filename,
|
|
513
|
+
messages: initialMessages,
|
|
514
|
+
outputs,
|
|
379
515
|
// is definitely assigned within the `rule-tester/validate-ast` rule
|
|
380
516
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
381
517
|
beforeAST: beforeAST,
|
|
@@ -384,271 +520,242 @@ class RuleTester extends TestFramework_1.TestFramework {
|
|
|
384
520
|
afterAST: (0, cloneDeeplyExcludesParent_1.cloneDeeplyExcludesParent)(afterAST),
|
|
385
521
|
};
|
|
386
522
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
*/
|
|
397
|
-
const getFilename = (testOptions) => {
|
|
398
|
-
const resolvedOptions = (0, eslint_utils_1.deepMerge)(__classPrivateFieldGet(this, _RuleTester_testerConfig, "f").parserOptions, testOptions);
|
|
399
|
-
const filename = resolvedOptions.ecmaFeatures?.jsx
|
|
400
|
-
? __classPrivateFieldGet(this, _RuleTester_testerConfig, "f").defaultFilenames.tsx
|
|
401
|
-
: __classPrivateFieldGet(this, _RuleTester_testerConfig, "f").defaultFilenames.ts;
|
|
402
|
-
if (resolvedOptions.project) {
|
|
403
|
-
return node_path_1.default.join(resolvedOptions.tsconfigRootDir ?? process.cwd(), filename);
|
|
523
|
+
/**
|
|
524
|
+
* Check if the template is valid or not
|
|
525
|
+
* all valid cases go through this
|
|
526
|
+
*/
|
|
527
|
+
#testValidTemplate(ruleName, rule, itemIn, seenValidTestCases) {
|
|
528
|
+
const item = typeof itemIn === 'object' ? itemIn : { code: itemIn };
|
|
529
|
+
node_assert_1.default.ok(typeof item.code === 'string', "Test case must specify a string value for 'code'");
|
|
530
|
+
if (item.name) {
|
|
531
|
+
node_assert_1.default.ok(typeof item.name === 'string', "Optional test case property 'name' must be a string");
|
|
404
532
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
533
|
+
checkDuplicateTestCase(item, seenValidTestCases);
|
|
534
|
+
const result = this.runRuleForItem(ruleName, rule, item);
|
|
535
|
+
const messages = result.messages;
|
|
536
|
+
node_assert_1.default.strictEqual(messages.length, 0, node_util_1.default.format('Should have no errors but had %d: %s', messages.length, node_util_1.default.inspect(messages)));
|
|
537
|
+
assertASTDidntChange(result.beforeAST, result.afterAST);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Check if the template is invalid or not
|
|
541
|
+
* all invalid cases go through this.
|
|
542
|
+
*/
|
|
543
|
+
#testInvalidTemplate(ruleName, rule, item, seenInvalidTestCases) {
|
|
544
|
+
node_assert_1.default.ok(typeof item.code === 'string', "Test case must specify a string value for 'code'");
|
|
545
|
+
if (item.name) {
|
|
546
|
+
node_assert_1.default.ok(typeof item.name === 'string', "Optional test case property 'name' must be a string");
|
|
410
547
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
filename: getFilename(test.parserOptions),
|
|
415
|
-
};
|
|
548
|
+
node_assert_1.default.ok(item.errors || item.errors === 0, `Did not specify errors for an invalid test of ${ruleName}`);
|
|
549
|
+
if (Array.isArray(item.errors) && item.errors.length === 0) {
|
|
550
|
+
node_assert_1.default.fail('Invalid cases must have at least one error');
|
|
416
551
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
.
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
}
|
|
436
|
-
for (const testCase of normalizedTests.invalid) {
|
|
437
|
-
yield testCase;
|
|
438
|
-
}
|
|
439
|
-
},
|
|
440
|
-
};
|
|
441
|
-
const hasOnly = (() => {
|
|
442
|
-
for (const test of allTestsIterator) {
|
|
443
|
-
if (test.only) {
|
|
444
|
-
return true;
|
|
552
|
+
checkDuplicateTestCase(item, seenInvalidTestCases);
|
|
553
|
+
const ruleHasMetaMessages = (0, hasOwnProperty_1.hasOwnProperty)(rule, 'meta') && (0, hasOwnProperty_1.hasOwnProperty)(rule.meta, 'messages');
|
|
554
|
+
const friendlyIDList = ruleHasMetaMessages
|
|
555
|
+
? `[${Object.keys(rule.meta.messages)
|
|
556
|
+
.map(key => `'${key}'`)
|
|
557
|
+
.join(', ')}]`
|
|
558
|
+
: null;
|
|
559
|
+
const result = this.runRuleForItem(ruleName, rule, item);
|
|
560
|
+
const messages = result.messages;
|
|
561
|
+
for (const message of messages) {
|
|
562
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(message, 'suggestions')) {
|
|
563
|
+
const seenMessageIndices = new Map();
|
|
564
|
+
for (let i = 0; i < message.suggestions.length; i += 1) {
|
|
565
|
+
const suggestionMessage = message.suggestions[i].desc;
|
|
566
|
+
const previous = seenMessageIndices.get(suggestionMessage);
|
|
567
|
+
node_assert_1.default.ok(!seenMessageIndices.has(suggestionMessage), `Suggestion message '${suggestionMessage}' reported from suggestion ${i} was previously reported by suggestion ${previous}. Suggestion messages should be unique within an error.`);
|
|
568
|
+
seenMessageIndices.set(suggestionMessage, i);
|
|
569
|
+
}
|
|
445
570
|
}
|
|
446
571
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
// if there is an `only: true` - don't try apply constraints - assume that
|
|
451
|
-
// we are in "local development" mode rather than "CI validation" mode
|
|
452
|
-
return normalizedTests;
|
|
453
|
-
}
|
|
454
|
-
const hasConstraints = (() => {
|
|
455
|
-
for (const test of allTestsIterator) {
|
|
456
|
-
if (test.dependencyConstraints &&
|
|
457
|
-
Object.keys(test.dependencyConstraints).length > 0) {
|
|
458
|
-
return true;
|
|
572
|
+
if (typeof item.errors === 'number') {
|
|
573
|
+
if (item.errors === 0) {
|
|
574
|
+
node_assert_1.default.fail("Invalid cases must have 'error' value greater than 0");
|
|
459
575
|
}
|
|
576
|
+
node_assert_1.default.strictEqual(messages.length, item.errors, node_util_1.default.format('Should have %d error%s but had %d: %s', item.errors, item.errors === 1 ? '' : 's', messages.length, node_util_1.default.inspect(messages)));
|
|
460
577
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
*/
|
|
472
|
-
const maybeMarkAsOnly = (test) => {
|
|
473
|
-
return {
|
|
474
|
-
...test,
|
|
475
|
-
skip: !(0, dependencyConstraints_1.satisfiesAllDependencyConstraints)(test.dependencyConstraints),
|
|
476
|
-
};
|
|
477
|
-
};
|
|
478
|
-
normalizedTests.valid = normalizedTests.valid.map(maybeMarkAsOnly);
|
|
479
|
-
normalizedTests.invalid = normalizedTests.invalid.map(maybeMarkAsOnly);
|
|
480
|
-
return normalizedTests;
|
|
481
|
-
}, _RuleTester_testValidTemplate = function _RuleTester_testValidTemplate(ruleName, rule, itemIn) {
|
|
482
|
-
const item = typeof itemIn === 'object' ? itemIn : { code: itemIn };
|
|
483
|
-
node_assert_1.default.ok(typeof item.code === 'string', "Test case must specify a string value for 'code'");
|
|
484
|
-
if (item.name) {
|
|
485
|
-
node_assert_1.default.ok(typeof item.name === 'string', "Optional test case property 'name' must be a string");
|
|
486
|
-
}
|
|
487
|
-
const result = this.runRuleForItem(ruleName, rule, item);
|
|
488
|
-
const messages = result.messages;
|
|
489
|
-
node_assert_1.default.strictEqual(messages.length, 0, node_util_1.default.format('Should have no errors but had %d: %s', messages.length, node_util_1.default.inspect(messages)));
|
|
490
|
-
assertASTDidntChange(result.beforeAST, result.afterAST);
|
|
491
|
-
}, _RuleTester_testInvalidTemplate = function _RuleTester_testInvalidTemplate(ruleName, rule, item) {
|
|
492
|
-
node_assert_1.default.ok(typeof item.code === 'string', "Test case must specify a string value for 'code'");
|
|
493
|
-
if (item.name) {
|
|
494
|
-
node_assert_1.default.ok(typeof item.name === 'string', "Optional test case property 'name' must be a string");
|
|
495
|
-
}
|
|
496
|
-
node_assert_1.default.ok(item.errors || item.errors === 0, `Did not specify errors for an invalid test of ${ruleName}`);
|
|
497
|
-
if (Array.isArray(item.errors) && item.errors.length === 0) {
|
|
498
|
-
node_assert_1.default.fail('Invalid cases must have at least one error');
|
|
499
|
-
}
|
|
500
|
-
const ruleHasMetaMessages = (0, hasOwnProperty_1.hasOwnProperty)(rule, 'meta') && (0, hasOwnProperty_1.hasOwnProperty)(rule.meta, 'messages');
|
|
501
|
-
const friendlyIDList = ruleHasMetaMessages
|
|
502
|
-
? `[${Object.keys(rule.meta.messages)
|
|
503
|
-
.map(key => `'${key}'`)
|
|
504
|
-
.join(', ')}]`
|
|
505
|
-
: null;
|
|
506
|
-
const result = this.runRuleForItem(ruleName, rule, item);
|
|
507
|
-
const messages = result.messages;
|
|
508
|
-
if (typeof item.errors === 'number') {
|
|
509
|
-
if (item.errors === 0) {
|
|
510
|
-
node_assert_1.default.fail("Invalid cases must have 'error' value greater than 0");
|
|
511
|
-
}
|
|
512
|
-
node_assert_1.default.strictEqual(messages.length, item.errors, node_util_1.default.format('Should have %d error%s but had %d: %s', item.errors, item.errors === 1 ? '' : 's', messages.length, node_util_1.default.inspect(messages)));
|
|
513
|
-
}
|
|
514
|
-
else {
|
|
515
|
-
node_assert_1.default.strictEqual(messages.length, item.errors.length, node_util_1.default.format('Should have %d error%s but had %d: %s', item.errors.length, item.errors.length === 1 ? '' : 's', messages.length, node_util_1.default.inspect(messages)));
|
|
516
|
-
const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);
|
|
517
|
-
for (let i = 0, l = item.errors.length; i < l; i++) {
|
|
518
|
-
const error = item.errors[i];
|
|
519
|
-
const message = messages[i];
|
|
520
|
-
(0, node_assert_1.default)(hasMessageOfThisRule, 'Error rule name should be the same as the name of the rule being tested');
|
|
521
|
-
if (typeof error === 'string' || error instanceof RegExp) {
|
|
522
|
-
// Just an error message.
|
|
523
|
-
assertMessageMatches(message.message, error);
|
|
524
|
-
}
|
|
525
|
-
else if (typeof error === 'object' && error != null) {
|
|
526
|
-
/*
|
|
527
|
-
* Error object.
|
|
528
|
-
* This may have a message, messageId, data, node type, line, and/or
|
|
529
|
-
* column.
|
|
530
|
-
*/
|
|
531
|
-
Object.keys(error).forEach(propertyName => {
|
|
532
|
-
node_assert_1.default.ok(validationHelpers_1.ERROR_OBJECT_PARAMETERS.has(propertyName), `Invalid error property name '${propertyName}'. Expected one of ${validationHelpers_1.FRIENDLY_ERROR_OBJECT_PARAMETER_LIST}.`);
|
|
533
|
-
});
|
|
534
|
-
// @ts-expect-error -- we purposely don't define `message` on our types as the current standard is `messageId`
|
|
535
|
-
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'message')) {
|
|
536
|
-
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(error, 'messageId'), "Error should not specify both 'message' and a 'messageId'.");
|
|
537
|
-
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(error, 'data'), "Error should not specify both 'data' and 'message'.");
|
|
538
|
-
assertMessageMatches(message.message,
|
|
539
|
-
// @ts-expect-error -- we purposely don't define `message` on our types as the current standard is `messageId`
|
|
540
|
-
error.message);
|
|
578
|
+
else {
|
|
579
|
+
node_assert_1.default.strictEqual(messages.length, item.errors.length, node_util_1.default.format('Should have %d error%s but had %d: %s', item.errors.length, item.errors.length === 1 ? '' : 's', messages.length, node_util_1.default.inspect(messages)));
|
|
580
|
+
const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);
|
|
581
|
+
for (let i = 0, l = item.errors.length; i < l; i++) {
|
|
582
|
+
const error = item.errors[i];
|
|
583
|
+
const message = messages[i];
|
|
584
|
+
(0, node_assert_1.default)(hasMessageOfThisRule, 'Error rule name should be the same as the name of the rule being tested');
|
|
585
|
+
if (typeof error === 'string' || error instanceof RegExp) {
|
|
586
|
+
// Just an error message.
|
|
587
|
+
assertMessageMatches(message.message, error);
|
|
541
588
|
}
|
|
542
|
-
else if (
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
589
|
+
else if (typeof error === 'object' && error != null) {
|
|
590
|
+
/*
|
|
591
|
+
* Error object.
|
|
592
|
+
* This may have a message, messageId, data, node type, line, and/or
|
|
593
|
+
* column.
|
|
594
|
+
*/
|
|
595
|
+
Object.keys(error).forEach(propertyName => {
|
|
596
|
+
node_assert_1.default.ok(validationHelpers_1.ERROR_OBJECT_PARAMETERS.has(propertyName), `Invalid error property name '${propertyName}'. Expected one of ${validationHelpers_1.FRIENDLY_ERROR_OBJECT_PARAMETER_LIST}.`);
|
|
597
|
+
});
|
|
598
|
+
// @ts-expect-error -- we purposely don't define `message` on our types as the current standard is `messageId`
|
|
599
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'message')) {
|
|
600
|
+
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(error, 'messageId'), "Error should not specify both 'message' and a 'messageId'.");
|
|
601
|
+
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(error, 'data'), "Error should not specify both 'data' and 'message'.");
|
|
602
|
+
assertMessageMatches(message.message,
|
|
603
|
+
// @ts-expect-error -- we purposely don't define `message` on our types as the current standard is `messageId`
|
|
604
|
+
error.message);
|
|
557
605
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
node_assert_1.default.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`);
|
|
576
|
-
}
|
|
577
|
-
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'suggestions')) {
|
|
578
|
-
// Support asserting there are no suggestions
|
|
579
|
-
if (!error.suggestions ||
|
|
580
|
-
((0, isReadonlyArray_1.isReadonlyArray)(error.suggestions) &&
|
|
581
|
-
error.suggestions.length === 0)) {
|
|
582
|
-
if (Array.isArray(message.suggestions) &&
|
|
583
|
-
message.suggestions.length > 0) {
|
|
584
|
-
node_assert_1.default.fail(`Error should have no suggestions on error with message: "${message.message}"`);
|
|
606
|
+
else if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'messageId')) {
|
|
607
|
+
node_assert_1.default.ok(ruleHasMetaMessages, "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'.");
|
|
608
|
+
if (!(0, hasOwnProperty_1.hasOwnProperty)(rule.meta.messages, error.messageId)) {
|
|
609
|
+
(0, node_assert_1.default)(false, `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`);
|
|
610
|
+
}
|
|
611
|
+
node_assert_1.default.strictEqual(message.messageId, error.messageId, `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.`);
|
|
612
|
+
const unsubstitutedPlaceholders = getUnsubstitutedMessagePlaceholders(message.message, rule.meta.messages[message.messageId], error.data);
|
|
613
|
+
node_assert_1.default.ok(unsubstitutedPlaceholders.length === 0, `The reported message has ${unsubstitutedPlaceholders.length > 1 ? `unsubstituted placeholders: ${unsubstitutedPlaceholders.map(name => `'${name}'`).join(', ')}` : `an unsubstituted placeholder '${unsubstitutedPlaceholders[0]}'`}. Please provide the missing ${unsubstitutedPlaceholders.length > 1 ? 'values' : 'value'} via the 'data' property in the context.report() call.`);
|
|
614
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'data')) {
|
|
615
|
+
/*
|
|
616
|
+
* if data was provided, then directly compare the returned message to a synthetic
|
|
617
|
+
* interpolated message using the same message ID and data provided in the test.
|
|
618
|
+
* See https://github.com/eslint/eslint/issues/9890 for context.
|
|
619
|
+
*/
|
|
620
|
+
const unformattedOriginalMessage = rule.meta.messages[error.messageId];
|
|
621
|
+
const rehydratedMessage = (0, interpolate_1.interpolate)(unformattedOriginalMessage, error.data);
|
|
622
|
+
node_assert_1.default.strictEqual(message.message, rehydratedMessage, `Hydrated message "${rehydratedMessage}" does not match "${message.message}"`);
|
|
585
623
|
}
|
|
586
624
|
}
|
|
587
|
-
|
|
588
|
-
(0,
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
625
|
+
node_assert_1.default.ok((0, hasOwnProperty_1.hasOwnProperty)(error, 'data')
|
|
626
|
+
? (0, hasOwnProperty_1.hasOwnProperty)(error, 'messageId')
|
|
627
|
+
: true, "Error must specify 'messageId' if 'data' is used.");
|
|
628
|
+
if (error.type) {
|
|
629
|
+
node_assert_1.default.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`);
|
|
630
|
+
}
|
|
631
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'line')) {
|
|
632
|
+
node_assert_1.default.strictEqual(message.line, error.line, `Error line should be ${error.line}`);
|
|
633
|
+
}
|
|
634
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'column')) {
|
|
635
|
+
node_assert_1.default.strictEqual(message.column, error.column, `Error column should be ${error.column}`);
|
|
636
|
+
}
|
|
637
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'endLine')) {
|
|
638
|
+
node_assert_1.default.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`);
|
|
639
|
+
}
|
|
640
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'endColumn')) {
|
|
641
|
+
node_assert_1.default.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`);
|
|
642
|
+
}
|
|
643
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(error, 'suggestions')) {
|
|
644
|
+
// Support asserting there are no suggestions
|
|
645
|
+
if (!error.suggestions ||
|
|
646
|
+
((0, isReadonlyArray_1.isReadonlyArray)(error.suggestions) &&
|
|
647
|
+
error.suggestions.length === 0)) {
|
|
648
|
+
if (Array.isArray(message.suggestions) &&
|
|
649
|
+
message.suggestions.length > 0) {
|
|
650
|
+
node_assert_1.default.fail(`Error should have no suggestions on error with message: "${message.message}"`);
|
|
605
651
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
(0, node_assert_1.default)(Array.isArray(message.suggestions), `Error should have an array of suggestions. Instead received "${String(message.suggestions)}" on error with message: "${message.message}"`);
|
|
655
|
+
const messageSuggestions = message.suggestions;
|
|
656
|
+
node_assert_1.default.strictEqual(messageSuggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${messageSuggestions.length} suggestions`);
|
|
657
|
+
error.suggestions.forEach((expectedSuggestion, index) => {
|
|
658
|
+
node_assert_1.default.ok(typeof expectedSuggestion === 'object' &&
|
|
659
|
+
expectedSuggestion != null, "Test suggestion in 'suggestions' array must be an object.");
|
|
660
|
+
Object.keys(expectedSuggestion).forEach(propertyName => {
|
|
661
|
+
node_assert_1.default.ok(validationHelpers_1.SUGGESTION_OBJECT_PARAMETERS.has(propertyName), `Invalid suggestion property name '${propertyName}'. Expected one of ${validationHelpers_1.FRIENDLY_SUGGESTION_OBJECT_PARAMETER_LIST}.`);
|
|
662
|
+
});
|
|
663
|
+
const actualSuggestion = messageSuggestions[index];
|
|
664
|
+
const suggestionPrefix = `Error Suggestion at index ${index} :`;
|
|
665
|
+
// @ts-expect-error -- we purposely don't define `desc` on our types as the current standard is `messageId`
|
|
666
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'desc')) {
|
|
667
|
+
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'data'), `${suggestionPrefix} Test should not specify both 'desc' and 'data'.`);
|
|
668
|
+
// @ts-expect-error -- we purposely don't define `desc` on our types as the current standard is `messageId`
|
|
669
|
+
const expectedDesc = expectedSuggestion.desc;
|
|
670
|
+
node_assert_1.default.strictEqual(actualSuggestion.desc, expectedDesc, `${suggestionPrefix} desc should be "${expectedDesc}" but got "${actualSuggestion.desc}" instead.`);
|
|
614
671
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
672
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'messageId')) {
|
|
673
|
+
node_assert_1.default.ok(ruleHasMetaMessages, `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.`);
|
|
674
|
+
node_assert_1.default.ok((0, hasOwnProperty_1.hasOwnProperty)(rule.meta.messages, expectedSuggestion.messageId), `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.`);
|
|
675
|
+
node_assert_1.default.strictEqual(actualSuggestion.messageId, expectedSuggestion.messageId, `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.`);
|
|
676
|
+
const unsubstitutedPlaceholders = getUnsubstitutedMessagePlaceholders(actualSuggestion.desc, rule.meta.messages[expectedSuggestion.messageId], expectedSuggestion.data);
|
|
677
|
+
node_assert_1.default.ok(unsubstitutedPlaceholders.length === 0, `The message of the suggestion has ${unsubstitutedPlaceholders.length > 1 ? `unsubstituted placeholders: ${unsubstitutedPlaceholders.map(name => `'${name}'`).join(', ')}` : `an unsubstituted placeholder '${unsubstitutedPlaceholders[0]}'`}. Please provide the missing ${unsubstitutedPlaceholders.length > 1 ? 'values' : 'value'} via the 'data' property for the suggestion in the context.report() call.`);
|
|
678
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'data')) {
|
|
679
|
+
const unformattedMetaMessage = rule.meta.messages[expectedSuggestion.messageId];
|
|
680
|
+
const rehydratedDesc = (0, interpolate_1.interpolate)(unformattedMetaMessage, expectedSuggestion.data);
|
|
681
|
+
node_assert_1.default.strictEqual(actualSuggestion.desc, rehydratedDesc, `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
else {
|
|
685
|
+
node_assert_1.default.ok(!(0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'data'), `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.`);
|
|
686
|
+
}
|
|
687
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(expectedSuggestion, 'output')) {
|
|
688
|
+
const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output;
|
|
689
|
+
// Verify if suggestion fix makes a syntax error or not.
|
|
690
|
+
const errorMessageInSuggestion = this.#linter
|
|
691
|
+
.verify(codeWithAppliedSuggestion, result.config, result.filename)
|
|
692
|
+
.find(m => m.fatal);
|
|
693
|
+
(0, node_assert_1.default)(!errorMessageInSuggestion, [
|
|
694
|
+
'A fatal parsing error occurred in suggestion fix.',
|
|
695
|
+
`Error: ${errorMessageInSuggestion?.message}`,
|
|
696
|
+
'Suggestion output:',
|
|
697
|
+
codeWithAppliedSuggestion,
|
|
698
|
+
].join('\n'));
|
|
699
|
+
node_assert_1.default.strictEqual(codeWithAppliedSuggestion, expectedSuggestion.output, `Expected the applied suggestion fix to match the test suggestion output for suggestion at index: ${index} on error with message: "${message.message}"`);
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
}
|
|
624
703
|
}
|
|
625
704
|
}
|
|
705
|
+
else {
|
|
706
|
+
// Message was an unexpected type
|
|
707
|
+
node_assert_1.default.fail(`Error should be a string, object, or RegExp, but found (${node_util_1.default.inspect(message)})`);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if ((0, hasOwnProperty_1.hasOwnProperty)(item, 'output')) {
|
|
712
|
+
if (item.output == null) {
|
|
713
|
+
if (result.outputs.length) {
|
|
714
|
+
node_assert_1.default.strictEqual(result.outputs[0], item.code, 'Expected no autofixes to be suggested.');
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
else if (typeof item.output === 'string') {
|
|
718
|
+
(0, node_assert_1.default)(result.outputs.length > 0, 'Expected autofix to be suggested.');
|
|
719
|
+
node_assert_1.default.strictEqual(result.outputs[0], item.output, 'Output is incorrect.');
|
|
720
|
+
if (result.outputs.length) {
|
|
721
|
+
node_assert_1.default.deepStrictEqual(result.outputs, [item.output], 'Multiple autofixes are required due to overlapping fix ranges - please use the array form of output to declare all of the expected autofix passes.');
|
|
722
|
+
}
|
|
626
723
|
}
|
|
627
724
|
else {
|
|
628
|
-
|
|
629
|
-
node_assert_1.default.
|
|
725
|
+
(0, node_assert_1.default)(result.outputs.length > 0, 'Expected autofix to be suggested.');
|
|
726
|
+
node_assert_1.default.deepStrictEqual(result.outputs, item.output, 'Outputs do not match.');
|
|
630
727
|
}
|
|
631
728
|
}
|
|
632
|
-
}
|
|
633
|
-
if ((0, hasOwnProperty_1.hasOwnProperty)(item, 'output')) {
|
|
634
|
-
if (item.output == null) {
|
|
635
|
-
node_assert_1.default.strictEqual(result.output, item.code, 'Expected no autofixes to be suggested');
|
|
636
|
-
}
|
|
637
729
|
else {
|
|
638
|
-
|
|
730
|
+
if (result.outputs.length) {
|
|
731
|
+
node_assert_1.default.strictEqual(result.outputs[0], item.code, "The rule fixed the code. Please add 'output' property.");
|
|
732
|
+
}
|
|
639
733
|
}
|
|
734
|
+
assertASTDidntChange(result.beforeAST, result.afterAST);
|
|
640
735
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
}
|
|
644
|
-
assertASTDidntChange(result.beforeAST, result.afterAST);
|
|
645
|
-
};
|
|
736
|
+
}
|
|
737
|
+
exports.RuleTester = RuleTester;
|
|
646
738
|
/**
|
|
647
739
|
* Check if the AST was changed
|
|
648
740
|
*/
|
|
649
741
|
function assertASTDidntChange(beforeAST, afterAST) {
|
|
650
742
|
node_assert_1.default.deepStrictEqual(beforeAST, afterAST, 'Rule should not modify AST.');
|
|
651
743
|
}
|
|
744
|
+
/**
|
|
745
|
+
* Check if this test case is a duplicate of one we have seen before.
|
|
746
|
+
*/
|
|
747
|
+
function checkDuplicateTestCase(item, seenTestCases) {
|
|
748
|
+
if (!(0, serialization_1.isSerializable)(item)) {
|
|
749
|
+
/*
|
|
750
|
+
* If we can't serialize a test case (because it contains a function, RegExp, etc), skip the check.
|
|
751
|
+
* This might happen with properties like: options, plugins, settings, languageOptions.parser, languageOptions.parserOptions.
|
|
752
|
+
*/
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
const serializedTestCase = (0, json_stable_stringify_without_jsonify_1.default)(item);
|
|
756
|
+
(0, node_assert_1.default)(!seenTestCases.has(serializedTestCase), 'detected duplicate test case');
|
|
757
|
+
seenTestCases.add(serializedTestCase);
|
|
758
|
+
}
|
|
652
759
|
/**
|
|
653
760
|
* Asserts that the message matches its expected value. If the expected
|
|
654
761
|
* value is a regular expression, it is checked against the actual
|