Skip to content

Commit a2b28c4

Browse files
authored
find correct Linter instance in a workspace with multiple directories (#97)
1 parent 994a537 commit a2b28c4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/processor_options.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
export const processor_options = {};
22

33
// find Linter instance
4-
const linter_path = Object.keys(require.cache).find(path => path.endsWith('/eslint/lib/linter/linter.js') || path.endsWith('\\eslint\\lib\\linter\\linter.js'));
5-
if (!linter_path) {
4+
const linter_paths = Object.keys(require.cache).filter(path => path.endsWith('/eslint/lib/linter/linter.js') || path.endsWith('\\eslint\\lib\\linter\\linter.js'));
5+
if (!linter_paths.length) {
66
throw new Error('Could not find ESLint Linter in require cache');
77
}
8+
// There may be more than one instance of the linter when we're in a workspace with multiple directories.
9+
// We first try to find the one that's inside the same node_modules directory as this plugin.
10+
// If that can't be found for some reason, we assume the one we want is the last one in the array.
11+
const current_node_modules_path = __dirname.replace(/(?<=[/\\]node_modules[/\\]).*$/, '')
12+
const linter_path = linter_paths.find(path => path.startsWith(current_node_modules_path)) || linter_paths.pop();
813
const { Linter } = require(linter_path);
914

1015
// patch Linter#verify

0 commit comments

Comments
 (0)