Jack Franklin | aba8800 | 2020-11-26 16:23:05 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | d6c33c4 | 2020-03-10 16:57:22 | [diff] [blame] | 5 | const path = require('path'); |
Jack Franklin | aba8800 | 2020-11-26 16:23:05 | [diff] [blame] | 6 | const rulesDirPlugin = require('eslint-plugin-rulesdir'); |
Tim van der Lippe | d6c33c4 | 2020-03-10 16:57:22 | [diff] [blame] | 7 | rulesDirPlugin.RULES_DIR = path.join(__dirname, 'scripts', 'eslint_rules', 'lib'); |
| 8 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 9 | module.exports = { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 10 | 'root': true, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 11 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 12 | 'env': {'browser': true, 'es6': true}, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 13 | |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 14 | 'parser': '@typescript-eslint/parser', |
| 15 | |
| 16 | 'plugins': [ |
| 17 | '@typescript-eslint', |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 | [diff] [blame] | 18 | 'mocha', |
Tim van der Lippe | d6c33c4 | 2020-03-10 16:57:22 | [diff] [blame] | 19 | 'rulesdir', |
Tim van der Lippe | 298b8cc | 2020-04-03 11:55:10 | [diff] [blame] | 20 | 'import', |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 21 | ], |
| 22 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 23 | 'parserOptions': {'ecmaVersion': 9, 'sourceType': 'module'}, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 24 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 25 | /** |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 26 | * ESLint rules |
| 27 | * |
| 28 | * All available rules: http://eslint.org/docs/rules/ |
| 29 | * |
| 30 | * Rules take the following form: |
| 31 | * 'rule-name', [severity, { opts }] |
| 32 | * Severity: 2 == error, 1 == warning, 0 == off. |
| 33 | */ |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 34 | 'rules': { |
| 35 | /** |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 36 | * Enforced rules |
| 37 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 39 | // syntax preferences |
Mathias Bynens | 23ee1aa | 2020-03-02 12:06:38 | [diff] [blame] | 40 | 'quotes': [2, 'single', {'avoidEscape': true, 'allowTemplateLiterals': false}], |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 41 | 'semi': 2, |
| 42 | 'no-extra-semi': 2, |
| 43 | 'comma-style': [2, 'last'], |
| 44 | 'wrap-iife': [2, 'inside'], |
| 45 | 'spaced-comment': [2, 'always', {'markers': ['*']}], |
| 46 | 'eqeqeq': [2], |
| 47 | 'accessor-pairs': [2, {'getWithoutSet': false, 'setWithoutGet': false}], |
| 48 | 'curly': 2, |
| 49 | 'new-parens': 2, |
| 50 | 'func-call-spacing': 2, |
| 51 | 'arrow-parens': [2, 'as-needed'], |
Tim van der Lippe | 20b29c2 | 2019-11-04 14:36:15 | [diff] [blame] | 52 | 'eol-last': 2, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 54 | // anti-patterns |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 55 | 'no-caller': 2, |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 56 | 'no-case-declarations': 2, |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 57 | 'no-cond-assign': 2, |
| 58 | 'no-console': [2, {'allow': ['assert', 'context', 'error', 'timeStamp', 'time', 'timeEnd', 'warn']}], |
| 59 | 'no-debugger': 2, |
| 60 | 'no-dupe-keys': 2, |
| 61 | 'no-duplicate-case': 2, |
| 62 | 'no-else-return': [2, {'allowElseIf': false}], |
| 63 | 'no-empty-character-class': 2, |
Mathias Bynens | 15213d0 | 2020-05-14 11:55:11 | [diff] [blame] | 64 | 'no-global-assign': 2, |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 65 | 'no-implied-eval': 2, |
| 66 | 'no-labels': 2, |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 67 | 'no-multi-str': 2, |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 68 | 'no-new-object': 2, |
| 69 | 'no-octal-escape': 2, |
| 70 | 'no-self-compare': 2, |
| 71 | 'no-shadow-restricted-names': 2, |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 72 | 'no-unreachable': 2, |
| 73 | 'no-unsafe-negation': 2, |
Mathias Bynens | f06e8c0 | 2020-02-28 13:58:28 | [diff] [blame] | 74 | 'no-unused-vars': [2, {'args': 'none', 'vars': 'local'}], |
| 75 | 'no-var': 2, |
| 76 | 'no-with': 2, |
| 77 | 'prefer-const': 2, |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 78 | 'radix': 2, |
| 79 | 'valid-typeof': 2, |
Patrick Brosset | e65aaac | 2020-06-22 08:04:40 | [diff] [blame] | 80 | 'no-return-assign': [2, 'always'], |
Tim van der Lippe | 4075559 | 2021-01-07 13:55:08 | [diff] [blame] | 81 | 'no-implicit-coercion': 2, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 82 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 83 | // es2015 features |
| 84 | 'require-yield': 2, |
| 85 | 'template-curly-spacing': [2, 'never'], |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | // file whitespace |
Kriti Sapra | 56daf78 | 2021-07-28 12:05:19 | [diff] [blame] | 88 | 'no-multiple-empty-lines': [2, {'max': 1}], |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 89 | 'no-mixed-spaces-and-tabs': 2, |
| 90 | 'no-trailing-spaces': 2, |
| 91 | 'linebreak-style': [2, 'unix'], |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 93 | /** |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 94 | * Disabled, aspirational rules |
| 95 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 97 | 'indent': [0, 2, {'SwitchCase': 1, 'CallExpression': {'arguments': 2}, 'MemberExpression': 2}], |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 99 | // brace-style is disabled, as eslint cannot enforce 1tbs as default, but allman for functions |
| 100 | 'brace-style': [0, 'allman', {'allowSingleLine': true}], |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 101 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 102 | // key-spacing is disabled, as some objects use value-aligned spacing, some not. |
| 103 | 'key-spacing': [0, {'beforeColon': false, 'afterColon': true, 'align': 'value'}], |
| 104 | // quote-props is diabled, as property quoting styles are too varied to enforce. |
| 105 | 'quote-props': [0, 'as-needed'], |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 107 | // no-implicit-globals will prevent accidental globals |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 108 | 'no-implicit-globals': [0], |
Tim van der Lippe | 4f11084 | 2021-12-01 14:55:10 | [diff] [blame] | 109 | 'no-unused-private-class-members': 2, |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 110 | |
Jack Franklin | 264237a | 2020-07-15 09:02:26 | [diff] [blame] | 111 | // forbids interfaces starting with an I prefix. |
Paul Lewis | 839037f | 2020-07-21 12:25:19 | [diff] [blame] | 112 | '@typescript-eslint/naming-convention': |
| 113 | [2, {'selector': 'interface', 'format': ['PascalCase'], 'custom': {'regex': '^I[A-Z]', 'match': false}}], |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 114 | '@typescript-eslint/explicit-member-accessibility': [0], |
Paul Lewis | 839037f | 2020-07-21 12:25:19 | [diff] [blame] | 115 | '@typescript-eslint/no-explicit-any': 2, |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 | [diff] [blame] | 116 | |
Tim van der Lippe | 298b8cc | 2020-04-03 11:55:10 | [diff] [blame] | 117 | // Closure does not properly typecheck default exports |
| 118 | 'import/no-default-export': 2, |
| 119 | |
Jack Franklin | 02f0dbb | 2021-03-02 14:10:49 | [diff] [blame] | 120 | // Try to spot '// console.log()' left over from debugging |
| 121 | 'rulesdir/commented_out_console': 2, |
| 122 | |
Tim van der Lippe | d6c33c4 | 2020-03-10 16:57:22 | [diff] [blame] | 123 | // DevTools specific rules |
| 124 | 'rulesdir/es_modules_import': 2, |
Tim van der Lippe | 4a8441c | 2020-03-12 15:45:18 | [diff] [blame] | 125 | 'rulesdir/check_license_header': 2, |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 126 | }, |
| 127 | 'overrides': [{ |
| 128 | 'files': ['*.ts'], |
Simon Zünd | 607774a | 2021-06-16 10:55:53 | [diff] [blame] | 129 | 'parserOptions': { |
| 130 | 'allowAutomaticSingleRunInference': true, |
Tim van der Lippe | 4624428 | 2021-06-21 16:10:59 | [diff] [blame] | 131 | 'project': './config/typescript/tsconfig.eslint.json', |
Simon Zünd | 607774a | 2021-06-16 10:55:53 | [diff] [blame] | 132 | }, |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 133 | 'rules': { |
Jack Franklin | 173a1ae | 2020-06-12 13:45:40 | [diff] [blame] | 134 | '@typescript-eslint/explicit-member-accessibility': [2, {'accessibility': 'no-public'}], |
Sigurd Schneider | 9c07567 | 2021-02-03 13:16:39 | [diff] [blame] | 135 | 'comma-dangle': 'off', |
| 136 | '@typescript-eslint/comma-dangle': [2, 'always-multiline'], |
Tim van der Lippe | 0ebbf49 | 2020-12-03 12:13:21 | [diff] [blame] | 137 | |
Jack Franklin | 9531032 | 2020-03-20 14:34:04 | [diff] [blame] | 138 | // run just the TypeScript unused-vars rule, else we get duplicate errors |
| 139 | 'no-unused-vars': 0, |
Tim van der Lippe | 159b890 | 2020-09-21 10:55:07 | [diff] [blame] | 140 | '@typescript-eslint/no-unused-vars': [2, {'argsIgnorePattern': '^_'}], |
Tim van der Lippe | 0ebbf49 | 2020-12-03 12:13:21 | [diff] [blame] | 141 | // run just the TypeScript semi rule, else we get duplicate errors |
| 142 | 'semi': 0, |
Sigurd Schneider | 1576186 | 2021-02-04 08:05:36 | [diff] [blame] | 143 | '@typescript-eslint/semi': ['error'], |
| 144 | '@typescript-eslint/member-delimiter-style': [ |
| 145 | 'error', { |
| 146 | 'multiline': {'delimiter': 'semi', 'requireLast': true}, |
| 147 | 'singleline': {'delimiter': 'comma', 'requireLast': false}, |
| 148 | 'overrides': { |
| 149 | 'interface': { |
| 150 | 'singleline': {'delimiter': 'semi', 'requireLast': false}, |
| 151 | 'multiline': {'delimiter': 'semi', 'requireLast': true} |
| 152 | }, |
| 153 | 'typeLiteral': { |
| 154 | 'singleline': {'delimiter': 'comma', 'requireLast': false}, |
| 155 | 'multiline': {'delimiter': 'comma', 'requireLast': true} |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | ], |
Songtao Xia | 165a119 | 2021-01-28 14:09:40 | [diff] [blame] | 160 | // func-call-spacing doesn't work well with .ts |
| 161 | 'func-call-spacing': 0, |
| 162 | '@typescript-eslint/func-call-spacing': 2, |
Tim van der Lippe | 0ebbf49 | 2020-12-03 12:13:21 | [diff] [blame] | 163 | |
Jack Franklin | aba8800 | 2020-11-26 16:23:05 | [diff] [blame] | 164 | /** |
| 165 | * Enforce that enum members are explicitly defined: |
| 166 | * const enum Foo { A = 'a' } rather than const enum Foo { A } |
| 167 | */ |
| 168 | '@typescript-eslint/prefer-enum-initializers': 2, |
Jack Franklin | fc49590 | 2020-12-01 17:24:37 | [diff] [blame] | 169 | /** |
| 170 | * Ban non-null assertion operator, e.g.: |
| 171 | * this.foo!.toLowerCase() |
| 172 | */ |
| 173 | '@typescript-eslint/no-non-null-assertion': 2, |
Jack Franklin | 5d89e07 | 2021-05-11 15:30:27 | [diff] [blame] | 174 | '@typescript-eslint/consistent-type-imports': 2, |
Jack Franklin | dd3be2e | 2020-11-27 15:08:54 | [diff] [blame] | 175 | 'rulesdir/const_enum': 2, |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 176 | 'rulesdir/no_underscored_properties': 2, |
Jack Franklin | 92fe8ff | 2020-12-03 10:51:23 | [diff] [blame] | 177 | 'rulesdir/prefer_readonly_keyword': 2, |
Tim van der Lippe | 399a956 | 2020-01-16 10:53:28 | [diff] [blame] | 178 | } |
| 179 | }] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | }; |