There have been eslint changes such as the config file format over time and I find the googled and other questions / answers to be confusing and not work for me. they push me to use a cjs file then i have problems with format and its a mess.
Is there a set of procedures for the current time that works well today?
1
-
Be on (at least) node 18 or yuou’ll get confusing error messages about “rules” key
-
Initialize eslint
npm init @eslint/config@latest`
- Install the following
npm install eslint-plugin-playwright
npm install @typescript-eslint/eslint-plugin
- Modify the
eslint.config.mjs
created in step 2 to be as follows, using the rules you see fit for your project:
import js from '@eslint/js';
import playwright from "eslint-plugin-playwright";
import typescriptEslintPlugin from '@typescript-eslint/eslint-plugin';
import typescriptEslintParser from '@typescript-eslint/parser';
export default [
js.configs.recommended,
{
plugins: {
'@typescript-eslint': typescriptEslintPlugin
},
files: ['e2e/**/*.[jt]s'],
rules: {
"indent": ['error', 2],
"max-len": ['error', 100],
"quotes": ['error', 'single'],
"eqeqeq": "error",
"semi": "error",
"no-var": "error",
"no-new": "error",
"vars-on-top": "error",
"default-param-last": "error",
"no-tabs": "error",
"no-trailing-spaces": "error",
"no-nested-ternary": "error",
"no-multiple-empty-lines": "error",
"no-extra-parens": "error",
"default-case": "error",
"no-unused-vars": "error",
"prefer-const": "error",
"no-const-assign": "error",
"no-constant-binary-expression": "error",
"no-constant-condition": "error",
"no-dupe-args": "error",
"no-dupe-else-if": "error",
"no-dupe-keys": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-empty-pattern": "error",
"no-irregular-whitespace": "error",
"no-undef": "error",
"no-unexpected-multiline": "error",
"no-unreachable": "error",
"no-unreachable-loop": "error",
"camelcase": "error",
"consistent-return": "error",
"max-lines": ["error", 120],
"no-else-return": "error",
"no-eq-null": "error",
"no-eval": "error",
"no-loop-func": "error",
"no-native-reassign": "error",
"no-param-reassign": "error",
"no-self-compare": "error",
"prefer-template": "error",
"comma-spacing": "error",
"no-multi-spaces": "error",
"space-in-parens": ['error', 'never'],
"array-bracket-spacing": ['error', 'never'],
"accessor-pairs": "error",
"grouped-accessor-pairs": "error",
"block-scoped-var": "error",
"no-implicit-coercion": "error",
"no-invalid-this": "error",
"no-magic-numbers": ["error", { "ignoreDefaultValues": true }],
"no-useless-return": "error",
"prefer-regex-literals": "error",
"require-await": "error",
},
languageOptions: {
ecmaVersion: 2022,
parser: typescriptEslintParser,
parserOptions: {
sourceType: 'module'
},
},
},
{
plugins: { playwright },
files: ['**/*.spec.[jt]s'],
...playwright.configs['flat/recommended'],
rules: {
"playwright/no-skipped-test": "error",
"playwright/no-commented-out-tests": "error",
"playwright/prefer-comparison-matcher": "error",
"playwright/prefer-equality-matcher": "error",
"playwright/prefer-strict-equal": "error",
"playwright/prefer-to-be": "error",
"playwright/prefer-to-contain": "error",
"playwright/prefer-to-have-count": "error",
"playwright/prefer-to-have-length": "error",
"playwright/require-top-level-describe": "error",
"playwright/valid-expect": "error",
"playwright/prefer-hooks-in-order": "error",
"playwright/prefer-hooks-on-top": "error",
}
}
];
Package.json
{
"engines": {
"node": ">=18.0.0"
},
"type": "module",
"name": "major_sites_typescript",
"version": "1.0.0",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@playwright/test": "^1.45.3",
"@types/lodash.isequal": "^4.5.8",
"@types/node": "^22.1.0",
"eslint": "^9.8.0",
"eslint-plugin-playwright": "^1.6.2"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^8.1.0",
"lodash": "^4.17.21",
"lodash.isequal": "^4.5.0"
}
}