Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
feat: split sonar/sonarjs configs, add more disable rules
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 13, 2021
1 parent f4ce85e commit b139d39
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 371 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tslint-config-eslint

> Yet another TSLint Configuration which disables all rules which has been handled by `eslint`, `@typescript-eslint` or [`eslint-plugin-sonar`](https://github.com/rx-ts/eslint-plugin-sonar)
> Yet another TSLint Configuration which disables all rules which has been handled by [`eslint`](https://github.com/eslint/eslint), [`@typescript-eslint/eslint-plugin`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin), [`eslint-plugin-sonarjs`](https://github.com/SonarSource/eslint-plugin-sonarjs) or [`eslint-plugin-sonar`](https://github.com/rx-ts/eslint-plugin-sonar).
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/rx-ts/tslint-config-eslint/Publish%20package)](https://github.com/rx-ts/tslint-config-eslint/actions?query=workflow%3A%22Publish+package%22)
[![Codacy Grade](https://img.shields.io/codacy/grade/5c70cd4efc864eb3b344e32be9aecce8)](https://www.codacy.com/app/JounQin/tslint-config-eslint)
Expand All @@ -17,7 +17,7 @@
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![codechecks.io](https://raw.githubusercontent.com/codechecks/docs/master/images/badges/badge-default.svg?sanitize=true)](https://codechecks.io)

[TSLint][] will be [deprecated](https://github.com/palantir/tslint/issues/4534) some time in 2019, but it has not been finished. So maybe you're using [ESLint][] with it together, then it would be terrible to lint codes twice, especially for those rules which has equivalent rules from [ESLint][] or [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin).
[TSLint][] will be [deprecated](https://github.com/palantir/tslint/issues/4534) some time in 2019, but it has not been finished. So maybe you're using [ESLint][] with it together, then it would be terrible to lint codes twice, especially for those rules which has equivalent rules from [`eslint`](https://github.com/eslint/eslint), [`@typescript-eslint/eslint-plugin`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin), [`eslint-plugin-sonarjs`](https://github.com/SonarSource/eslint-plugin-sonarjs) or [`eslint-plugin-sonar`](https://github.com/rx-ts/eslint-plugin-sonar).

You may tried something like [tslint-to-eslint-config](https://github.com/typescript-eslint/tslint-to-eslint-config) to help you to migrate, while this package/configuration will help you to use [ESLint][] quickly without remove or refactor your original `tslint.json` heavily.

Expand Down
159 changes: 131 additions & 28 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from 'fs'

export interface RuleReplacement {
[rule: string]: string | string[]
}

export interface DisabledRules {
[rule: string]: false
}

const CORE_AS_IS_REPLACEMENTS = [
'curly',
'no-bitwise',
Expand Down Expand Up @@ -28,7 +36,7 @@ const CORE_AS_IS_REPLACEMENTS = [
'no-irregular-whitespace',
]

const CORE_REPLACEMENTS = CORE_AS_IS_REPLACEMENTS.reduce(
const CORE_REPLACEMENTS = CORE_AS_IS_REPLACEMENTS.reduce<RuleReplacement>(
(rules, rule) => Object.assign(rules, { [rule]: rule }),
{
'no-for-in': 'guard-for-in',
Expand Down Expand Up @@ -74,7 +82,7 @@ const CORE_REPLACEMENTS = CORE_AS_IS_REPLACEMENTS.reduce(
)

// from `@typescript-eslint/eslint-plugin`
const ADDITIONAL_TS_AS_IS_REPLACEMENTS = [
const TS_AS_IS_REPLACEMENTS = [
'adjacent-overload-signatures',
'array-type',
'ban-ts-ignore',
Expand Down Expand Up @@ -102,8 +110,9 @@ const ADDITIONAL_TS_AS_IS_REPLACEMENTS = [
'use-isnan',
]

const ADDITIONAL_TS_REPLACEMENTS = ADDITIONAL_TS_AS_IS_REPLACEMENTS.reduce(
(rules, rule) => Object.assign(rules, { [rule]: rule }),
const TS_REPLACEMENTS = TS_AS_IS_REPLACEMENTS.reduce<RuleReplacement>(
(rules, rule) =>
Object.assign(rules, { [rule]: '@typescript-eslint/' + rule }),
{
'await-promise': 'await-thenable',
'class-name': 'class-name-casing',
Expand Down Expand Up @@ -134,33 +143,127 @@ const ADDITIONAL_TS_REPLACEMENTS = ADDITIONAL_TS_AS_IS_REPLACEMENTS.reduce(
},
)

// form `eslint-plugin-deprecation`
const ADDITIONAL_SONAR_AS_IS_REPLACEMENTS = ['deprecation']
const SONARJS_AS_IS_REPLACEMENTS = [
'no-all-duplicated-branches',
'cognitive-complexity',
'max-switch-cases',
'no-collapsible-if',
'no-collection-size-mischeck',
'no-duplicate-string',
'no-duplicated-branches',
'no-element-overwrite',
'no-gratuitous-expressions',
'no-identical-conditions',
'no-identical-expressions',
'no-identical-functions',
'no-inverted-boolean-check',
'no-redundant-boolean',
'no-redundant-jump',
'no-small-switch',
'no-same-line-conditional',
'no-use-of-empty-return-value',
'no-useless-catch',
'prefer-immediate-return',
'prefer-promise-shorthand',
'prefer-type-guard',
]

const ADDITIONAL_SONAR_REPLACEMENTS = ADDITIONAL_SONAR_AS_IS_REPLACEMENTS.reduce(
(rules, rule) => Object.assign(rules, { [rule]: rule }),
{},
const SONARJS_REPLACEMENTS = SONARJS_AS_IS_REPLACEMENTS.reduce<RuleReplacement>(
(rules, rule) => Object.assign(rules, { [rule]: 'sonarjs/' + rule }),
{
'no-big-function': [
'max-lines-per-function',
'sonarjs/sonar-max-lines-per-function',
],
'parameters-max-number': 'max-params',

// core
'no-empty-destructuring': 'no-empty-pattern',
'no-empty-nested-blocks': 'no-empty',
'no-extra-semicolon': 'no-extra-semi',
'no-multiline-string-literals': 'no-multi-str',
'no-self-assignment': 'no-self-assign',
'no-statements-same-line': 'no-same-line-conditional',
'no-unconditional-jump': 'no-redundant-jump',
'no-unused-array': 'no-unused-collection',
'prefer-optional': 'no-redundant-optional',

// typescript
'consecutive-overloads': 'adjacent-overload-signatures',
'no-useless-cast': 'no-unnecessary-type-assertion',
},
)

const DISABLED_RULES = [
...Object.keys(CORE_REPLACEMENTS),
...Object.keys(ADDITIONAL_TS_REPLACEMENTS),
...Object.keys(ADDITIONAL_SONAR_REPLACEMENTS),
].reduce(
(rules, rule) =>
Object.assign(rules, {
[rule]: false,
}),
{},
// form `eslint-plugin-sonar`
const SONAR_AS_IS_REPLACEMENTS = [
'arguments-order',
'bool-param-default',
'deprecation',
'max-union-size',
'no-accessor-field-mismatch',
'no-alphabetical-sort',
'no-array-delete',
'no-commented-code',
'no-dead-store',
'no-duplicate-in-composite',
'no-hardcoded-credentials',
'no-gratuitous-expressions',
'no-ignored-return',
'no-in-misuse',
'no-invalid-await',
'no-misleading-array-reverse',
'no-nested-incdec',
'no-nested-switch',
'no-nested-template-literals',
'no-redundant-parentheses',
'no-return-type-any',
'no-try-promise',
'no-undefined-argument',
'no-unenclosed-multiline-block',
'no-unthrown-error',
'no-useless-increment',
'no-useless-intersection',
'no-variable-usage-before-declaration',
'use-type-alias',
]

const SONAR_REPLACEMENTS = SONAR_AS_IS_REPLACEMENTS.reduce<RuleReplacement>(
(rules, rule) => Object.assign(rules, { [rule]: 'sonar/' + rule }),
{
'mccabe-complexity': 'cyclomatic-complexity',
'no-empty-array': 'no-empty-collection',
'no-ignored-initial-value': 'no-parameter-reassignment',
'no-inconsistent-return': 'no-inconsistent-returns',
'no-invariant-return': 'no-invariant-returns',
'no-misspelled-operator': 'non-existent-operator',
'use-primitive-type': 'no-primitive-wrappers',
},
)

fs.writeFileSync(
'base.json',
JSON.stringify(
{
rules: DISABLED_RULES,
},
null,
2,
) + '\n',
const getDisabledRules = (rules: string[] | RuleReplacement) =>
(Array.isArray(rules) ? rules : Object.keys(rules)).reduce<DisabledRules>(
(acc, rule) =>
Object.assign(acc, {
[rule]: false,
}),
{},
)

const writeConfig = (config: string, rules: DisabledRules) =>
fs.writeFileSync(
config + '.json',
JSON.stringify(
{
rules,
},
null,
2,
) + '\n',
)

writeConfig(
'base',
getDisabledRules({ ...CORE_REPLACEMENTS, ...TS_REPLACEMENTS }),
)
writeConfig('sonarjs', getDisabledRules(SONARJS_REPLACEMENTS))
writeConfig('sonar', getDisabledRules(SONAR_REPLACEMENTS))
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"typescript": ">=3.0.0"
},
"devDependencies": {
"@1stg/common-config": "^1.2.9",
"@1stg/tslint-config": "^1.0.1",
"@types/node": "^14.14.31",
"@1stg/common-config": "^1.2.10",
"@1stg/tslint-config": "^1.1.0",
"@types/node": "^14.14.34",
"core-js": "^3.9.1",
"npm-run-all": "^4.1.5",
"standard-version": "^9.1.1",
Expand All @@ -40,10 +40,10 @@
"yarn-deduplicate": "^3.1.0"
},
"resolutions": {
"@babel/core": "^7.13.8",
"@babel/preset-env": "^7.13.9",
"@babel/core": "^7.13.10",
"@babel/preset-env": "^7.13.10",
"prettier": "^2.2.1",
"typescript": "^4.2.2"
"typescript": "^4.2.3"
},
"typeCoverage": {
"atLeast": 100,
Expand Down
51 changes: 36 additions & 15 deletions sonar.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
{
"$schema": "http://json.schemastore.org/tslint",
"extends": "./base.json",
"rules": {
"cognitive-complexity": false,
"max-switch-cases": false,
"no-big-function": false, // max-lines-per-function
"no-collapsible-if": false,
"no-duplicate-string": false,
"no-duplicated-branches": false,
"no-identical-functions": false,
"no-inverted-boolean-check": false,
"no-redundant-boolean": false,
"no-small-switch": false,
"no-useless-catch": false,
"parameters-max-number": false, // max-params
"prefer-immediate-return": false
"mccabe-complexity": false,
"no-empty-array": false,
"no-ignored-initial-value": false,
"no-inconsistent-return": false,
"no-invariant-return": false,
"no-misspelled-operator": false,
"use-primitive-type": false,
"arguments-order": false,
"bool-param-default": false,
"deprecation": false,
"max-union-size": false,
"no-accessor-field-mismatch": false,
"no-alphabetical-sort": false,
"no-array-delete": false,
"no-commented-code": false,
"no-dead-store": false,
"no-duplicate-in-composite": false,
"no-hardcoded-credentials": false,
"no-gratuitous-expressions": false,
"no-ignored-return": false,
"no-in-misuse": false,
"no-invalid-await": false,
"no-misleading-array-reverse": false,
"no-nested-incdec": false,
"no-nested-switch": false,
"no-nested-template-literals": false,
"no-redundant-parentheses": false,
"no-return-type-any": false,
"no-try-promise": false,
"no-undefined-argument": false,
"no-unenclosed-multiline-block": false,
"no-unthrown-error": false,
"no-useless-increment": false,
"no-useless-intersection": false,
"no-variable-usage-before-declaration": false,
"use-type-alias": false
}
}
39 changes: 39 additions & 0 deletions sonarjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"rules": {
"no-big-function": false,
"parameters-max-number": false,
"no-empty-destructuring": false,
"no-empty-nested-blocks": false,
"no-extra-semicolon": false,
"no-multiline-string-literals": false,
"no-self-assignment": false,
"no-statements-same-line": false,
"no-unconditional-jump": false,
"no-unused-array": false,
"prefer-optional": false,
"consecutive-overloads": false,
"no-useless-cast": false,
"no-all-duplicated-branches": false,
"cognitive-complexity": false,
"max-switch-cases": false,
"no-collapsible-if": false,
"no-collection-size-mischeck": false,
"no-duplicate-string": false,
"no-duplicated-branches": false,
"no-element-overwrite": false,
"no-gratuitous-expressions": false,
"no-identical-conditions": false,
"no-identical-expressions": false,
"no-identical-functions": false,
"no-inverted-boolean-check": false,
"no-redundant-boolean": false,
"no-redundant-jump": false,
"no-small-switch": false,
"no-same-line-conditional": false,
"no-use-of-empty-return-value": false,
"no-useless-catch": false,
"prefer-immediate-return": false,
"prefer-promise-shorthand": false,
"prefer-type-guard": false
}
}
Loading

0 comments on commit b139d39

Please sign in to comment.