oxlint-react-compiler-experimental 0.0.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/README.md +59 -0
- package/bin/oxlint +3 -0
- package/configuration_schema.json +800 -0
- package/dist/bindings.js +404 -0
- package/dist/cli.js +84 -0
- package/dist/config.js +16 -0
- package/dist/index.d.ts +735 -0
- package/dist/index.js +2 -0
- package/dist/js_config.js +66 -0
- package/dist/lint.js +22559 -0
- package/dist/oxlint.darwin-arm64.node +0 -0
- package/dist/plugins-dev.d.ts +4203 -0
- package/dist/plugins-dev.js +866 -0
- package/dist/plugins.js +23 -0
- package/dist/utils.js +44 -0
- package/dist/workspace.js +2 -0
- package/dist/workspace2.js +35 -0
- package/package.json +66 -0
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
import { _ as BUFFER_SIZE, b as __toESM, c as allOptions, d as diagnostics, f as replacePlaceholders, g as BUFFER_ALIGN, h as ACTIVE_SIZE, i as resetStateAfterError, l as setOptions, m as getNodeByRangeIndex, o as registerPlugin, p as getLineColumnFromOffset, r as lintFileImpl, s as registeredRules, t as buffers, u as PLACEHOLDER_REGEX, y as __commonJSMin } from "./lint.js";
|
|
2
|
+
import { a as rawTransferSupported$1, i as parseRawSync, n as getBufferOffset, t as applyFixes } from "./bindings.js";
|
|
3
|
+
import { d as ObjectDefineProperty, f as ObjectEntries, g as ObjectValues, h as ObjectKeys, m as ObjectHasOwn, n as ArrayFrom, o as JSONStringify, r as ArrayIsArray } from "./utils.js";
|
|
4
|
+
import assert, { AssertionError } from "node:assert";
|
|
5
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
6
|
+
import util from "node:util";
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src-js/package/parse.ts
|
|
9
|
+
var import_json_stable_stringify_without_jsonify = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
10
|
+
module.exports = function(obj, opts) {
|
|
11
|
+
opts ||= {}, typeof opts == "function" && (opts = { cmp: opts });
|
|
12
|
+
var space = opts.space || "";
|
|
13
|
+
typeof space == "number" && (space = Array(space + 1).join(" "));
|
|
14
|
+
var cycles = typeof opts.cycles == "boolean" ? opts.cycles : !1, replacer = opts.replacer || function(key, value) {
|
|
15
|
+
return value;
|
|
16
|
+
}, cmp = opts.cmp && (function(f) {
|
|
17
|
+
return function(node) {
|
|
18
|
+
return function(a, b) {
|
|
19
|
+
return f({
|
|
20
|
+
key: a,
|
|
21
|
+
value: node[a]
|
|
22
|
+
}, {
|
|
23
|
+
key: b,
|
|
24
|
+
value: node[b]
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
})(opts.cmp), seen = [];
|
|
29
|
+
return (function stringify(parent, key, node, level) {
|
|
30
|
+
var indent = space ? "\n" + Array(level + 1).join(space) : "", colonSeparator = space ? ": " : ":";
|
|
31
|
+
if (node && node.toJSON && typeof node.toJSON == "function" && (node = node.toJSON()), node = replacer.call(parent, key, node), node !== void 0) {
|
|
32
|
+
if (typeof node != "object" || !node) return JSON.stringify(node);
|
|
33
|
+
if (isArray(node)) {
|
|
34
|
+
for (var out = [], i = 0; i < node.length; i++) {
|
|
35
|
+
var item = stringify(node, i, node[i], level + 1) || JSON.stringify(null);
|
|
36
|
+
out.push(indent + space + item);
|
|
37
|
+
}
|
|
38
|
+
return "[" + out.join(",") + indent + "]";
|
|
39
|
+
} else {
|
|
40
|
+
if (seen.indexOf(node) !== -1) {
|
|
41
|
+
if (cycles) return JSON.stringify("__cycle__");
|
|
42
|
+
throw TypeError("Converting circular structure to JSON");
|
|
43
|
+
} else seen.push(node);
|
|
44
|
+
for (var keys = objectKeys(node).sort(cmp && cmp(node)), out = [], i = 0; i < keys.length; i++) {
|
|
45
|
+
var key = keys[i], value = stringify(node, key, node[key], level + 1);
|
|
46
|
+
if (value) {
|
|
47
|
+
var keyValue = JSON.stringify(key) + colonSeparator + value;
|
|
48
|
+
out.push(indent + space + keyValue);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return seen.splice(seen.indexOf(node), 1), "{" + out.join(",") + indent + "}";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
})({ "": obj }, "", obj, 0);
|
|
55
|
+
};
|
|
56
|
+
var isArray = Array.isArray || function(x) {
|
|
57
|
+
return {}.toString.call(x) === "[object Array]";
|
|
58
|
+
}, objectKeys = Object.keys || function(obj) {
|
|
59
|
+
var has = Object.prototype.hasOwnProperty || function() {
|
|
60
|
+
return !0;
|
|
61
|
+
}, keys = [];
|
|
62
|
+
for (var key in obj) has.call(obj, key) && keys.push(key);
|
|
63
|
+
return keys;
|
|
64
|
+
};
|
|
65
|
+
})))(), 1);
|
|
66
|
+
const ARRAY_BUFFER_SIZE = BUFFER_SIZE + BUFFER_ALIGN, textEncoder = new TextEncoder();
|
|
67
|
+
let buffer = null, rawTransferIsSupported = null;
|
|
68
|
+
/**
|
|
69
|
+
* Parser source text into buffer.
|
|
70
|
+
* @param path - Path of file to parse
|
|
71
|
+
* @param sourceText - Source text to parse
|
|
72
|
+
* @param options - Parsing options
|
|
73
|
+
* @throws {Error} If raw transfer is not supported on this platform, or parsing failed
|
|
74
|
+
*/
|
|
75
|
+
function parse(path, sourceText, options) {
|
|
76
|
+
if (!rawTransferSupported()) throw Error("`RuleTester` is not supported on 32-bit or big-endian systems, versions of NodeJS prior to v22.0.0, versions of Deno prior to v2.0.0, or other runtimes");
|
|
77
|
+
buffer === null && initBuffer();
|
|
78
|
+
let maxSourceByteLen = sourceText.length * 3;
|
|
79
|
+
if (maxSourceByteLen > 1073741824) throw Error("Source text is too long");
|
|
80
|
+
let sourceStartPos = ACTIVE_SIZE - maxSourceByteLen, sourceBuffer = new Uint8Array(buffer.buffer, buffer.byteOffset + sourceStartPos, maxSourceByteLen), { read, written: sourceByteLen } = textEncoder.encodeInto(sourceText, sourceBuffer);
|
|
81
|
+
if (read !== sourceText.length) throw Error("Failed to write source text into buffer");
|
|
82
|
+
if (parseRawSync(path, buffer, sourceStartPos, sourceByteLen, options), buffer.uint32[536870900] === 0) throw Error("Parsing failed");
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a `Uint8Array` which is 2 GiB in size, with its start aligned on 4 GiB.
|
|
86
|
+
*
|
|
87
|
+
* Store it in `buffer`, and also in `buffers` array, so it's accessible to `lintFileImpl` by passing `0`as `bufferId`.
|
|
88
|
+
*
|
|
89
|
+
* Achieve this by creating a 6 GiB `ArrayBuffer`, getting the offset within it that's aligned to 4 GiB,
|
|
90
|
+
* chopping off that number of bytes from the start, and shortening to 2 GiB.
|
|
91
|
+
*
|
|
92
|
+
* It's always possible to obtain a 2 GiB slice aligned on 4 GiB within a 6 GiB buffer,
|
|
93
|
+
* no matter how the 6 GiB buffer is aligned.
|
|
94
|
+
*
|
|
95
|
+
* Note: On systems with virtual memory, this only consumes 6 GiB of *virtual* memory.
|
|
96
|
+
* It does not consume physical memory until data is actually written to the `Uint8Array`.
|
|
97
|
+
* Physical memory consumed corresponds to the quantity of data actually written.
|
|
98
|
+
*/
|
|
99
|
+
function initBuffer() {
|
|
100
|
+
let arrayBuffer = new ArrayBuffer(ARRAY_BUFFER_SIZE), offset = getBufferOffset(new Uint8Array(arrayBuffer));
|
|
101
|
+
buffer = new Uint8Array(arrayBuffer, offset, BUFFER_SIZE), buffer.uint32 = new Uint32Array(arrayBuffer, offset, BUFFER_SIZE / 4), buffer.float64 = new Float64Array(arrayBuffer, offset, BUFFER_SIZE / 8), buffers.push(buffer);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Returns `true` if raw transfer is supported.
|
|
105
|
+
*
|
|
106
|
+
* Raw transfer is only supported on 64-bit little-endian systems,
|
|
107
|
+
* and NodeJS >= v22.0.0 or Deno >= v2.0.0.
|
|
108
|
+
*
|
|
109
|
+
* Versions of NodeJS prior to v22.0.0 do not support creating an `ArrayBuffer` larger than 4 GiB.
|
|
110
|
+
* Bun (as at v1.2.4) also does not support creating an `ArrayBuffer` larger than 4 GiB.
|
|
111
|
+
* Support on Deno v1 is unknown and it's EOL, so treating Deno before v2.0.0 as unsupported.
|
|
112
|
+
*
|
|
113
|
+
* No easy way to determining pointer width (64 bit or 32 bit) in JS,
|
|
114
|
+
* so call a function on Rust side to find out.
|
|
115
|
+
*
|
|
116
|
+
* @returns {boolean} - `true` if raw transfer is supported on this platform
|
|
117
|
+
*/
|
|
118
|
+
function rawTransferSupported() {
|
|
119
|
+
return rawTransferIsSupported === null && (rawTransferIsSupported = rawTransferRuntimeSupported() && rawTransferSupported$1()), rawTransferIsSupported;
|
|
120
|
+
}
|
|
121
|
+
function rawTransferRuntimeSupported() {
|
|
122
|
+
let global;
|
|
123
|
+
try {
|
|
124
|
+
global = globalThis;
|
|
125
|
+
} catch {
|
|
126
|
+
return !1;
|
|
127
|
+
}
|
|
128
|
+
if (global.Bun || global.process?.versions?.bun) return !1;
|
|
129
|
+
if (global.Deno) {
|
|
130
|
+
let match = Deno.version?.deno?.match(/^(\d+)\./);
|
|
131
|
+
return !!match && +match[1] >= 2;
|
|
132
|
+
}
|
|
133
|
+
if (global.process?.release?.name !== "node") return !1;
|
|
134
|
+
let match = process.version?.match(/^v(\d+)\./);
|
|
135
|
+
return !!match && +match[1] >= 22;
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src-js/package/rule_tester.ts
|
|
139
|
+
/**
|
|
140
|
+
* Default `describe` function, if `describe` doesn't exist as a global.
|
|
141
|
+
* @param text - Description of the test case
|
|
142
|
+
* @param method - Test case logic
|
|
143
|
+
* @returns Returned value of `method`
|
|
144
|
+
*/
|
|
145
|
+
function defaultDescribe(text, method) {
|
|
146
|
+
return method.call(this);
|
|
147
|
+
}
|
|
148
|
+
const globalObj = globalThis;
|
|
149
|
+
let describe = typeof globalObj.describe == "function" ? globalObj.describe : defaultDescribe;
|
|
150
|
+
/**
|
|
151
|
+
* Default `it` function, if `it` doesn't exist as a global.
|
|
152
|
+
* @param text - Description of the test case
|
|
153
|
+
* @param method - Test case logic
|
|
154
|
+
* @throws {Error} Any error upon execution of `method`
|
|
155
|
+
* @returns Returned value of `method`
|
|
156
|
+
*/
|
|
157
|
+
function defaultIt(text, method) {
|
|
158
|
+
try {
|
|
159
|
+
return method.call(this);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
throw err instanceof AssertionError && (err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`), err;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
let it = typeof globalObj.it == "function" ? globalObj.it : defaultIt, itOnly = it !== defaultIt && typeof it.only == "function" ? it.only.bind(it) : null;
|
|
165
|
+
/**
|
|
166
|
+
* Get `it` function.
|
|
167
|
+
* @param only - `true` if `it.only` should be used
|
|
168
|
+
* @throws {Error} If `it.only` is not available
|
|
169
|
+
* @returns `it` or `it.only` function
|
|
170
|
+
*/
|
|
171
|
+
function getIt(only) {
|
|
172
|
+
return only ? getItOnly() : it;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get `it.only` function.
|
|
176
|
+
* @throws {Error} If `it.only` is not available
|
|
177
|
+
* @returns `it.only` function
|
|
178
|
+
*/
|
|
179
|
+
function getItOnly() {
|
|
180
|
+
if (itOnly === null) throw Error("To use `only`, use `RuleTester` with a test framework that provides `it.only()` like Mocha, or provide a custom `it.only` function by assigning it to `RuleTester.itOnly`");
|
|
181
|
+
return itOnly;
|
|
182
|
+
}
|
|
183
|
+
const EMPTY_LANGUAGE_OPTIONS = {};
|
|
184
|
+
let sharedConfig = {};
|
|
185
|
+
const TEST_CASE_PROP_KEYS = new Set([
|
|
186
|
+
"code",
|
|
187
|
+
"name",
|
|
188
|
+
"only",
|
|
189
|
+
"filename",
|
|
190
|
+
"options",
|
|
191
|
+
"settings",
|
|
192
|
+
"before",
|
|
193
|
+
"after",
|
|
194
|
+
"output",
|
|
195
|
+
"errors",
|
|
196
|
+
"__proto__"
|
|
197
|
+
]), DEFAULT_CWD = dirname(import.meta.dirname);
|
|
198
|
+
/**
|
|
199
|
+
* Utility class for testing rules.
|
|
200
|
+
*/
|
|
201
|
+
var RuleTester = class {
|
|
202
|
+
#config;
|
|
203
|
+
/**
|
|
204
|
+
* Creates a new instance of RuleTester.
|
|
205
|
+
* @param config? - Extra configuration for the tester (optional)
|
|
206
|
+
*/
|
|
207
|
+
constructor(config) {
|
|
208
|
+
if (config === void 0) config = null;
|
|
209
|
+
else if (config !== null && typeof config != "object") throw TypeError("`config` must be an object if provided");
|
|
210
|
+
this.#config = config;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Set the configuration to use for all future tests.
|
|
214
|
+
* @param config - The configuration to use
|
|
215
|
+
* @throws {TypeError} If `config` is not an object
|
|
216
|
+
*/
|
|
217
|
+
static setDefaultConfig(config) {
|
|
218
|
+
if (typeof config != "object" || !config) throw TypeError("`config` must be an object");
|
|
219
|
+
sharedConfig = config;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get the current configuration used for all tests.
|
|
223
|
+
* @returns The current configuration
|
|
224
|
+
*/
|
|
225
|
+
static getDefaultConfig() {
|
|
226
|
+
return sharedConfig;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Reset the configuration to the initial configuration of the tester removing
|
|
230
|
+
* any changes made until now.
|
|
231
|
+
* @returns {void}
|
|
232
|
+
*/
|
|
233
|
+
static resetDefaultConfig() {
|
|
234
|
+
sharedConfig = {};
|
|
235
|
+
}
|
|
236
|
+
static get describe() {
|
|
237
|
+
return describe;
|
|
238
|
+
}
|
|
239
|
+
static set describe(value) {
|
|
240
|
+
describe = value;
|
|
241
|
+
}
|
|
242
|
+
static get it() {
|
|
243
|
+
return it;
|
|
244
|
+
}
|
|
245
|
+
static set it(value) {
|
|
246
|
+
it = value, itOnly = typeof it.only == "function" ? it.only.bind(it) : null;
|
|
247
|
+
}
|
|
248
|
+
static get itOnly() {
|
|
249
|
+
return getItOnly();
|
|
250
|
+
}
|
|
251
|
+
static set itOnly(value) {
|
|
252
|
+
itOnly = value;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Add the `only` property to a test to run it in isolation.
|
|
256
|
+
* @param item - A single test to run by itself
|
|
257
|
+
* @returns The test with `only` set
|
|
258
|
+
*/
|
|
259
|
+
static only(item) {
|
|
260
|
+
return typeof item == "string" ? {
|
|
261
|
+
code: item,
|
|
262
|
+
only: !0
|
|
263
|
+
} : {
|
|
264
|
+
...item,
|
|
265
|
+
only: !0
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Adds a new rule test to execute.
|
|
270
|
+
* @param ruleName - Name of the rule to run
|
|
271
|
+
* @param rule - Rule to test
|
|
272
|
+
* @param tests - Collection of tests to run
|
|
273
|
+
* @throws {TypeError|Error} If `rule` is not an object with a `create` method,
|
|
274
|
+
* or if non-object `test`, or if a required scenario of the given type is missing
|
|
275
|
+
*/
|
|
276
|
+
run(ruleName, rule, tests) {
|
|
277
|
+
let plugin = {
|
|
278
|
+
meta: { name: "rule-to-test" },
|
|
279
|
+
rules: { [ruleName]: rule }
|
|
280
|
+
}, config = createConfigForRun(this.#config);
|
|
281
|
+
describe(ruleName, () => {
|
|
282
|
+
tests.valid.length > 0 && describe("valid", () => {
|
|
283
|
+
let seenTestCases = /* @__PURE__ */ new Set();
|
|
284
|
+
for (let test of tests.valid) typeof test == "string" && (test = { code: test }), getIt(test.only)(getTestName(test), () => {
|
|
285
|
+
runValidTestCase(test, plugin, config, seenTestCases);
|
|
286
|
+
});
|
|
287
|
+
}), tests.invalid.length > 0 && describe("invalid", () => {
|
|
288
|
+
let seenTestCases = /* @__PURE__ */ new Set();
|
|
289
|
+
for (let test of tests.invalid) getIt(test.only)(getTestName(test), () => {
|
|
290
|
+
runInvalidTestCase(test, plugin, config, seenTestCases);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
/**
|
|
297
|
+
* Run valid test case.
|
|
298
|
+
* @param test - Valid test case
|
|
299
|
+
* @param plugin - Plugin containing rule being tested
|
|
300
|
+
* @param config - Config from `RuleTester` instance
|
|
301
|
+
* @param seenTestCases - Set of serialized test cases to check for duplicates
|
|
302
|
+
* @throws {AssertionError} If the test case fails
|
|
303
|
+
*/
|
|
304
|
+
function runValidTestCase(test, plugin, config, seenTestCases) {
|
|
305
|
+
try {
|
|
306
|
+
runBeforeHook(test), assertValidTestCaseIsWellFormed(test, seenTestCases), assertValidTestCasePasses(test, plugin, config);
|
|
307
|
+
} finally {
|
|
308
|
+
runAfterHook(test);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Assert that valid test case passes.
|
|
313
|
+
* @param test - Valid test case
|
|
314
|
+
* @param plugin - Plugin containing rule being tested
|
|
315
|
+
* @param config - Config from `RuleTester` instance
|
|
316
|
+
* @throws {AssertionError} If the test case fails
|
|
317
|
+
*/
|
|
318
|
+
function assertValidTestCasePasses(test, plugin, config) {
|
|
319
|
+
test = mergeConfigIntoTestCase(test, config), assertErrorCountIsCorrect(lint(test, plugin), 0);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Run invalid test case.
|
|
323
|
+
* @param test - Invalid test case
|
|
324
|
+
* @param plugin - Plugin containing rule being tested
|
|
325
|
+
* @param config - Config from `RuleTester` instance
|
|
326
|
+
* @param seenTestCases - Set of serialized test cases to check for duplicates
|
|
327
|
+
* @throws {AssertionError} If the test case fails
|
|
328
|
+
*/
|
|
329
|
+
function runInvalidTestCase(test, plugin, config, seenTestCases) {
|
|
330
|
+
let ruleName = ObjectKeys(plugin.rules)[0];
|
|
331
|
+
try {
|
|
332
|
+
runBeforeHook(test), assertInvalidTestCaseIsWellFormed(test, seenTestCases, ruleName), assertInvalidTestCasePasses(test, plugin, config);
|
|
333
|
+
} finally {
|
|
334
|
+
runAfterHook(test);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Assert that invalid test case passes.
|
|
339
|
+
* @param test - Invalid test case
|
|
340
|
+
* @param plugin - Plugin containing rule being tested
|
|
341
|
+
* @param config - Config from `RuleTester` instance
|
|
342
|
+
* @throws {AssertionError} If the test case fails
|
|
343
|
+
*/
|
|
344
|
+
function assertInvalidTestCasePasses(test, plugin, config) {
|
|
345
|
+
test = mergeConfigIntoTestCase(test, config);
|
|
346
|
+
let diagnostics = lint(test, plugin), { errors } = test;
|
|
347
|
+
if (typeof errors == "number") assertErrorCountIsCorrect(diagnostics, errors);
|
|
348
|
+
else {
|
|
349
|
+
assertErrorCountIsCorrect(diagnostics, errors.length), diagnostics.sort((diag1, diag2) => diag1.line - diag2.line || diag1.column - diag2.column);
|
|
350
|
+
let messages = ObjectValues(plugin.rules)[0].meta?.messages ?? null;
|
|
351
|
+
for (let errorIndex = 0; errorIndex < errors.length; errorIndex++) {
|
|
352
|
+
let error = errors[errorIndex], diagnostic = diagnostics[errorIndex];
|
|
353
|
+
typeof error == "string" || error instanceof RegExp ? (assertMessageMatches(diagnostic.message, error), assert(diagnostic.suggestions === null, `Error at index ${errorIndex} has suggestions. Please convert the test error into an object and specify \`suggestions\` property on it to test suggestions`)) : (assertInvalidTestCaseMessageIsCorrect(diagnostic, error, messages), assertInvalidTestCaseLocationIsCorrect(diagnostic, error, test), ObjectHasOwn(error, "suggestions") && (error.suggestions == null ? assert(diagnostic.suggestions === null, "Rule produced suggestions") : assertSuggestionsAreCorrect(diagnostic, error, messages, test)));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
let { code } = test, eslintCompat = test.eslintCompat === !0, fixedCode = runFixes(diagnostics, code, eslintCompat);
|
|
357
|
+
fixedCode === null && (fixedCode = code);
|
|
358
|
+
let { recursive } = test, extraPassCount = typeof recursive == "number" ? recursive : recursive === !0 ? 10 : 0;
|
|
359
|
+
if (extraPassCount > 0 && fixedCode !== code) for (let pass = 0; pass < extraPassCount; pass++) {
|
|
360
|
+
let newFixedCode = runFixes(lint({
|
|
361
|
+
...test,
|
|
362
|
+
code: fixedCode
|
|
363
|
+
}, plugin), fixedCode, eslintCompat);
|
|
364
|
+
if (newFixedCode === null) break;
|
|
365
|
+
fixedCode = newFixedCode;
|
|
366
|
+
}
|
|
367
|
+
if (ObjectHasOwn(test, "output")) {
|
|
368
|
+
let expectedOutput = test.output;
|
|
369
|
+
expectedOutput === null ? assert.strictEqual(fixedCode, code, "Expected no autofixes to be suggested") : (assert.strictEqual(fixedCode, expectedOutput, "Output is incorrect"), assert.notStrictEqual(code, expectedOutput, "Test property `output` matches `code`. If no autofix is expected, set output to `null`."));
|
|
370
|
+
} else assert.strictEqual(fixedCode, code, "The rule fixed the code. Please add `output` property.");
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Run fixes on code and return fixed code.
|
|
374
|
+
* If no fixes to apply, returns `null`.
|
|
375
|
+
*
|
|
376
|
+
* @param diagnostics - Array of `Diagnostic`s returned by `lint`
|
|
377
|
+
* @param code - Code to run fixes on
|
|
378
|
+
* @returns Fixed code, or `null` if no fixes to apply
|
|
379
|
+
* @throws {Error} If error when applying fixes
|
|
380
|
+
*/
|
|
381
|
+
function runFixes(diagnostics, code, eslintCompat) {
|
|
382
|
+
let fixGroups = [];
|
|
383
|
+
for (let diagnostic of diagnostics) diagnostic.fixes !== null && fixGroups.push(diagnostic.fixes);
|
|
384
|
+
if (fixGroups.length === 0) return null;
|
|
385
|
+
let fixedCode = applyFixes(code, JSONStringify(fixGroups), eslintCompat);
|
|
386
|
+
if (fixedCode === null) throw Error("Failed to apply fixes");
|
|
387
|
+
return fixedCode;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Assert that message reported by rule under test matches the expected message.
|
|
391
|
+
* @param diagnostic - Diagnostic emitted by rule under test
|
|
392
|
+
* @param error - Error object from test case
|
|
393
|
+
* @param messages - Messages from rule under test
|
|
394
|
+
* @throws {AssertionError} If `message` / `messageId` is not correct
|
|
395
|
+
*/
|
|
396
|
+
function assertInvalidTestCaseMessageIsCorrect(diagnostic, error, messages) {
|
|
397
|
+
if (ObjectHasOwn(error, "message")) {
|
|
398
|
+
assert(!ObjectHasOwn(error, "messageId"), "Error should not specify both `message` and a `messageId`"), assert(!ObjectHasOwn(error, "data"), "Error should not specify both `data` and `message`"), assertMessageMatches(diagnostic.message, error.message);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
assert(ObjectHasOwn(error, "messageId"), "Test error must specify either a `messageId` or `message`"), assertMessageIdIsCorrect(diagnostic.messageId, diagnostic.message, error.messageId, error.data, messages, "");
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Assert that a `messageId` used by the rule under test is correct, and validate `data` (if provided).
|
|
405
|
+
*
|
|
406
|
+
* @param reportedMessageId - `messageId` from the diagnostic or suggestion
|
|
407
|
+
* @param reportedMessage - Message from the diagnostic or suggestion
|
|
408
|
+
* @param messageId - Expected `messageId` from the test case
|
|
409
|
+
* @param data - Data from the test case (if provided)
|
|
410
|
+
* @param messages - Messages from the rule under test
|
|
411
|
+
* @param prefix - Prefix for assertion error messages (e.g. "" or "Suggestion at index 0: ")
|
|
412
|
+
* @throws {AssertionError} If messageId is not correct
|
|
413
|
+
* @throws {AssertionError} If message tenplate with placeholder data inserted does not match reported message
|
|
414
|
+
*/
|
|
415
|
+
function assertMessageIdIsCorrect(reportedMessageId, reportedMessage, messageId, data, messages, prefix) {
|
|
416
|
+
if (assert(messages !== null, `${prefix}Cannot use 'messageId' if rule under test doesn't define 'meta.messages'`), !ObjectHasOwn(messages, messageId)) {
|
|
417
|
+
let legalMessageIds = `[${ObjectKeys(messages).map((key) => `'${key}'`).join(", ")}]`;
|
|
418
|
+
assert.fail(`${prefix}Invalid messageId '${messageId}'. Expected one of ${legalMessageIds}.`);
|
|
419
|
+
}
|
|
420
|
+
assert.strictEqual(reportedMessageId, messageId, `${prefix}messageId '${reportedMessageId}' does not match expected messageId '${messageId}'`);
|
|
421
|
+
let ruleMessage = messages[messageId], unsubstitutedPlaceholders = getUnsubstitutedMessagePlaceholders(reportedMessage, ruleMessage, data);
|
|
422
|
+
if (unsubstitutedPlaceholders.length !== 0 && assert.fail(`${prefix}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.`), data !== void 0) {
|
|
423
|
+
let rehydratedMessage = replacePlaceholders(ruleMessage, data);
|
|
424
|
+
assert.strictEqual(reportedMessage, rehydratedMessage, `${prefix}Hydrated message "${rehydratedMessage}" does not match "${reportedMessage}"`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Assert that location reported by rule under test matches the expected location.
|
|
429
|
+
* @param diagnostic - Diagnostic emitted by rule under test
|
|
430
|
+
* @param error - Error object from test case
|
|
431
|
+
* @param config - Config for this test case
|
|
432
|
+
* @throws {AssertionError} If diagnostic's location does not match expected location
|
|
433
|
+
*/
|
|
434
|
+
function assertInvalidTestCaseLocationIsCorrect(diagnostic, error, test) {
|
|
435
|
+
let actualLocation = {}, expectedLocation = {}, columnOffset = test.eslintCompat === !0 ? 1 : 0;
|
|
436
|
+
ObjectHasOwn(error, "line") && (actualLocation.line = diagnostic.line, expectedLocation.line = error.line), ObjectHasOwn(error, "column") && (actualLocation.column = diagnostic.column + columnOffset, expectedLocation.column = error.column);
|
|
437
|
+
let canVoidEndLocation = test.eslintCompat === !0 && diagnostic.endLine === diagnostic.line && diagnostic.endColumn === diagnostic.column;
|
|
438
|
+
ObjectHasOwn(error, "endLine") && (error.endLine === void 0 && canVoidEndLocation ? actualLocation.endLine = void 0 : actualLocation.endLine = diagnostic.endLine, expectedLocation.endLine = error.endLine), ObjectHasOwn(error, "endColumn") && (error.endColumn === void 0 && canVoidEndLocation ? actualLocation.endColumn = void 0 : actualLocation.endColumn = diagnostic.endColumn + columnOffset, expectedLocation.endColumn = error.endColumn), ObjectKeys(expectedLocation).length > 0 && assert.deepStrictEqual(actualLocation, expectedLocation, "Actual error location does not match expected error location.");
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Assert that suggestions reported by the rule under test match expected suggestions.
|
|
442
|
+
* @param diagnostic - Diagnostic emitted by the rule under test
|
|
443
|
+
* @param error - Error object from the test case
|
|
444
|
+
* @param messages - Messages from the rule under test
|
|
445
|
+
* @param test - Test case
|
|
446
|
+
* @throws {AssertionError} If suggestions do not match
|
|
447
|
+
*/
|
|
448
|
+
function assertSuggestionsAreCorrect(diagnostic, error, messages, test) {
|
|
449
|
+
let actualSuggestions = diagnostic.suggestions ?? [], expectedSuggestions = error.suggestions;
|
|
450
|
+
assert.strictEqual(actualSuggestions.length, expectedSuggestions.length, `Error should have ${expectedSuggestions.length} suggestion${expectedSuggestions.length > 1 ? "s" : ""}. Instead found ${actualSuggestions.length} suggestion${actualSuggestions.length > 1 ? "s" : ""}.`);
|
|
451
|
+
let eslintCompat = test.eslintCompat === !0;
|
|
452
|
+
for (let i = 0; i < expectedSuggestions.length; i++) {
|
|
453
|
+
let actual = actualSuggestions[i], expected = expectedSuggestions[i], prefix = `Suggestion at index ${i}`;
|
|
454
|
+
assertSuggestionMessageIsCorrect(actual, expected, messages, prefix), assert(ObjectHasOwn(expected, "output"), `${prefix}: \`output\` property is required`);
|
|
455
|
+
let suggestedCode = applyFixes(test.code, JSONStringify([actual.fixes]), eslintCompat);
|
|
456
|
+
assert(suggestedCode !== null, `${prefix}: Failed to apply suggestion fix`), assert.strictEqual(suggestedCode, expected.output, `${prefix}: Expected the applied suggestion fix to match the test suggestion output`), assert.notStrictEqual(expected.output, test.code, `${prefix}: The output of a suggestion should differ from the original source code`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Assert that a suggestion's message matches expectations.
|
|
461
|
+
* @param actual - Actual suggestion from the diagnostic
|
|
462
|
+
* @param expected - Expected suggestion from the test case
|
|
463
|
+
* @param messages - Messages from the rule under test
|
|
464
|
+
* @param prefix - Prefix for assertion error messages
|
|
465
|
+
* @throws {AssertionError} If suggestion message does not match
|
|
466
|
+
*/
|
|
467
|
+
function assertSuggestionMessageIsCorrect(actual, expected, messages, prefix) {
|
|
468
|
+
if (ObjectHasOwn(expected, "desc")) {
|
|
469
|
+
assert(!ObjectHasOwn(expected, "messageId"), `${prefix}: Test should not specify both \`desc\` and \`messageId\``), assert(!ObjectHasOwn(expected, "data"), `${prefix}: Test should not specify both \`desc\` and \`data\``), assert.strictEqual(actual.message, expected.desc, `${prefix}: \`desc\` should be "${expected.desc}" but got "${actual.message}" instead`);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
if (ObjectHasOwn(expected, "messageId")) {
|
|
473
|
+
assertMessageIdIsCorrect(actual.messageId, actual.message, expected.messageId, expected.data, messages, `${prefix}: `);
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
ObjectHasOwn(expected, "data") && assert.fail(`${prefix}: Test must specify \`messageId\` if \`data\` is used`), assert.fail(`${prefix}: Test must specify either \`messageId\` or \`desc\``);
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Assert that the number of errors reported for test case is as expected.
|
|
480
|
+
* @param diagnostics - Diagnostics reported by the rule under test
|
|
481
|
+
* @param expectedErrorCount - Expected number of diagnistics
|
|
482
|
+
* @throws {AssertionError} If the number of diagnostics is not as expected
|
|
483
|
+
*/
|
|
484
|
+
function assertErrorCountIsCorrect(diagnostics, expectedErrorCount) {
|
|
485
|
+
diagnostics.length !== expectedErrorCount && assert.strictEqual(diagnostics.length, expectedErrorCount, util.format("Should have %s error%s but had %d: %s", expectedErrorCount === 0 ? "no" : expectedErrorCount, expectedErrorCount === 1 ? "" : "s", diagnostics.length, util.inspect(diagnostics)));
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Assert that message is matched by matcher.
|
|
489
|
+
* Matcher can be a string or a regular expression.
|
|
490
|
+
* @param message - Message
|
|
491
|
+
* @param matcher - Matcher
|
|
492
|
+
* @throws {AssertionError} If message does not match
|
|
493
|
+
*/
|
|
494
|
+
function assertMessageMatches(message, matcher) {
|
|
495
|
+
typeof matcher == "string" ? assert.strictEqual(message, matcher) : assert(matcher.test(message), `Expected '${message}' to match ${matcher}`);
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Get placeholders in the reported messages but only includes the placeholders available in the raw message
|
|
499
|
+
* and not in the provided data.
|
|
500
|
+
* @param message - Reported message
|
|
501
|
+
* @param raw - Raw message specified in the rule's `meta.messages`
|
|
502
|
+
* @param data - Data from the test case's error object
|
|
503
|
+
* @returns Missing placeholder names
|
|
504
|
+
*/
|
|
505
|
+
function getUnsubstitutedMessagePlaceholders(message, raw, data) {
|
|
506
|
+
let unsubstituted = getMessagePlaceholders(message);
|
|
507
|
+
if (unsubstituted.length === 0) return [];
|
|
508
|
+
let known = getMessagePlaceholders(raw), provided = data === void 0 ? [] : ObjectKeys(data);
|
|
509
|
+
return unsubstituted.filter((name) => known.includes(name) && !provided.includes(name));
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Extract names of `{{ name }}` placeholders from a message.
|
|
513
|
+
* @param message - Message
|
|
514
|
+
* @returns Array of placeholder names
|
|
515
|
+
*/
|
|
516
|
+
function getMessagePlaceholders(message) {
|
|
517
|
+
return ArrayFrom(message.matchAll(PLACEHOLDER_REGEX), ([, name]) => name.trim());
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Create config for a test run.
|
|
521
|
+
* Merges config from `RuleTester` instance on top of shared config.
|
|
522
|
+
* Removes properties which are not allowed in `Config`s, as they can only be properties of `TestCase`.
|
|
523
|
+
*
|
|
524
|
+
* @param config - Config from `RuleTester` instance
|
|
525
|
+
* @returns Merged config
|
|
526
|
+
*/
|
|
527
|
+
function createConfigForRun(config) {
|
|
528
|
+
let merged = {};
|
|
529
|
+
return addConfigPropsFrom(sharedConfig, merged), config !== null && addConfigPropsFrom(config, merged), merged;
|
|
530
|
+
}
|
|
531
|
+
function addConfigPropsFrom(config, merged) {
|
|
532
|
+
for (let key of ObjectKeys(config)) TEST_CASE_PROP_KEYS.has(key) || (key === "languageOptions" ? merged.languageOptions = mergeLanguageOptions(config.languageOptions, merged.languageOptions) : merged[key] = config[key]);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Create config for a test case.
|
|
536
|
+
* Merges properties of test case on top of config from `RuleTester` instance.
|
|
537
|
+
*
|
|
538
|
+
* @param test - Test case
|
|
539
|
+
* @param config - Config from `RuleTester` instance / shared config
|
|
540
|
+
* @returns Merged config
|
|
541
|
+
*/
|
|
542
|
+
function mergeConfigIntoTestCase(test, config) {
|
|
543
|
+
return {
|
|
544
|
+
...config,
|
|
545
|
+
...test,
|
|
546
|
+
languageOptions: mergeLanguageOptions(test.languageOptions, config.languageOptions)
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Merge language options from test case / config onto language options from base config.
|
|
551
|
+
* @param localLanguageOptions - Language options from test case / config
|
|
552
|
+
* @param baseLanguageOptions - Language options from base config
|
|
553
|
+
* @returns Merged language options, or `undefined` if neither has language options
|
|
554
|
+
*/
|
|
555
|
+
function mergeLanguageOptions(localLanguageOptions, baseLanguageOptions) {
|
|
556
|
+
return localLanguageOptions == null ? baseLanguageOptions ?? void 0 : baseLanguageOptions == null ? localLanguageOptions : {
|
|
557
|
+
...baseLanguageOptions,
|
|
558
|
+
...localLanguageOptions,
|
|
559
|
+
parserOptions: mergeParserOptions(localLanguageOptions.parserOptions, baseLanguageOptions.parserOptions),
|
|
560
|
+
globals: mergeGlobals(localLanguageOptions.globals, baseLanguageOptions.globals)
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Merge parser options from test case / config onto language options from base config.
|
|
565
|
+
* @param localParserOptions - Parser options from test case / config
|
|
566
|
+
* @param baseParserOptions - Parser options from base config
|
|
567
|
+
* @returns Merged parser options, or `undefined` if neither has parser options
|
|
568
|
+
*/
|
|
569
|
+
function mergeParserOptions(localParserOptions, baseParserOptions) {
|
|
570
|
+
return localParserOptions == null ? baseParserOptions ?? void 0 : baseParserOptions == null ? localParserOptions : {
|
|
571
|
+
...baseParserOptions,
|
|
572
|
+
...localParserOptions,
|
|
573
|
+
ecmaFeatures: mergeEcmaFeatures(localParserOptions.ecmaFeatures, baseParserOptions.ecmaFeatures)
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Merge ecma features from test case / config onto ecma features from base config.
|
|
578
|
+
* @param localEcmaFeatures - Ecma features from test case / config
|
|
579
|
+
* @param baseEcmaFeatures - Ecma features from base config
|
|
580
|
+
* @returns Merged ecma features, or `undefined` if neither has ecma features
|
|
581
|
+
*/
|
|
582
|
+
function mergeEcmaFeatures(localEcmaFeatures, baseEcmaFeatures) {
|
|
583
|
+
return localEcmaFeatures == null ? baseEcmaFeatures ?? void 0 : baseEcmaFeatures == null ? localEcmaFeatures : {
|
|
584
|
+
...baseEcmaFeatures,
|
|
585
|
+
...localEcmaFeatures
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Merge globals from test case / config onto globals from base config.
|
|
590
|
+
* @param localGlobals - Globals from test case / config
|
|
591
|
+
* @param baseGlobals - Globals from base config
|
|
592
|
+
* @returns Merged globals
|
|
593
|
+
*/
|
|
594
|
+
function mergeGlobals(localGlobals, baseGlobals) {
|
|
595
|
+
return localGlobals == null ? baseGlobals ?? void 0 : baseGlobals == null ? localGlobals : {
|
|
596
|
+
...baseGlobals,
|
|
597
|
+
...localGlobals
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Lint a test case.
|
|
602
|
+
* @param test - Test case
|
|
603
|
+
* @param plugin - Plugin containing rule being tested
|
|
604
|
+
* @returns Array of diagnostics
|
|
605
|
+
*/
|
|
606
|
+
function lint(test, plugin) {
|
|
607
|
+
let parseOptions = getParseOptions(test), path, { filename, cwd } = test;
|
|
608
|
+
if (filename != null && isAbsolute(filename)) cwd ??= dirname(filename), path = filename;
|
|
609
|
+
else {
|
|
610
|
+
if (filename == null) {
|
|
611
|
+
let ext = parseOptions.lang;
|
|
612
|
+
ext == null ? ext = "js" : ext === "dts" && (ext = "d.ts"), filename = `file.${ext}`;
|
|
613
|
+
}
|
|
614
|
+
cwd ??= DEFAULT_CWD, path = join(cwd, filename);
|
|
615
|
+
}
|
|
616
|
+
try {
|
|
617
|
+
registerPlugin(plugin, null, !1, null);
|
|
618
|
+
let optionsId = setupOptions(test, cwd);
|
|
619
|
+
parse(path, test.code, parseOptions);
|
|
620
|
+
let globalsJSON = getGlobalsJson(test), settingsJSON = JSONStringify(test.settings ?? {});
|
|
621
|
+
lintFileImpl(path, 0, null, [0], [optionsId], settingsJSON, globalsJSON, null);
|
|
622
|
+
let ruleId = `${plugin.meta.name}/${ObjectKeys(plugin.rules)[0]}`;
|
|
623
|
+
return diagnostics.map((diagnostic) => {
|
|
624
|
+
let line, column, endLine, endColumn;
|
|
625
|
+
({line, column} = getLineColumnFromOffset(diagnostic.start)), {line: endLine, column: endColumn} = getLineColumnFromOffset(diagnostic.end);
|
|
626
|
+
let node = getNodeByRangeIndex(diagnostic.start);
|
|
627
|
+
return {
|
|
628
|
+
ruleId,
|
|
629
|
+
message: diagnostic.message,
|
|
630
|
+
messageId: diagnostic.messageId,
|
|
631
|
+
severity: 1,
|
|
632
|
+
nodeType: node === null ? null : node.type,
|
|
633
|
+
line,
|
|
634
|
+
column,
|
|
635
|
+
endLine,
|
|
636
|
+
endColumn,
|
|
637
|
+
fixes: diagnostic.fixes,
|
|
638
|
+
suggestions: diagnostic.suggestions
|
|
639
|
+
};
|
|
640
|
+
});
|
|
641
|
+
} finally {
|
|
642
|
+
registeredRules.length = 0, allOptions !== null && (allOptions.length = 1), resetStateAfterError();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Get parse options for a test case.
|
|
647
|
+
* @param test - Test case
|
|
648
|
+
* @returns Parse options
|
|
649
|
+
*/
|
|
650
|
+
function getParseOptions(test) {
|
|
651
|
+
let parseOptions = {}, languageOptions = test.languageOptions;
|
|
652
|
+
if (languageOptions ??= EMPTY_LANGUAGE_OPTIONS, languageOptions.parser != null) throw Error("Custom parsers are not supported");
|
|
653
|
+
let { sourceType } = languageOptions;
|
|
654
|
+
if (sourceType != null) {
|
|
655
|
+
if (test.eslintCompat === !0 && sourceType === "unambiguous") throw Error("'unambiguous' source type is not supported in ESLint compatibility mode.\nDisable ESLint compatibility mode by setting `eslintCompat` to `false` in the config / test case.");
|
|
656
|
+
parseOptions.sourceType = sourceType;
|
|
657
|
+
} else test.eslintCompat === !0 && (parseOptions.sourceType = "module");
|
|
658
|
+
let { parserOptions } = languageOptions;
|
|
659
|
+
if (parserOptions != null && (parserOptions.ignoreNonFatalErrors === !0 && (parseOptions.ignoreNonFatalErrors = !0), test.filename == null)) {
|
|
660
|
+
let { lang } = parserOptions;
|
|
661
|
+
lang == null ? parserOptions.ecmaFeatures?.jsx === !0 && (parseOptions.lang = "jsx") : parseOptions.lang = lang;
|
|
662
|
+
}
|
|
663
|
+
return parseOptions;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Get globals and envs as JSON for test case.
|
|
667
|
+
*
|
|
668
|
+
* Normalizes globals values to "readonly", "writable", or "off", same as Rust side does.
|
|
669
|
+
* `null` is only supported in ESLint compatibility mode.
|
|
670
|
+
*
|
|
671
|
+
* Removes envs which are false, same as Rust side does.
|
|
672
|
+
*
|
|
673
|
+
* @param test - Test case
|
|
674
|
+
* @returns Globals and envs as JSON string of form `{ "globals": { ... }, "envs": { ... } }`
|
|
675
|
+
*/
|
|
676
|
+
function getGlobalsJson(test) {
|
|
677
|
+
let globals = { ...test.languageOptions?.globals }, eslintCompat = !!test.eslintCompat;
|
|
678
|
+
for (let key in globals) {
|
|
679
|
+
let value = globals[key];
|
|
680
|
+
switch (value) {
|
|
681
|
+
case "readonly":
|
|
682
|
+
case "writable":
|
|
683
|
+
case "off": continue;
|
|
684
|
+
case "writeable":
|
|
685
|
+
case "true":
|
|
686
|
+
case !0:
|
|
687
|
+
value = "writable";
|
|
688
|
+
break;
|
|
689
|
+
case "readable":
|
|
690
|
+
case "false":
|
|
691
|
+
case !1:
|
|
692
|
+
value = "readonly";
|
|
693
|
+
break;
|
|
694
|
+
case null: if (eslintCompat) {
|
|
695
|
+
value = "readonly";
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
default: throw Error(`'${value}' is not a valid configuration for a global (use 'readonly', 'writable', or 'off')`);
|
|
699
|
+
}
|
|
700
|
+
globals[key] = value;
|
|
701
|
+
}
|
|
702
|
+
let originalEnvs = test.languageOptions?.env, envs = {};
|
|
703
|
+
if (originalEnvs != null) for (let [key, value] of ObjectEntries(originalEnvs)) value !== !1 && ObjectDefineProperty(envs, key, {
|
|
704
|
+
value: !0,
|
|
705
|
+
writable: !0,
|
|
706
|
+
enumerable: !0,
|
|
707
|
+
configurable: !0
|
|
708
|
+
});
|
|
709
|
+
return JSONStringify({
|
|
710
|
+
globals,
|
|
711
|
+
envs
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Set up options for the test case.
|
|
716
|
+
*
|
|
717
|
+
* In linter, all options for all rules are sent over from Rust as a JSON string,
|
|
718
|
+
* and `setOptions` is called to merge them with the default options for each rule.
|
|
719
|
+
* The merged options are stored in a global variable `allOptions`.
|
|
720
|
+
*
|
|
721
|
+
* This function builds a JSON string in same format as Rust does, and calls `setOptions` with it.
|
|
722
|
+
*
|
|
723
|
+
* Returns the options ID to pass to `lintFileImpl` (either 0 for default options, or 1 for user-provided options).
|
|
724
|
+
*
|
|
725
|
+
* @param test - Test case
|
|
726
|
+
* @param cwd - Current working directory for test case
|
|
727
|
+
* @returns Options ID to pass to `lintFileImpl`
|
|
728
|
+
*/
|
|
729
|
+
function setupOptions(test, cwd) {
|
|
730
|
+
let allOptions = [[]], allRuleIds = [0], optionsId = 0, testOptions = test.options;
|
|
731
|
+
testOptions != null && (allOptions.push(testOptions), allRuleIds.push(0), optionsId = 1);
|
|
732
|
+
let allOptionsJson;
|
|
733
|
+
try {
|
|
734
|
+
allOptionsJson = JSONStringify({
|
|
735
|
+
options: allOptions,
|
|
736
|
+
ruleIds: allRuleIds,
|
|
737
|
+
cwd,
|
|
738
|
+
workspaceUri: null
|
|
739
|
+
});
|
|
740
|
+
} catch (err) {
|
|
741
|
+
throw Error(`Failed to serialize options: ${err}`);
|
|
742
|
+
}
|
|
743
|
+
return setOptions(allOptionsJson), optionsId;
|
|
744
|
+
}
|
|
745
|
+
const CONTROL_CHAR_REGEX = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/gu;
|
|
746
|
+
/**
|
|
747
|
+
* Get name of test case.
|
|
748
|
+
* Control characters in name are replaced with `\u00xx` form.
|
|
749
|
+
* @param test - Test case
|
|
750
|
+
* @returns Name of test case
|
|
751
|
+
*/
|
|
752
|
+
function getTestName(test) {
|
|
753
|
+
let name = test.name || test.code;
|
|
754
|
+
return typeof name == "string" ? name.replace(CONTROL_CHAR_REGEX, (c) => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}`) : "";
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Runs before hook on the given test case.
|
|
758
|
+
* @param test - Test to run the hook on
|
|
759
|
+
* @throws {Error} - If the hook is not a function
|
|
760
|
+
* @throws {*} - Value thrown by the hook function
|
|
761
|
+
*/
|
|
762
|
+
function runBeforeHook(test) {
|
|
763
|
+
ObjectHasOwn(test, "before") && runHook(test, test.before, "before");
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Runs after hook on the given test case.
|
|
767
|
+
* @param test - Test to run the hook on
|
|
768
|
+
* @throws {Error} - If the hook is not a function
|
|
769
|
+
* @throws {*} - Value thrown by the hook function
|
|
770
|
+
*/
|
|
771
|
+
function runAfterHook(test) {
|
|
772
|
+
ObjectHasOwn(test, "after") && runHook(test, test.after, "after");
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Runs a hook on the given test case.
|
|
776
|
+
* @param test - Test to run the hook on
|
|
777
|
+
* @param hook - Hook function
|
|
778
|
+
* @param name - Name of the hook
|
|
779
|
+
* @throws {Error} - If the property is not a function
|
|
780
|
+
* @throws {*} - Value thrown by the hook function
|
|
781
|
+
*/
|
|
782
|
+
function runHook(test, hook, name) {
|
|
783
|
+
assert.strictEqual(typeof hook, "function", `Optional test case property \`${name}\` must be a function`), hook.call(test);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Assert that a valid test case object is valid.
|
|
787
|
+
* A valid test case must specify a string value for `code`.
|
|
788
|
+
* Optional properties are checked for correct types.
|
|
789
|
+
*
|
|
790
|
+
* @param test - Valid test case object to check
|
|
791
|
+
* @param seenTestCases - Set of serialized test cases to check for duplicates
|
|
792
|
+
* @throws {AssertionError} If the test case is not valid
|
|
793
|
+
*/
|
|
794
|
+
function assertValidTestCaseIsWellFormed(test, seenTestCases) {
|
|
795
|
+
assertTestCaseCommonPropertiesAreWellFormed(test), assert(!("errors" in test) || test.errors === void 0, "Valid test case must not have `errors` property"), assert(!("output" in test) || test.output === void 0, "Valid test case must not have `output` property"), assertNotDuplicateTestCase(test, seenTestCases);
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Assert that an invalid test case object is valid.
|
|
799
|
+
* An invalid test case must specify a string value for `code` and must have an `errors` property.
|
|
800
|
+
* Optional properties are checked for correct types.
|
|
801
|
+
*
|
|
802
|
+
* @param test - Invalid test case object to check
|
|
803
|
+
* @param seenTestCases - Set of serialized test cases to check for duplicates
|
|
804
|
+
* @param ruleName - Name of the rule being tested
|
|
805
|
+
* @throws {AssertionError} If the test case is not valid
|
|
806
|
+
*/
|
|
807
|
+
function assertInvalidTestCaseIsWellFormed(test, seenTestCases, ruleName) {
|
|
808
|
+
assertTestCaseCommonPropertiesAreWellFormed(test);
|
|
809
|
+
let { errors } = test;
|
|
810
|
+
typeof errors == "number" ? assert(errors > 0, "Invalid cases must have `errors` value greater than 0") : (assert(errors !== void 0, `Did not specify errors for an invalid test of rule \`${ruleName}\``), assert(ArrayIsArray(errors), `Invalid 'errors' property for invalid test of rule \`${ruleName}\`:expected a number or an array but got ${errors === null ? "null" : typeof errors}`), assert(errors.length !== 0, "Invalid cases must have at least one error")), ObjectHasOwn(test, "output") && assert(test.output === null || typeof test.output == "string", "Test property `output`, if specified, must be a string or null. If no autofix is expected, then omit the `output` property or set it to null."), assertNotDuplicateTestCase(test, seenTestCases);
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Assert that the common properties of a valid/invalid test case have the correct types.
|
|
814
|
+
* @param {Object} test - Test case object to check
|
|
815
|
+
* @throws {AssertionError} If the test case is not valid
|
|
816
|
+
*/
|
|
817
|
+
function assertTestCaseCommonPropertiesAreWellFormed(test) {
|
|
818
|
+
assert(typeof test.code == "string", "Test case must specify a string value for `code`"), test.name && assert(typeof test.name == "string", "Optional test case property `name` must be a string"), ObjectHasOwn(test, "only") && assert(typeof test.only == "boolean", "Optional test case property `only` must be a boolean"), ObjectHasOwn(test, "filename") && assert(typeof test.filename == "string", "Optional test case property `filename` must be a string"), ObjectHasOwn(test, "options") && assert(ArrayIsArray(test.options), "Optional test case property `options` must be an array");
|
|
819
|
+
}
|
|
820
|
+
const DUPLICATION_IGNORED_PROPS = new Set([
|
|
821
|
+
"name",
|
|
822
|
+
"errors",
|
|
823
|
+
"output"
|
|
824
|
+
]);
|
|
825
|
+
/**
|
|
826
|
+
* Assert that this test case is not a duplicate of one we have seen before.
|
|
827
|
+
* @param test - Test case object
|
|
828
|
+
* @param seenTestCases - Set of serialized test cases we have seen so far (managed by this function)
|
|
829
|
+
* @throws {AssertionError} If the test case is a duplicate
|
|
830
|
+
*/
|
|
831
|
+
function assertNotDuplicateTestCase(test, seenTestCases) {
|
|
832
|
+
if (!isSerializable(test)) return;
|
|
833
|
+
let serializedTestCase = (0, import_json_stable_stringify_without_jsonify.default)(test, { replacer(key, value) {
|
|
834
|
+
return test !== this || !DUPLICATION_IGNORED_PROPS.has(key) ? value : void 0;
|
|
835
|
+
} });
|
|
836
|
+
assert(!seenTestCases.has(serializedTestCase), "Detected duplicate test case"), seenTestCases.add(serializedTestCase);
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Check if a value is serializable.
|
|
840
|
+
* Functions or objects like RegExp cannot be serialized by JSON.stringify().
|
|
841
|
+
* Inspired by: https://stackoverflow.com/questions/30579940/reliable-way-to-check-if-objects-is-serializable-in-javascript
|
|
842
|
+
* @param value - Value
|
|
843
|
+
* @param seenObjects - Objects already seen in this path from the root object.
|
|
844
|
+
* @returns {boolean} `true` if the value is serializable
|
|
845
|
+
*/
|
|
846
|
+
function isSerializable(value, seenObjects = /* @__PURE__ */ new Set()) {
|
|
847
|
+
if (!isSerializablePrimitiveOrPlainObject(value)) return !1;
|
|
848
|
+
if (typeof value != "object" || !value) return !0;
|
|
849
|
+
if (seenObjects.has(value)) return !1;
|
|
850
|
+
for (let property in value) {
|
|
851
|
+
if (!ObjectHasOwn(value, property)) continue;
|
|
852
|
+
let prop = value[property];
|
|
853
|
+
if (!isSerializablePrimitiveOrPlainObject(prop) || !(typeof prop != "object" || !prop) && !isSerializable(prop, new Set([...seenObjects, value]))) return !1;
|
|
854
|
+
}
|
|
855
|
+
return !0;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Check if a value is a primitive or plain object created by the `Object` constructor.
|
|
859
|
+
* @param value - Value to check
|
|
860
|
+
* @returns `true` if `value` is a primitive or plain object
|
|
861
|
+
*/
|
|
862
|
+
function isSerializablePrimitiveOrPlainObject(value) {
|
|
863
|
+
return value === null || typeof value == "string" || typeof value == "boolean" || typeof value == "number" || typeof value == "object" && (value.constructor === Object || ArrayIsArray(value));
|
|
864
|
+
}
|
|
865
|
+
//#endregion
|
|
866
|
+
export { RuleTester };
|