|
| 1 | +import js from '@eslint/js'; |
| 2 | +import globals from 'globals'; |
| 3 | +import { FlatCompat } from '@eslint/eslintrc'; |
| 4 | +import tsParser from '@typescript-eslint/parser'; |
| 5 | + |
| 6 | +import pluginTypescriptEslint from '@typescript-eslint/eslint-plugin'; |
| 7 | +import pluginImport from 'eslint-plugin-import'; |
| 8 | +import pluginUnusedImports from 'eslint-plugin-unused-imports'; |
| 9 | +import pluginHeader from 'eslint-plugin-header'; |
| 10 | +import pluginStylistic from '@stylistic/eslint-plugin'; |
| 11 | + |
| 12 | +import path from 'node:path'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | + |
| 15 | +const __filename = fileURLToPath(import.meta.url); |
| 16 | +const __dirname = path.dirname(__filename); |
| 17 | + |
| 18 | +const compat = new FlatCompat({ |
| 19 | + baseDirectory: __dirname, |
| 20 | + recommendedConfig: js.configs.recommended, |
| 21 | + allConfig: js.configs.all |
| 22 | +}); |
| 23 | + |
| 24 | +// Workaround, see https://github.com/Stuk/eslint-plugin-header/issues/57#issuecomment-2378485611 |
| 25 | +pluginHeader.rules.header.meta.schema = false; |
| 26 | + |
| 27 | +export default [{ |
| 28 | + ignores: [ |
| 29 | + '**/node_modules/**/*', |
| 30 | + '**/bin/**/*', |
| 31 | + '**/bundle/**/*', |
| 32 | + '**/dist/**/*', |
| 33 | + '**/lib/**/*', |
| 34 | + '**/out/**/*', |
| 35 | + '**/resources/**/*', |
| 36 | + '**/production/**/*', |
| 37 | + '**/scripts/**/*', |
| 38 | + '**/packages/generator-langium/app/**/*', |
| 39 | + '**/packages/generator-langium/**/templates/**/*', |
| 40 | + '**/*env.d.ts', |
| 41 | + '**/esbuild.mjs', |
| 42 | + // WA: 'no-useless-escape': 'off' has no effect |
| 43 | + '**/examples/**/*.monarch.ts' |
| 44 | + ], |
| 45 | +}, ...compat.extends('eslint:recommended', 'plugin:@typescript-eslint/recommended'), { |
| 46 | + files: [ |
| 47 | + '**/src/**/*.ts', |
| 48 | + '**/src/**/*.tsx', |
| 49 | + '**/test/**/*.ts', |
| 50 | + '**/test/**/*.tsx' |
| 51 | + ], |
| 52 | + plugins: { |
| 53 | + '@typescript-eslint': pluginTypescriptEslint, |
| 54 | + import: pluginImport, |
| 55 | + 'unused-imports': pluginUnusedImports, |
| 56 | + pluginHeader, |
| 57 | + '@stylistic': pluginStylistic |
| 58 | + }, |
| 59 | + languageOptions: { |
| 60 | + globals: { |
| 61 | + ...globals.node, |
| 62 | + ...globals.browser |
| 63 | + }, |
| 64 | + parser: tsParser, |
| 65 | + ecmaVersion: 2020, |
| 66 | + sourceType: 'module', |
| 67 | + parserOptions: { |
| 68 | + project: ['./tsconfig.json'] |
| 69 | + } |
| 70 | + }, |
| 71 | + // List of [ESLint rules](https://eslint.org/docs/rules/) |
| 72 | + rules: { |
| 73 | + // do not force arrow function parentheses |
| 74 | + 'arrow-parens': ['off', 'as-needed'], |
| 75 | + // checks the correct use of super() in sub-classes |
| 76 | + 'constructor-super': 'error', |
| 77 | + // obj.a instead of obj['a'] when possible |
| 78 | + 'dot-notation': 'error', |
| 79 | + // ban '==', don't use 'smart' option! |
| 80 | + eqeqeq: 'error', |
| 81 | + // needs obj.hasOwnProperty(key) checks |
| 82 | + 'guard-for-in': 'error', |
| 83 | + // new Error() instead of new Error |
| 84 | + 'new-parens': 'error', |
| 85 | + // bitwise operators &, | can be confused with &&, || |
| 86 | + 'no-bitwise': 'error', |
| 87 | + // ECMAScript deprecated arguments.caller and arguments.callee |
| 88 | + 'no-caller': 'error', |
| 89 | + // assignments if (a = '1') are error-prone |
| 90 | + 'no-cond-assign': 'error', |
| 91 | + // disallow debugger; statements |
| 92 | + 'no-debugger': 'error', |
| 93 | + // eval is considered unsafe |
| 94 | + 'no-eval': 'error', |
| 95 | + // we need to have 'namespace' functions when using TS 'export =' |
| 96 | + 'no-inner-declarations': 'off', |
| 97 | + // GOTO is only used in BASIC ;) |
| 98 | + 'no-labels': 'error', |
| 99 | + // two or more empty lines need to be fused to one |
| 100 | + 'no-multiple-empty-lines': ['error', { |
| 101 | + max: 1 |
| 102 | + }], |
| 103 | + // there is no reason to wrap primitve values |
| 104 | + 'no-new-wrappers': 'error', |
| 105 | + // only throw Error but no objects {} |
| 106 | + 'no-throw-literal': 'error', |
| 107 | + // trim end of lines |
| 108 | + 'no-trailing-spaces': 'error', |
| 109 | + // safe try/catch/finally behavior |
| 110 | + 'no-unsafe-finally': 'error', |
| 111 | + // use const and let instead of var |
| 112 | + 'no-var': 'error', |
| 113 | + // space in function decl: f() vs async () => {} |
| 114 | + 'space-before-function-paren': ['error', { |
| 115 | + anonymous: 'never', |
| 116 | + asyncArrow: 'always', |
| 117 | + named: 'never' |
| 118 | + }], |
| 119 | + // Always use semicolons at end of statement |
| 120 | + semi: [2, 'always'], |
| 121 | + // Prefer single quotes |
| 122 | + quotes: [2, 'single', { |
| 123 | + avoidEscape: true |
| 124 | + }], |
| 125 | + // isNaN(i) Number.isNaN(i) instead of i === NaN |
| 126 | + 'use-isnan': 'error', |
| 127 | + // Use MIT file header |
| 128 | + 'pluginHeader/header': [2, 'block', [ |
| 129 | + { pattern: 'MIT License|DO NOT EDIT MANUALLY!' } |
| 130 | + ]], |
| 131 | + 'no-restricted-imports': ['error', { |
| 132 | + paths: [{ |
| 133 | + name: 'vscode-jsonrpc', |
| 134 | + importNames: [ 'CancellationToken' ], |
| 135 | + message: 'Import "CancellationToken" via "Cancellation.CancellationToken" from "langium", or directly from "./utils/cancellation.ts" within Langium.' |
| 136 | + }, { |
| 137 | + name: 'vscode-jsonrpc/', |
| 138 | + importNames: [ 'CancellationToken'], |
| 139 | + message: 'Import "CancellationToken" via "Cancellation.CancellationToken" from "langium", or directly from "./utils/cancellation.ts" within Langium.' |
| 140 | + }], |
| 141 | + patterns: [ { |
| 142 | + group: [ 'vscode-jsonrpc' ], |
| 143 | + importNamePattern: '^(?!CancellationToken)', |
| 144 | + message: 'Don\'t import types or symbols from "vscode-jsonrpc" (package index), as that brings a large overhead in bundle size. Import from "vscode-jsonrpc/lib/common/...js" and add a // eslint-disable..., if really necessary.' |
| 145 | + }] |
| 146 | + }], |
| 147 | + // use @typescript-eslint/no-unused-vars instead |
| 148 | + 'no-unused-vars': 'off', |
| 149 | + // Disallow unnecessary escape characters |
| 150 | + 'no-useless-escape': 'off', |
| 151 | + |
| 152 | + // List of [@typescript-eslint rules](https://typescript-eslint.io/rules/) |
| 153 | + // Require that function overload signatures be consecutive |
| 154 | + '@typescript-eslint/adjacent-overload-signatures': 'error', |
| 155 | + // Require consistently using either T[] or Array<T> for arrays |
| 156 | + '@typescript-eslint/array-type': ['error', { |
| 157 | + default: 'array-simple' |
| 158 | + }], |
| 159 | + // Disallow accidentally using the 'empty object' type |
| 160 | + '@typescript-eslint/no-empty-object-type': 'error', |
| 161 | + // Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean |
| 162 | + '@typescript-eslint/no-inferrable-types': 'off', |
| 163 | + // Disallow using the unsafe built-in Function type |
| 164 | + '@typescript-eslint/no-unsafe-function-type': 'error', |
| 165 | + // Disallow using confusing built-in primitive class wrappers |
| 166 | + '@typescript-eslint/no-wrapper-object-types': 'error', |
| 167 | + // Disallow the `any` type |
| 168 | + '@typescript-eslint/no-explicit-any': 'error', |
| 169 | + // Enforce valid definition of `new` and `constructor` |
| 170 | + '@typescript-eslint/no-misused-new': 'error', |
| 171 | + // Disallow TypeScript namespaces |
| 172 | + '@typescript-eslint/no-namespace': 'off', |
| 173 | + // Disallow non-null assertions using the ! postfix operator |
| 174 | + '@typescript-eslint/no-non-null-assertion': 'off', |
| 175 | + // Require or disallow parameter properties in class constructors |
| 176 | + '@typescript-eslint/parameter-properties': 'off', |
| 177 | + // Disallow unused variables |
| 178 | + '@typescript-eslint/no-unused-vars': ['error', { |
| 179 | + caughtErrorsIgnorePattern: '^_', |
| 180 | + argsIgnorePattern: '^_', |
| 181 | + varsIgnorePattern: '^_' |
| 182 | + }], |
| 183 | + // isallow require statements except in import statements |
| 184 | + '@typescript-eslint/no-var-requires': 'error', |
| 185 | + // Enforce the use of `for-of` loop over the standard `for` loop where possible |
| 186 | + '@typescript-eslint/prefer-for-of': 'error', |
| 187 | + // Require using `namespace` keyword over `module` keyword to declare custom TypeScript modules |
| 188 | + '@typescript-eslint/prefer-namespace-keyword': 'error', |
| 189 | + // Disallow certain triple slash directives in favor of ES6-style import declarations |
| 190 | + '@typescript-eslint/triple-slash-reference': 'error', |
| 191 | + // Disallow conditionals where the type is always truthy or always falsy |
| 192 | + '@typescript-eslint/no-unnecessary-condition': 'off', |
| 193 | + // Disallow unused expressions |
| 194 | + '@typescript-eslint/no-unused-expressions': 'off', |
| 195 | + // Enforce consistent usage of type imports |
| 196 | + '@typescript-eslint/consistent-type-imports': 'error', |
| 197 | + |
| 198 | + // List of [@stylistic rules](https://eslint.style/rules) |
| 199 | + // Enforce consistent indentation |
| 200 | + '@stylistic/indent': 'error', |
| 201 | + // Require consistent spacing around type annotations |
| 202 | + '@stylistic/type-annotation-spacing': 'error' |
| 203 | + } |
| 204 | +}]; |
0 commit comments