-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
test-lazy-loading-rules.js
66 lines (51 loc) · 1.93 KB
/
test-lazy-loading-rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @fileoverview Tests lazy-loading of core rules
* @author Milos Djermanovic
*/
/*
* This module should be run as a child process, with `fork()`,
* because it is important to run this test with a separate, clean Node process
* in order to add hooks before any of the ESLint modules is loaded.
*/
"use strict";
const path = require("node:path");
const assert = require("node:assert");
const { addHook } = require("pirates");
const {
dir: rulesDirectoryPath,
name: rulesDirectoryIndexFilename
} = path.parse(require.resolve("../../lib/rules"));
// Show full stack trace. The default 10 is usually not enough to find the root cause of this problem.
Error.stackTraceLimit = Infinity;
const [cwd, pattern, usedRulesCommaSeparated] = process.argv.slice(2);
assert.ok(cwd, "cwd argument isn't provided");
assert.ok(pattern, "pattern argument isn't provided");
assert.ok(usedRulesCommaSeparated, "used rules argument isn't provided");
const usedRules = usedRulesCommaSeparated.split(",");
// `require()` hook
addHook(
(_code, filename) => {
throw new Error(`Unexpected attempt to load unused rule ${filename}`);
},
{
// returns `true` if the hook (the function passed in as the first argument) should be called for this filename
matcher(filename) {
const { dir, name } = path.parse(filename);
if (dir === rulesDirectoryPath && ![rulesDirectoryIndexFilename, ...usedRules].includes(name)) {
return true;
}
return false;
}
}
);
/*
* Everything related to loading any ESLint modules should be in this IIFE
*/
(async () => {
const { LegacyESLint } = require("../../lib/unsupported-api");
const eslint = new LegacyESLint({ cwd });
await eslint.lintFiles([pattern]);
})().catch(({ message, stack }) => {
process.send({ message, stack });
process.exit(1); // eslint-disable-line n/no-process-exit -- this is a child process
});