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

Commit b6bdeb1

Browse files
authored
Merge pull request #873 from not-an-aardvark/use-node-api
Use ESLint's CLIEngine API rather than the command line (fixes #797)
2 parents bd2e81f + b2c9f76 commit b6bdeb1

File tree

15 files changed

+110
-108
lines changed

15 files changed

+110
-108
lines changed

lib/reporter.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/worker-helpers.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports.refreshModulesPath = refreshModulesPath;
1111
exports.getESLintInstance = getESLintInstance;
1212
exports.getConfigPath = getConfigPath;
1313
exports.getRelativePath = getRelativePath;
14-
exports.getArgv = getArgv;
14+
exports.getCLIEngineOptions = getCLIEngineOptions;
1515

1616
var _path = require('path');
1717

@@ -103,13 +103,13 @@ function getESLintFromDirectory(modulesDir, config, projectPath) {
103103

104104
try {
105105
// eslint-disable-next-line import/no-dynamic-require
106-
return require(_path2.default.join(ESLintDirectory, 'lib', 'cli.js'));
106+
return require(ESLintDirectory);
107107
} catch (e) {
108108
if (config.useGlobalEslint && e.code === 'MODULE_NOT_FOUND') {
109109
throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly');
110110
}
111111
// eslint-disable-next-line import/no-dynamic-require
112-
return require(_path2.default.join(Cache.ESLINT_LOCAL_PATH, 'lib', 'cli.js'));
112+
return require(Cache.ESLINT_LOCAL_PATH);
113113
}
114114
}
115115

@@ -158,22 +158,21 @@ function getRelativePath(fileDir, filePath, config) {
158158
return _path2.default.basename(filePath);
159159
}
160160

161-
function getArgv(type, config, rules, filePath, fileDir, givenConfigPath) {
161+
function getCLIEngineOptions(type, config, rules, filePath, fileDir, givenConfigPath) {
162162
let configPath;
163163
if (givenConfigPath === null) {
164164
configPath = config.eslintrcPath || null;
165165
} else configPath = givenConfigPath;
166166

167-
const argv = [process.execPath, 'a-b-c' // dummy value for eslint executable
168-
];
169-
if (type === 'lint') {
170-
argv.push('--stdin');
171-
}
172-
argv.push('--format', _path2.default.join(__dirname, 'reporter.js'));
167+
const cliEngineConfig = {
168+
rules,
169+
ignore: !config.disableEslintIgnore,
170+
fix: type === 'fix'
171+
};
173172

174173
const ignoreFile = config.disableEslintIgnore ? null : (0, _atomLinter.findCached)(fileDir, '.eslintignore');
175174
if (ignoreFile) {
176-
argv.push('--ignore-path', ignoreFile);
175+
cliEngineConfig.ignorePath = ignoreFile;
177176
}
178177

179178
if (config.eslintRulesDir) {
@@ -182,24 +181,12 @@ function getArgv(type, config, rules, filePath, fileDir, givenConfigPath) {
182181
rulesDir = (0, _atomLinter.findCached)(fileDir, rulesDir);
183182
}
184183
if (rulesDir) {
185-
argv.push('--rulesdir', rulesDir);
184+
cliEngineConfig.rulePaths = [rulesDir];
186185
}
187186
}
188187
if (configPath) {
189-
argv.push('--config', (0, _resolveEnv2.default)(configPath));
190-
}
191-
if (rules && Object.keys(rules).length > 0) {
192-
argv.push('--rule', JSON.stringify(rules));
193-
}
194-
if (config.disableEslintIgnore) {
195-
argv.push('--no-ignore');
196-
}
197-
if (type === 'lint') {
198-
argv.push('--stdin-filename', filePath);
199-
} else if (type === 'fix') {
200-
argv.push(filePath);
201-
argv.push('--fix');
188+
cliEngineConfig.configFile = (0, _resolveEnv2.default)(configPath);
202189
}
203190

204-
return argv;
191+
return cliEngineConfig;
205192
}

lib/worker.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,43 @@ const ignoredMessages = [
3333
// supress warning that the current file is ignored by eslint by default
3434
'File ignored by default. Use a negated ignore pattern (like "--ignore-pattern \'!<relative' + '/path/to/filename>\'") to override.', 'File ignored by default. Use "--ignore-pattern \'!node_modules/*\'" to override.', 'File ignored by default. Use "--ignore-pattern \'!bower_components/*\'" to override.'];
3535

36-
function lintJob(argv, contents, eslint, configPath, config) {
37-
const noProjectConfig = configPath === null || (0, _isConfigAtHomeRoot.isConfigAtHomeRoot)(configPath);
38-
if (noProjectConfig && config.disableWhenNoEslintConfig) {
39-
return [];
40-
}
41-
eslint.execute(argv, contents);
42-
return global.__LINTER_ESLINT_RESPONSE.filter(e => !ignoredMessages.includes(e.message));
36+
function shouldBeReported(problem) {
37+
return !ignoredMessages.includes(problem.message);
38+
}
39+
40+
function lintJob(_ref) {
41+
let cliEngineOptions = _ref.cliEngineOptions,
42+
contents = _ref.contents,
43+
eslint = _ref.eslint,
44+
filePath = _ref.filePath;
45+
46+
const cliEngine = new eslint.CLIEngine(cliEngineOptions);
47+
48+
return typeof contents === 'string' ? cliEngine.executeOnText(contents, filePath) : cliEngine.executeOnFiles([filePath]);
4349
}
4450

45-
function fixJob(argv, eslint) {
46-
const exit = eslint.execute(argv);
47-
if (exit === 0) {
51+
function fixJob(_ref2) {
52+
let cliEngineOptions = _ref2.cliEngineOptions,
53+
eslint = _ref2.eslint,
54+
filePath = _ref2.filePath;
55+
56+
const report = lintJob({ cliEngineOptions, eslint, filePath });
57+
58+
eslint.CLIEngine.outputFixes(report);
59+
60+
if (!report.results.length || !report.results[0].messages.filter(shouldBeReported).length) {
4861
return 'Linter-ESLint: Fix complete.';
4962
}
5063
return 'Linter-ESLint: Fix attempt complete, but linting errors remain.';
5164
}
5265

53-
(0, _processCommunication.create)().onRequest('job', (_ref, job) => {
54-
let contents = _ref.contents,
55-
type = _ref.type,
56-
config = _ref.config,
57-
filePath = _ref.filePath,
58-
projectPath = _ref.projectPath,
59-
rules = _ref.rules;
60-
61-
global.__LINTER_ESLINT_RESPONSE = [];
66+
(0, _processCommunication.create)().onRequest('job', (_ref3, job) => {
67+
let contents = _ref3.contents,
68+
type = _ref3.type,
69+
config = _ref3.config,
70+
filePath = _ref3.filePath,
71+
projectPath = _ref3.projectPath,
72+
rules = _ref3.rules;
6273

6374
if (config.disableFSCache) {
6475
_atomLinter.FindCache.clear();
@@ -69,16 +80,18 @@ function fixJob(argv, eslint) {
6980
const configPath = Helpers.getConfigPath(fileDir);
7081
const relativeFilePath = Helpers.getRelativePath(fileDir, filePath, config);
7182

72-
const argv = Helpers.getArgv(type, config, rules, relativeFilePath, fileDir, configPath);
83+
const cliEngineOptions = Helpers.getCLIEngineOptions(type, config, rules, relativeFilePath, fileDir, configPath);
7384

74-
if (type === 'lint') {
75-
job.response = lintJob(argv, contents, eslint, configPath, config);
85+
const noProjectConfig = configPath === null || (0, _isConfigAtHomeRoot.isConfigAtHomeRoot)(configPath);
86+
if (noProjectConfig && config.disableWhenNoEslintConfig) {
87+
job.response = [];
88+
} else if (type === 'lint') {
89+
const report = lintJob({ cliEngineOptions, contents, eslint, filePath });
90+
job.response = report.results.length ? report.results[0].messages.filter(shouldBeReported) : [];
7691
} else if (type === 'fix') {
77-
job.response = fixJob(argv, eslint);
92+
job.response = fixJob({ cliEngineOptions, eslint, filePath });
7893
} else if (type === 'debug') {
7994
const modulesDir = _path2.default.dirname((0, _atomLinter.findCached)(fileDir, 'node_modules/eslint') || '');
8095
job.response = Helpers.findESLintDirectory(modulesDir, config);
8196
}
82-
});
83-
84-
process.exit = function () {/* Stop eslint from closing the daemon */};
97+
});

spec/fixtures/global-eslint/lib/node_modules/eslint/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/global-eslint/node_modules/eslint/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/indirect-local-eslint/testing/eslint/node_modules/eslint/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)