From 6373dab9901a29105712ec040006f2352a318dd6 Mon Sep 17 00:00:00 2001 From: Sharmila Date: Wed, 21 Feb 2018 20:18:56 -0800 Subject: [PATCH] Adds config entry point with only whitespace rules enabled Adds a change to eslint-config-airbnb and eslint-config-airbnb-base to pull a list of rules from the project root and return a config with all rules turned off except the whitespace rules explicitly listed in the array. Also adds entry point data to readme. --- packages/eslint-config-airbnb-base/README.md | 4 + .../eslint-config-airbnb-base/package.json | 3 +- .../eslint-config-airbnb-base/whitespace.js | 73 ++++++++++++++++ packages/eslint-config-airbnb/README.md | 4 + packages/eslint-config-airbnb/package.json | 3 +- packages/eslint-config-airbnb/whitespace.js | 87 +++++++++++++++++++ 6 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 packages/eslint-config-airbnb-base/whitespace.js create mode 100644 packages/eslint-config-airbnb/whitespace.js diff --git a/packages/eslint-config-airbnb-base/README.md b/packages/eslint-config-airbnb-base/README.md index 686ccb06e8..fa0152eb04 100644 --- a/packages/eslint-config-airbnb-base/README.md +++ b/packages/eslint-config-airbnb-base/README.md @@ -84,6 +84,10 @@ Lints ES5 and below. Requires `eslint` and `eslint-plugin-import`. See [Airbnb's overarching ESLint config](https://npmjs.com/eslint-config-airbnb), [Airbnb's Javascript styleguide](https://github.com/airbnb/javascript), and the [ESlint config docs](https://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information. +### eslint-config-airbnb-base/whitespace + +This entry point that only warns on whitespace rules and sets all other rules to warnings. View the list of whitespace rules [here](https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/whitespace.js). + ## Improving this config Consider adding test cases if you're making complicated rules changes, like anything involving regexes. Perhaps in a distant future, we could use literate programming to structure our README as test cases for our .eslintrc? diff --git a/packages/eslint-config-airbnb-base/package.json b/packages/eslint-config-airbnb-base/package.json index f74a57d8e2..6dd7461c8e 100644 --- a/packages/eslint-config-airbnb-base/package.json +++ b/packages/eslint-config-airbnb-base/package.json @@ -66,6 +66,7 @@ "node": ">= 4" }, "dependencies": { - "eslint-restricted-globals": "^0.1.1" + "eslint-restricted-globals": "^0.1.1", + "object.entries": "^1.0.4" } } diff --git a/packages/eslint-config-airbnb-base/whitespace.js b/packages/eslint-config-airbnb-base/whitespace.js new file mode 100644 index 0000000000..408c0b72b6 --- /dev/null +++ b/packages/eslint-config-airbnb-base/whitespace.js @@ -0,0 +1,73 @@ +const baseConfig = require('.'); +const entries = require('object.entries'); +const { CLIEngine } = require('eslint'); + +function onlyErrorOnRules(rulesToError, config) { + const errorsOnly = { ...config }; + const cli = new CLIEngine({ baseConfig: config, useEslintrc: false }); + const baseRules = cli.getConfigForFile('./').rules; + + entries(baseRules).forEach(([key, value]) => { + if (rulesToError.indexOf(key) === -1) { + if (Array.isArray(value)) { + errorsOnly.rules[key] = ['warn'].concat(value.slice(1)); + } else if (typeof value === 'number') { + errorsOnly.rules[key] = 1; + } else { + errorsOnly.rules[key] = 'warn'; + } + } + }); + + return errorsOnly; +} + +module.exports = onlyErrorOnRules([ + 'array-bracket-newline', + 'array-bracket-spacing', + 'array-element-newline', + 'arrow-spacing', + 'block-spacing', + 'comma-spacing', + 'computed-property-spacing', + 'dot-location', + 'eol-last', + 'func-call-spacing', + 'function-paren-newline', + 'generator-star-spacing', + 'implicit-arrow-linebreak', + 'indent', + 'key-spacing', + 'keyword-spacing', + 'line-comment-position', + 'linebreak-style', + 'multiline-ternary', + 'newline-per-chained-call', + 'no-irregular-whitespace', + 'no-mixed-spaces-and-tabs', + 'no-multi-spaces', + 'no-regex-spaces', + 'no-spaced-func', + 'no-trailing-spaces', + 'no-whitespace-before-property', + 'nonblock-statement-body-position', + 'object-curly-newline', + 'object-curly-spacing', + 'object-property-newline', + 'one-var-declaration-per-line', + 'operator-linebreak', + 'padded-blocks', + 'padding-line-between-statements', + 'rest-spread-spacing', + 'semi-spacing', + 'semi-style', + 'space-before-blocks', + 'space-before-function-paren', + 'space-in-parens', + 'space-infix-ops', + 'space-unary-ops', + 'spaced-comment', + 'switch-colon-spacing', + 'template-tag-spacing', + 'import/newline-after-import', +], baseConfig); diff --git a/packages/eslint-config-airbnb/README.md b/packages/eslint-config-airbnb/README.md index 88e6c80d23..2f341ec187 100644 --- a/packages/eslint-config-airbnb/README.md +++ b/packages/eslint-config-airbnb/README.md @@ -55,6 +55,10 @@ If you use yarn, run `npm info "eslint-config-airbnb@latest" peerDependencies` t 2. Add `"extends": "airbnb"` to your .eslintrc +### eslint-config-airbnb/whitespace + +This entry point that only warns on whitespace rules and sets all other rules to warnings. View the list of whitespace rules [here](https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb/whitespace.js). + ### eslint-config-airbnb/base This entry point is deprecated. See [eslint-config-airbnb-base](https://npmjs.com/eslint-config-airbnb-base). diff --git a/packages/eslint-config-airbnb/package.json b/packages/eslint-config-airbnb/package.json index b67dd57cdb..9dc6c92a45 100644 --- a/packages/eslint-config-airbnb/package.json +++ b/packages/eslint-config-airbnb/package.json @@ -48,7 +48,8 @@ }, "homepage": "https://github.com/airbnb/javascript", "dependencies": { - "eslint-config-airbnb-base": "^12.1.0" + "eslint-config-airbnb-base": "^12.1.0", + "object.entries": "^1.0.4" }, "devDependencies": { "babel-preset-airbnb": "^2.4.0", diff --git a/packages/eslint-config-airbnb/whitespace.js b/packages/eslint-config-airbnb/whitespace.js new file mode 100644 index 0000000000..f533385e80 --- /dev/null +++ b/packages/eslint-config-airbnb/whitespace.js @@ -0,0 +1,87 @@ +const baseConfig = require('.'); +const entries = require('object.entries'); +const { CLIEngine } = require('eslint'); + +function onlyErrorOnRules(rulesToError, config) { + const errorsOnly = { ...config }; + const cli = new CLIEngine({ baseConfig: config, useEslintrc: false }); + const baseRules = cli.getConfigForFile('./').rules; + + entries(baseRules).forEach(([key, value]) => { + if (rulesToError.indexOf(key) === -1) { + if (Array.isArray(value)) { + errorsOnly.rules[key] = ['warn'].concat(value.slice(1)); + } else if (typeof value === 'number') { + errorsOnly.rules[key] = 1; + } else { + errorsOnly.rules[key] = 'warn'; + } + } + }); + + return errorsOnly; +} + +module.exports = onlyErrorOnRules([ + 'array-bracket-newline', + 'array-bracket-spacing', + 'array-element-newline', + 'arrow-spacing', + 'block-spacing', + 'comma-spacing', + 'computed-property-spacing', + 'dot-location', + 'eol-last', + 'func-call-spacing', + 'function-paren-newline', + 'generator-star-spacing', + 'implicit-arrow-linebreak', + 'indent', + 'key-spacing', + 'keyword-spacing', + 'line-comment-position', + 'linebreak-style', + 'multiline-ternary', + 'newline-per-chained-call', + 'no-irregular-whitespace', + 'no-mixed-spaces-and-tabs', + 'no-multi-spaces', + 'no-regex-spaces', + 'no-spaced-func', + 'no-trailing-spaces', + 'no-whitespace-before-property', + 'nonblock-statement-body-position', + 'object-curly-newline', + 'object-curly-spacing', + 'object-property-newline', + 'one-var-declaration-per-line', + 'operator-linebreak', + 'padded-blocks', + 'padding-line-between-statements', + 'rest-spread-spacing', + 'semi-spacing', + 'semi-style', + 'space-before-blocks', + 'space-before-function-paren', + 'space-in-parens', + 'space-infix-ops', + 'space-unary-ops', + 'spaced-comment', + 'switch-colon-spacing', + 'template-tag-spacing', + 'import/newline-after-import', + // eslint-plugin-react rules + 'react/jsx-child-element-spacing', + 'react/jsx-closing-bracket-location', + 'react/jsx-closing-tag-location', + 'react/jsx-curly-spacing', + 'react/jsx-equals-spacing', + 'react/jsx-first-prop-newline', + 'react/jsx-indent', + 'react/jsx-indent-props', + 'react/jsx-max-props-per-line', + 'react/jsx-one-expression-per-line', + 'react/jsx-space-before-closing', + 'react/jsx-tag-spacing', + 'react/jsx-wrap-multilines', +], baseConfig);