Skip to content

Commit a475dd2

Browse files
authored
Merge pull request #2965 from lukashass/eslint-8x
[eslint-patch] Add support for ESLint 8.x
2 parents a94d78e + ad39fde commit a475dd2

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/eslint-patch",
5+
"comment": "Add support for ESLint 8.0.0",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/eslint-patch"
10+
}

stack/eslint-patch/src/modern-module-resolution.ts

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
const path = require('path');
1111
const fs = require('fs');
1212

13+
// Module path for eslintrc.cjs
14+
// Example: ".../@eslint/eslintrc/dist/eslintrc.cjs"
15+
let eslintrcBundlePath: string | undefined = undefined;
16+
1317
// Module path for config-array-factory.js
1418
// Example: ".../@eslint/eslintrc/lib/config-array-factory"
1519
let configArrayFactoryPath: string | undefined = undefined;
@@ -22,15 +26,14 @@ let moduleResolverPath: string | undefined = undefined;
2226
// Example: ".../node_modules/eslint"
2327
let eslintFolder: string | undefined = undefined;
2428

25-
// Probe for the ESLint >=7.8.0 layout:
29+
// Probe for the ESLint >=8.0.0 layout:
2630
for (let currentModule = module; ; ) {
27-
if (!configArrayFactoryPath) {
28-
// For ESLint >=7.8.0, config-array-factory.js is at this path:
29-
// .../@eslint/eslintrc/lib/config-array-factory.js
30-
if (/[\\/]@eslint[\\/]eslintrc[\\/]lib[\\/]config-array-factory\.js$/i.test(currentModule.filename)) {
31+
if (!eslintrcBundlePath) {
32+
// For ESLint >=8.0.0, all @eslint/eslintrc code is bundled at this path:
33+
// .../@eslint/eslintrc/dist/eslintrc.cjs
34+
if (/[\\/]@eslint[\\/]eslintrc[\\/]dist[\\/]eslintrc\.cjs$/i.test(currentModule.filename)) {
3135
const eslintrcFolder: string = path.join(path.dirname(currentModule.filename), '..');
32-
configArrayFactoryPath = path.join(eslintrcFolder, 'lib/config-array-factory');
33-
moduleResolverPath = path.join(eslintrcFolder, 'lib/shared/relative-module-resolver');
36+
eslintrcBundlePath = path.join(eslintrcFolder, 'dist/eslintrc.cjs');
3437
}
3538
} else {
3639
// Next look for a file in ESLint's folder
@@ -47,6 +50,33 @@ for (let currentModule = module; ; ) {
4750
currentModule = currentModule.parent;
4851
}
4952

53+
if (!eslintFolder) {
54+
// Probe for the ESLint >=7.8.0 layout:
55+
for (let currentModule = module; ; ) {
56+
if (!configArrayFactoryPath) {
57+
// For ESLint >=7.8.0, config-array-factory.js is at this path:
58+
// .../@eslint/eslintrc/lib/config-array-factory.js
59+
if (/[\\/]@eslint[\\/]eslintrc[\\/]lib[\\/]config-array-factory\.js$/i.test(currentModule.filename)) {
60+
const eslintrcFolder: string = path.join(path.dirname(currentModule.filename), '..');
61+
configArrayFactoryPath = path.join(eslintrcFolder, 'lib/config-array-factory');
62+
moduleResolverPath = path.join(eslintrcFolder, 'lib/shared/relative-module-resolver');
63+
}
64+
} else {
65+
// Next look for a file in ESLint's folder
66+
// .../eslint/lib/cli-engine/cli-engine.js
67+
if (/[\\/]eslint[\\/]lib[\\/]cli-engine[\\/]cli-engine\.js$/i.test(currentModule.filename)) {
68+
eslintFolder = path.join(path.dirname(currentModule.filename), '../..');
69+
break;
70+
}
71+
}
72+
73+
if (!currentModule.parent) {
74+
break;
75+
}
76+
currentModule = currentModule.parent;
77+
}
78+
}
79+
5080
if (!eslintFolder) {
5181
// Probe for the <7.8.0 layout:
5282
for (let currentModule = module; ; ) {
@@ -80,22 +110,30 @@ if (!versionMatch) {
80110
throw new Error('Unable to parse ESLint version: ' + eslintPackageVersion);
81111
}
82112
const eslintMajorVersion = Number(versionMatch[1]);
83-
if (!(eslintMajorVersion >= 6 && eslintMajorVersion <= 7)) {
113+
if (!(eslintMajorVersion >= 6 && eslintMajorVersion <= 8)) {
84114
throw new Error(
85-
'The patch-eslint.js script has only been tested with ESLint version 6.x or 7.x.' +
115+
'The patch-eslint.js script has only been tested with ESLint version 6.x, 7.x, and 8.x.' +
86116
` (Your version: ${eslintPackageVersion})\n` +
87117
'Consider reporting a GitHub issue:\n' +
88118
'https://github.com/microsoft/rushstack/issues'
89119
);
90120
}
91121

92-
const ConfigArrayFactory = require(configArrayFactoryPath!).ConfigArrayFactory;
93-
122+
let ConfigArrayFactory;
123+
if (eslintMajorVersion === 8) {
124+
ConfigArrayFactory = require(eslintrcBundlePath!).Legacy.ConfigArrayFactory;
125+
} else {
126+
ConfigArrayFactory = require(configArrayFactoryPath!).ConfigArrayFactory;
127+
}
94128
if (!ConfigArrayFactory.__patched) {
95129
ConfigArrayFactory.__patched = true;
96130

97-
const ModuleResolver = require(moduleResolverPath!);
98-
131+
let ModuleResolver: { resolve: any };
132+
if (eslintMajorVersion === 8) {
133+
ModuleResolver = require(eslintrcBundlePath!).Legacy.ModuleResolver;
134+
} else {
135+
ModuleResolver = require(moduleResolverPath!);
136+
}
99137
const originalLoadPlugin = ConfigArrayFactory.prototype._loadPlugin;
100138

101139
if (eslintMajorVersion === 6) {
@@ -117,7 +155,7 @@ if (!ConfigArrayFactory.__patched) {
117155
}
118156
};
119157
} else {
120-
// ESLint 7.x
158+
// ESLint 7.x || 8.x
121159
ConfigArrayFactory.prototype._loadPlugin = function (name: string, ctx: Record<string, unknown>) {
122160
const originalResolve = ModuleResolver.resolve;
123161
try {

0 commit comments

Comments
 (0)