Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend CRA ESLint rules with EXTEND_ESLINT environment variable

Recently Facebook's Create React App (CRA) released a new feature which allows you to extend their base ESLint rules.

We recognise that in some cases, further customisation is required. It is now possible to extend the base ESLint config by setting the EXTEND_ESLINT environment variable to true. Setting Up Your Editor

Here is the example given but with no detail such as filename or what "shared-config" is.

{
    "eslintConfig": {
        "extends": ["react-app", "shared-config"],
        "rules": {
            "additional-rule": "warn"
        },
        "overrides": [
            {
                 "files": ["**/*.ts?(x)"],
                 "rules": {
                     "additional-typescript-only-rule": "warn"
                 }
             }
        ]
    }
}

The feature is enabled by added an environment variable.

EXTEND_ESLINT=true

but on the documentation page it also doesn't give any information how to use it - Advanced configuation

I've added their example code to my build in a file called .eslintrc.json but I get a build error:

"Error: ESLint configuration in .eslintrc.json is invalid: - Unexpected top-level property "eslintConfig"."

Has anyone got this working? Does the file need to export a module?

like image 211
sidonaldson Avatar asked Jan 23 '20 12:01

sidonaldson


People also ask

How do you change the ESLint rule?

To change a rule setting, you must set the rule ID equal to one of these values: "off" or 0 - turn the rule off. "warn" or 1 - turn the rule on as a warning (doesn't affect exit code) "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)

Does CRA include ESLint?

CRA will usually show the Lint warnings/errors in the terminal when you run the application. Also if you have the ESLint plugins properly configured in you Editor or IDE, the errors/warnings will be shown inline.

How do I make ESLint run faster?

Speeding up ESLint Execution One of the most convenient ways to speed up ESLint execution on big projects is to run it on only files that have been changed while you are working. It's possible to achieve this by using lint-staged. The exact technique is covered in the Automation chapter.

How do I configure ESLint?

There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.


1 Answers

While unclear from the Create-React-App documentation, the example they give is as if the project's ESLint configuration was inside the eslintConfig property of the package.json file.

You need to configure ESLint as described in its documentation. So if you choose the .eslintrc.json way, it must be a valid ESLint configuration file, which doesn't have a eslintConfig property.

The only things that matter in the example are:

  • they're extending from "react-app" before any other configuration
  • any additional rule is set to "warn" to avoid stopping the project from building
  • if using TypeScript, place the specific TS related configuration in the "overrides" section.

A simple .eslintrc.js (notice the extension) configuration file for a CRA project using TypeScript could be as follows:

const defaultRules = [
  'react-app',
  'eslint:recommended',
  // any other plugins or custom configuration you'd like to extend from.
];

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2017,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  env: {
    browser: true,
    node: true,
    es6: true,
    jest: true,
  },
  extends: defaultRules,
  rules: {
    'array-callback-return': 'warn',
    'consistent-return': 'warn',
    'default-case': 'warn',
    // etc.
  },
  overrides: [
    {
      files: ['**/*.ts', '**/*.tsx'],
      plugins: ['@typescript-eslint'],
      extends: [
        ...defaultRules,
        'plugin:@typescript-eslint/recommended',
        // any other TypeScript specific config (from a plugin, or custom)
      ],
      rules: {
        '@typescript-eslint/no-explicit-any': 'warn',
        '@typescript-eslint/no-unused-vars': 'warn',
        '@typescript-eslint/no-unused-expressions': 'warn',
        // etc.
      },
    },
  ],
  settings: {
    react: {
      // React version. "detect" automatically picks the version you have installed.
      version: 'detect',
    },
  },
};
like image 115
Emile Bergeron Avatar answered Nov 04 '22 10:11

Emile Bergeron