From 6d2e0c1f90cc01474607e0053f04a2f0e7c2a3c8 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 12 Jul 2016 15:36:24 -0700 Subject: [PATCH] Add --fix option (uses eslint's --fix) Fixes #107 --- bin/cmd.js | 20 +++++++++++++++----- index.js | 3 +++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 718a967..c308205 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -27,6 +27,7 @@ function Cli (opts) { verbose: 'v' }, boolean: [ + 'fix', 'format', 'help', 'stdin', @@ -77,19 +78,22 @@ function Cli (opts) { If FILES is omitted, then all JavaScript source files (*.js, *.jsx) in the current working directory are checked, recursively. - Certain paths (node_modules/, .git/, coverage/, *.min.js, bundle.js) are + Certain paths (node_modules/, .git/, coverage/, *.min.js, bundle.js, vendor/) are automatically ignored. Flags: %s - -v, --verbose Show error codes. (so you can ignore specific rules) - --stdin Read file text from stdin. + -v, --verbose Show rule names for errors (to ignore specific rules) + --fix Automatically fix problems + --version Show current version + -h, --help Show usage information + + Flags (advanced): + --stdin Read file text from stdin --global Declare global variable --plugin Use custom eslint plugin --env Use custom eslint environment --parser Use custom js parser (e.g. babel-eslint) - --version Show current version - -h, --help Show usage information */ }), opts.cmd, fmtMsg) exit(0) @@ -103,6 +107,7 @@ function Cli (opts) { } var lintOpts = { + fix: argv.fix, globals: argv.global, plugins: argv.plugin, envs: argv.env, @@ -131,6 +136,11 @@ function Cli (opts) { function onResult (err, result) { if (err) return onError(err) + + if (argv.fix && !argv.stdin) { + opts.eslint.CLIEngine.outputFixes(result) + } + if (!result.errorCount && !result.warningCount) { exit(0) return diff --git a/index.js b/index.js index 22122d0..71db8fd 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ function Linter (opts) { self.eslintConfig = defaults(opts.eslintConfig, { envs: [], + fix: false, globals: [], ignore: false, plugins: [], @@ -124,6 +125,8 @@ Linter.prototype.parseOpts = function (opts) { if (!opts.ignore) opts.ignore = [] opts.ignore = opts.ignore.concat(DEFAULT_IGNORE) + if (opts.fix != null) opts.eslintConfig.fix = opts.fix + setGlobals(opts.globals || opts.global) setPlugins(opts.plugins || opts.plugin) setEnvs(opts.envs || opts.env)