|
| 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 | + |
| 5 | +// clang-format off |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const rulesDirPlugin = require('eslint-plugin-rulesdir'); |
| 9 | +rulesDirPlugin.RULES_DIR = path.join(__dirname, 'scripts', 'eslint_rules', 'lib'); |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + 'root': true, |
| 13 | + |
| 14 | + 'env': {'browser': true, 'es6': true}, |
| 15 | + |
| 16 | + 'parser': '@typescript-eslint/parser', |
| 17 | + |
| 18 | + 'plugins': [ |
| 19 | + '@typescript-eslint', |
| 20 | + 'mocha', |
| 21 | + 'rulesdir', |
| 22 | + 'import', |
| 23 | + 'jsdoc', |
| 24 | + ], |
| 25 | + |
| 26 | + 'parserOptions': {'ecmaVersion': 9, 'sourceType': 'module'}, |
| 27 | + |
| 28 | + /** |
| 29 | + * ESLint rules |
| 30 | + * |
| 31 | + * All available rules: http://eslint.org/docs/rules/ |
| 32 | + */ |
| 33 | + 'rules': { |
| 34 | + /** |
| 35 | + * Enforced rules |
| 36 | + */ |
| 37 | + |
| 38 | + // syntax preferences |
| 39 | + 'quotes': ['error', 'single', {'avoidEscape': true, 'allowTemplateLiterals': false}], |
| 40 | + 'semi': 'error', |
| 41 | + 'no-extra-semi': 'error', |
| 42 | + 'comma-style': ['error', 'last'], |
| 43 | + 'wrap-iife': ['error', 'inside'], |
| 44 | + 'spaced-comment': ['error', 'always', {'markers': ['*']}], |
| 45 | + 'eqeqeq': 'error', |
| 46 | + 'accessor-pairs': ['error', {'getWithoutSet': false, 'setWithoutGet': false}], |
| 47 | + 'curly': 'error', |
| 48 | + 'new-parens': 'error', |
| 49 | + 'func-call-spacing': 'error', |
| 50 | + 'arrow-parens': ['error', 'as-needed'], |
| 51 | + 'eol-last': 'error', |
| 52 | + |
| 53 | + // anti-patterns |
| 54 | + 'no-caller': 'error', |
| 55 | + 'no-case-declarations': 'error', |
| 56 | + 'no-cond-assign': 'error', |
| 57 | + 'no-console': ['error', {'allow': ['assert', 'context', 'error', 'timeStamp', 'time', 'timeEnd', 'warn']}], |
| 58 | + 'no-debugger': 'error', |
| 59 | + 'no-dupe-keys': 'error', |
| 60 | + 'no-duplicate-case': 'error', |
| 61 | + 'no-else-return': ['error', {'allowElseIf': false}], |
| 62 | + 'no-empty-character-class': 'error', |
| 63 | + 'no-global-assign': 'error', |
| 64 | + 'no-implied-eval': 'error', |
| 65 | + 'no-labels': 'error', |
| 66 | + 'no-multi-str': 'error', |
| 67 | + 'no-new-object': 'error', |
| 68 | + 'no-octal-escape': 'error', |
| 69 | + 'no-self-compare': 'error', |
| 70 | + 'no-shadow-restricted-names': 'error', |
| 71 | + 'no-unreachable': 'error', |
| 72 | + 'no-unsafe-negation': 'error', |
| 73 | + 'no-unused-vars': ['error', {'args': 'none', 'vars': 'local'}], |
| 74 | + 'no-var': 'error', |
| 75 | + 'no-with': 'error', |
| 76 | + 'prefer-const': 'error', |
| 77 | + 'radix': 'error', |
| 78 | + 'valid-typeof': 'error', |
| 79 | + 'no-return-assign': ['error', 'always'], |
| 80 | + 'no-implicit-coercion': 'error', |
| 81 | + |
| 82 | + // es2015 features |
| 83 | + 'require-yield': 'error', |
| 84 | + 'template-curly-spacing': ['error', 'never'], |
| 85 | + |
| 86 | + // file whitespace |
| 87 | + 'no-multiple-empty-lines': ['error', {'max': 1}], |
| 88 | + 'no-mixed-spaces-and-tabs': 'error', |
| 89 | + 'no-trailing-spaces': 'error', |
| 90 | + 'linebreak-style': ['error', 'unix'], |
| 91 | + |
| 92 | + /** |
| 93 | + * Disabled, aspirational rules |
| 94 | + */ |
| 95 | + |
| 96 | + 'indent': ['off', 2, {'SwitchCase': 1, 'CallExpression': {'arguments': 2}, 'MemberExpression': 2}], |
| 97 | + |
| 98 | + // brace-style is disabled, as eslint cannot enforce 1tbs as default, but allman for functions |
| 99 | + 'brace-style': ['off', 'allman', {'allowSingleLine': true}], |
| 100 | + |
| 101 | + // key-spacing is disabled, as some objects use value-aligned spacing, some not. |
| 102 | + 'key-spacing': ['off', {'beforeColon': false, 'afterColon': true, 'align': 'value'}], |
| 103 | + // quote-props is diabled, as property quoting styles are too varied to enforce. |
| 104 | + 'quote-props': ['off', 'as-needed'], |
| 105 | + |
| 106 | + // no-implicit-globals will prevent accidental globals |
| 107 | + 'no-implicit-globals': 'off', |
| 108 | + 'no-unused-private-class-members': 'error', |
| 109 | + |
| 110 | + // forbids interfaces starting with an I prefix. |
| 111 | + '@typescript-eslint/naming-convention': |
| 112 | + ['error', {'selector': 'interface', 'format': ['PascalCase'], 'custom': {'regex': '^I[A-Z]', 'match': false}}], |
| 113 | + '@typescript-eslint/explicit-member-accessibility': 'off', |
| 114 | + '@typescript-eslint/no-explicit-any': [ |
| 115 | + "error", |
| 116 | + { |
| 117 | + "ignoreRestArgs": true |
| 118 | + } |
| 119 | + ], |
| 120 | + |
| 121 | + // Closure does not properly typecheck default exports |
| 122 | + 'import/no-default-export': 'error', |
| 123 | + |
| 124 | + /** |
| 125 | + * Catch duplicate import paths. For example this would catch the following example: |
| 126 | + * import {Foo} from './foo.js' |
| 127 | + * import * as FooModule from './foo.js' |
| 128 | + **/ |
| 129 | + 'import/no-duplicates': 'error', |
| 130 | + |
| 131 | + // Try to spot '// console.log()' left over from debugging |
| 132 | + 'rulesdir/commented_out_console': 'error', |
| 133 | + |
| 134 | + // Prevent imports being commented out rather than deleted. |
| 135 | + 'rulesdir/commented_out_import': 'error', |
| 136 | + |
| 137 | + // DevTools specific rules |
| 138 | + 'rulesdir/es_modules_import': 'error', |
| 139 | + 'rulesdir/check_license_header': 'error', |
| 140 | + /** |
| 141 | + * Ensures that JS Doc comments are properly aligned - all the starting |
| 142 | + * `*` are in the right place. |
| 143 | + */ |
| 144 | + 'jsdoc/check-alignment': 'error', |
| 145 | + }, |
| 146 | + 'overrides': [{ |
| 147 | + 'files': ['*.ts'], |
| 148 | + 'parserOptions': { |
| 149 | + 'allowAutomaticSingleRunInference': true, |
| 150 | + 'project': path.join(__dirname, 'config', 'typescript', 'tsconfig.eslint.json'), |
| 151 | + }, |
| 152 | + 'rules': { |
| 153 | + '@typescript-eslint/explicit-member-accessibility': ['error', {'accessibility': 'no-public'}], |
| 154 | + 'comma-dangle': 'off', |
| 155 | + '@typescript-eslint/comma-dangle': ['error', 'always-multiline'], |
| 156 | + |
| 157 | + // run just the TypeScript unused-vars rule, else we get duplicate errors |
| 158 | + 'no-unused-vars': 'off', |
| 159 | + '@typescript-eslint/no-unused-vars': ['error', {'argsIgnorePattern': '^_'}], |
| 160 | + // run just the TypeScript semi rule, else we get duplicate errors |
| 161 | + 'semi': 'off', |
| 162 | + '@typescript-eslint/semi': 'error', |
| 163 | + '@typescript-eslint/member-delimiter-style': [ |
| 164 | + 'error', { |
| 165 | + 'multiline': {'delimiter': 'semi', 'requireLast': true}, |
| 166 | + 'singleline': {'delimiter': 'comma', 'requireLast': false}, |
| 167 | + 'overrides': { |
| 168 | + 'interface': { |
| 169 | + 'singleline': {'delimiter': 'semi', 'requireLast': false}, |
| 170 | + 'multiline': {'delimiter': 'semi', 'requireLast': true} |
| 171 | + }, |
| 172 | + 'typeLiteral': { |
| 173 | + 'singleline': {'delimiter': 'comma', 'requireLast': false}, |
| 174 | + 'multiline': {'delimiter': 'comma', 'requireLast': true} |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + ], |
| 179 | + '@typescript-eslint/no-floating-promises': ['error', {ignoreVoid: true}], |
| 180 | + // func-call-spacing doesn't work well with .ts |
| 181 | + 'func-call-spacing': 'off', |
| 182 | + '@typescript-eslint/func-call-spacing': 'error', |
| 183 | + |
| 184 | + /** |
| 185 | + * Enforce that enum members are explicitly defined: |
| 186 | + * const enum Foo { A = 'a' } rather than const enum Foo { A } |
| 187 | + */ |
| 188 | + '@typescript-eslint/prefer-enum-initializers': 'error', |
| 189 | + /** |
| 190 | + * Ban non-null assertion operator, e.g.: |
| 191 | + * this.foo!.toLowerCase() |
| 192 | + */ |
| 193 | + '@typescript-eslint/no-non-null-assertion': 'error', |
| 194 | + '@typescript-eslint/consistent-type-imports': 'error', |
| 195 | + 'rulesdir/no_underscored_properties': 'error', |
| 196 | + 'rulesdir/prefer_readonly_keyword': 'error', |
| 197 | + 'rulesdir/inline_type_imports': 'error', |
| 198 | + } |
| 199 | + }, { |
| 200 | + 'files': ['*.test.ts', 'test/**/*.ts', '**/testing/*.ts'], |
| 201 | + 'rules': { |
| 202 | + // errors on it('test') with no body |
| 203 | + 'mocha/no-pending-tests' : 'error', |
| 204 | + |
| 205 | + // errors on {describe, it}.only |
| 206 | + 'mocha/no-exclusive-tests' : 'error', |
| 207 | + |
| 208 | + 'mocha/no-async-describe': 'error', |
| 209 | + 'mocha/no-global-tests': 'error', |
| 210 | + 'mocha/no-nested-tests': 'error', |
| 211 | + |
| 212 | + 'rulesdir/check_test_definitions' : 'error', |
| 213 | + 'rulesdir/avoid_assert_equal' : 'error', |
| 214 | + 'rulesdir/no_repeated_tests' : 'error', |
| 215 | + 'rulesdir/compare_arrays_with_assert_deepequal' : 'error', |
| 216 | + 'rulesdir/ban_screenshot_test_outside_perf_panel' : 'error', |
| 217 | + 'rulesdir/trace_engine_test_timeouts' : 'error', |
| 218 | + '@typescript-eslint/naming-convention' : |
| 219 | + [ |
| 220 | + 'error', |
| 221 | + { |
| 222 | + 'selector' : |
| 223 | + [ |
| 224 | + 'function', 'accessor', 'method', 'property', |
| 225 | + 'parameterProperty' |
| 226 | + ], |
| 227 | + 'format' : ['camelCase'], |
| 228 | + }, |
| 229 | + { |
| 230 | + // Allow PascalCase as well as it is used for dynamic module imports. |
| 231 | + 'selector' : 'variable', |
| 232 | + 'format' : ['camelCase', 'PascalCase', 'UPPER_CASE'], |
| 233 | + }, |
| 234 | + { |
| 235 | + 'selector' : 'classProperty', |
| 236 | + 'modifiers' : ['static', 'readonly'], |
| 237 | + 'format' : ['UPPER_CASE'], |
| 238 | + }, |
| 239 | + { |
| 240 | + 'selector' : 'enumMember', |
| 241 | + 'format' : ['PascalCase', 'UPPER_CASE'], |
| 242 | + }, |
| 243 | + { |
| 244 | + 'selector' : ['typeLike'], |
| 245 | + 'format' : ['PascalCase'], |
| 246 | + }, |
| 247 | + { |
| 248 | + // Also allow UPPER_CASE so argument function to evaluate can take constants as arguments without renaming. |
| 249 | + 'selector' : 'parameter', |
| 250 | + 'format' : ['camelCase', 'UPPER_CASE'], |
| 251 | + 'leadingUnderscore' : 'allow', |
| 252 | + }, |
| 253 | + { |
| 254 | + // Object literals may be constructed as arguments to external libraries which follow different styles. |
| 255 | + 'selector' : ['objectLiteralMethod', 'objectLiteralProperty'], |
| 256 | + 'modifiers' : ['public'], |
| 257 | + 'format' : null, |
| 258 | + }, |
| 259 | + ], |
| 260 | + '@typescript-eslint/no-non-null-assertion' : 'off', |
| 261 | + }, |
| 262 | + 'settings': { |
| 263 | + 'mocha/additionalCustomNames': [ |
| 264 | + { 'name': 'describeWithDevtoolsExtension', 'type': 'suite', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 265 | + { 'name': 'describeWithEnvironment', 'type': 'suite', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 266 | + { 'name': 'describeWithLocale', 'type': 'suite', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 267 | + { 'name': 'describeWithMockConnection', 'type': 'suite', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 268 | + { 'name': 'describeWithRealConnection', 'type': 'suite', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 269 | + { 'name': 'itScreenshot', 'type': 'testCase', 'interfaces': [ 'BDD', 'TDD' ] }, |
| 270 | + ] |
| 271 | + } |
| 272 | + }], |
| 273 | +}; |
| 274 | + |
| 275 | +// clang-format on |
0 commit comments