Skip to content

Commit

Permalink
refactor: Reuse rule list
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Aug 25, 2017
1 parent 94d539c commit f898741
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 117 deletions.
35 changes: 20 additions & 15 deletions es5.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
'use strict';

module.exports = {
const opinions = require('./lib/rules/opinions');
const conventions = require('./lib/rules/opinions');
const mistakes = require('./lib/rules/mistakes');

module.exports = Object.assign({
env: {
es6: false,
node: true,
},
parserOptions: {
sourceType: 'script',
ecmaVersion: 5,
sourceType: 'script',
},
rules: {
'no-param-reassign': 0,
'prefer-rest-params': 0,
'no-underscore-dangle': [2, { allowAfterThis: true }],
strict: [2, 'global'],
'comma-dangle': [2, {
arrays: 'always-multiline',
objects: 'always-multiline',
functions: 'never',
}],
'prefer-spread': 0,
}
};
plugins: ['import', 'node', 'prettier'],
rules: Object.assign(opinions, conventions, mistakes, {
// Additional opinions

// Additional conventions

// Additional mistakes
'node/no-unsupported-features': [2, { version: 4 }],
})
});
10 changes: 5 additions & 5 deletions examples/eslint/node4/comma-dangle.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

function foo() { }
function foo() {}

// no comma-dangle for functions
foo(
1,
2
'some very long argument to force line break',
'another very long argument to force line break'
);

// yes comma-dangle for arrays/objects
foo([
1,
2,
'some very long argument to force line break',
'another very long argument to force line break',
]);
foo({
x: 1,
Expand Down
4 changes: 3 additions & 1 deletion examples/eslint/node4/generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';

function* foo() { yield 42; }
function* foo() {
yield 42;
}
foo();
2 changes: 1 addition & 1 deletion examples/eslint/node4/prefer-spread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function foo() { }
function foo() {}

const arr = [1, 2, 3];

Expand Down
3 changes: 3 additions & 0 deletions lib/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('../node8.js');
16 changes: 16 additions & 0 deletions lib/basics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

/**
* Reasonable defaults for most lint settings
*/
module.exports = {
env: {
es6: true,
node: true,
},
parserOptions: {
ecmaVersion: 2017,
sourceType: 'script',
},
plugins: ['import', 'node', 'prettier'],
};
15 changes: 15 additions & 0 deletions lib/rules/conventions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/**
* Rules in this file have good reasons but don't prevent bugs.
*
* If any of the rules in here can't be fixed, they should at most warn.
* Not following conventions should not break builds, especially since they
* might be a moving target.
*/
module.exports = {
'no-underscore-dangle': ['warn', { allowAfterThis: true }],
'no-undef-init': 'off', // pending: Can't be auto-fixed
'import/no-dynamic-require': 'off', // pending: Can't be auto-fixed
strict: [2, 'global'],
};
65 changes: 65 additions & 0 deletions lib/rules/mistakes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

/**
* Only things we are pretty sure are mistakes
*
* There should be a fairly high bar to end up in this list unless the issue
* can be fixed automatically. It's expected that each rule comes with a
* documented (class of) bug it prevents.
*/
module.exports = {
'no-debugger': 'error',
'no-empty': 'error',
'no-func-assign': 'error',
'no-template-curly-in-string': 'error',
'no-unreachable': 'error',
'no-unsafe-finally': 'error',
'use-isnan': 'error',
'no-unused-vars': [
'error',
{ vars: 'all', args: 'after-used', ignoreRestSiblings: true },
],
'no-use-before-define': [
'error',
{ functions: true, classes: true, variables: true },
],
'no-undef': 'error',
'no-shadow': 'error',
'no-label-var': 'error',
'no-delete-var': 'error',
'no-catch-shadow': 'off',

'import/no-unresolved': ['error', { commonjs: true, caseSensitive: true }],
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: [
'test/**', // tape, common npm pattern
'tests/**', // also common npm pattern
'spec/**', // mocha, rspec-like pattern
'**/__tests__/**', // jest pattern
'**/*.test.{js,jsx}', // repos with inline test files
'test.{js,jsx}', // repos with a single test file
'test-*.{js,jsx}', // repos with multiple top-level test files
'**/*.{test,spec}.{js,jsx}', // tests where the extension denotes that it is a test
'**/jest.config.js', // jest config
'**/webpack.config.js', // webpack config
'**/webpack.config.*.js', // webpack config
'**/rollup.config.js', // rollup config
'**/rollup.config.*.js', // rollup config
'**/gulpfile.js', // gulp config
'**/gulpfile.*.js', // gulp config
'**/Gruntfile{,.js}', // grunt config
'**/protractor.conf.*.js', // protractor config

// Allow script & example files
'scripts/**',
'tasks/**',
'examples/**',
'example/**',
],
optionalDependencies: true, // allow optional deps to be required
},
],
'import/no-duplicates': 'error',
};
19 changes: 19 additions & 0 deletions lib/rules/opinions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

/**
* Rules in this file are ultimately random choices.
*
* No human should have to type on a keyboard to comply.
* If it can't be --fix'ed, it doesn't belong in here.
*/
module.exports = {
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'es5',
},
],
'lines-around-directive': ['error', 'always'],
'import/newline-after-import': 'error',
};
36 changes: 15 additions & 21 deletions node4.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
'use strict';

module.exports = {
parserOptions: {
ecmaVersion: 2017,
sourceType: 'script',
},
plugins: [
'node',
],
rules: {
const basics = require('./lib/basics');

const opinions = require('./lib/rules/opinions');
const conventions = require('./lib/rules/opinions');
const mistakes = require('./lib/rules/mistakes');

module.exports = Object.assign(basics, {
rules: Object.assign(opinions, conventions, mistakes, {
// Additional opinions

// Additional conventions

// Additional mistakes
'node/no-unsupported-features': [2, { version: 4 }],
'no-param-reassign': 0,
'prefer-rest-params': 0,
'no-underscore-dangle': [2, { allowAfterThis: true }],
strict: [2, 'global'],
'comma-dangle': [2, {
arrays: 'always-multiline',
objects: 'always-multiline',
functions: 'never',
}],
'prefer-spread': 0,
}
};
})
});
86 changes: 12 additions & 74 deletions node6.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,18 @@
'use strict';

module.exports = {
env: {
es6: true,
node: true
},
parserOptions: {
ecmaVersion: 2017,
sourceType: 'script',
},
plugins: [
'import',
'node',
'prettier',
],
rules: {
// Random stylistic choices
'prettier/prettier': ['error', {
singleQuote: true,
trailingComma: 'es5',
}],
'lines-around-directive': ['error', 'always'],
'import/newline-after-import': 'error',
const basics = require('./lib/basics');

// Code convention
'no-underscore-dangle': [2, { allowAfterThis: true }],
'no-undef-init': 'off', // pending: Can't be auto-fixed
'import/no-dynamic-require': 'off', // pending: Can't be auto-fixed
const opinions = require('./lib/rules/opinions');
const conventions = require('./lib/rules/opinions');
const mistakes = require('./lib/rules/mistakes');

// Bug prevention
strict: [2, 'global'],
'node/no-unsupported-features': [2, { version: 6 }],
'no-debugger': 'error',
'no-empty': 'error',
'no-func-assign': 'error',
'no-template-curly-in-string': 'error',
'no-unreachable': 'error',
'no-unsafe-finally': 'error',
'use-isnan': 'error',
'no-unused-vars': ['error', { vars: 'all', args: 'after-used', ignoreRestSiblings: true }],
'no-use-before-define': ['error', { functions: true, classes: true, variables: true }],
'no-undef': 'error',
'no-shadow': 'error',
'no-label-var': 'error',
'no-delete-var': 'error',
'no-catch-shadow': 'off',
module.exports = Object.assign(basics, {
rules: Object.assign(opinions, conventions, mistakes, {
// Additional opinions

'import/no-unresolved': ['error', { commonjs: true, caseSensitive: true }],
'import/no-extraneous-dependencies': ['error', {
devDependencies: [
'test/**', // tape, common npm pattern
'tests/**', // also common npm pattern
'spec/**', // mocha, rspec-like pattern
'**/__tests__/**', // jest pattern
'**/*.test.{js,jsx}', // repos with inline test files
'test.{js,jsx}', // repos with a single test file
'test-*.{js,jsx}', // repos with multiple top-level test files
'**/*.{test,spec}.{js,jsx}', // tests where the extension denotes that it is a test
'**/jest.config.js', // jest config
'**/webpack.config.js', // webpack config
'**/webpack.config.*.js', // webpack config
'**/rollup.config.js', // rollup config
'**/rollup.config.*.js', // rollup config
'**/gulpfile.js', // gulp config
'**/gulpfile.*.js', // gulp config
'**/Gruntfile{,.js}', // grunt config
'**/protractor.conf.*.js', // protractor config
// Additional conventions

// Allow script & example files
'scripts/**',
'tasks/**',
'examples/**',
'example/**',
],
optionalDependencies: true, // allow optional deps to be required
}],
'import/no-duplicates': 'error',
}
};
// Additional mistakes
'node/no-unsupported-features': [2, { version: 6 }],
})
});
18 changes: 18 additions & 0 deletions node8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const basics = require('./lib/basics');

const opinions = require('./lib/rules/opinions');
const conventions = require('./lib/rules/opinions');
const mistakes = require('./lib/rules/mistakes');

module.exports = Object.assign(basics, {
rules: Object.assign(opinions, conventions, mistakes, {
// Additional opinions

// Additional conventions

// Additional mistakes
'node/no-unsupported-features': [2, { version: 8 }],
})
});

0 comments on commit f898741

Please sign in to comment.