10
10
const path = require ( 'path' ) ;
11
11
const fs = require ( 'fs' ) ;
12
12
13
+ // Module path for eslintrc.cjs
14
+ // Example: ".../@eslint/eslintrc/dist/eslintrc.cjs"
15
+ let eslintrcBundlePath : string | undefined = undefined ;
16
+
13
17
// Module path for config-array-factory.js
14
18
// Example: ".../@eslint/eslintrc/lib/config-array-factory"
15
19
let configArrayFactoryPath : string | undefined = undefined ;
@@ -22,15 +26,14 @@ let moduleResolverPath: string | undefined = undefined;
22
26
// Example: ".../node_modules/eslint"
23
27
let eslintFolder : string | undefined = undefined ;
24
28
25
- // Probe for the ESLint >=7.8 .0 layout:
29
+ // Probe for the ESLint >=8.0 .0 layout:
26
30
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 ( / [ \\ / ] @ e s l i n t [ \\ / ] e s l i n t r c [ \\ / ] l i b [ \\ / ] c o n f i g - a r r a y - f a c t o r y \. j s $ / 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 ( / [ \\ / ] @ e s l i n t [ \\ / ] e s l i n t r c [ \\ / ] d i s t [ \\ / ] e s l i n t r c \. c j s $ / i. test ( currentModule . filename ) ) {
31
35
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' ) ;
34
37
}
35
38
} else {
36
39
// Next look for a file in ESLint's folder
@@ -47,6 +50,33 @@ for (let currentModule = module; ; ) {
47
50
currentModule = currentModule . parent ;
48
51
}
49
52
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 ( / [ \\ / ] @ e s l i n t [ \\ / ] e s l i n t r c [ \\ / ] l i b [ \\ / ] c o n f i g - a r r a y - f a c t o r y \. j s $ / 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 ( / [ \\ / ] e s l i n t [ \\ / ] l i b [ \\ / ] c l i - e n g i n e [ \\ / ] c l i - e n g i n e \. j s $ / 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
+
50
80
if ( ! eslintFolder ) {
51
81
// Probe for the <7.8.0 layout:
52
82
for ( let currentModule = module ; ; ) {
@@ -80,22 +110,30 @@ if (!versionMatch) {
80
110
throw new Error ( 'Unable to parse ESLint version: ' + eslintPackageVersion ) ;
81
111
}
82
112
const eslintMajorVersion = Number ( versionMatch [ 1 ] ) ;
83
- if ( ! ( eslintMajorVersion >= 6 && eslintMajorVersion <= 7 ) ) {
113
+ if ( ! ( eslintMajorVersion >= 6 && eslintMajorVersion <= 8 ) ) {
84
114
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.' +
86
116
` (Your version: ${ eslintPackageVersion } )\n` +
87
117
'Consider reporting a GitHub issue:\n' +
88
118
'https://github.com/microsoft/rushstack/issues'
89
119
) ;
90
120
}
91
121
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
+ }
94
128
if ( ! ConfigArrayFactory . __patched ) {
95
129
ConfigArrayFactory . __patched = true ;
96
130
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
+ }
99
137
const originalLoadPlugin = ConfigArrayFactory . prototype . _loadPlugin ;
100
138
101
139
if ( eslintMajorVersion === 6 ) {
@@ -117,7 +155,7 @@ if (!ConfigArrayFactory.__patched) {
117
155
}
118
156
} ;
119
157
} else {
120
- // ESLint 7.x
158
+ // ESLint 7.x || 8.x
121
159
ConfigArrayFactory . prototype . _loadPlugin = function ( name : string , ctx : Record < string , unknown > ) {
122
160
const originalResolve = ModuleResolver . resolve ;
123
161
try {
0 commit comments