|
| 1 | +const { rules: baseBestPracticesRules } = require('eslint-config-airbnb-base/rules/best-practices'); |
| 2 | +const { rules: baseES6Rules } = require('eslint-config-airbnb-base/rules/es6'); |
| 3 | +const { rules: baseImportsRules } = require('eslint-config-airbnb-base/rules/imports'); |
| 4 | +const { rules: baseStyleRules } = require('eslint-config-airbnb-base/rules/style'); |
| 5 | +const { rules: baseVariablesRules } = require('eslint-config-airbnb-base/rules/variables'); |
| 6 | + |
| 7 | +module.exports = { |
| 8 | + settings: { |
| 9 | + // Apply special parsing for TypeScript files |
| 10 | + 'import/parsers': { |
| 11 | + '@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'], |
| 12 | + }, |
| 13 | + 'import/resolver': { |
| 14 | + node: { |
| 15 | + extensions: ['.mjs', '.js', '.jsx', '.json', '.ts', '.tsx', '.d.ts'], |
| 16 | + }, |
| 17 | + }, |
| 18 | + // Append 'ts' extensions to Airbnb 'import/extensions' setting |
| 19 | + // Original: ['.js', '.mjs', '.jsx'] |
| 20 | + 'import/extensions': ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.d.ts'], |
| 21 | + // Resolve type definition packages |
| 22 | + 'import/external-module-folders': ['node_modules', 'node_modules/@types'], |
| 23 | + }, |
| 24 | + rules: { |
| 25 | + camelcase: 'off', |
| 26 | + // The `@typescript-eslint/naming-convention` rule allows `leadingUnderscore` and `trailingUnderscore` settings. However, the existing `no-underscore-dangle` rule already takes care of this. |
| 27 | + '@typescript-eslint/naming-convention': [ |
| 28 | + 'error', |
| 29 | + // Allow camelCase variables (23.2), PascalCase variables (23.8), and UPPER_CASE variables (23.10) |
| 30 | + { |
| 31 | + selector: 'variable', |
| 32 | + format: ['camelCase', 'PascalCase', 'UPPER_CASE'], |
| 33 | + }, |
| 34 | + // Allow camelCase functions (23.2), and PascalCase functions (23.8) |
| 35 | + { |
| 36 | + selector: 'function', |
| 37 | + format: ['camelCase', 'PascalCase'], |
| 38 | + }, |
| 39 | + // Airbnb recommends PascalCase for classes (23.3), and although Airbnb does not make TypeScript recommendations, we are assuming this rule would similarly apply to anything "type like", including interfaces, type aliases, and enums |
| 40 | + { |
| 41 | + selector: 'typeLike', |
| 42 | + format: ['PascalCase'], |
| 43 | + }, |
| 44 | + ], |
| 45 | + 'default-param-last': 'off', |
| 46 | + '@typescript-eslint/default-param-last': baseBestPracticesRules['default-param-last'], |
| 47 | + 'no-array-constructor': 'off', |
| 48 | + '@typescript-eslint/no-array-constructor': baseStyleRules['no-array-constructor'], |
| 49 | + 'no-empty-function': 'off', |
| 50 | + '@typescript-eslint/no-empty-function': baseBestPracticesRules['no-empty-function'], |
| 51 | + 'no-loss-of-precision': 'error', |
| 52 | + 'no-loop-func': 'off', |
| 53 | + '@typescript-eslint/no-loop-func': baseBestPracticesRules['no-loop-func'], |
| 54 | + 'no-magic-numbers': 'off', |
| 55 | + '@typescript-eslint/no-magic-numbers': baseBestPracticesRules['no-magic-numbers'], |
| 56 | + 'no-shadow': 'off', |
| 57 | + '@typescript-eslint/no-shadow': baseVariablesRules['no-shadow'], |
| 58 | + 'no-unused-expressions': 'off', |
| 59 | + '@typescript-eslint/no-unused-expressions': baseBestPracticesRules['no-unused-expressions'], |
| 60 | + 'no-useless-constructor': 'off', |
| 61 | + '@typescript-eslint/no-useless-constructor': baseES6Rules['no-useless-constructor'], |
| 62 | + 'require-await': 'off', |
| 63 | + '@typescript-eslint/require-await': baseBestPracticesRules['require-await'], |
| 64 | + |
| 65 | + // Append 'ts' and 'tsx' to Airbnb 'import/extensions' rule |
| 66 | + // https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md |
| 67 | + 'import/extensions': [ |
| 68 | + baseImportsRules['import/extensions'][0], |
| 69 | + baseImportsRules['import/extensions'][1], |
| 70 | + typeof baseImportsRules['import/extensions'][2] === 'object' |
| 71 | + ? { |
| 72 | + ...baseImportsRules['import/extensions'][2], |
| 73 | + ts: 'never', |
| 74 | + tsx: 'never', |
| 75 | + } |
| 76 | + : { ts: 'never', tsx: 'never' }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + overrides: [ |
| 80 | + { |
| 81 | + files: ['*.ts', '*.tsx'], |
| 82 | + rules: { |
| 83 | + // The following rules are enabled in Airbnb config, but are already checked (more thoroughly) by the TypeScript compiler |
| 84 | + // Some of the rules also fail in TypeScript files, for example: https://github.com/typescript-eslint/typescript-eslint/issues/662#issuecomment-507081586 |
| 85 | + // Rules are inspired by: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/eslint-recommended.ts |
| 86 | + 'constructor-super': 'off', |
| 87 | + 'getter-return': 'off', |
| 88 | + 'no-const-assign': 'off', |
| 89 | + 'no-dupe-args': 'off', |
| 90 | + 'no-dupe-class-members': 'off', |
| 91 | + 'no-dupe-keys': 'off', |
| 92 | + 'no-func-assign': 'off', |
| 93 | + 'no-import-assign': 'off', |
| 94 | + 'no-new-symbol': 'off', |
| 95 | + 'no-obj-calls': 'off', |
| 96 | + 'no-redeclare': 'off', |
| 97 | + 'no-setter-return': 'off', |
| 98 | + 'no-this-before-super': 'off', |
| 99 | + 'no-undef': 'off', |
| 100 | + 'no-unreachable': 'off', |
| 101 | + 'no-unsafe-negation': 'off', |
| 102 | + 'valid-typeof': 'off', |
| 103 | + // The following rules are enabled in Airbnb config, but are recommended to be disabled within TypeScript projects |
| 104 | + // See: https://github.com/typescript-eslint/typescript-eslint/blob/13583e65f5973da2a7ae8384493c5e00014db51b/docs/linting/TROUBLESHOOTING.md#eslint-plugin-import |
| 105 | + 'import/named': 'off', |
| 106 | + 'import/no-named-as-default-member': 'off', |
| 107 | + 'import/no-unresolved': 'off', |
| 108 | + }, |
| 109 | + }, |
| 110 | + ], |
| 111 | +}; |
0 commit comments