Skip to content

Commit 893c9b3

Browse files
author
Josh Goldberg
authored
Lowered lib version to support Node 10 (#265)
* Lowered lib version to support Node 10 * Added tests, heh
1 parent abb17d2 commit 893c9b3

File tree

6 files changed

+63
-18
lines changed

6 files changed

+63
-18
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
export const withKeysSorted = (input: any) => {
2-
return Object.fromEntries(
3-
Object.keys(input)
4-
.sort((a, b) => a.localeCompare(b))
5-
.map(key => [key, input[key]]),
6-
);
2+
const output: Record<string, any> = {};
3+
const keys = Object.keys(input).sort((a, b) => a.localeCompare(b));
4+
5+
for (const key of keys) {
6+
output[key] = input[key];
7+
}
8+
9+
return output;
710
};

src/input/findESLintConfiguration.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Exec } from "../adapters/exec";
22
import { SansDependencies } from "../binding";
33
import { ESLintRuleSeverity } from "../rules/types";
44
import { TSLintToESLintSettings } from "../types";
5+
import { uniqueFromSources } from "../utils";
56
import { findRawConfiguration } from "./findRawConfiguration";
67
import { findReportedConfiguration } from "./findReportedConfiguration";
78
import { OriginalConfigurations } from "./findOriginalConfigurations";
@@ -60,9 +61,7 @@ export const findESLintConfiguration = async (
6061
return reportedConfiguration;
6162
}
6263

63-
const extensions = [rawConfiguration.extends, [reportedConfiguration.extends || []]].flat(
64-
Infinity,
65-
);
64+
const extensions = uniqueFromSources(rawConfiguration.extends, reportedConfiguration.extends);
6665

6766
return {
6867
full: {

src/input/findTSLintConfiguration.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { findRawConfiguration } from "./findRawConfiguration";
22
import { findReportedConfiguration } from "./findReportedConfiguration";
33
import { Exec } from "../adapters/exec";
44
import { SansDependencies } from "../binding";
5+
import { uniqueFromSources } from "../utils";
56
import { importer } from "./importer";
6-
import { isDefined } from "../utils";
77

88
export type TSLintConfiguration = {
99
extends?: string[];
@@ -44,13 +44,7 @@ export const findTSLintConfiguration = async (
4444
return rawConfiguration;
4545
}
4646

47-
const extensions = Array.from(
48-
new Set(
49-
[[rawConfiguration.extends], [reportedConfiguration.extends]]
50-
.flat(Infinity)
51-
.filter(isDefined),
52-
),
53-
);
47+
const extensions = uniqueFromSources(rawConfiguration.extends, reportedConfiguration.extends);
5448

5549
const rules = {
5650
...rawConfiguration.rules,

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 } from "./utils";
1+
import { isDefined, isError, uniqueFromSources } from "./utils";
22

33
describe("isDefined", () => {
44
it("returns true when the item is defined", () => {
@@ -47,3 +47,38 @@ describe("isError", () => {
4747
expect(result).toBe(false);
4848
});
4949
});
50+
51+
describe("uniqueFromSources", () => {
52+
it("returns unique items when multiple are given", () => {
53+
// Arange
54+
const sources = ["a", "b", "b", "c"];
55+
56+
// Act
57+
const result = uniqueFromSources(...sources);
58+
59+
// Assert
60+
expect(result).toEqual(["a", "b", "c"]);
61+
});
62+
63+
it("returns items from a nested array", () => {
64+
// Arange
65+
const sources = ["a", ["b"], "c"];
66+
67+
// Act
68+
const result = uniqueFromSources(...sources);
69+
70+
// Assert
71+
expect(result).toEqual(["a", "b", "c"]);
72+
});
73+
74+
it("filters out undefined inputs", () => {
75+
// Arange
76+
const sources = ["a", "b", "c", undefined];
77+
78+
// Act
79+
const result = uniqueFromSources(...sources);
80+
81+
// Assert
82+
expect(result).toEqual(["a", "b", "c"]);
83+
});
84+
});

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ export type RemoveErrors<Items> = {
77
};
88

99
export type PromiseValue<T> = T extends Promise<infer R> ? R : never;
10+
11+
export const uniqueFromSources = <T>(...sources: (T | T[] | undefined)[]) => {
12+
const items: T[] = [];
13+
14+
for (const source of sources) {
15+
if (source instanceof Array) {
16+
items.push(...source.filter(isDefined));
17+
} else if (source !== undefined) {
18+
items.push(source);
19+
}
20+
}
21+
22+
return Array.from(new Set(items));
23+
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"declaration": true,
55
"esModuleInterop": true,
66
"incremental": true,
7-
"lib": ["esnext"],
7+
"lib": ["es2017"],
88
"module": "commonjs",
99
"noFallthroughCasesInSwitch": true,
1010
"noImplicitAny": true,

0 commit comments

Comments
 (0)