Skip to content

Commit 8df61df

Browse files
author
Josh Goldberg
authored
Added error reporting for optional configurations (#126)
1 parent 95c17ca commit 8df61df

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/input/findOriginalConfigurations.test.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import {
22
findOriginalConfigurations,
33
FindOriginalConfigurationsDependencies,
44
} from "./findOriginalConfigurations";
5-
import { ResultStatus } from "../types";
5+
import { ResultStatus, TSLintToESLintSettings } from "../types";
66
import { TSLintConfiguration } from "./findTSLintConfiguration";
77
import { ESLintConfiguration } from "./findESLintConfiguration";
88

9-
const createRawSettings = () => ({
9+
const createRawSettings = (overrides: Partial<TSLintToESLintSettings> = {}) => ({
1010
config: "./eslintrc.js",
11-
eslint: "",
12-
tslint: "",
13-
typescript: "",
11+
...overrides,
1412
});
1513

1614
const createDependencies = (overrides: Partial<FindOriginalConfigurationsDependencies> = {}) => ({
@@ -38,7 +36,7 @@ const createDependencies = (overrides: Partial<FindOriginalConfigurationsDepende
3836
});
3937

4038
describe("findOriginalConfigurations", () => {
41-
it("returns an errors when the tslint finder returns an error", async () => {
39+
it("returns errors when the tslint finder returns an error", async () => {
4240
// Arrange
4341
const complaint = "Complaint from TSLint";
4442
const dependencies = createDependencies({
@@ -78,6 +76,58 @@ describe("findOriginalConfigurations", () => {
7876
});
7977
});
8078

79+
it("returns an error when an optional configuration returns an error and the user asked for it", async () => {
80+
// Arrange
81+
const eslint = new Error("one");
82+
const dependencies = createDependencies({
83+
findESLintConfiguration: async () => eslint,
84+
});
85+
86+
// Act
87+
const result = await findOriginalConfigurations(
88+
dependencies,
89+
createRawSettings({
90+
eslint: "./eslintrc.js",
91+
}),
92+
);
93+
94+
// Assert
95+
expect(result).toEqual({
96+
complaints: [eslint.message],
97+
status: ResultStatus.ConfigurationError,
98+
});
99+
});
100+
101+
it("returns successful results when an optional configuration returns an error and the user didn't ask for it", async () => {
102+
// Arrange
103+
const dependencies = createDependencies({
104+
findESLintConfiguration: async () => new Error("one"),
105+
});
106+
107+
// Act
108+
const result = await findOriginalConfigurations(dependencies, createRawSettings());
109+
110+
// Assert
111+
expect(result).toEqual({
112+
data: {
113+
packages: {
114+
dependencies: {},
115+
devDependencies: {},
116+
},
117+
tslint: {
118+
rulesDirectory: [],
119+
rules: {},
120+
},
121+
typescript: {
122+
compilerOptions: {
123+
target: "es3",
124+
},
125+
},
126+
},
127+
status: ResultStatus.Succeeded,
128+
});
129+
});
130+
81131
it("returns successful results when all finders succeed", async () => {
82132
// Arrange
83133
const dependencies = createDependencies();

src/input/findOriginalConfigurations.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,35 @@ export const findOriginalConfigurations = async (
2828
dependencies: FindOriginalConfigurationsDependencies,
2929
rawSettings: TSLintToESLintSettings,
3030
): Promise<ResultWithDataStatus<OriginalConfigurations>> => {
31+
// Simultaneously search for all required configuration types
3132
const [eslint, packages, tslint, typescript] = await Promise.all([
3233
dependencies.findESLintConfiguration(rawSettings),
3334
dependencies.findPackagesConfiguration(rawSettings.package),
3435
dependencies.findTSLintConfiguration(rawSettings.tslint),
3536
dependencies.findTypeScriptConfiguration(rawSettings.typescript),
3637
]);
3738

39+
// Out of those configurations, only TSLint's is required to run
3840
if (tslint instanceof Error) {
3941
return {
4042
complaints: [tslint.message],
4143
status: ResultStatus.ConfigurationError,
4244
};
4345
}
4446

47+
// Other configuration errors only halt the program if the user asked for them
48+
const configurationErrors = ([
49+
[eslint, rawSettings.eslint],
50+
[packages, rawSettings.package],
51+
[typescript, rawSettings.typescript],
52+
] as const).filter(configurationResultIsError);
53+
if (configurationErrors.length !== 0) {
54+
return {
55+
complaints: configurationErrors.map(([configuration]) => configuration.message),
56+
status: ResultStatus.ConfigurationError,
57+
};
58+
}
59+
4560
return {
4661
data: {
4762
...(!(eslint instanceof Error) && { eslint }),
@@ -52,3 +67,9 @@ export const findOriginalConfigurations = async (
5267
status: ResultStatus.Succeeded,
5368
};
5469
};
70+
71+
const configurationResultIsError = (
72+
result: readonly [{} | Error | undefined, string | undefined],
73+
): result is [Error, string] => {
74+
return result[0] instanceof Error && typeof result[1] === "string";
75+
};

0 commit comments

Comments
 (0)