Skip to content

Commit

Permalink
Stopped writing empty root members in output configurations (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg committed May 9, 2020
1 parent daee715 commit b165a6b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
6 changes: 0 additions & 6 deletions src/creation/writeConversionResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ describe("writeConversionResults", () => {
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {},
}),
);
});
Expand Down Expand Up @@ -137,8 +136,6 @@ describe("writeConversionResults", () => {
es6: true,
node: true,
},
extends: [],
rules: {},
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
Expand Down Expand Up @@ -182,7 +179,6 @@ describe("writeConversionResults", () => {
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {},
}),
);
});
Expand Down Expand Up @@ -224,8 +220,6 @@ describe("writeConversionResults", () => {
es6: true,
node: true,
},
extends: [],
rules: {},
globals: {
Promise: true,
},
Expand Down
7 changes: 4 additions & 3 deletions src/creation/writeConversionResults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FileSystem } from "../adapters/fileSystem";
import { AllOriginalConfigurations } from "../input/findOriginalConfigurations";
import { removeEmptyMembers } from "../utils";
import { createEnv } from "./eslint/createEnv";
import { formatConvertedRules } from "./formatConvertedRules";
import { formatOutput } from "./formatting/formatOutput";
Expand All @@ -22,11 +23,11 @@ export const writeConversionResults = async (
plugins.push("@typescript-eslint/tslint");
}

const output = {
const output = removeEmptyMembers({
...eslint?.full,
env: createEnv(originalConfigurations),
...(eslint && { globals: eslint.raw.globals }),
...(summarizedResults.extends?.length !== 0 && { extends: summarizedResults.extends }),
...(summarizedResults.extends && { extends: summarizedResults.extends }),
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
Expand All @@ -37,7 +38,7 @@ export const writeConversionResults = async (
// ...trimESLintRules(eslint?.full.rules, summarizedResults.extensionRules),
...formatConvertedRules(summarizedResults, tslint.full),
},
};
});

return await dependencies.fileSystem.writeFile(outputPath, formatOutput(outputPath, output));
};
37 changes: 36 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDefined, isError, uniqueFromSources, separateErrors } from "./utils";
import { isDefined, isError, uniqueFromSources, separateErrors, removeEmptyMembers } from "./utils";

describe("isDefined", () => {
it("returns true when the item is defined", () => {
Expand Down Expand Up @@ -48,6 +48,41 @@ describe("isError", () => {
});
});

describe("removeEmptyMembers", () => {
it("remove an object member when it is empty", () => {
// Arrange
const items = { kept: { value: true }, removed: {} };

// Act
const result = removeEmptyMembers(items);

// Assert
expect(result).toEqual({ kept: { value: true } });
});

it("removes an array member when it is empty", () => {
// Arrange
const items = { kept: [true], removed: [] };

// Act
const result = removeEmptyMembers(items);

// Assert
expect(result).toEqual({ kept: [true] });
});

it("keeps a member when it isn't an array or object", () => {
// Arrange
const items = { kept: true };

// Act
const result = removeEmptyMembers(items);

// Assert
expect(result).toEqual({ kept: true });
});
});

describe("separateErrors", () => {
it("splits the input array into errors and items", () => {
// Arrange
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ export const isError = <Item>(item: Item | Error): item is Error => item instanc

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

export const removeEmptyMembers = <Item>(items: Item): Item => {
const result: any = {};

for (const [key, value] of Object.entries(items)) {
if (
!(value instanceof Array && value.length === 0) &&
!(value instanceof Object && Object.keys(value).length === 0)
) {
result[key] = value;
}
}

return result;
};

export const separateErrors = <Item>(mixed: (Error | Item)[]): [Error[], Item[]] => {
const errors: Error[] = [];
const items: Item[] = [];
Expand Down

0 comments on commit b165a6b

Please sign in to comment.