Skip to content

Commit

Permalink
feat: remove old & duplicate rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed May 3, 2022
1 parent dfbd8e0 commit 448da13
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 98 deletions.
63 changes: 13 additions & 50 deletions packages/eslint-plugin/src/configs/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,68 +26,34 @@ export default defineConfig({
ignorePatterns,
rules: {
// Common
'semi': ['error', 'never'],
'curly': ['error', 'multi-or-nest', 'consistent'],
'quotes': ['error', 'single'],
'quote-props': ['error', 'consistent-as-needed'],
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'no-param-reassign': 'off',
'array-bracket-spacing': ['error', 'never'],
'brace-style': ['error', '1tbs', { allowSingleLine: true }],
'block-spacing': ['error', 'always'],
'camelcase': 'off',
'comma-spacing': ['error', { before: false, after: true }],
'comma-style': ['error', 'last'],
'comma-dangle': ['error', 'always-multiline'],
'no-constant-condition': 'warn',
'no-debugger': 'error',
'no-console': 'error',
'no-cond-assign': ['error', 'always'],
'no-tabs': 'error',
'func-call-spacing': ['off', 'never'],
'key-spacing': ['error', { beforeColon: false, afterColon: true }],
'indent': ['error', 2, { SwitchCase: 1, VariableDeclarator: 1, outerIIFEBody: 1 }],
'no-return-await': 'error',
'operator-linebreak': ['error', 'before'],
'space-before-function-paren': ['error', {
anonymous: 'always',
named: 'never',
asyncArrow: 'always',
}],
'no-use-before-define': ['error', {
functions: false,
classes: false,
variables: true,
}],
'no-restricted-syntax': [
'error',
'DebuggerStatement',
'ForInStatement',
'LabeledStatement',
'WithStatement',
],
'object-curly-spacing': ['error', 'always'],
'no-return-await': 'off',
'space-before-function-paren': ['error', 'never'],

// ES6
'no-var': 'error',
'prefer-const': [
'error',
{
destructuring: 'any',
ignoreReadBeforeAssign: true,
},
],
'prefer-arrow-callback': [
'error',
{
allowNamedFunctions: false,
allowUnboundThis: true,
},
],
'object-shorthand': [
'error',
'always',
{
ignoreConstructors: false,
avoidQuotes: true,
},
],
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'template-curly-spacing': 'error',
'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
'generator-star-spacing': 'off',
// formatting
'spaced-comment': ['error', 'always', {
line: {
markers: ['/'],
Expand All @@ -99,8 +65,6 @@ export default defineConfig({
balanced: true,
},
}],

// Order
'sort-imports': [
'error',
{
Expand All @@ -111,7 +75,6 @@ export default defineConfig({
allowSeparatedGroups: false,
},
],
'no-use-before-define': ['error', { functions: false, classes: false, variables: true }],

...eslintComments,
...bestPracticeRules,
Expand Down
42 changes: 33 additions & 9 deletions packages/eslint-plugin/src/configs/esnext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { defineConfig } from '../utils'
import { resolverExtensions } from '../constants'
import unicornRules from './rules/unicorn'

const allExtensions = ['.js', '.jsx', '.mjs', '.cjs', '.json']

export default defineConfig({
env: {
es6: true,
Expand All @@ -22,16 +21,12 @@ export default defineConfig({
plugins: ['unicorn'],
settings: {
'import/resolver': {
node: { extensions: allExtensions },
node: {
extensions: resolverExtensions,
},
},
},
overrides: [
{
files: ['*.d.ts'],
rules: {
'import/no-duplicates': 'off',
},
},
{
files: ['scripts/**/*'],
rules: {
Expand All @@ -46,6 +41,35 @@ export default defineConfig({
},
],
rules: {
'no-var': 'error',
'prefer-const': [
'error',
{
destructuring: 'any',
ignoreReadBeforeAssign: true,
},
],
'prefer-arrow-callback': [
'error',
{
allowNamedFunctions: false,
allowUnboundThis: true,
},
],
'object-shorthand': [
'error',
'always',
{
ignoreConstructors: false,
avoidQuotes: true,
},
],
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'prefer-template': 'error',
'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
'generator-star-spacing': ['error', { before: true, after: false }],

// import
'import/order': [
'error',
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-plugin/src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export default defineConfig({
},
{
files: ['*.jsx', '*.tsx'],
extends: ['plugin:react/recommended'],
extends: [
'plugin:react/recommended',
'plugin:react/jsx-runtime',
],
settings: {
react: {
version: '17',
Expand All @@ -22,7 +25,6 @@ export default defineConfig({
],
// off
'react/prop-types': 'off',
'react/react-in-jsx-scope': 'off',
'react/no-unknown-property': 'off',

...reactRules,
Expand Down
13 changes: 1 addition & 12 deletions packages/eslint-plugin/src/configs/rules/best-practice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@ import { defineRules } from '../../utils'

export default defineRules({
// best-practice
'array-callback-return': 'error',
'block-scoped-var': 'error',
'consistent-return': 'off',
'complexity': ['off', 11],
'eqeqeq': ['error', 'smart'],
'no-alert': 'warn',
'no-case-declarations': 'error',
'no-multi-spaces': 'error',
'no-multi-str': 'error',
'no-with': 'error',
'no-void': 'error',
'no-useless-escape': 'off',
'vars-on-top': 'error',
'require-await': 'off',
'no-return-assign': 'off',
'operator-linebreak': ['error', 'before'],
'require-await': 'error',
})
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default defineConfig({
'error',
{ onlyInlineLambdas: false },
],
'@typescript-eslint/promise-function-async': 'error',
},
},
],
Expand Down
60 changes: 35 additions & 25 deletions packages/eslint-plugin/src/configs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
import { defineConfig } from '../utils'

const extensions = ['.ts', '.tsx', '.js', '.cjs', '.mjs', '.jsx']
import { defineConfig, fromEntries, ruleFromStandard } from '../utils'
import { equivalents, resolverExtensionsWithTS } from '../constants'

export default defineConfig({
extends: [
'plugin:@hannoeru/esnext',
'plugin:import/typescript',
],
settings: {
'import/extensions': extensions,
'import/extensions': resolverExtensionsWithTS,
'import/external-module-folders': ['node_modules', 'node_modules/@types'],
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
node: {
extensions,
extensions: resolverExtensionsWithTS,
},
typescript: {
alwaysTryTypes: true,
},
},
},
overrides: [
{
files: ['*.d.ts'],
rules: {
'import/no-duplicates': 'off',
},
},
{
files: ['*.ts', '*.tsx'],
extends: ['plugin:@typescript-eslint/recommended'],
rules: {
// Rules replaced by @typescript-eslint versions:
...fromEntries(equivalents.map(name => [name, 'off'])),
// @typescript-eslint versions of Standard.js rules:
...fromEntries(equivalents.map(name => [`@typescript-eslint/${name}`, ruleFromStandard(name)])),
// Override custom JS rules
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/no-use-before-define': ['error', {
functions: false,
classes: false,
enums: false,
variables: true,
typedefs: false, // Only the TypeScript rule has this option.
}],
'@typescript-eslint/comma-dangle': ['error', 'always-multiline'],

// TS
'@typescript-eslint/semi': ['error', 'never'],
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
'@typescript-eslint/member-delimiter-style': ['error', { multiline: { delimiter: 'none' } }],
'@typescript-eslint/type-annotation-spacing': ['error', {}],
'@typescript-eslint/member-delimiter-style': [
'error',
{
multiline: { delimiter: 'none' },
singleline: { delimiter: 'comma', requireLast: false },
},
],
'@typescript-eslint/type-annotation-spacing': 'error',
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports', disallowTypeAnnotations: false }],
'@typescript-eslint/consistent-indexed-object-style': ['error', 'record'],
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
'@typescript-eslint/prefer-ts-expect-error': 'error',

// Override JS
'no-useless-constructor': 'off',
'indent': 'off',
'@typescript-eslint/indent': ['error', 2, { ignoredNodes: ['PropertyDefinition'] }],
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'error',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': ['error', { functions: false, classes: false, variables: true }],
'brace-style': 'off',
'@typescript-eslint/brace-style': ['error', '1tbs', { allowSingleLine: true }],
'comma-dangle': 'off',
'@typescript-eslint/comma-dangle': ['error', 'always-multiline'],
'object-curly-spacing': 'off',
'@typescript-eslint/object-curly-spacing': ['error', 'always'],

// off
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-parameter-properties': 'off',
'@typescript-eslint/parameter-properties': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/no-empty-function': 'off',
Expand Down
29 changes: 29 additions & 0 deletions packages/eslint-plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import config from 'eslint-config-standard/.eslintrc.json'
import type { TSESLint } from '@typescript-eslint/utils'

export const equivalents = [
'comma-spacing',
'dot-notation',
'brace-style',
'func-call-spacing',
'indent',
'keyword-spacing',
'lines-between-class-members',
'no-array-constructor',
'no-dupe-class-members',
'no-redeclare',
'no-throw-literal',
'no-unused-vars',
'no-unused-expressions',
'no-useless-constructor',
'quotes',
'semi',
'space-before-function-paren',
'space-infix-ops',
'object-curly-spacing',
] as const

export const configStandard = config as unknown as TSESLint.Linter.Config

export const resolverExtensions = ['.js', '.jsx', '.mjs', '.cjs', '.json']
export const resolverExtensionsWithTS = ['.ts', '.tsx', ...resolverExtensions]
16 changes: 16 additions & 0 deletions packages/eslint-plugin/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { configStandard } from './constants'
import type { TSESLint } from '@typescript-eslint/utils'

export function defineConfig(config: TSESLint.Linter.Config): TSESLint.Linter.Config {
Expand All @@ -11,3 +12,18 @@ export function defineRules(rules: TSESLint.Linter.Config['rules']): TSESLint.Li
export function definePlugin(plugin: TSESLint.Linter.Plugin): TSESLint.Linter.Plugin {
return plugin
}

export const ruleFromStandard = (name: string): TSESLint.Linter.RuleEntry => {
if (configStandard.rules === undefined) throw new Error('rules can not be undefined')
const rule = configStandard.rules[name]
if (rule === undefined) throw new Error('rule can not be undefined')
if (typeof rule !== 'object') return rule
return JSON.parse(JSON.stringify(rule))
}

export function fromEntries<T>(iterable: Array<[string, T]>): Record<string, T> {
return [...iterable].reduce<Record<string, T>>((obj, [key, val]) => {
obj[key] = val
return obj
}, {})
}

0 comments on commit 448da13

Please sign in to comment.