Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions src/rules/convertRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe("convertRules", () => {
{
ruleName: "eslint-rule-a",
ruleSeverity: "error",
notices: [],
},
],
]),
Expand Down Expand Up @@ -183,6 +184,51 @@ describe("convertRules", () => {
);
});

it("merges and deduplicates rule notices", () => {
// Arrange
const tslintRule: TSLintRuleOptions = {
ruleArguments: [],
ruleName: "tslint-rule-a",
ruleSeverity: "error",
};
const conversionResult = {
rules: [
{
ruleName: "eslint-rule-a",
notices: ["notice-1", "notice-2"],
},
{
ruleName: "eslint-rule-a",
notices: ["notice-1"],
},
],
};
const mergedArguments = [{ merged: true }];
const converters = new Map([[tslintRule.ruleName, () => conversionResult]]);
const mergers = new Map([[conversionResult.rules[0].ruleName, () => mergedArguments]]);

// Act
const { converted } = convertRules(
{ converters, mergers },
{ [tslintRule.ruleName]: tslintRule },
);

// Assert
expect(converted).toEqual(
new Map([
[
"eslint-rule-a",
{
ruleArguments: mergedArguments,
ruleName: "eslint-rule-a",
ruleSeverity: "error",
notices: ["notice-1", "notice-2"],
},
],
]),
);
});

it("marks a new plugin when a conversion has a new plugin", () => {
// Arrange
const tslintRule: TSLintRuleOptions = {
Expand Down
11 changes: 7 additions & 4 deletions src/rules/convertRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ export const convertRules = (

for (const changes of conversion.rules) {
const existingConversion = converted.get(changes.ruleName);

const newConversion = {
...changes,
notices: changes.notices || [],
ruleSeverity: convertTSLintRuleSeverity(tslintRule.ruleSeverity),
};

if (existingConversion === undefined) {
converted.set(changes.ruleName, newConversion);
continue;
} else {
existingConversion.notices = existingConversion.notices || [];
}

const merger = dependencies.mergers.get(changes.ruleName);
Expand All @@ -69,16 +73,15 @@ export const convertRules = (
),
);
} else {
const existingNotices = existingConversion.notices || [];
const newNotices = newConversion.notices || [];

converted.set(changes.ruleName, {
...existingConversion,
ruleArguments: merger(
existingConversion.ruleArguments,
newConversion.ruleArguments,
),
notices: [...existingNotices, ...newNotices],
notices: Array.from(
new Set([...existingConversion.notices, ...newConversion.notices]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this is better than it was previously (setting a default value for notices earlier on vs in the existingNotices and newNotices variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@egiurleo unfortunately no, if newNotices is undefined, this will throw an error:

Trying out in a Node CLI:

> [...undefined, ...[]]
Thrown:
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

...which seems like a missing unit test!

),
});
}
}
Expand Down