Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 4d65ef9

Browse files
Add fallback-to-our-own-eslint behavior in the worker
1 parent 00c0406 commit 4d65ef9

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

lib/worker.js

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ function emitError (obj) {
4848
process.stderr.write(`${obj}\n`);
4949
}
5050

51+
function log (message) {
52+
emit({ log: message });
53+
}
54+
5155
function getPathRoot (filePath, eslintPath) {
56+
log(`getPathRoot ${filePath} // ${eslintPath}`);
5257
filePath = normalize(filePath).split(sep);
5358
eslintPath = normalize(eslintPath).split(sep);
5459

@@ -83,35 +88,26 @@ function clearESLintCache () {
8388
}
8489
}
8590

86-
function getESLint (filePath, projectPath, config, { legacyPackagePresent }) {
87-
// `eslint` will get resolved relative to the path of the file we're going to
88-
// lint — unless the user has specified their own `localNodeModules` path.
89-
let { advanced: { localNodeModules } } = config;
90-
let resolvePath = filePath;
91-
if (localNodeModules) {
92-
resolvePath = normalize([projectPath, localNodeModules].join(sep));
91+
function resolveESLint (filePath) {
92+
try {
93+
return createRequire(filePath).resolve('eslint');
94+
} catch (e) {
95+
return createRequire(__dirname).resolve('eslint');
9396
}
97+
}
9498

95-
if (!PATHS_CACHE[resolvePath]) {
96-
PATHS_CACHE[resolvePath] = createRequire(resolvePath).resolve('eslint');
99+
function getESLint (filePath, projectPath, config, { legacyPackagePresent }) {
100+
if (!PATHS_CACHE[filePath]) {
101+
PATHS_CACHE[filePath] = resolveESLint(filePath);
97102
}
98103

99-
const eslintPath = PATHS_CACHE[filePath];
100-
const eslintRootPath = eslintPath.replace(/eslint([/\\]).*?$/, 'eslint$1');
104+
let eslintPath = PATHS_CACHE[filePath];
101105

102106
if (!ESLINT_CACHE[eslintPath]) {
107+
const eslintRootPath = eslintPath.replace(/eslint([/\\]).*?$/, 'eslint$1');
103108
const packageMeta = require(join(eslintRootPath, 'package.json'));
104-
if (compareVersions(packageMeta.version, MINIMUM_ESLINT_VERSION) < 1) {
105-
// Unsupported version.
106-
throw new IncompatibleVersionError(packageMeta.version);
107-
} if ((compareVersions(packageMeta.version, '8.0.0') < 1) && legacyPackagePresent) {
108-
// We're dealing with version 7 of ESLint. The legacy `linter-eslint`
109-
// package is present and capable of linting with this version, so we
110-
// should halt instead of trying to lint everything twice.
111-
throw new VersionOverlapError(packageMeta.version);
112-
}
113109

114-
const { ESLint } = createRequire(filePath)('eslint');
110+
let { ESLint } = createRequire(eslintPath)('eslint');
115111
let commonOptions = buildCommonConstructorOptions(config);
116112

117113
// `fix` is a predicate in `commonOptions` and represents the "yes,
@@ -123,9 +119,23 @@ function getESLint (filePath, projectPath, config, { legacyPackagePresent }) {
123119
eslintFix: new ESLint({ ...commonOptions }),
124120
workingDir: getPathRoot(filePath, eslintPath),
125121
eslintPath: eslintRootPath,
126-
eslintVersion: packageMeta.version
122+
eslintVersion: packageMeta.version,
123+
isBuiltIn: eslintPath === PATHS_CACHE[__dirname]
127124
};
128125
}
126+
127+
let cache = ESLINT_CACHE[eslintPath];
128+
129+
if (compareVersions(cache.eslintVersion, MINIMUM_ESLINT_VERSION) < 1) {
130+
// Unsupported version.
131+
throw new IncompatibleVersionError(cache.eslintVersion);
132+
} else if ((compareVersions(cache.eslintVersion, '8.0.0') < 1) && legacyPackagePresent) {
133+
// We're dealing with version 7 of ESLint. The legacy `linter-eslint`
134+
// package is present and capable of linting with this version, so we
135+
// should halt instead of trying to lint everything twice.
136+
throw new VersionOverlapError(cache.eslintVersion);
137+
}
138+
129139
return ESLINT_CACHE[eslintPath];
130140
}
131141

@@ -173,7 +183,7 @@ function formatResults (files, rules, config, { isModified, key }) {
173183

174184
let idTag = '';
175185
if (showRuleIdInMessage && message.ruleId) {
176-
idTag = `[${message.ruleId}] `;
186+
idTag = ` (${message.ruleId})`;
177187
}
178188
let position;
179189
if (message.fatal) {
@@ -202,7 +212,7 @@ function formatResults (files, rules, config, { isModified, key }) {
202212
position,
203213
},
204214
fix: message.fix,
205-
excerpt: `${idTag}${message.message}`
215+
excerpt: `${message.message}${idTag}`
206216
});
207217
}
208218
}
@@ -240,14 +250,14 @@ async function processMessage (bundle) {
240250
return;
241251
}
242252

243-
if (!projectPath) {
244-
emit({
245-
key,
246-
error: `Must provide projectPath`,
247-
type: 'no-project'
248-
});
249-
return;
250-
}
253+
// if (!projectPath) {
254+
// emit({
255+
// key,
256+
// error: `Must provide projectPath`,
257+
// type: 'no-project'
258+
// });
259+
// return;
260+
// }
251261

252262
let eslint;
253263
try {
@@ -268,6 +278,7 @@ async function processMessage (bundle) {
268278
type: 'version-overlap'
269279
});
270280
} else {
281+
log(`Error: ${err.message}`);
271282
emit({
272283
key,
273284
error: `Can't find an ESLint for file: ${filePath}`,
@@ -278,7 +289,7 @@ async function processMessage (bundle) {
278289
}
279290

280291
if (type === 'debug') {
281-
let { eslintPath, eslintVersion } = eslint;
292+
let { eslintPath, eslintVersion, isBuiltIn } = eslint;
282293
let isIncompatible = compareVersions(eslintVersion, MINIMUM_ESLINT_VERSION) < 1;
283294
let isOverlap = (compareVersions(eslintVersion, '8.0.0') < 1) && !isIncompatible;
284295
emit({
@@ -287,7 +298,8 @@ async function processMessage (bundle) {
287298
eslintPath,
288299
eslintVersion,
289300
isIncompatible,
290-
isOverlap
301+
isOverlap,
302+
isBuiltIn
291303
});
292304
return;
293305
}

0 commit comments

Comments
 (0)