-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: fix lazy loading of core rules (#15606)
* perf: fix lazy loading of core rules * add test
- Loading branch information
1 parent
3fc9196
commit 39a2fb3
Showing
6 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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("path"); | ||
const assert = require("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 { ESLint } = require("../.."); | ||
const eslint = new ESLint({ cwd }); | ||
|
||
await eslint.lintFiles([pattern]); | ||
})().catch(({ message, stack }) => { | ||
process.send({ message, stack }); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
root: true, | ||
rules: { | ||
semi: 2 | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* content is not necessary */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters