Skip to content

Commit

Permalink
Again allowed rules from extended rulesets to be factored in (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg authored Oct 29, 2019
1 parent 3adfbd4 commit c9b4e6c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/creation/formatMissingRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("formatMissingRules", () => {
];

// Act
const output = formatMissingRules(missing, []);
const output = formatMissingRules(missing);

// Assert
expect(output).toEqual([
Expand Down
4 changes: 2 additions & 2 deletions src/creation/formatMissingRules.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};

for (const rule of missing.sort((a, b) => a.ruleName.localeCompare(b.ruleName))) {
if (rule.ruleSeverity !== "off") {
Expand Down
2 changes: 1 addition & 1 deletion src/input/findRawConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { importer } from "./importer";
export const findRawConfiguration = async <Configuration>(
fileImporter: SansDependencies<typeof importer>,
filePath: string,
defaults: Partial<Configuration>,
defaults: Partial<Configuration> = {},
): Promise<Configuration | Error> => {
let results: Configuration;

Expand Down
38 changes: 19 additions & 19 deletions src/input/findTSLintConfiguration.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;

const defaultTSLintConfiguration = {
extends: [],
rulesDirectory: [],
rules: {},
};

export type FindTSLintConfigurationDependencies = {
exec: Exec;
importer: SansDependencies<typeof importer>;
Expand All @@ -27,12 +21,10 @@ export type FindTSLintConfigurationDependencies = {
export const findTSLintConfiguration = async (
dependencies: FindTSLintConfigurationDependencies,
config: string | undefined,
): Promise<OriginalConfigurations<TSLintConfiguration> | Error> => {
) => {
const filePath = config || "./tslint.json";
const [rawConfiguration, reportedConfiguration] = await Promise.all([
findRawConfiguration<Partial<TSLintConfiguration>>(dependencies.importer, filePath, {
extends: [],
}),
findRawConfiguration<Partial<TSLintConfiguration>>(dependencies.importer, filePath),
findReportedConfiguration<TSLintConfiguration>(
dependencies.exec,
"tslint --print-config",
Expand All @@ -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,
};
Expand Down
36 changes: 35 additions & 1 deletion src/input/findTslintConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});

0 comments on commit c9b4e6c

Please sign in to comment.