Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop package.json settings from overriding explicit options #146

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,4 @@ The following aliases are available:

Note that `globals`, `plugins` and `envs` take preference.

The `parser` option takes preference over any `parser` setting in the project's `package.json`.
When `globals`, `plugins`, `envs` and `parser` options (and their aliases) are provided to the above APIs, the corresponding settings in the project's `package.json` are ignored. Use a custom `parseOpts()` function to merge the settings as needed.
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ Linter.prototype.parseOpts = function (opts) {

if (opts.fix != null) opts.eslintConfig.fix = opts.fix

setGlobals(opts.globals || opts.global)
setPlugins(opts.plugins || opts.plugin)
setEnvs(opts.envs || opts.env)
setParser(opts.parser)
var preventOverride = {}

preventOverride.globals = setGlobals(opts.globals || opts.global)
preventOverride.plugins = setPlugins(opts.plugins || opts.plugin)
preventOverride.envs = setEnvs(opts.envs || opts.env)
preventOverride.parser = setParser(opts.parser)

var root
try { root = findRoot(opts.cwd) } catch (e) {}
Expand All @@ -142,10 +144,10 @@ Linter.prototype.parseOpts = function (opts) {

if (packageOpts) {
setIgnore(packageOpts.ignore)
setGlobals(packageOpts.globals || packageOpts.global)
setPlugins(packageOpts.plugins || packageOpts.plugin)
setEnvs(packageOpts.envs || packageOpts.env)
if (!opts.parser) setParser(packageOpts.parser)
if (!preventOverride.globals) setGlobals(packageOpts.globals || packageOpts.global)
if (!preventOverride.plugins) setPlugins(packageOpts.plugins || packageOpts.plugin)
if (!preventOverride.envs) setEnvs(packageOpts.envs || packageOpts.env)
if (!preventOverride.parser) setParser(packageOpts.parser)
}
}

Expand All @@ -155,27 +157,31 @@ Linter.prototype.parseOpts = function (opts) {
}

function setGlobals (globals) {
if (!globals) return
if (!globals) return false
opts.eslintConfig.globals = self.eslintConfig.globals.concat(globals)
return true
}

function setPlugins (plugins) {
if (!plugins) return
if (!plugins) return false
opts.eslintConfig.plugins = self.eslintConfig.plugins.concat(plugins)
return true
}

function setEnvs (envs) {
if (!envs) return
if (!envs) return false
if (!Array.isArray(envs) && typeof envs !== 'string') {
// envs can be an object in `package.json`
envs = Object.keys(envs).filter(function (env) { return envs[env] })
}
opts.eslintConfig.envs = self.eslintConfig.envs.concat(envs)
return true
}

function setParser (parser) {
if (!parser) return
if (!parser) return false
opts.eslintConfig.parser = parser
return true
}

return opts
Expand Down