|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// we import the base configuration from Neostandard (a modern ESLint preset) |
| 7 | +// and also its internal plugins |
| 8 | +import neostandard, { plugins } from 'neostandard' |
| 9 | +import { defineConfig } from 'eslint/config' |
| 10 | + |
| 11 | +// we export the ESLint configuration |
| 12 | +export default defineConfig([ |
| 13 | + ...neostandard({ |
| 14 | + // enable support for TypeScript |
| 15 | + ts: true, |
| 16 | + // external dependencies (never lint them) |
| 17 | + ignores: [ |
| 18 | + 'node_modules/**', |
| 19 | + // generated code (CommonJS build) It regenerates automatically, linting makes no sense |
| 20 | + 'lib/**', |
| 21 | + // generated code (ESM build) Same as lib, it is overwritten in each build |
| 22 | + 'esm/**', |
| 23 | + // coverage reports (not source code) |
| 24 | + 'coverage/**' |
| 25 | + ] |
| 26 | + }), |
| 27 | + |
| 28 | + // style rules applied to the important project files |
| 29 | + // applies to: |
| 30 | + // src: source code |
| 31 | + // test: tests |
| 32 | + // scripts: internal tools |
| 33 | + { |
| 34 | + files: ['src/**/*.js', 'src/**/*.ts', 'test/**/*.js', 'test/**/*.ts', 'scripts/**/*.js'], |
| 35 | + plugins: { |
| 36 | + // plugin that controls formatting (indentation, spaces, and other syntax rules) |
| 37 | + '@stylistic': plugins['@stylistic'] |
| 38 | + }, |
| 39 | + rules: { |
| 40 | + // requires a space after comments |
| 41 | + '@stylistic/spaced-comment': ['error', 'always', { |
| 42 | + // allows exceptions like //#region |
| 43 | + exceptions: ['#', '/'], |
| 44 | + markers: ['/'] |
| 45 | + }], |
| 46 | + // prohibits the use of semicolon ; |
| 47 | + '@stylistic/semi': ['error', 'never'], |
| 48 | + // force 2-space indentation |
| 49 | + '@stylistic/indent': ['error', 2], |
| 50 | + // forces a new line or line break at the end of the file |
| 51 | + '@stylistic/eol-last': ['error', 'always'], |
| 52 | + // requires space before parentheses in functions () {} |
| 53 | + '@stylistic/space-before-function-paren': ['error', 'always'], |
| 54 | + // disables the requirement for lines between class methods |
| 55 | + '@stylistic/lines-between-class-members': 'off' |
| 56 | + } |
| 57 | + }, |
| 58 | + |
| 59 | + // relaxed rules for code generated inside src |
| 60 | + { |
| 61 | + files: ['src/**/*.ts'], |
| 62 | + // this is code generated as an API client |
| 63 | + rules: { |
| 64 | + // snake_case |
| 65 | + camelcase: 'off', |
| 66 | + // generator uses void expressions |
| 67 | + 'no-void': 'off', |
| 68 | + // the generated regexes may seem incorrect but they are not |
| 69 | + 'no-useless-escape': 'off', |
| 70 | + // the generated code can use let even though it could be const, to be more permissive |
| 71 | + 'prefer-const': 'off', |
| 72 | + // side effects in some expressions |
| 73 | + 'no-unused-expressions': 'off', |
| 74 | + // allows use of comma operator in generated code |
| 75 | + 'no-sequences': 'off', |
| 76 | + // variables can exist even if they are not being used |
| 77 | + 'no-unused-vars': 'off', |
| 78 | + // variables in TS that are not being used |
| 79 | + '@typescript-eslint/no-unused-vars': 'off', |
| 80 | + // allows automatically generated lowercase constructors |
| 81 | + 'new-cap': 'off' |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + // ci .buildkite, scripts exceptions |
| 86 | + { |
| 87 | + files: ['.buildkite/**/*.mjs'], |
| 88 | + rules: { |
| 89 | + // scripts can have unused variables (args) |
| 90 | + 'no-unused-vars': 'off', |
| 91 | + // regex in scripts may require special escapes |
| 92 | + 'no-useless-escape': 'off' |
| 93 | + } |
| 94 | + }, |
| 95 | + |
| 96 | + // rules for the tests |
| 97 | + { |
| 98 | + files: ['test/**/*.js', 'test/**/*.ts', 'test/**/*.mjs'], |
| 99 | + rules: { |
| 100 | + // tests may use snake_case data |
| 101 | + camelcase: 'off', |
| 102 | + // tests often declare unused helpers |
| 103 | + 'no-unused-vars': 'off', |
| 104 | + // tests often declare unused helpers or variable of ts |
| 105 | + '@typescript-eslint/no-unused-vars': 'off', |
| 106 | + // regex in tests may seem unnecessary |
| 107 | + 'no-useless-escape': 'off' |
| 108 | + } |
| 109 | + } |
| 110 | +]) |
0 commit comments