iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🧶

Best Practices for Migrating to ESLint Flat Config

に公開

In ESLint v9.0.0, Flat Config (eslint.config.js) has become the default. While the previous .eslintrc file format is still supported, it seems you need to set the ESLINT_USE_FLAT_CONFIG environment variable.

https://eslint.org/blog/2024/04/eslint-v9.0.0-released/

Taking this opportunity, I decided to organize and rewrite my usual rules using Flat Config. Here is a summary of what I found useful during the process.

The configurations I created are published as a Sharable Config.

https://github.com/mkizka/eslint-config

Using typescript-eslint's tseslint.config

Flat config is written as an array like this:

export default [
  {
    rules: {
      "no-unused-vars": "error",
      "no-undef": "error"
    }
  }
];

This is fine, but having type safety is convenient. In typescript-eslint, you can use a function called tseslint.config, which provides the type equivalent for this array portion.

https://typescript-eslint.io/getting-started

// @ts-check
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  {
    rules: {
      // Add rules here
    }
  }
);

tseslint.config supports extends

tseslint.config is a function that simply provides types for its arguments and returns them as an array; however, you can also use an extends property.

This extends is used as follows:

export default tseslint.config({
  files: ['**/*.ts'],
  extends: [
    eslint.configs.recommended,
    ...tseslint.configs.recommended,
  ],
  rules: {
    '@typescript-eslint/array-type': 'error',
    '@typescript-eslint/consistent-type-imports': 'error',
  },
});

This is equivalent to the following:

export default [
  {
    files: ['**/*.ts'],
    rules: {
      // Many rules from eslint.configs.recommended
    }
  }
   {
    files: ['**/*.ts'],
    rules: {
      // Many rules from tseslint.configs.recommended
    }
  }
  // Other tseslint.configs.recommended rules (omitted)
  {
    files: ['**/*.ts'],
    rules: {
      // The rules originally specified at the end
      '@typescript-eslint/array-type': 'error',
      '@typescript-eslint/consistent-type-imports': 'error',
    },
  },
];

This function takes the values specified in files (or ignores) and applies them to each rule in extends as well as the rules outside of extends. Objects containing extends will ultimately result in (number of elements in extends + 1) objects.

Checking the implementation makes it easier to understand. It is a fairly short function:

https://github.com/typescript-eslint/typescript-eslint/blob/679ed8efacd0cdecc3daab8f5dd0625c1075afb9/packages/typescript-eslint/src/config-helper.ts#L85-L107

This allows you to specify files for multiple ESLint or typescript-eslint configurations at once. It is convenient when you have rules that you want to apply only to files with specific extensions, as in the example above.

Combining with @eslint/eslintrc

When combined with FlatCompat from @eslint/eslintrc, you can also write it like this:

[
   {
    extends: compat.extends("Library that does not support Flat Config")
    rules: {
      // Rules for the library above
    },
  }
]

This results in the same outcome as the code below, but I personally find it easier to read since the plugin and its rules are grouped into a single object:

[
   ...compat.extends("Library that does not support Flat Config")
  {
    rules: {
      // Rules for the library above
    },
  }
]

Writing snapshot tests for your rules

If you want to track changes in rules before and after migrating to Flat Config, or if you want to change how you write your settings while keeping the same rules, snapshot testing is very useful.

For snapshot testing, you can use the calculateConfigForFile method of the ESLint or FlatESLint class.

https://eslint.org/docs/latest/integrate/nodejs-api#-eslintcalculateconfigforfilefilepath

You can test the configuration for a .js file by writing it like this:

import { ESLint } from "eslint";
test("index.js", async () => {
  const eslint = new ESLint();
  const result = await eslint.calculateConfigForFile("index.js");
  expect(result).toMatchSnapshot();
});

If you are using ESLint v8

The test above is confirmed to work with ESLint 9.0.0.

In ESLint v8, the ESLint class supports the .eslintrc format, while the FlatESLint class supports Flat Config.

The FlatESLint class is located in eslint/use-at-your-own-risk. When I tried it, it didn't have type definitions, but I was able to import and use it.

import { FlatESLint as ESLint } from "eslint/use-at-your-own-risk.js";

https://eslint.org/blog/2022/08/new-config-system-part-3/#using-flat-config-with-the-eslint-class

You should also be able to use this to output previous settings using the ESLint class and new settings using the FlatESLint class, then compare them.

ESLint plugins can (sometimes) work without FlatCompat

Among ESLint libraries, libraries of the eslint-config-xxx type that you use by extending them require you to use the extends method of FlatCompat if they do not support Flat Config.

Libraries of the eslint-plugin-xxx type can also use the plugins method of FlatCompat, but sometimes they work just fine as they are.

For example, with eslint-plugin-unused-imports, writing it as follows worked.

import unusedImports from "eslint-plugin-unused-imports";
// Omitted
  {
    plugins: {
      "unused-imports": unusedImports,
    },
    rules: {
      "@typescript-eslint/no-unused-vars": "off",
      "unused-imports/no-unused-imports": "error",
    },
  },

There was an issue in the repository regarding this same topic.

https://github.com/sweepline/eslint-plugin-unused-imports/issues/74

@eslint/config-inspector is useful

I was planning to write about this as well, but just as I started writing, the official article was released, so I will just include the link here.

https://eslint.org/blog/2024/04/eslint-config-inspector/

This is a tool that allows you to check in a UI what your configured rules ultimately look like.

Conclusion

I have summarized what I did when writing ESLint settings with Flat Config. I highly recommend creating your own Sharable Config.

GitHubで編集を提案

Discussion