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

Commit ad79020

Browse files
fix: set cwd based on ESLint directory; fixes #13
Co-authored-by: Andrew Dupont <github@andrewdupont.net>
1 parent d97c17f commit ad79020

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

lib/worker.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ async function processMessage (bundle) {
365365
return;
366366
}
367367

368+
const oldCwd = process.cwd();
369+
process.chdir(eslint.cwd);
370+
368371
if (type === 'debug') {
369372
let { eslintPath, eslintVersion, isBuiltIn } = eslint;
370373
let isIncompatible = compareVersions(eslintVersion, MINIMUM_ESLINT_VERSION) < 1;
@@ -379,6 +382,7 @@ async function processMessage (bundle) {
379382
isBuiltIn,
380383
workerPid: process.pid
381384
});
385+
process.chdir(oldCwd);
382386
return;
383387
}
384388

@@ -417,6 +421,8 @@ async function processMessage (bundle) {
417421
emitError(
418422
JSON.stringify(error, Object.getOwnPropertyNames(error))
419423
);
424+
} finally {
425+
process.chdir(oldCwd);
420426
}
421427
}
422428

spec/fixtures/dynamic-cwd/.eslintignore

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { join } = require('path');
2+
3+
const getEcmaVersionForNodeVersion = (nodeVersion) => {
4+
if (nodeVersion === '16.0.0') {
5+
return 'es2021';
6+
}
7+
8+
// Would really need to do more detection
9+
return 'es2015';
10+
};
11+
12+
const detectNodeVersion = (packagePath) => {
13+
const { engines: { node } } = require(packagePath);
14+
15+
// Would really want to use semver.minVersion, wrap in try-catch, etc.
16+
return node;
17+
};
18+
19+
const nodeVersion = detectNodeVersion(join(process.cwd(), 'package.json'));
20+
const ecmaVersion = getEcmaVersionForNodeVersion(nodeVersion);
21+
22+
module.exports = {
23+
"root": true,
24+
"env": {
25+
[ecmaVersion]: true
26+
}
27+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let a = true;
2+
3+
a ||= false;
4+
5+
console.log(a);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"engines": {
3+
"node": "16.0.0"
4+
}
5+
}

spec/linter-eslint-node-spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const paths = {
1616
good: path.join(fixturesDir, 'files', 'with-config', 'good.js'),
1717
bad: path.join(fixturesDir, 'files', 'with-config', 'bad.js'),
1818
badInline: path.join(fixturesDir, 'files', 'inline', 'badInline.js'),
19+
dynamicCwd: path.join(fixturesDir, 'dynamic-cwd', 'logical-or-assignment.js'),
1920
empty: path.join(fixturesDir, 'files', 'with-config', 'empty.js'),
2021
fix: path.join(fixturesDir, 'files', 'with-config', 'fix.js'),
2122
cache: path.join(fixturesDir, 'files', 'with-config', '.eslintcache'),
@@ -166,6 +167,18 @@ describe('The eslint provider for Linter', () => {
166167
expect(messages.length).toBe(0);
167168
});
168169

170+
it('finds no cwd problems with a valid file (and proper dynamic ecmaVersion)', async () => {
171+
const cwd = process.cwd();
172+
173+
const editor = await atom.workspace.open(paths.dynamicCwd);
174+
const messages = await lint(editor);
175+
176+
expect(cwd).toEqual(process.cwd());
177+
process.chdir(cwd);
178+
179+
expect(messages.length).toBe(0);
180+
});
181+
169182
it('reports the fixes for fixable errors', async () => {
170183
const editor = await atom.workspace.open(paths.fix);
171184
const messages = await lint(editor);

0 commit comments

Comments
 (0)