Skip to content

Commit

Permalink
feat(eslint-config-airlight-base): add @stylistic preset
Browse files Browse the repository at this point in the history
- Now it supports `@stylistic` presets to include code style enforce
- Use `typing` rules only for `*.ts` files for both performance and reliability reason
- Fixes and improvements
  • Loading branch information
dalisoft committed Aug 14, 2024
1 parent 8d82b47 commit 98ba9da
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 109 deletions.
74 changes: 15 additions & 59 deletions packages/eslint-config-base/airbnb-rules.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair
/* eslint-disable max-lines */
// @ts-check

// @ts-expect-error Cannot find namespace 'SharedConfig'
Expand All @@ -6,40 +8,15 @@ module.exports = {
// Replace Airbnb 'brace-style' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/brace-style.md
'brace-style': 'off',
'@typescript-eslint/brace-style': [
'error',
'1tbs',
{ allowSingleLine: true }
],
'@stylistic/brace-style': ['error', '1tbs', { allowSingleLine: true }],

// Replace Airbnb 'camelcase' rule with '@typescript-eslint/naming-convention'
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/naming-convention.md
camelcase: 'off',
// The `@typescript-eslint/naming-convention` rule allows `leadingUnderscore` and `trailingUnderscore` settings. However, the existing `no-underscore-dangle` rule already takes care of this.
'@typescript-eslint/naming-convention': [
'error',
// Allow camelCase variables (23.2), PascalCase variables (23.8), and UPPER_CASE variables (23.10)
{
selector: 'variable',
format: ['camelCase', 'PascalCase', 'UPPER_CASE']
},
// Allow camelCase functions (23.2), and PascalCase functions (23.8)
{
selector: 'function',
format: ['camelCase', 'PascalCase']
},
// 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
{
selector: 'typeLike',
format: ['PascalCase']
}
],

// Replace Airbnb 'comma-dangle' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/comma-dangle.md
// The TypeScript version also adds 3 new options, all of which should be set to the same value as the base config
'comma-dangle': 'off',
'@typescript-eslint/comma-dangle': [
'@stylistic/comma-dangle': [
'error',
{
arrays: 'always-multiline',
Expand All @@ -56,22 +33,17 @@ module.exports = {
// Replace Airbnb 'comma-spacing' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/comma-spacing.md
'comma-spacing': 'off',
'@typescript-eslint/comma-spacing': ['error', { before: false, after: true }],
'@stylistic/comma-spacing': ['error', { before: false, after: true }],

// Replace Airbnb 'default-param-last' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/default-param-last.md
'default-param-last': 'off',
'@typescript-eslint/default-param-last': 'error',

// Replace Airbnb 'dot-notation' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/dot-notation.md
'dot-notation': 'off',
'@typescript-eslint/dot-notation': 'error',

// Replace Airbnb 'func-call-spacing' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/func-call-spacing.md
'func-call-spacing': 'off',
'@typescript-eslint/func-call-spacing': ['error', 'never'],
'@stylistic/function-call-spacing': ['error', 'never'],

// Replace Airbnb 'indent' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/indent.md
Expand Down Expand Up @@ -125,7 +97,7 @@ module.exports = {
// Replace Airbnb 'keyword-spacing' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/keyword-spacing.md
'keyword-spacing': 'off',
'@typescript-eslint/keyword-spacing': [
'@stylistic/keyword-spacing': [
'error',
{
before: true,
Expand All @@ -141,7 +113,7 @@ module.exports = {
// Replace Airbnb 'lines-between-class-members' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/lines-between-class-members.md
'lines-between-class-members': 'off',
'@typescript-eslint/lines-between-class-members': [
'@stylistic/lines-between-class-members': [
'error',
'always',
{ exceptAfterSingleLine: false }
Expand Down Expand Up @@ -185,13 +157,7 @@ module.exports = {
// Replace Airbnb 'no-extra-semi' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-extra-semi.md
'no-extra-semi': 'off',
'@typescript-eslint/no-extra-semi': 'error',

// Replace Airbnb 'no-implied-eval' and 'no-new-func' rules with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-implied-eval.md
'no-implied-eval': 'off',
'no-new-func': 'off',
'@typescript-eslint/no-implied-eval': 'error',
'@stylistic/no-extra-semi': 'error',

// Replace Airbnb 'no-loss-of-precision' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-loss-of-precision.md
Expand Down Expand Up @@ -229,12 +195,7 @@ module.exports = {
// Replace Airbnb 'space-before-blocks' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/space-before-blocks.md
'space-before-blocks': 'off',
'@typescript-eslint/space-before-blocks': 'error',

// Replace Airbnb 'no-throw-literal' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-throw-literal.md
'no-throw-literal': 'off',
'@typescript-eslint/no-throw-literal': 'error',
'@stylistic/space-before-blocks': 'error',

// Replace Airbnb 'no-unused-expressions' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-expressions.md
Expand Down Expand Up @@ -281,17 +242,17 @@ module.exports = {
// Replace Airbnb 'quotes' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/quotes.md
quotes: 'off',
'@typescript-eslint/quotes': ['error', 'single', { avoidEscape: true }],
'@stylistic/quotes': ['error', 'single', { avoidEscape: true }],

// Replace Airbnb 'semi' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/semi.md
semi: 'off',
'@typescript-eslint/semi': ['error', 'always'],
'@stylistic/semi': ['error', 'always'],

// Replace Airbnb 'space-before-function-paren' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/space-before-function-paren.md
'space-before-function-paren': 'off',
'@typescript-eslint/space-before-function-paren': [
'@stylistic/space-before-function-paren': [
'error',
{
anonymous: 'always',
Expand All @@ -305,20 +266,15 @@ module.exports = {
'require-await': 'off',
'@typescript-eslint/require-await': 'off',

// Replace Airbnb 'no-return-await' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/return-await.md
'no-return-await': 'off',
'@typescript-eslint/return-await': ['error', 'in-try-catch'],

// Replace Airbnb 'space-infix-ops' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/space-infix-ops.md
'space-infix-ops': 'off',
'@typescript-eslint/space-infix-ops': 'error',
'@stylistic/space-infix-ops': 'error',

// Replace Airbnb 'object-curly-spacing' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/object-curly-spacing.md
'object-curly-spacing': 'off',
'@typescript-eslint/object-curly-spacing': ['error', 'always'],
'@stylistic/object-curly-spacing': ['error', 'always'],

'@typescript-eslint/ban-ts-comment': 'error',

Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-config-base/flat.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const eslintJs = require('@eslint/js');
const eslintConfigPrettier = require('eslint-config-prettier');
const globals = require('globals');
const tseslint = require('typescript-eslint');
const stylistic = require('@stylistic/eslint-plugin');
const airbnbRules = require('./airbnb-rules.cjs');
const rules = require('./rules.cjs');
const cjsFiles = require('./overrides/cjs-files.cjs');
Expand All @@ -27,7 +28,9 @@ module.exports = tseslint.config(
{
name: 'eslint-config-airlight-base',
plugins: {
'@typescript-eslint': tseslint.plugin
'@typescript-eslint': tseslint.plugin,
// @ts-expect-error No typings yet?
'@stylistic': stylistic
},
languageOptions: {
ecmaVersion: 2020,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config-base/legacy.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'@stylistic',
'plugin:@eslint-community/eslint-comments/recommended',
'plugin:promise/recommended',
'prettier',
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-base/overrides/cjs-files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = tseslint.config({
name: 'eslint-override-cjs-files',
files: ['**/*.cjs'],
rules: {
'import-x/extensions': 'off'
'import-x/extensions': 'off',
'@typescript-eslint/no-require-imports': 'off'
}
});
46 changes: 44 additions & 2 deletions packages/eslint-config-base/overrides/ts-files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = tseslint.config({
offsetTernaryExpressions: true
}
],
'@typescript-eslint/quotes': [
'@stylistic/quotes': [
'error',
'single',
{
Expand All @@ -54,9 +54,51 @@ module.exports = tseslint.config({
'@typescript-eslint/no-shadow': ['error', { hoist: 'functions' }],
'@typescript-eslint/no-empty-function': ['error'],
'@typescript-eslint/consistent-indexed-object-style': ['error'],
'@typescript-eslint/restrict-plus-operands': ['error'],
indent: 'off',
quotes: 'off',
'no-shadow': 'off',
'no-empty-function': 'off'
'no-empty-function': 'off',

// Replace Airbnb 'dot-notation' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/dot-notation.md
'dot-notation': 'off',
'@typescript-eslint/dot-notation': 'error',

// Replace Airbnb 'no-implied-eval' and 'no-new-func' rules with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-implied-eval.md
'no-implied-eval': 'off',
'no-new-func': 'off',
'@typescript-eslint/no-implied-eval': 'error',

// Replace Airbnb 'no-throw-literal' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-throw-literal.md
'no-throw-literal': 'off',
'@typescript-eslint/only-throw-error': 'error',

// Replace Airbnb 'no-return-await' rule with '@typescript-eslint' version
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/return-await.md
'no-return-await': 'off',
'@typescript-eslint/return-await': ['error', 'in-try-catch'],

// The `@typescript-eslint/naming-convention` rule allows `leadingUnderscore` and `trailingUnderscore` settings. However, the existing `no-underscore-dangle` rule already takes care of this.
'@typescript-eslint/naming-convention': [
'error',
// Allow camelCase variables (23.2), PascalCase variables (23.8), and UPPER_CASE variables (23.10)
{
selector: 'variable',
format: ['camelCase', 'PascalCase', 'UPPER_CASE']
},
// Allow camelCase functions (23.2), and PascalCase functions (23.8)
{
selector: 'function',
format: ['camelCase', 'PascalCase']
},
// 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
{
selector: 'typeLike',
format: ['PascalCase']
}
]
}
});
8 changes: 5 additions & 3 deletions packages/eslint-config-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@
"@eslint/compat": "^1.1.1",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.9.0",
"@stylistic/eslint-plugin": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-filename-rules": "^1.3.1",
"eslint-plugin-import-x": "^3.1.0",
"eslint-plugin-no-secrets": "^1.0.2",
"eslint-plugin-optimize-regex": "^1.2.1",
"eslint-plugin-promise": "^7.1.0",
"typescript-eslint": "^8.1.0",
"globals": "^15.9.0",
"@typescript-eslint/parser": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^8.1.0"
"typescript-eslint": "^8.1.0"
},
"devDependencies": {
"@types/eslint": "~9.6.0",
"@types/eslint-config-prettier": "^6.11.3",
"@types/eslint__eslintrc": "^2.1.2",
"@types/eslint__js": "^8.42.3",
Expand Down
46 changes: 3 additions & 43 deletions packages/eslint-config-base/rules.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,15 @@
module.exports = {
'@eslint-community/eslint-comments/no-unused-disable': 'error',
'@eslint-community/eslint-comments/disable-enable-pair': 'warn',
'@typescript-eslint/naming-convention': [
'error',
{
selector: ['function', 'method'],
format: ['strictCamelCase'],
leadingUnderscore: 'forbid'
},
{
selector: 'property',
format: ['snake_case', 'strictCamelCase'],
leadingUnderscore: 'forbid'
},
{
selector: 'objectLiteralProperty',
format: [],
leadingUnderscore: 'forbid'
},
{
selector: 'variable',
modifiers: ['const'],
types: ['boolean', 'number'],
format: ['UPPER_CASE'],
leadingUnderscore: 'forbid'
},
{
selector: 'typeLike',
format: ['StrictPascalCase'],
leadingUnderscore: 'forbid'
},
{
selector: 'interface',
format: ['PascalCase'],
leadingUnderscore: 'forbid',
custom: {
regex: '^I[A-Z]',
match: true
}
}
],
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-shadow': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/indent': ['off'],
'@typescript-eslint/class-literal-property-style': ['error'],
'@typescript-eslint/comma-dangle': ['error', 'never'],
'@typescript-eslint/comma-spacing': ['error', { before: false, after: true }],
'@stylistic/comma-dangle': ['error', 'never'],
'@stylistic/comma-spacing': ['error', { before: false, after: true }],
'@typescript-eslint/no-non-null-asserted-optional-chain': ['error'],
'@typescript-eslint/restrict-plus-operands': ['error'],
'operator-linebreak': ['off', 'after'],
'prefer-template': 'error',
quotes: [
Expand Down Expand Up @@ -80,7 +40,7 @@ module.exports = {
],
'max-lines': [
'error',
{ max: 280, skipBlankLines: true, skipComments: true }
{ max: 100, skipBlankLines: true, skipComments: true }
],
'no-useless-concat': 'error',
'no-console': process.env.CI ? 'error' : 'warn',
Expand Down

0 comments on commit 98ba9da

Please sign in to comment.