Skip to content

Commit b165a6b

Browse files
author
Josh Goldberg
authored
Stopped writing empty root members in output configurations (#539)
1 parent daee715 commit b165a6b

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

src/creation/writeConversionResults.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe("writeConversionResults", () => {
4646
sourceType: "module",
4747
},
4848
plugins: ["@typescript-eslint"],
49-
rules: {},
5049
}),
5150
);
5251
});
@@ -137,8 +136,6 @@ describe("writeConversionResults", () => {
137136
es6: true,
138137
node: true,
139138
},
140-
extends: [],
141-
rules: {},
142139
parser: "@typescript-eslint/parser",
143140
parserOptions: {
144141
project: "tsconfig.json",
@@ -182,7 +179,6 @@ describe("writeConversionResults", () => {
182179
sourceType: "module",
183180
},
184181
plugins: ["@typescript-eslint"],
185-
rules: {},
186182
}),
187183
);
188184
});
@@ -224,8 +220,6 @@ describe("writeConversionResults", () => {
224220
es6: true,
225221
node: true,
226222
},
227-
extends: [],
228-
rules: {},
229223
globals: {
230224
Promise: true,
231225
},

src/creation/writeConversionResults.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FileSystem } from "../adapters/fileSystem";
22
import { AllOriginalConfigurations } from "../input/findOriginalConfigurations";
3+
import { removeEmptyMembers } from "../utils";
34
import { createEnv } from "./eslint/createEnv";
45
import { formatConvertedRules } from "./formatConvertedRules";
56
import { formatOutput } from "./formatting/formatOutput";
@@ -22,11 +23,11 @@ export const writeConversionResults = async (
2223
plugins.push("@typescript-eslint/tslint");
2324
}
2425

25-
const output = {
26+
const output = removeEmptyMembers({
2627
...eslint?.full,
2728
env: createEnv(originalConfigurations),
2829
...(eslint && { globals: eslint.raw.globals }),
29-
...(summarizedResults.extends?.length !== 0 && { extends: summarizedResults.extends }),
30+
...(summarizedResults.extends && { extends: summarizedResults.extends }),
3031
parser: "@typescript-eslint/parser",
3132
parserOptions: {
3233
project: "tsconfig.json",
@@ -37,7 +38,7 @@ export const writeConversionResults = async (
3738
// ...trimESLintRules(eslint?.full.rules, summarizedResults.extensionRules),
3839
...formatConvertedRules(summarizedResults, tslint.full),
3940
},
40-
};
41+
});
4142

4243
return await dependencies.fileSystem.writeFile(outputPath, formatOutput(outputPath, output));
4344
};

src/utils.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isDefined, isError, uniqueFromSources, separateErrors } from "./utils";
1+
import { isDefined, isError, uniqueFromSources, separateErrors, removeEmptyMembers } from "./utils";
22

33
describe("isDefined", () => {
44
it("returns true when the item is defined", () => {
@@ -48,6 +48,41 @@ describe("isError", () => {
4848
});
4949
});
5050

51+
describe("removeEmptyMembers", () => {
52+
it("remove an object member when it is empty", () => {
53+
// Arrange
54+
const items = { kept: { value: true }, removed: {} };
55+
56+
// Act
57+
const result = removeEmptyMembers(items);
58+
59+
// Assert
60+
expect(result).toEqual({ kept: { value: true } });
61+
});
62+
63+
it("removes an array member when it is empty", () => {
64+
// Arrange
65+
const items = { kept: [true], removed: [] };
66+
67+
// Act
68+
const result = removeEmptyMembers(items);
69+
70+
// Assert
71+
expect(result).toEqual({ kept: [true] });
72+
});
73+
74+
it("keeps a member when it isn't an array or object", () => {
75+
// Arrange
76+
const items = { kept: true };
77+
78+
// Act
79+
const result = removeEmptyMembers(items);
80+
81+
// Assert
82+
expect(result).toEqual({ kept: true });
83+
});
84+
});
85+
5186
describe("separateErrors", () => {
5287
it("splits the input array into errors and items", () => {
5388
// Arrange

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ export const isError = <Item>(item: Item | Error): item is Error => item instanc
44

55
export const isTruthy = <Item>(item: Item | false | undefined | null | 0): item is Item => !!item;
66

7+
export const removeEmptyMembers = <Item>(items: Item): Item => {
8+
const result: any = {};
9+
10+
for (const [key, value] of Object.entries(items)) {
11+
if (
12+
!(value instanceof Array && value.length === 0) &&
13+
!(value instanceof Object && Object.keys(value).length === 0)
14+
) {
15+
result[key] = value;
16+
}
17+
}
18+
19+
return result;
20+
};
21+
722
export const separateErrors = <Item>(mixed: (Error | Item)[]): [Error[], Item[]] => {
823
const errors: Error[] = [];
924
const items: Item[] = [];

0 commit comments

Comments
 (0)