eslint-config
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,
{
plugins: ["html"],
settings: {
extensions: [".html"],
},
},
{
plugins: ["react"],
settings: {
extensions: [".jsx"],
},
},
);
module.exports = eslintConfig;
Composable eslint configs
Advanced configuration example
The following code is meant to be put into an .eslintrc.cjs file and does the following:
- Reuse jsenv configuration for ESLint rules
- Use ESLint import plugin with a custom resolver
- Consider files as written for node by default
- Consider a subset of files as written for browsers
- Use html plugin to enable linting of html files
- 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"],
},
},
{
plugins: ["import"],
settings: {
"import/resolver": {
"@jsenv/eslint-import-resolver": {
rootDirectoryUrl: __dirname,
packageConditions: ["node", "import"],
},
},
"import/extensions": [".js", ".mjs"],
},
rules: jsenvEslintRulesForImport,
},
{
env: {
node: true,
},
},
{
globals: {
__filename: "off",
__dirname: "off",
require: "off",
exports: "off",
},
overrides: [
{
files: ["**/*.cjs"],
env: {
commonjs: true,
},
globals: {
__filename: true,
__dirname: true,
require: true,
exports: true,
},
},
],
},
{
overrides: [
{
files: ["**/**/*.html", "**/src/**/*.js"],
env: {
browser: true,
node: false,
},
settings: {
"import/resolver": {
"@jsenv/eslint-import-resolver": {
rootDirectoryUrl: __dirname,
packageConditions: ["browser", "import"],
},
},
},
},
],
},
{
plugins: ["html"],
settings: {
extensions: [".html"],
},
},
eslintConfigToPreferExplicitGlobals,
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
Common use cases
Top level await
It will be supported by default in ESLint 8. Until then you need:
"@babel/eslint-parser"
and "@babel/core"
in your devDependencies- 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,
{
parser: "@babel/eslint-parser",
parserOptions: {
requireConfigFile: false,
},
},
);
module.exports = eslintConfig;
React
const {
composeEslintConfig,
eslintConfigBase,
jsenvEslintRulesForReact,
} = require("@jsenv/eslint-config");
const eslintConfig = composeEslintConfig(
eslintConfigBase,
{
plugins: ["react"],
settings: {
react: {
version: "detect",
},
},
rules: jsenvEslintRulesForReact,
},
);
module.exports = eslintConfig;
JSX
"@babel/eslint-parser"
and "@babel/plugin-syntax-jsx"
in your devDependencies- Enable
@babel/plugin-syntax-jsx
in babel config file - 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,
{
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"]