3.2.14 • Published 28 days ago

dk-eslint-config v3.2.14

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

Enterprise-quality ESLint config with TypeScript support

npm license

Use as is or as an inspiration for your projects.

Includes

  • perfectly configured rules for JavaScript files with React
  • perfectly configured rules for TypeScript files
  • auto-formatting with Prettier integrated in ESLint (runs by eslint --fix)
  • StyleLint rules for *.scss files

Core concepts

  • Syntax with async | generator | await | yield is restricted, use // eslint-ignore if you need this
  • Max depth of blocks is 4, simplify your code by using function composition
  • React is configured for using Class Components, so no long functions with mix of state, side-effects, lifecycle, markup are allowed
  • UNSAFE_componentWillMount is allowed - this is a perfect solution for starting async actions that are executed in SSR
  • Use Type | Props prefixes for types, T prefix for generics - no more confusion whether it is component, class or type
  • Use camelCase for functions, UPPER_CASE for constants and PascalCase for types and classes
  • Use import _ from 'lodash' with babel-plugin-lodash instead of import ... from lodash/.... No more get(), merge() without namespace
  • Use dot object notation like object.param instead of object['param']
  • No forgotten console statements
  • Auto-sort imports
  • Everything can be reconfigured in your own config

And many more best-practices. Recommended for use in scalable enterprise applications.

Usage

  1. Add dk-eslint-config to package.json
  2. Create .eslintrc.js with content:
const { getEslintConfig } = require('dk-eslint-config');

// When you need TypeScript
// const eslintConfig = getEslintConfig({ tsConfigPath: path.resolve(__dirname, './tsconfig.json'), react: true });

const eslintConfig = getEslintConfig({});

module.exports = eslintConfig;
  1. Create .stylelintrc.js with content:
const { stylelintConfig } = require('dk-eslint-config');

module.exports = stylelintConfig;
  1. Create .editorconfig with content:
[*]
max_line_length = 100
indent_style = space
indent_size = 2
  1. Create .formatignore with content (maybe you need other folders like build or dist):
node_modules
.yarn
cache
.cache
.vscode
  1. Add scripts to package.json (example for formatting only pointed files but analyze all files):
{
  "scripts": {
    "analyze:js": "eslint --ignore-path .formatignore --ext \".js,.ts,.tsx\" ./",
    "analyze:style": "stylelint --ignore-path .formatignore \"**/*.scss\"",
    "format:js": "eslint --ignore-path .formatignore --ext \".js,.ts,.tsx\" --fix",
    "format:style": "stylelint --ignore-path .formatignore --fix"
  }
}

Attach StyleLint and ESLint to your IDE, don't forget to enable auto-formatting on save.

It's recommended to add pre-commit hooks. For examples look through this repository. Example of usage:

  1. Add husky (4.3.8 is cross-platform, higher versions are buggy) and lint-staged to devDependencies
  2. Create lint-staged.config.js with content:
const { lintStagedConfig } = require('dk-eslint-config');

module.exports = lintStagedConfig;
  1. Add husky section to package.json:
{
  "husky": {
    "hooks": {
      "pre-commit": "set -e&&lint-staged"
    }
  }
}

Handling imports order

Sometimes you need to define custom groups for imports sorting. Here is an example of .eslintrc.js

const fs = require('fs');
const path = require('path');

const { getEslintConfig } = require('dk-eslint-config');

const eslintConfig = getEslintConfig({
  react: true,
  tsConfigPath: path.resolve(__dirname, './tsconfig.json'),
});

/**
 * eslint-plugin-import considers all imports like './env.ts' | './paths.ts' as external
 *
 * but for better readability we need to treat them as internal to separate from
 * imports from 'node_modules' folder
 *
 */

const pathGroups = [
  { pattern: 'env', group: 'internal' },
  { pattern: 'paths', group: 'internal' },
];

/**
 * let's treat all files in ./src as internal (for ex. Webpack is configured with this alias)
 *
 */

fs.readdirSync(path.resolve(__dirname, 'src')).forEach((fileName) => {
  const fileNameNoExt = path.parse(fileName).name;

  pathGroups.push({ pattern: fileNameNoExt, group: 'internal' });
  pathGroups.push({ pattern: `${fileNameNoExt}/**`, group: 'internal' });
});

eslintConfig.rules = {
  'import/order': [
    'error',
    {
      'newlines-between': 'always',
      groups: ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index'],
      pathGroups,
      pathGroupsExcludedImportTypes: ['internal'],
    },
  ],
};

module.exports = eslintConfig;
3.2.9

29 days ago

3.2.13

28 days ago

3.2.12

28 days ago

3.2.14

28 days ago

3.2.11

29 days ago

3.2.10

29 days ago

3.2.8

1 month ago

3.2.7

1 month ago

3.2.6

1 month ago

3.2.6-alpha.0

1 month ago

3.2.2

1 month ago

3.2.3

1 month ago

3.2.1

1 month ago

3.2.0

1 month ago

3.1.43

3 months ago

3.1.42

3 months ago

3.1.41

3 months ago

3.1.40

3 months ago

3.1.39

3 months ago

3.1.38

4 months ago

3.1.16

10 months ago

3.1.15

10 months ago

3.1.18

10 months ago

3.1.17

10 months ago

3.1.23

9 months ago

3.1.22

10 months ago

3.1.25

9 months ago

3.1.24

9 months ago

3.1.27

7 months ago

3.1.26

9 months ago

3.1.29

7 months ago

3.1.28

7 months ago

3.1.21

10 months ago

3.1.20

10 months ago

3.1.19

10 months ago

3.1.34

6 months ago

3.1.33

6 months ago

3.1.36

5 months ago

3.1.35

6 months ago

3.1.37

5 months ago

3.1.30

7 months ago

3.1.32

6 months ago

3.1.31

6 months ago

3.1.12

11 months ago

3.1.11

11 months ago

3.1.14

11 months ago

3.1.13

11 months ago

3.1.9

11 months ago

3.1.8

11 months ago

3.0.4

11 months ago

3.0.13

11 months ago

3.0.10

11 months ago

3.0.11

11 months ago

3.0.16

11 months ago

3.0.8

11 months ago

3.0.17

11 months ago

3.0.7

11 months ago

3.0.14

11 months ago

3.0.6

11 months ago

3.0.15

11 months ago

3.0.18

11 months ago

3.0.19

11 months ago

3.0.9

11 months ago

3.1.2

11 months ago

3.1.1

11 months ago

3.1.0

11 months ago

3.1.7

11 months ago

3.1.6

11 months ago

3.1.5

11 months ago

3.1.4

11 months ago

2.0.2

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago