Skip to content

Fixed crash when ESLint rule has multiple rule arguments #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/creation/summarization/normalizeESLintRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ describe("normalizeESLintRules", () => {
expect(result).toEqual(new Map());
});

it("converts a rule when given as a severity level", () => {
// Arrange
const userRules: ESLintConfigurationRules = {
[ruleName]: "error",
};

// Act
const result = normalizeESLintRules(userRules);

// Assert
expect(result).toEqual(
new Map([
[
ruleName,
{
ruleArguments: [{}],
ruleName: "rule-a",
ruleSeverity: "error",
},
],
]),
);
});

it("converts a rule when given as an array", () => {
// Arrange
const userRules: ESLintConfigurationRules = {
Expand All @@ -30,7 +54,7 @@ describe("normalizeESLintRules", () => {
[
ruleName,
{
ruleArguments: {},
ruleArguments: [{}],
ruleName: "rule-a",
ruleSeverity: "error",
},
Expand All @@ -39,10 +63,10 @@ describe("normalizeESLintRules", () => {
);
});

it("converts a rule when given as a severity level", () => {
it("converts a rule when given as an array with multiple arguments", () => {
// Arrange
const userRules: ESLintConfigurationRules = {
[ruleName]: "error",
[ruleName]: ["error", 4, { value: true }],
};

// Act
Expand All @@ -54,7 +78,7 @@ describe("normalizeESLintRules", () => {
[
ruleName,
{
ruleArguments: {},
ruleArguments: [4, { value: true }],
ruleName: "rule-a",
ruleSeverity: "error",
},
Expand Down
20 changes: 15 additions & 5 deletions src/creation/summarization/normalizeESLintRules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ESLintConfigurationRules } from "../../input/findESLintConfiguration";
import { ESLintRuleOptions } from "../../rules/types";
import {
ESLintConfigurationRules,
ESLintConfigurationRuleValue,
} from "../../input/findESLintConfiguration";
import { ESLintRuleOptions, RawESLintRuleSeverity } from "../../rules/types";
import { normalizeRawESLintRuleSeverity } from "../pruning/normalizeRawESLintRuleSeverity";

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

for (const [ruleName, configuration] of Object.entries(userRules ?? {})) {
const [rawRuleSeverity, ruleArguments] =
configuration instanceof Array ? configuration : [configuration, {}];
for (const [ruleName, rawRuleValue] of Object.entries(userRules ?? {})) {
const [rawRuleSeverity, ruleArguments] = parseRawRuleValue(rawRuleValue);
const ruleSeverity = normalizeRawESLintRuleSeverity(rawRuleSeverity);

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

return output;
};

const parseRawRuleValue = (
configuration: ESLintConfigurationRuleValue,
): [RawESLintRuleSeverity, any[]] => {
return configuration instanceof Array
? [configuration[0], configuration.slice(1)]
: [configuration, [{}]];
};
5 changes: 1 addition & 4 deletions src/creation/writeConversionResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ export const writeConversionResults = async (
sourceType: "module",
},
plugins,
rules: {
// ...trimESLintRules(eslint?.full.rules, summarizedResults.extensionRules),
...formatConvertedRules(summarizedResults, tslint.full),
},
rules: formatConvertedRules(summarizedResults, tslint.full),
});

return await dependencies.fileSystem.writeFile(outputPath, formatOutput(outputPath, output));
Expand Down
3 changes: 1 addition & 2 deletions src/input/findESLintConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export type ESLintConfigurationRules = {

export type ESLintConfigurationRuleValue =
| RawESLintRuleSeverity
| [RawESLintRuleSeverity]
| [RawESLintRuleSeverity, any];
| [RawESLintRuleSeverity, ...any[]];

const defaultESLintConfiguration = {
env: {},
Expand Down