3.4.10 • Published 7 months ago

dk-eslint-config v3.4.10

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Enterprise-quality ESLint config with TypeScript support

npm license

!WARNING
It's fine if you use this library from NPM package with a static versioning in case you want it for some pet-project or to test it's capabilities.

But for production use it's strongly recommended to create a fork, because I do not write Changelogs and may break / add some functionality without notice.

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.4.10

7 months ago

3.4.5

10 months ago

3.4.4

11 months ago

3.4.3

11 months ago

3.4.2

11 months ago

3.4.0

1 year ago

3.4.1

1 year ago

3.3.9

1 year ago

3.3.8

1 year ago

3.3.6

1 year ago

3.3.1

1 year ago

3.3.0

1 year ago

3.3.5

1 year ago

3.3.4

1 year ago

3.3.3

1 year ago

3.3.2

1 year ago

3.2.24

1 year ago

3.2.23

1 year ago

3.2.26

1 year ago

3.2.25

1 year ago

3.2.28

1 year ago

3.2.27

1 year ago

3.2.20

1 year ago

3.2.22

1 year ago

3.2.21

1 year ago

3.2.17

1 year ago

3.2.16

1 year ago

3.2.19

1 year ago

3.2.18

1 year ago

3.2.15

1 year ago

3.2.9

1 year ago

3.2.13

1 year ago

3.2.12

1 year ago

3.2.14

1 year ago

3.2.11

1 year ago

3.2.10

1 year ago

3.2.8

1 year ago

3.2.7

1 year ago

3.2.6

1 year ago

3.2.6-alpha.0

1 year ago

3.2.2

1 year ago

3.2.3

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.43

1 year ago

3.1.42

1 year ago

3.1.41

1 year ago

3.1.40

1 year ago

3.1.39

1 year ago

3.1.38

1 year ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.18

2 years ago

3.1.17

2 years ago

3.1.23

2 years ago

3.1.22

2 years ago

3.1.25

2 years ago

3.1.24

2 years ago

3.1.27

2 years ago

3.1.26

2 years ago

3.1.29

2 years ago

3.1.28

2 years ago

3.1.21

2 years ago

3.1.20

2 years ago

3.1.19

2 years ago

3.1.34

2 years ago

3.1.33

2 years ago

3.1.36

2 years ago

3.1.35

2 years ago

3.1.37

2 years ago

3.1.30

2 years ago

3.1.32

2 years ago

3.1.31

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.0.4

2 years ago

3.0.13

2 years ago

3.0.10

2 years ago

3.0.11

2 years ago

3.0.16

2 years ago

3.0.8

2 years ago

3.0.17

2 years ago

3.0.7

2 years ago

3.0.14

2 years ago

3.0.6

2 years ago

3.0.15

2 years ago

3.0.18

2 years ago

3.0.19

2 years ago

3.0.9

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

2.0.2

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago