diff --git a/src/creation/formatMissingRules.test.ts b/src/creation/formatMissingRules.test.ts index 21913b2f..cef22e7a 100644 --- a/src/creation/formatMissingRules.test.ts +++ b/src/creation/formatMissingRules.test.ts @@ -13,7 +13,7 @@ describe("formatMissingRules", () => { ]; // Act - const output = formatMissingRules(missing, []); + const output = formatMissingRules(missing); // Assert expect(output).toEqual([ diff --git a/src/creation/formatMissingRules.ts b/src/creation/formatMissingRules.ts index 4497abd1..aab1c49d 100644 --- a/src/creation/formatMissingRules.ts +++ b/src/creation/formatMissingRules.ts @@ -1,7 +1,7 @@ import { TSLintRuleOptions } from "../rules/types"; -export const formatMissingRules = (missing: TSLintRuleOptions[], rulesDirectory: string[]) => { - const rules: { [i: string]: unknown } = {}; +export const formatMissingRules = (missing: TSLintRuleOptions[], rulesDirectory: string[] = []) => { + const rules: Record = {}; for (const rule of missing.sort((a, b) => a.ruleName.localeCompare(b.ruleName))) { if (rule.ruleSeverity !== "off") { diff --git a/src/input/findRawConfiguration.ts b/src/input/findRawConfiguration.ts index 4faf5f4a..fd217517 100644 --- a/src/input/findRawConfiguration.ts +++ b/src/input/findRawConfiguration.ts @@ -4,7 +4,7 @@ import { importer } from "./importer"; export const findRawConfiguration = async ( fileImporter: SansDependencies, filePath: string, - defaults: Partial, + defaults: Partial = {}, ): Promise => { let results: Configuration; diff --git a/src/input/findTSLintConfiguration.ts b/src/input/findTSLintConfiguration.ts index d14bfdc4..1fe43245 100644 --- a/src/input/findTSLintConfiguration.ts +++ b/src/input/findTSLintConfiguration.ts @@ -1,24 +1,18 @@ import { findRawConfiguration } from "./findRawConfiguration"; import { findReportedConfiguration } from "./findReportedConfiguration"; import { Exec } from "../adapters/exec"; -import { OriginalConfigurations } from "./findOriginalConfigurations"; import { SansDependencies } from "../binding"; import { importer } from "./importer"; +import { isDefined } from "../utils"; export type TSLintConfiguration = { extends?: string[]; - rulesDirectory: string[]; + rulesDirectory?: string[]; rules: TSLintConfigurationRules; }; export type TSLintConfigurationRules = Record; -const defaultTSLintConfiguration = { - extends: [], - rulesDirectory: [], - rules: {}, -}; - export type FindTSLintConfigurationDependencies = { exec: Exec; importer: SansDependencies; @@ -27,12 +21,10 @@ export type FindTSLintConfigurationDependencies = { export const findTSLintConfiguration = async ( dependencies: FindTSLintConfigurationDependencies, config: string | undefined, -): Promise | Error> => { +) => { const filePath = config || "./tslint.json"; const [rawConfiguration, reportedConfiguration] = await Promise.all([ - findRawConfiguration>(dependencies.importer, filePath, { - extends: [], - }), + findRawConfiguration>(dependencies.importer, filePath), findReportedConfiguration( dependencies.exec, "tslint --print-config", @@ -52,15 +44,23 @@ export const findTSLintConfiguration = async ( return rawConfiguration; } + const extensions = Array.from( + new Set( + [[rawConfiguration.extends], [reportedConfiguration.extends]] + .flat(Infinity) + .filter(isDefined), + ), + ); + + const rules = { + ...rawConfiguration.rules, + ...reportedConfiguration.rules, + }; + return { full: { - ...defaultTSLintConfiguration, - ...rawConfiguration, - extends: Array.from( - new Set( - [[rawConfiguration.extends], [reportedConfiguration.extends]].flat(Infinity), - ), - ), + ...(extensions.length !== 0 && { extends: extensions }), + rules, }, raw: rawConfiguration, }; diff --git a/src/input/findTslintConfiguration.test.ts b/src/input/findTslintConfiguration.test.ts index 557c7115..3b16615e 100644 --- a/src/input/findTslintConfiguration.test.ts +++ b/src/input/findTslintConfiguration.test.ts @@ -119,10 +119,44 @@ describe("findTSLintConfiguration", () => { expect(result).toEqual({ full: { extends: ["raw", "duplicated", "reported"], - rulesDirectory: [], rules: {}, }, raw, }); }); + + it("adds reported configuration rules on top of raw rules", async () => { + // Arrange + const raw = { + rules: { + "raw-rule": true, + }, + }; + const reported = { + rules: { + "reported-rule": true, + }, + }; + const dependencies = createStubDependencies({ + exec: createStubExec({ + stdout: JSON.stringify(reported), + }), + importer: async () => raw, + }); + const config = "./custom/tslint.json"; + + // Act + const result = await findTSLintConfiguration(dependencies, config); + + // Assert + expect(result).toEqual({ + full: { + rules: { + ...raw.rules, + ...reported.rules, + }, + }, + raw, + }); + }); });