diff --git a/package.json b/package.json index 24db3d27..75a937ea 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "LICENSE" ], "scripts": { - "build": "rimraf lib && tsc -d -p tsconfig.json", + "build": "rimraf lib && tsc -d -p src", "test": "jest", "ruling": "ts-node --files ruling/index.ts", "precommit": "pretty-quick --staged", diff --git a/src/index.ts b/src/index.ts index ff9fd3ce..d1c1ff01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,50 +19,80 @@ */ import type { TSESLint } from '@typescript-eslint/utils'; import type { FlatConfig } from '@typescript-eslint/utils/ts-eslint'; +import * as cognitiveComplexity from './rules/cognitive-complexity'; +import * as elseifWithoutElse from './rules/elseif-without-else'; +import * as maxSwitchCases from './rules/max-switch-cases'; +import * as noAllDuplicatedBranches from './rules/no-all-duplicated-branches'; +import * as noCollapsibleIf from './rules/no-collapsible-if'; +import * as noCollectionSizeMischeck from './rules/no-collection-size-mischeck'; +import * as noDuplicateString from './rules/no-duplicate-string'; +import * as noDuplicatedBranches from './rules/no-duplicated-branches'; +import * as noElementOverwrite from './rules/no-element-overwrite'; +import * as noEmptyCollection from './rules/no-empty-collection'; +import * as noExtraArguments from './rules/no-extra-arguments'; +import * as noGratuitousExpressions from './rules/no-gratuitous-expressions'; +import * as noIdenticalConditions from './rules/no-identical-conditions'; +import * as noIdenticalExpressions from './rules/no-identical-expressions'; +import * as noIdenticalFunctions from './rules/no-identical-functions'; +import * as noIgnoredReturn from './rules/no-ignored-return'; +import * as noInvertedBooleanCheck from './rules/no-inverted-boolean-check'; +import * as noNestedSwitch from './rules/no-nested-switch'; +import * as noNestedTemplateLiterals from './rules/no-nested-template-literals'; +import * as noOneIterationLoop from './rules/no-one-iteration-loop'; +import * as noRedundantBoolean from './rules/no-redundant-boolean'; +import * as noRedundantJump from './rules/no-redundant-jump'; +import * as noSameLineConditional from './rules/no-same-line-conditional'; +import * as noSmallSwitch from './rules/no-small-switch'; +import * as noUnusedCollection from './rules/no-unused-collection'; +import * as noUseOfEmptyReturnValue from './rules/no-use-of-empty-return-value'; +import * as noUselessCatch from './rules/no-useless-catch'; +import * as nonExistentOperator from './rules/non-existent-operator'; +import * as preferImmediateReturn from './rules/prefer-immediate-return'; +import * as preferObjectLiteral from './rules/prefer-object-literal'; +import * as preferSingleBooleanReturn from './rules/prefer-single-boolean-return'; +import * as preferWhile from './rules/prefer-while'; -const sonarjsRules: string[] = [ - 'cognitive-complexity', - 'elseif-without-else', - 'max-switch-cases', - 'no-all-duplicated-branches', - 'no-collapsible-if', - 'no-collection-size-mischeck', - 'no-duplicate-string', - 'no-duplicated-branches', - 'no-element-overwrite', - 'no-empty-collection', - 'no-extra-arguments', - 'no-gratuitous-expressions', - 'no-identical-conditions', - 'no-identical-expressions', - 'no-identical-functions', - 'no-ignored-return', - 'no-inverted-boolean-check', - 'no-nested-switch', - 'no-nested-template-literals', - 'no-one-iteration-loop', - 'no-redundant-boolean', - 'no-redundant-jump', - 'no-same-line-conditional', - 'no-small-switch', - 'no-unused-collection', - 'no-use-of-empty-return-value', - 'no-useless-catch', - 'non-existent-operator', - 'prefer-immediate-return', - 'prefer-object-literal', - 'prefer-single-boolean-return', - 'prefer-while', -]; - -const sonarjsRuleModules: { [key: string]: any } = {}; +const rules: Record>> = { + 'cognitive-complexity': cognitiveComplexity, + 'elseif-without-else': elseifWithoutElse, + 'max-switch-cases': maxSwitchCases, + 'no-all-duplicated-branches': noAllDuplicatedBranches, + 'no-collapsible-if': noCollapsibleIf, + 'no-collection-size-mischeck': noCollectionSizeMischeck, + 'no-duplicate-string': noDuplicateString, + 'no-duplicated-branches': noDuplicatedBranches, + 'no-element-overwrite': noElementOverwrite, + 'no-empty-collection': noEmptyCollection, + 'no-extra-arguments': noExtraArguments, + 'no-gratuitous-expressions': noGratuitousExpressions, + 'no-identical-conditions': noIdenticalConditions, + 'no-identical-expressions': noIdenticalExpressions, + 'no-identical-functions': noIdenticalFunctions, + 'no-ignored-return': noIgnoredReturn, + 'no-inverted-boolean-check': noInvertedBooleanCheck, + 'no-nested-switch': noNestedSwitch, + 'no-nested-template-literals': noNestedTemplateLiterals, + 'no-one-iteration-loop': noOneIterationLoop, + 'no-redundant-boolean': noRedundantBoolean, + 'no-redundant-jump': noRedundantJump, + 'no-same-line-conditional': noSameLineConditional, + 'no-small-switch': noSmallSwitch, + 'no-unused-collection': noUnusedCollection, + 'no-use-of-empty-return-value': noUseOfEmptyReturnValue, + 'no-useless-catch': noUselessCatch, + 'non-existent-operator': nonExistentOperator, + 'prefer-immediate-return': preferImmediateReturn, + 'prefer-object-literal': preferObjectLiteral, + 'prefer-single-boolean-return': preferSingleBooleanReturn, + 'prefer-while': preferWhile, +}; const plugin = { configs: {}, rules: {}, }; -const recommendedLegacyConfig: TSESLint.Linter.Config = { plugins: ['sonarjs'], rules: {} }; +const recommendedLegacyConfig: TSESLint.Linter.ConfigType = { plugins: ['sonarjs'], rules: {} }; const recommendedConfig: FlatConfig.Config = { plugins: { sonarjs: plugin, @@ -70,15 +100,13 @@ const recommendedConfig: FlatConfig.Config = { rules: {}, }; -sonarjsRules.forEach(rule => { - sonarjsRuleModules[rule] = require(`./rules/${rule}`); - const { - meta: { - docs: { recommended }, - }, - } = sonarjsRuleModules[rule]; - recommendedConfig.rules![`sonarjs/${rule}`] = recommended === undefined ? 'off' : 'error'; -}); +for (const key in rules) { + const rule = rules[key]; + const recommended = rule.meta.docs?.recommended; + + recommendedConfig.rules![`sonarjs/${key}`] = recommended === undefined ? 'off' : 'error'; +} + recommendedLegacyConfig.rules = recommendedConfig.rules; const configs = { @@ -87,4 +115,4 @@ const configs = { }; plugin.configs = configs; -export { sonarjsRuleModules as rules, configs }; +export { rules, configs }; diff --git a/src/tsconfig.json b/src/tsconfig.json new file mode 100644 index 00000000..159bec18 --- /dev/null +++ b/src/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "sourceMap": false + }, + "include": ["index.ts"] +} diff --git a/tests/index.test.ts b/tests/index.test.ts index 35767494..fd532146 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -45,7 +45,7 @@ it('should document all rules', () => { existingRules.forEach(rule => { expect(README.includes(rule)).toBe(true); expect(fs.existsSync(`${root}/docs/rules/${rule}.md`)).toBe(true); - expect(rules[rule].meta.docs.url).toBe( + expect(rules[rule].meta.docs!.url).toBe( `https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/${rule}.md`, ); }); diff --git a/tsconfig.json b/tsconfig.json index f3b50ea9..026adce6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,5 @@ "outDir": "lib", "sourceMap": true, "moduleResolution": "nodenext" - }, - "include": ["src/**/*.ts"] + } }