|
| 1 | +import eslint from "@eslint/js"; |
| 2 | +import tseslint from "typescript-eslint"; |
| 3 | +import globals from "globals"; |
| 4 | + |
| 5 | +export default tseslint.config( |
| 6 | + { |
| 7 | + ignores: [ |
| 8 | + "node_modules/**", |
| 9 | + "dist/**", |
| 10 | + "build/**", |
| 11 | + "out/**", |
| 12 | + ".github/**", |
| 13 | + "src/glue/wasm/**", |
| 14 | + "std/assembly/**", |
| 15 | + "std/portable/**", |
| 16 | + "tests/compiler/**", |
| 17 | + "tests/parser/**", |
| 18 | + "tests/asconfig/**/assembly/**", |
| 19 | + "lib/loader/tests/assembly/**" |
| 20 | + ] |
| 21 | + }, |
| 22 | + |
| 23 | + eslint.configs.recommended, |
| 24 | + |
| 25 | + // === General rules ========================================================= |
| 26 | + |
| 27 | + { |
| 28 | + languageOptions: { |
| 29 | + ecmaVersion: "latest", |
| 30 | + sourceType: "module", |
| 31 | + globals: { |
| 32 | + "globalThis": "readonly", |
| 33 | + "BigInt64Array": "readonly", |
| 34 | + "BigUint64Array": "readonly", |
| 35 | + "WebAssembly": "readonly", |
| 36 | + "FinalizationRegistry": "readonly", |
| 37 | + "fetch": "readonly", |
| 38 | + "URL": "readonly", |
| 39 | + "console": "readonly" |
| 40 | + } |
| 41 | + }, |
| 42 | + rules: { |
| 43 | + // Omitted semicolons are hugely popular, yet within the compiler it makes |
| 44 | + // sense to be better safe than sorry. |
| 45 | + "semi": "error", |
| 46 | + |
| 47 | + // Our code bases uses 2 spaces for indentation, and we enforce it here so |
| 48 | + // files don't mix spaces, tabs or different indentation levels. |
| 49 | + "indent": ["error", 2, { |
| 50 | + "SwitchCase": 1, |
| 51 | + "VariableDeclarator": "first", |
| 52 | + "offsetTernaryExpressions": true, |
| 53 | + "ignoredNodes": [ // FIXME: something's odd here |
| 54 | + "ConditionalExpression > *", |
| 55 | + "ConditionalExpression > * > *", |
| 56 | + "ConditionalExpression > * > * > *" |
| 57 | + ] |
| 58 | + }], |
| 59 | + |
| 60 | + // This is mostly visual style, making comments look uniform. |
| 61 | + "spaced-comment": ["error", "always", { |
| 62 | + "markers": ["/"], // triple-slash |
| 63 | + "exceptions": ["/"] // all slashes |
| 64 | + }], |
| 65 | + |
| 66 | + // This tends to be annoying as it encourages developers to make everything |
| 67 | + // that is never reassigned a 'const', sometimes semantically incorrect so, |
| 68 | + // typically leading to huge diffs in follow-up PRs modifying affected code. |
| 69 | + "prefer-const": "off", |
| 70 | + |
| 71 | + // It is perfectly fine to declare top-level variables with `var`, yet this |
| 72 | + // rule doesn't provide configuration options that would help. |
| 73 | + "no-var": "off", |
| 74 | + |
| 75 | + // Quite often, dealing with multiple related cases at once or otherwise |
| 76 | + // falling through is exactly the point of using a switch. |
| 77 | + "no-fallthrough": "off", |
| 78 | + |
| 79 | + // Typical false-positives here are `do { ... } while (true)` statements or |
| 80 | + // similar, but the only option provided here is not checking any loops. |
| 81 | + "no-constant-condition": ["error", { checkLoops: false }], |
| 82 | + |
| 83 | + // Functions are nested in blocks occasionally, and there haven't been any |
| 84 | + // problems with this so far, so turning the check off. |
| 85 | + "no-inner-declarations": "off", |
| 86 | + |
| 87 | + // Disabled here, but enabled again for JavaScript files. |
| 88 | + "no-unused-vars": "off", |
| 89 | + } |
| 90 | + }, |
| 91 | + |
| 92 | + // === JavaScript rules ==================================================== |
| 93 | + |
| 94 | + { |
| 95 | + files: [ |
| 96 | + "**/*.js", |
| 97 | + "bin/*" |
| 98 | + ], |
| 99 | + languageOptions: { |
| 100 | + globals: { |
| 101 | + ...globals.browser, |
| 102 | + ...globals.amd, |
| 103 | + ...globals.node, |
| 104 | + ...globals.es6 |
| 105 | + } |
| 106 | + }, |
| 107 | + rules: { |
| 108 | + // Enforcing to remove function parameters on stubs makes code less |
| 109 | + // maintainable, so we instead allow unused function parameters. |
| 110 | + "no-unused-vars": [ |
| 111 | + "warn", { |
| 112 | + "vars": "local", |
| 113 | + "args": "none", |
| 114 | + "ignoreRestSiblings": false |
| 115 | + } |
| 116 | + ], |
| 117 | + |
| 118 | + "no-loss-of-precision": "error", |
| 119 | + } |
| 120 | + }, |
| 121 | + |
| 122 | + // === TypeScript rules ==================================================== |
| 123 | + |
| 124 | + { |
| 125 | + files: [ |
| 126 | + "**/*.ts" |
| 127 | + ], |
| 128 | + languageOptions: { |
| 129 | + parser: tseslint.parser, |
| 130 | + parserOptions: { |
| 131 | + ecmaVersion: "latest", |
| 132 | + sourceType: "module", |
| 133 | + experimentalDecorators: true, |
| 134 | + ecmaFeatures: {} |
| 135 | + } |
| 136 | + }, |
| 137 | + plugins: { |
| 138 | + "@typescript-eslint": tseslint.plugin |
| 139 | + }, |
| 140 | + rules: { |
| 141 | + // Quite common in scenarios where an iteration starts at `current = this`. |
| 142 | + "@typescript-eslint/no-this-alias": "off", |
| 143 | + |
| 144 | + // Interferes with tests and 64-bit literals |
| 145 | + "@typescript-eslint/no-loss-of-precision": "off", |
| 146 | + "no-loss-of-precision": "off", |
| 147 | + |
| 148 | + // TypeScript handles this |
| 149 | + "no-redeclare": "off", |
| 150 | + |
| 151 | + // Disabled here, but enabled again for TypeScript files. |
| 152 | + "@typescript-eslint/no-unused-vars": "off", |
| 153 | + |
| 154 | + // We use require in some places |
| 155 | + "@typescript-eslint/no-require-imports": "off", |
| 156 | + |
| 157 | + // Allow unused expressions (used in some code patterns) |
| 158 | + "@typescript-eslint/no-unused-expressions": "off", |
| 159 | + |
| 160 | + // Empty object types are used in definitions |
| 161 | + "@typescript-eslint/no-empty-object-type": "off" |
| 162 | + } |
| 163 | + }, |
| 164 | + |
| 165 | + { |
| 166 | + files: [ |
| 167 | + "**/*.ts" |
| 168 | + ], |
| 169 | + rules: { |
| 170 | + // Enforcing to remove function parameters on stubs makes code less |
| 171 | + // maintainable, so we instead allow unused function parameters. |
| 172 | + "@typescript-eslint/no-unused-vars": [ |
| 173 | + "warn", { |
| 174 | + "vars": "local", |
| 175 | + "varsIgnorePattern": "^[A-Z](?:From|To)?$", // ignore type params |
| 176 | + "args": "none", |
| 177 | + "ignoreRestSiblings": false |
| 178 | + } |
| 179 | + ] |
| 180 | + } |
| 181 | + }, |
| 182 | + |
| 183 | + // === AssemblyScript rules (extends TypeScript rules) ===================== |
| 184 | + |
| 185 | + { |
| 186 | + files: [ |
| 187 | + "**/assembly/**/*.ts", |
| 188 | + "src/**/*.ts", |
| 189 | + "lib/parse/src/**/*.ts" |
| 190 | + ], |
| 191 | + rules: { |
| 192 | + // AssemblyScript has its own globals |
| 193 | + "no-undef": "off", |
| 194 | + |
| 195 | + // Namespaces are quite useful in AssemblyScript |
| 196 | + "@typescript-eslint/no-namespace": "off", |
| 197 | + |
| 198 | + // There is actually codegen difference here |
| 199 | + "@typescript-eslint/no-array-constructor": "off", |
| 200 | + |
| 201 | + // Sometimes it can't be avoided to add a @ts-ignore |
| 202 | + "@typescript-eslint/ban-ts-comment": "off", |
| 203 | + |
| 204 | + // Utilized to achieve portability in some cases |
| 205 | + "@typescript-eslint/no-non-null-assertion": "off", |
| 206 | + } |
| 207 | + }, |
| 208 | + |
| 209 | + // === Compiler rules (extends AssemblyScript rules) ======================= |
| 210 | + |
| 211 | + { |
| 212 | + files: [ |
| 213 | + "src/**/*.ts", |
| 214 | + "std/assembly/**/*.ts" |
| 215 | + ], |
| 216 | + rules: { |
| 217 | + // There is an actual codegen difference here - TODO: revisit |
| 218 | + "no-cond-assign": "off", |
| 219 | + |
| 220 | + // Not all types can be omitted in AS yet - TODO: revisit |
| 221 | + "@typescript-eslint/no-inferrable-types": "off", |
| 222 | + |
| 223 | + // Used rarely to reference internals that are not user-visible |
| 224 | + "@typescript-eslint/triple-slash-reference": "off", |
| 225 | + |
| 226 | + // The compiler has its own `Function` class for example |
| 227 | + "no-shadow-restricted-names": "off", |
| 228 | + "@typescript-eslint/ban-types": "off" |
| 229 | + } |
| 230 | + }, |
| 231 | + |
| 232 | + // === Standard Library rules (extends AssemblyScript rules) =============== |
| 233 | + |
| 234 | + { |
| 235 | + files: [ |
| 236 | + "std/assembly/**/*.ts" |
| 237 | + ], |
| 238 | + rules: { |
| 239 | + // We are implementing with --noLib, so we shadow all the time |
| 240 | + "no-shadow-restricted-names": "off", |
| 241 | + |
| 242 | + // Similarly, sometimes we need the return type to be String, not string |
| 243 | + "@typescript-eslint/ban-types": "off" |
| 244 | + } |
| 245 | + }, |
| 246 | + |
| 247 | + // === Standard Definition rules (extends TypeScript rules) ================ |
| 248 | + |
| 249 | + { |
| 250 | + files: [ |
| 251 | + "std/**/*.d.ts" |
| 252 | + ], |
| 253 | + rules: { |
| 254 | + // Definition files have their own globals |
| 255 | + "no-undef": "off", |
| 256 | + |
| 257 | + // Often required to achieve compatibility with TypeScript |
| 258 | + "@typescript-eslint/no-explicit-any": "off", |
| 259 | + |
| 260 | + // Interfaces can be stubs here, i.e. not yet fully implemented |
| 261 | + "@typescript-eslint/no-empty-object-type": "off", |
| 262 | + |
| 263 | + // Definitions make use of `object` to model rather unusual constraints |
| 264 | + "@typescript-eslint/ban-types": "off" |
| 265 | + } |
| 266 | + }, |
| 267 | + |
| 268 | + // === Compiler Definition rules (extends TypeScript rules) ================ |
| 269 | + |
| 270 | + { |
| 271 | + files: [ |
| 272 | + "./dist/*.d.ts" |
| 273 | + ], |
| 274 | + rules: { |
| 275 | + // Our definitions are complicated, and all attempts to describe them |
| 276 | + // as modules have failed so far. As such, we re-export namespaces. |
| 277 | + "@typescript-eslint/no-namespace": "off", |
| 278 | + "@typescript-eslint/triple-slash-reference": "off" |
| 279 | + } |
| 280 | + }, |
| 281 | + |
| 282 | + // === Other Definition rules ============================================== |
| 283 | + |
| 284 | + { |
| 285 | + files: [ |
| 286 | + "**/*.d.ts" |
| 287 | + ], |
| 288 | + rules: { |
| 289 | + // Definition files have their own globals |
| 290 | + "no-undef": "off" |
| 291 | + } |
| 292 | + }, |
| 293 | + |
| 294 | + // === Test rules (extends TypeScript rules) =============================== |
| 295 | + |
| 296 | + { |
| 297 | + files: [ |
| 298 | + "./tests/compiler/**/*.ts", |
| 299 | + "./lib/loader/tests/assembly/**/*.ts" |
| 300 | + ], |
| 301 | + rules: { |
| 302 | + // Test files have their own globals |
| 303 | + "no-undef": "off", |
| 304 | + |
| 305 | + // Tests typically include unusual code patterns on purpose. This is |
| 306 | + // very likely not an extensive list, but covers what's there so far. |
| 307 | + "no-empty": "off", |
| 308 | + "no-cond-assign": "off", |
| 309 | + "no-compare-neg-zero": "off", |
| 310 | + "no-inner-declarations": "off", |
| 311 | + "no-constant-condition": "off", |
| 312 | + "use-isnan": "off", |
| 313 | + "@typescript-eslint/no-namespace": "off", |
| 314 | + "@typescript-eslint/no-unused-vars": "off", |
| 315 | + "@typescript-eslint/no-empty-function": "off", |
| 316 | + "@typescript-eslint/no-non-null-assertion": "off", |
| 317 | + "@typescript-eslint/no-extra-semi": "off", |
| 318 | + "@typescript-eslint/no-inferrable-types": "off", |
| 319 | + "@typescript-eslint/ban-types": "off", |
| 320 | + "@typescript-eslint/triple-slash-reference": "off", |
| 321 | + "@typescript-eslint/ban-ts-comment": "off", |
| 322 | + "@typescript-eslint/no-extra-non-null-assertion": "off", |
| 323 | + "@typescript-eslint/no-empty-object-type": "off" |
| 324 | + } |
| 325 | + }, |
| 326 | +); |
0 commit comments