From 68606b43e62b12b5da736a9a81e8d46daa92c2cb Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Sat, 25 Feb 2017 18:06:32 -0600 Subject: [PATCH] Add custom method for generating an eslintrc config (#63) --- docs/presets/neutrino-lint-base/README.md | 21 +++++++++++++++++++ .../neutrino-preset-airbnb-base/README.md | 21 +++++++++++++++++++ packages/neutrino-lint-base/package.json | 3 ++- packages/neutrino-lint-base/src/index.js | 17 ++++++++++++++- packages/neutrino-lint-base/yarn.lock | 4 ++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/docs/presets/neutrino-lint-base/README.md b/docs/presets/neutrino-lint-base/README.md index 47aab4c..fb08f67 100644 --- a/docs/presets/neutrino-lint-base/README.md +++ b/docs/presets/neutrino-lint-base/README.md @@ -164,6 +164,27 @@ that clearly in your preset. For specifics on what options are configured when using this linting base, please [view the source](https://github.com/mozilla-neutrino/neutrino-dev/blob/master/packages/neutrino-lint-base/src/index.js). +## eslintrc Config + +`neutrino-lint-base` also provides a method for getting the ESLint configuration suitable for use in an eslintrc file. +Typically this is used for providing hints or fix solutions to the development environment, e.g. IDEs and text editors. +Doing this requires [creating an instance of the Neutrino API](/api/README.md) and providing the presets uses. If you +keep this information in `config.presets` in package.json, this should be relatively straightforward. By providing +all the presets used to Neutrino, you can ensure all the linting options used across all those preset will be merged +together for your development environment, without the need for copying, duplication, or loss of organization and +separation. + +_Example: Create a .eslintrc.js file in the root of the project._ + +```js +// .eslintrc.js +const Neutrino = require('neutrino'); +const pkg = require('./package.json'); +const api = new Neutrino(pkg.config.presets); + +module.exports = api.custom.eslintrc(); +``` + --- ## Contributing diff --git a/docs/presets/neutrino-preset-airbnb-base/README.md b/docs/presets/neutrino-preset-airbnb-base/README.md index cb615a3..9e256a3 100644 --- a/docs/presets/neutrino-preset-airbnb-base/README.md +++ b/docs/presets/neutrino-preset-airbnb-base/README.md @@ -199,6 +199,27 @@ module.exports = neutrino => { }; ``` +## eslintrc Config + +`neutrino-lint-airbnb-base` also provides a method for getting the ESLint configuration suitable for use in an eslintrc +file. Typically this is used for providing hints or fix solutions to the development environment, e.g. IDEs and text +editors. Doing this requires [creating an instance of the Neutrino API](/api/README.md) and providing the presets uses. +If you keep this information in `config.presets` in package.json, this should be relatively straightforward. By +providing all the presets used to Neutrino, you can ensure all the linting options used across all those preset will be +merged together for your development environment, without the need for copying, duplication, or loss of organization and +separation. + +_Example: Create a .eslintrc.js file in the root of the project._ + +```js +// .eslintrc.js +const Neutrino = require('neutrino'); +const pkg = require('./package.json'); +const api = new Neutrino(pkg.config.presets); + +module.exports = api.custom.eslintrc(); +``` + ## Contributing This preset is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo diff --git a/packages/neutrino-lint-base/package.json b/packages/neutrino-lint-base/package.json index 0284563..d69e523 100644 --- a/packages/neutrino-lint-base/package.json +++ b/packages/neutrino-lint-base/package.json @@ -16,7 +16,8 @@ "eslint": "^3.16.1", "eslint-loader": "^1.6.1", "eslint-plugin-babel": "^4.0.1", - "eslint-plugin-import": "^2.2.0" + "eslint-plugin-import": "^2.2.0", + "lodash.clonedeep": "^4.5.0" }, "peerDependencies": { "neutrino": "^4.0.0" diff --git a/packages/neutrino-lint-base/src/index.js b/packages/neutrino-lint-base/src/index.js index 31017b9..41aa20c 100644 --- a/packages/neutrino-lint-base/src/index.js +++ b/packages/neutrino-lint-base/src/index.js @@ -1,9 +1,12 @@ +const clone = require('lodash.clonedeep'); const path = require('path'); const MODULES = path.join(__dirname, '../node_modules'); const IF_NOT_DEV = process.env.NODE_ENV !== 'development'; -module.exports = ({ config }) => { +module.exports = neutrino => { + const { config } = neutrino; + config .module .rule('lint') @@ -37,4 +40,16 @@ module.exports = ({ config }) => { config.resolve.modules.add(MODULES); config.resolveLoader.modules.add(MODULES); + + neutrino.custom.eslintrc = () => { + const options = clone(config.module.rule('lint').loaders.get('eslint').options); + + options.extends = options.baseConfig.extends; + options.useEslintrc = true; + options.env = options.envs.reduce((env, key) => Object.assign(env, { [key]: true }, {})); + options.globals = options.globals.reduce((globals, key) => Object.assign(env, { [key]: true }, {})); + ['envs', 'baseConfig', 'failOnError', 'emitWarning', 'emitError'].map(method => delete options[method]); + + return options; + }; }; diff --git a/packages/neutrino-lint-base/yarn.lock b/packages/neutrino-lint-base/yarn.lock index 65bf771..5dcd9b1 100644 --- a/packages/neutrino-lint-base/yarn.lock +++ b/packages/neutrino-lint-base/yarn.lock @@ -685,6 +685,10 @@ loader-utils@^1.0.2: emojis-list "^2.0.0" json5 "^0.5.0" +lodash.clonedeep@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" + lodash.cond@^4.3.0: version "4.5.2" resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5"