Skip to content

Adds eslint config extends with only whitespace rules enabled #1749

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

Merged
merged 1 commit into from
Mar 1, 2018
Merged
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
4 changes: 4 additions & 0 deletions packages/eslint-config-airbnb-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-airbnb-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"node": ">= 4"
},
"dependencies": {
"eslint-restricted-globals": "^0.1.1"
"eslint-restricted-globals": "^0.1.1",
"object.entries": "^1.0.4"
}
}
73 changes: 73 additions & 0 deletions packages/eslint-config-airbnb-base/whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const baseConfig = require('.');
const entries = require('object.entries');
const { CLIEngine } = require('eslint');

function onlyErrorOnRules(rulesToError, config) {
const errorsOnly = { ...config };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This object spread property in nodejs is introduced at version 8+. If this file is meant to be run with nodejs 8+, then we don't need to depend on more lib e.g. object.entries, Object.entries already exists there.

I can make a PR to clean this. What do you think, @ljharb?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we convert this file to be compatible with nodejs 4 which is declared in package.json, we still don't need this extra dependency, since eslint already depends on lodash, then we can add it too and use _.forEach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, this should use object.assign instead of spread.

There’s no cost to an extra dev dep, and I’d prefer not to use lodash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then we need to change array destructuring at line 10 too.

I'll make a PR, ok?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

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);
4 changes: 4 additions & 0 deletions packages/eslint-config-airbnb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-airbnb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
87 changes: 87 additions & 0 deletions packages/eslint-config-airbnb/whitespace.js
Original file line number Diff line number Diff line change
@@ -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);