Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Allow linting configs to be defined in package.json #250

Open
westonruter opened this issue Aug 2, 2017 · 6 comments
Open

Allow linting configs to be defined in package.json #250

westonruter opened this issue Aug 2, 2017 · 6 comments

Comments

@westonruter
Copy link
Contributor

Currently dev-lib looks for the presence of .eslintrc, .jshintrc, and other such files in order to determine whether or not it should invoke those tools. However, increasingly the configurations for these tools can be defined in package.json. Therefore, dev-lib should also look at the package.json to determine whether or no a linting tool should be used.

@rheinardkorf
Copy link

Yes please! Because dev-lib provides the config files which invokes those tools anyway it does seem like overkill to run 3 differing linters for JS. If we just pick our preferred linter in package.json this would be way more convenient... and perhaps setting a default linter.

@westonruter
Copy link
Contributor Author

To do this we'd need a way for our Bash script to read the package.json and detect whether or not a given linter is configured. Any suggestions? Could be a little one-liner PHP script added to php -e to json_parses the file and looks for the presence of a given key.

@rheinardkorf
Copy link

If its a single key e.g. js_linter that appears in package.json then it could be as simple as

LINT_JS_TYPE=$(grep -m1 js_linter package.json | awk '{$1=""; print $0 }' | sed -e 's/\(^ *\)//' -e 's/[",]//g')

@westonruter
Copy link
Contributor Author

Yeah, so for ESLint it would be whether or not "eslintConfig" occurs inside of the package.json file. Only problem with just grepping for a string would be the possibility for false positives. So I was thinking maybe it would be better to do something like:

if php -r 'exit( array_key_exists( "eslintConfig", json_decode( file_get_contents( "package.json" ), true ) ) ? 0 : 1 );'; then
    eslint
fi

Just tried that out and it works! So we'd just need to take that condition in addition to whether or not $ESLINT_CONFIG is set, which is the path to the file, though that probable should be refactored to properly take into the order of precedence for configs.

@rheinardkorf
Copy link

Could we just look for $ESLINT_CONFIG and if its empty keep doing what we're currently doing? But if its set then ignore the checking for the existence of the files?

@westonruter
Copy link
Contributor Author

@rheinardkorf this is what we'd need to check for:

If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is:

.eslintrc.js
.eslintrc.yaml
.eslintrc.yml
.eslintrc.json
.eslintrc
package.json

http://eslint.org/docs/user-guide/configuring#configuration-file-formats

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants