Skip to content

Commit fc527b0

Browse files
author
Josh Goldberg
authored
Fixed crash when ESLint rule has multiple rule arguments (#586)
1 parent 31c3a18 commit fc527b0

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

src/creation/summarization/normalizeESLintRules.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ describe("normalizeESLintRules", () => {
1515
expect(result).toEqual(new Map());
1616
});
1717

18+
it("converts a rule when given as a severity level", () => {
19+
// Arrange
20+
const userRules: ESLintConfigurationRules = {
21+
[ruleName]: "error",
22+
};
23+
24+
// Act
25+
const result = normalizeESLintRules(userRules);
26+
27+
// Assert
28+
expect(result).toEqual(
29+
new Map([
30+
[
31+
ruleName,
32+
{
33+
ruleArguments: [{}],
34+
ruleName: "rule-a",
35+
ruleSeverity: "error",
36+
},
37+
],
38+
]),
39+
);
40+
});
41+
1842
it("converts a rule when given as an array", () => {
1943
// Arrange
2044
const userRules: ESLintConfigurationRules = {
@@ -30,7 +54,7 @@ describe("normalizeESLintRules", () => {
3054
[
3155
ruleName,
3256
{
33-
ruleArguments: {},
57+
ruleArguments: [{}],
3458
ruleName: "rule-a",
3559
ruleSeverity: "error",
3660
},
@@ -39,10 +63,10 @@ describe("normalizeESLintRules", () => {
3963
);
4064
});
4165

42-
it("converts a rule when given as a severity level", () => {
66+
it("converts a rule when given as an array with multiple arguments", () => {
4367
// Arrange
4468
const userRules: ESLintConfigurationRules = {
45-
[ruleName]: "error",
69+
[ruleName]: ["error", 4, { value: true }],
4670
};
4771

4872
// Act
@@ -54,7 +78,7 @@ describe("normalizeESLintRules", () => {
5478
[
5579
ruleName,
5680
{
57-
ruleArguments: {},
81+
ruleArguments: [4, { value: true }],
5882
ruleName: "rule-a",
5983
ruleSeverity: "error",
6084
},
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { ESLintConfigurationRules } from "../../input/findESLintConfiguration";
2-
import { ESLintRuleOptions } from "../../rules/types";
1+
import {
2+
ESLintConfigurationRules,
3+
ESLintConfigurationRuleValue,
4+
} from "../../input/findESLintConfiguration";
5+
import { ESLintRuleOptions, RawESLintRuleSeverity } from "../../rules/types";
36
import { normalizeRawESLintRuleSeverity } from "../pruning/normalizeRawESLintRuleSeverity";
47

58
/**
@@ -8,13 +11,20 @@ import { normalizeRawESLintRuleSeverity } from "../pruning/normalizeRawESLintRul
811
export const normalizeESLintRules = (userRules: ESLintConfigurationRules | undefined) => {
912
const output: Map<string, ESLintRuleOptions> = new Map();
1013

11-
for (const [ruleName, configuration] of Object.entries(userRules ?? {})) {
12-
const [rawRuleSeverity, ruleArguments] =
13-
configuration instanceof Array ? configuration : [configuration, {}];
14+
for (const [ruleName, rawRuleValue] of Object.entries(userRules ?? {})) {
15+
const [rawRuleSeverity, ruleArguments] = parseRawRuleValue(rawRuleValue);
1416
const ruleSeverity = normalizeRawESLintRuleSeverity(rawRuleSeverity);
1517

1618
output.set(ruleName, { ruleArguments, ruleName, ruleSeverity });
1719
}
1820

1921
return output;
2022
};
23+
24+
const parseRawRuleValue = (
25+
configuration: ESLintConfigurationRuleValue,
26+
): [RawESLintRuleSeverity, any[]] => {
27+
return configuration instanceof Array
28+
? [configuration[0], configuration.slice(1)]
29+
: [configuration, [{}]];
30+
};

src/creation/writeConversionResults.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ export const writeConversionResults = async (
3434
sourceType: "module",
3535
},
3636
plugins,
37-
rules: {
38-
// ...trimESLintRules(eslint?.full.rules, summarizedResults.extensionRules),
39-
...formatConvertedRules(summarizedResults, tslint.full),
40-
},
37+
rules: formatConvertedRules(summarizedResults, tslint.full),
4138
});
4239

4340
return await dependencies.fileSystem.writeFile(outputPath, formatOutput(outputPath, output));

src/input/findESLintConfiguration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export type ESLintConfigurationRules = {
2121

2222
export type ESLintConfigurationRuleValue =
2323
| RawESLintRuleSeverity
24-
| [RawESLintRuleSeverity]
25-
| [RawESLintRuleSeverity, any];
24+
| [RawESLintRuleSeverity, ...any[]];
2625

2726
const defaultESLintConfiguration = {
2827
env: {},

0 commit comments

Comments
 (0)