From ecfb2a1541f8a9b47a0d099ca448c36e0f644edd Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Sun, 8 Sep 2024 11:25:43 -0700 Subject: [PATCH] Update eslint config --- eslint.config.js | 77 ++++++++++++++++++++++++++---------------------- package.json | 4 ++- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 5d719d18e..77eed47e1 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -4,17 +4,24 @@ * SPDX-License-Identifier: Apache-2.0 */ -import pluginJs from '@eslint/js'; +import eslint from '@eslint/js'; import noOnlyTests from 'eslint-plugin-no-only-tests'; import tseslint from 'typescript-eslint'; +/** + * We want to be able to lint non-TypeScript files. If we don't guard our + * TypeScript rules with a "files" constraint, eslint will try to lint all files + * using the TypeScript parser, which will fail for projects outside a + * TypeScript project. Maybe there is a simpler way to do this? + */ +const onlyTypeScriptFiles = (configs) => + configs.map((config) => ({...config, files: config.files ?? ['**/*.ts']})); + export default [ { + // List all visible files: + // npx eslint --debug 2>&1 | grep "eslint:eslint Lint" | cut -f 4- -d" " | sort ignores: [ - '**/*.js', - '**/*.cjs', - '**/*.mjs', - '**/*.d.ts', '**/.wireit/', '**/node_modules/', 'lib/', @@ -23,37 +30,35 @@ export default [ 'vscode-extension/built/', ], }, - { - files: ['**/*.ts'], - languageOptions: { - parser: tseslint.parser, - parserOptions: { - project: ['./tsconfig.json', './vscode-extension/tsconfig.json'], + eslint.configs.recommended, + ...onlyTypeScriptFiles([ + ...tseslint.configs.strictTypeChecked, + { + languageOptions: { + parserOptions: { + projectService: true, + tsconfigRootDir: import.meta.dirname, + }, + }, + plugins: { + 'no-only-tests': noOnlyTests, + }, + rules: { + 'no-only-tests/no-only-tests': 'error', + '@typescript-eslint/no-unused-vars': [ + 'error', + {argsIgnorePattern: '^_', varsIgnorePattern: '^_'}, + ], + '@typescript-eslint/no-non-null-assertion': 'off', + '@typescript-eslint/no-useless-constructor': 'off', + '@typescript-eslint/only-throw-error': 'off', + '@typescript-eslint/no-confusing-void-expression': 'off', + '@typescript-eslint/restrict-template-expressions': 'off', + '@typescript-eslint/no-unnecessary-condition': 'off', + '@typescript-eslint/no-unnecessary-type-arguments': 'off', + '@typescript-eslint/no-unnecessary-template-expression': 'off', + '@typescript-eslint/use-unknown-in-catch-callback-variable': 'off', }, }, - plugins: { - '@typescript-eslint': tseslint.plugin, - 'no-only-tests': noOnlyTests, - }, - }, - pluginJs.configs.recommended, - ...tseslint.configs.strictTypeChecked, - { - rules: { - 'no-only-tests/no-only-tests': 'error', - '@typescript-eslint/no-unused-vars': [ - 'error', - {argsIgnorePattern: '^_', varsIgnorePattern: '^_'}, - ], - '@typescript-eslint/no-non-null-assertion': 'off', - '@typescript-eslint/no-useless-constructor': 'off', - '@typescript-eslint/only-throw-error': 'off', - '@typescript-eslint/no-confusing-void-expression': 'off', - '@typescript-eslint/restrict-template-expressions': 'off', - '@typescript-eslint/no-unnecessary-condition': 'off', - '@typescript-eslint/no-unnecessary-type-arguments': 'off', - '@typescript-eslint/no-unnecessary-template-expression': 'off', - '@typescript-eslint/use-unknown-in-catch-callback-variable': 'off', - }, - }, + ]), ]; diff --git a/package.json b/package.json index c9584d5f9..e7baf95d9 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,9 @@ "#comment": "Need to build first so that the vscode-extension lint can see our types.", "command": "eslint --color --cache --cache-location .eslintcache .", "files": [ - "eslint.config.js" + "eslint.config.js", + "bin/*.js", + "website/*.{js,cjs}" ], "output": [] },