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

Commit 49592e5

Browse files
fix: ensure we clear cache when .eslintrc changes
Also refrain from clearing cache when `.eslintrc` is removed or added from a `node_modules` path.
1 parent 98b9fa4 commit 49592e5

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

lib/main.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ function eventInvolvesFile (event, fileName) {
3434
return event.path.endsWith(fileName) || (event.oldPath && event.oldPath.endsWith(fileName));
3535
}
3636

37+
// Catch file-change events that involve any of `.eslintrc`, `.eslintrc.js`, or
38+
// `.eslintrc.yml`.
39+
function eventInvolvesEslintrc (event) {
40+
return [event.path, event.oldPath].some((p) => {
41+
return p && (
42+
p.includes(`${Path.sep}.eslintrc`) || p.startsWith(`.eslintrc`)
43+
);
44+
});
45+
}
46+
3747
export default {
3848

3949
shouldAutoFix (textEditor) {
@@ -48,7 +58,7 @@ export default {
4858
},
4959

5060
async activate () {
51-
Config.initialize();
61+
Config.initialize(atom.project.getPaths()[0]);
5262
this.workerPath = Path.join(__dirname, 'worker.js');
5363
this.jobManager = new JobManager();
5464

@@ -78,18 +88,15 @@ export default {
7888
});
7989
}
8090
),
91+
8192
// Scan for new .linter-eslint config files when project paths change.
82-
atom.project.onDidChangePaths(
83-
projectPaths => {
84-
this.projectPaths = projectPaths;
85-
Config.rescan();
86-
}
87-
),
93+
atom.project.onDidChangePaths(() => Config.rescan()),
94+
8895
// React to changes that happen either in a .linter-eslint file or the
8996
// base package settings.
9097
Config.onConfigDidChange(
9198
(config, prevConfig) => {
92-
console.debug('Config.onConfigDidChange', config, prevConfig);
99+
console.debug('Config changed:', config, prevConfig);
93100
if (helpers.configShouldInvalidateWorkerCache(config, prevConfig)) {
94101
this.clearESLintCache();
95102
}
@@ -101,11 +108,10 @@ export default {
101108

102109
if (config.nodeBin !== prevConfig.nodeBin) {
103110
this.notified.invalidNodeBin = false;
104-
console.log('Testing bin:', config.nodeBin, this.notified.invalid);
105111
NodePathTester
106-
.schedule(config.nodeBin)
112+
.test(config.nodeBin)
107113
.then((version) => {
108-
console.log(`Switched Node to version`, version);
114+
console.log(`Switched Node to version:`, version);
109115
this.jobManager.createWorker();
110116
})
111117
.catch(() => {
@@ -114,6 +120,7 @@ export default {
114120
}
115121
}
116122
),
123+
117124
atom.commands.add('atom-text-editor', {
118125
'linter-eslint-node:fix-file': async () => {
119126
await this.fixJob();
@@ -123,11 +130,13 @@ export default {
123130
}
124131
}),
125132

126-
// Keep track of `.eslintignore` updates.
127-
// TODO: If the changed file's relative project path includes
128-
// `node_modules`, ignore it.
133+
// Keep track of `.eslintignore` and `.eslintrc` updates.
129134
atom.project.onDidChangeFiles(events => {
130135
for (const event of events) {
136+
// Without this, `npm install` inside an already-open project
137+
// triggers a _bunch_ of cache clearances.
138+
if (event.path.includes('node_modules')) { return false; }
139+
131140
if (eventInvolvesFile(event, '.linter-eslint')) {
132141
Config.rescan();
133142
}
@@ -140,6 +149,10 @@ export default {
140149
if (eventInvolvesFile(event, '.eslintignore')) {
141150
this.clearESLintCache();
142151
}
152+
153+
if (eventInvolvesEslintrc(event)) {
154+
this.clearESLintCache();
155+
}
143156
}
144157
}),
145158

@@ -372,7 +385,6 @@ export default {
372385
},
373386

374387
handleError (err, type, editor = null) {
375-
console.debug('handleError:', err);
376388
if (err.name && err.name === 'InvalidWorkerError') {
377389
// The worker script never got created. Aside from notifying the user
378390
// (which will be skipped if they've already gotten such a message in
@@ -555,7 +567,7 @@ export default {
555567
filteredResults.push(result);
556568
}
557569

558-
console.log('RETURNED RESULTS:', filteredResults);
570+
console.debug('Linting results:', filteredResults);
559571
return filteredResults;
560572
} catch (err) {
561573
return this.handleError(err, 'lint', textEditor);

0 commit comments

Comments
 (0)