CodingGarden / react-ts-starter

A bare-bones vite + react + typescript starter template with eslint + prettier, vitest + @testing-library and react-router
MIT License
204 stars 73 forks source link

eslintrc.cjs is Outdated #6

Open topeogunleye opened 1 month ago

topeogunleye commented 1 month ago

Please kindly include a link to https://eslint.org/blog/2024/05/eslint-configuration-migrator/ in your video description so that people who encounter module not defined error from their eslintrc.cjs file can use the migrator to update their config file: eslintrc.cjs to the latest eslint config format which is eslint.config.mjs or eslint.config.js.

Old vs New Config Format
Old Config image
New Config image
ofer-shaham commented 2 weeks ago

@topeogunleye would you mind providing the code as well ?

topeogunleye commented 1 week ago

encounter module not defined error from their eslintrc.cjs file can use the migrator to

@ofer-shaham, I don't mind. Thank you. Please kindly find the code below: Old config file: eslintrc.cjs:

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
  ],
  overrides: [],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    project: ['./tsconfig.json', './tsconfig.node.json'],
  },
  plugins: ['react', '@typescript-eslint', 'prettier'],
  rules: {
    'react/react-in-jsx-scope': 0,
  },
};

New config Filename: eslint.config.mjs

import react from "eslint-plugin-react";
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import prettier from "eslint-plugin-prettier";
import globals from "globals";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
    baseDirectory: __dirname,
    recommendedConfig: js.configs.recommended,
    allConfig: js.configs.all
});

export default [...compat.extends(
    "airbnb",
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
), {
    plugins: {
        react,
        "@typescript-eslint": typescriptEslint,
        prettier,
    },

    languageOptions: {
        globals: {
            ...globals.browser,
        },

        parser: tsParser,
        ecmaVersion: "latest",
        sourceType: "module",

        parserOptions: {
            project: ["./tsconfig.json", "./tsconfig.node.json"],
        },
    },

    rules: {
        "react/react-in-jsx-scope": 0,
    },
}];