16.5.0 • Published 4 months ago

@jsenv/eslint-config v16.5.0

Weekly downloads
64
License
MIT
Repository
github
Last release
4 months ago

eslint-config npm package

ESLint config file consists into a single big object. This package allows to split this object to compose and reuse them.

  • :+1: Part of configuration that belongs together can be regrouped
  • :+1: ESLint configuration is easier to read

This is achieved by a function capable to compose subsets of ESLint configuration.

composeEslintConfig

composeEslintConfig is a function returning an eslint config object being the composition of eslint config objects passed in arguments.

const {
  composeEslintConfig,
  eslintConfigBase,
} = require("@jsenv/eslint-config");

const eslintConfig = composeEslintConfig(
  eslintConfigBase,
  // first "group": enable html plugin
  {
    plugins: ["html"],
    settings: {
      extensions: [".html"],
    },
  },
  // second "group": enable react plugin
  {
    plugins: ["react"],
    settings: {
      extensions: [".jsx"],
    },
  },
);

module.exports = eslintConfig;

Composable eslint configs

ESLint configDescription
eslintConfigBaseEnable latest js features
eslintConfigForPrettierDisable eslint rules already handled by prettier
eslintConfigToPreferExplicitGlobalsForce code to use global variables explicitly; like window.event

Advanced configuration example

The following code is meant to be put into an .eslintrc.cjs file and does the following:

  1. Reuse jsenv configuration for ESLint rules
  2. Use ESLint import plugin with a custom resolver
  3. Consider files as written for node by default
  4. Consider a subset of files as written for browsers
  5. Use html plugin to enable linting of html files
  6. Disable ESLint rules already handled by prettier
const {
  composeEslintConfig,
  eslintConfigBase,
  jsenvEslintRules,
  jsenvEslintRulesForImport,
  eslintConfigToPreferExplicitGlobals,
  eslintConfigForPrettier,
} = require("@jsenv/eslint-config");

const eslintConfig = composeEslintConfig(
  eslintConfigBase,
  {
    rules: {
      ...jsenvEslintRules,
      "operator-assignment": ["error", "always"], // override jsenv rules
    },
  },
  // import plugin
  {
    plugins: ["import"],
    settings: {
      "import/resolver": {
        "@jsenv/eslint-import-resolver": {
          rootDirectoryUrl: __dirname,
          packageConditions: ["node", "import"],
        },
      },
      "import/extensions": [".js", ".mjs"],
    },
    rules: jsenvEslintRulesForImport,
  },
  // files are written for Node.js by default
  {
    env: {
      node: true,
    },
  },
  // package is "type": "module" so:
  // 1. disable commonjs globals by default
  // 2. Re-enable commonjs into *.cjs files
  {
    globals: {
      __filename: "off",
      __dirname: "off",
      require: "off",
      exports: "off",
    },
    overrides: [
      {
        files: ["**/*.cjs"],
        env: {
          commonjs: true,
        },
        // inside *.cjs files. restore commonJS "globals"
        globals: {
          __filename: true,
          __dirname: true,
          require: true,
          exports: true,
        },
      },
    ],
  },
  // several files are written for browsers, not Node.js
  {
    overrides: [
      {
        files: ["**/**/*.html", "**/src/**/*.js"],
        env: {
          browser: true,
          node: false,
        },
        settings: {
          "import/resolver": {
            "@jsenv/eslint-import-resolver": {
              rootDirectoryUrl: __dirname,
              packageConditions: ["browser", "import"],
            },
          },
        },
      },
    ],
  },
  // html plugin
  {
    plugins: ["html"],
    settings: {
      extensions: [".html"],
    },
  },
  eslintConfigToPreferExplicitGlobals,
  // We are using prettier, disable all eslint rules
  // already handled by prettier.
  eslintConfigForPrettier,
);

module.exports = eslintConfig;

The above configuration uses @jsenv/eslint-import-resolver to resolve import so it needs to be installed.

npm install --save-dev @jsenv/eslint-import-resolver

Composable ESLint rules

RulesDescription
jsenvEslintRulesjsenv rules for ESLint
jsenvEslintRulesForImportjsenv rules for eslint-plugin-import
jsenvEslintRulesForReactjsenv rules for project using react and eslint-plugin-react

Common use cases

Top level await

It will be supported by default in ESLint 8. Until then you need:

  1. "@babel/eslint-parser" and "@babel/core" in your devDependencies
  2. Configure ESLint parser to "@babel/eslint-parser"
npm install --save-dev @babel/eslint-parser
npm install --save-dev @babel/core

.eslintrc.cjs:

const {
  composeEslintConfig,
  eslintConfigBase,
} = require("@jsenv/eslint-config");

const eslintConfig = composeEslintConfig(
  eslintConfigBase,
  // use "@babel/eslint-parser" until top level await is supported by ESLint default parser
  {
    parser: "@babel/eslint-parser",
    parserOptions: {
      requireConfigFile: false,
    },
  },
);

module.exports = eslintConfig;

React

const {
  composeEslintConfig,
  eslintConfigBase,
  jsenvEslintRulesForReact,
} = require("@jsenv/eslint-config");

const eslintConfig = composeEslintConfig(
  eslintConfigBase,
  // react
  {
    plugins: ["react"],
    settings: {
      react: {
        version: "detect",
      },
    },
    rules: jsenvEslintRulesForReact,
  },
);

module.exports = eslintConfig;

JSX

  1. "@babel/eslint-parser" and "@babel/plugin-syntax-jsx" in your devDependencies
  2. Enable @babel/plugin-syntax-jsx in babel config file
  3. Configure ESLint parser to "@babel/eslint-parser"
npm install --save-dev @babel/eslint-parser
npm install --save-dev @babel/plugin-syntax-jsx

babel.config.cjs:

const babelPluginSyntaxJSX = require("@babel/plugin-syntax-jsx");

module.exports = {
  plugins: [
    [
      babelPluginSyntaxJSX,
      {
        pragma: "React.createElement",
        pragmaFrag: "React.Fragment",
      },
    ],
  ],
};

.eslintrc.cjs:

const {
  composeEslintConfig,
  eslintConfigBase,
  jsenvEslintRulesForReact,
} = require("@jsenv/eslint-config");

const eslintConfig = composeEslintConfig(
  eslintConfigBase,
  // jsx
  {
    parser: "@babel/eslint-parser",
    parserOptions: {
      ecmaFeatures: {
        jsx: true,
      },
    },
    settings: {
      extensions: [".jsx"],
    },
  },
);

module.exports = eslintConfig;

HTML in VSCode

In ".vscode/settings.json" file, add

"eslint.validate": ["javascript", "html"]
16.5.0

4 months ago

16.4.5

6 months ago

16.4.4

7 months ago

16.4.3

8 months ago

16.4.2

9 months ago

16.4.1

10 months ago

16.4.0

10 months ago

16.3.7

1 year ago

16.3.6

1 year ago

16.3.5

1 year ago

16.3.4

1 year ago

16.3.9

1 year ago

16.3.8

1 year ago

16.2.10

1 year ago

16.3.3

1 year ago

16.3.2

1 year ago

16.3.1

1 year ago

16.3.0

1 year ago

16.2.8

1 year ago

16.2.7

1 year ago

16.2.9

1 year ago

16.2.6

2 years ago

16.2.5

2 years ago

16.2.4

2 years ago

16.2.3

2 years ago

16.2.2

2 years ago

16.1.0

2 years ago

16.2.0

2 years ago

16.2.1

2 years ago

16.0.9

2 years ago

16.0.8

3 years ago

16.0.7

3 years ago

16.0.6

3 years ago

16.0.5

3 years ago

16.0.4

3 years ago

16.0.2

3 years ago

16.0.3

3 years ago

16.0.1

3 years ago

16.0.0

3 years ago

15.0.2

3 years ago

15.0.0

3 years ago

15.0.1

3 years ago

14.0.0

3 years ago

13.0.0

3 years ago

13.0.1

3 years ago

12.10.0

3 years ago

12.9.1

3 years ago

12.9.0

3 years ago

12.8.0

3 years ago

12.7.0

4 years ago

12.6.0

4 years ago

12.6.0-alpha.0

4 years ago

12.5.0

4 years ago

12.4.1

4 years ago

12.3.0

4 years ago

12.3.1

4 years ago

12.2.0

4 years ago

12.1.0

4 years ago

12.0.1

4 years ago

12.0.0

4 years ago

11.3.0

5 years ago

11.2.0

5 years ago

11.1.0

5 years ago

11.0.0

5 years ago

10.1.0

5 years ago

10.0.0

5 years ago

9.9.0

5 years ago

9.8.0

5 years ago

9.7.0

5 years ago

9.6.0

5 years ago

9.5.0

5 years ago

9.4.0

5 years ago

9.3.0

5 years ago

9.2.0

5 years ago

9.1.0

5 years ago

9.0.0

5 years ago

8.0.0

5 years ago

7.0.0

5 years ago