-
Notifications
You must be signed in to change notification settings - Fork 141
Use ESLint's CLIEngine API rather than the command line (fixes #797) #873
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,27 +24,30 @@ const ignoredMessages = [ | |
'File ignored by default. Use "--ignore-pattern \'!bower_components/*\'" to override.', | ||
] | ||
|
||
function lintJob(argv, contents, eslint, configPath, config) { | ||
const noProjectConfig = (configPath === null || isConfigAtHomeRoot(configPath)) | ||
if (noProjectConfig && config.disableWhenNoEslintConfig) { | ||
return [] | ||
} | ||
eslint.execute(argv, contents) | ||
return global.__LINTER_ESLINT_RESPONSE | ||
.filter(e => !ignoredMessages.includes(e.message)) | ||
function shouldBeReported(problem) { | ||
return !ignoredMessages.includes(problem.message) | ||
} | ||
|
||
function lintJob({ cliEngineOptions, contents, eslint, filePath }) { | ||
const cliEngine = new eslint.CLIEngine(cliEngineOptions) | ||
|
||
return typeof contents === 'string' | ||
? cliEngine.executeOnText(contents, filePath) | ||
: cliEngine.executeOnFiles([filePath]) | ||
} | ||
|
||
function fixJob(argv, eslint) { | ||
const exit = eslint.execute(argv) | ||
if (exit === 0) { | ||
function fixJob({ cliEngineOptions, eslint, filePath }) { | ||
const report = lintJob({ cliEngineOptions, eslint, filePath }) | ||
|
||
eslint.CLIEngine.outputFixes(report) | ||
|
||
if (!report.results.length || !report.results[0].messages.filter(shouldBeReported).length) { | ||
return 'Linter-ESLint: Fix complete.' | ||
} | ||
return 'Linter-ESLint: Fix attempt complete, but linting errors remain.' | ||
} | ||
|
||
create().onRequest('job', ({ contents, type, config, filePath, projectPath, rules }, job) => { | ||
global.__LINTER_ESLINT_RESPONSE = [] | ||
|
||
if (config.disableFSCache) { | ||
FindCache.clear() | ||
} | ||
|
@@ -54,16 +57,20 @@ create().onRequest('job', ({ contents, type, config, filePath, projectPath, rule | |
const configPath = Helpers.getConfigPath(fileDir) | ||
const relativeFilePath = Helpers.getRelativePath(fileDir, filePath, config) | ||
|
||
const argv = Helpers.getArgv(type, config, rules, relativeFilePath, fileDir, configPath) | ||
const cliEngineOptions = Helpers.getCLIEngineOptions( | ||
type, config, rules, relativeFilePath, fileDir, configPath | ||
) | ||
|
||
if (type === 'lint') { | ||
job.response = lintJob(argv, contents, eslint, configPath, config) | ||
const noProjectConfig = (configPath === null || isConfigAtHomeRoot(configPath)) | ||
if (noProjectConfig && config.disableWhenNoEslintConfig) { | ||
job.response = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call moving this check down here, it should definitely apply to lint jobs and fix jobs. 👍 |
||
} else if (type === 'lint') { | ||
const report = lintJob({ cliEngineOptions, contents, eslint, filePath }) | ||
job.response = report.results.length ? report.results[0].messages.filter(shouldBeReported) : [] | ||
} else if (type === 'fix') { | ||
job.response = fixJob(argv, eslint) | ||
job.response = fixJob({ cliEngineOptions, eslint, filePath }) | ||
} else if (type === 'debug') { | ||
const modulesDir = Path.dirname(findCached(fileDir, 'node_modules/eslint') || '') | ||
job.response = Helpers.findESLintDirectory(modulesDir, config) | ||
} | ||
}) | ||
|
||
process.exit = function () { /* Stop eslint from closing the daemon */ } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works for now, and it was the previous behavior, but I think a better thing to do would be to actually tell the user that nothing happened because the file was ignored. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It used to be that way, but users complained, which is why it's this way in the first place. Can't please everyone I guess.
Personally I would prefer to have the messages shown myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you mean we used to show the 'file ignored' messages when linting. And yeah, that's noisy. But when a user manually runs the fix command, we should show a meaningful message. I'll push a PR later if I can get around to it.