Skip to content

Print Rule Notices #87

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 12 commits into from
Jul 20, 2019
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
22 changes: 20 additions & 2 deletions src/reporting/reportConversionResults.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EOL } from "os";
import { ESLintRuleOptions } from "../rules/types";
import { reportConversionResults } from "./reportConversionResults";
import { createStubLogger, expectEqualWrites } from "../adapters/logger.stubs";
Expand All @@ -13,6 +14,7 @@ describe("reportConversionResults", () => {
[
"tslint-rule-one",
{
notices: ["1", "2"],
ruleArguments: ["a", "b"],
ruleName: "tslint-rule-one",
ruleSeverity: "error",
Expand All @@ -27,7 +29,14 @@ describe("reportConversionResults", () => {
reportConversionResults({ logger }, conversionResults);

// Assert
expectEqualWrites(logger.stdout.write, "✨ 1 rule replaced with its ESLint equivalent. ✨");
expectEqualWrites(
logger.stdout.write,
`✨ 1 rule replaced with its ESLint equivalent. ✨${EOL}` +
`📢 1 ESLint rule behaves differently from their TSLint counterparts: 📢${EOL}` +
`* tslint-rule-one:${EOL}` +
` - 1${EOL}` +
` - 2${EOL}`,
);
});

it("logs successful conversions when there are multiple converted rules", () => {
Expand All @@ -37,6 +46,7 @@ describe("reportConversionResults", () => {
[
"tslint-rule-one",
{
notices: ["1", "2"],
ruleArguments: ["a", "b"],
ruleName: "tslint-rule-one",
ruleSeverity: "error",
Expand All @@ -45,6 +55,7 @@ describe("reportConversionResults", () => {
[
"tslint-rule-two",
{
notices: ["3", "4"],
ruleArguments: ["c", "d"],
ruleName: "tslint-rule-two",
ruleSeverity: "warn",
Expand All @@ -61,7 +72,14 @@ describe("reportConversionResults", () => {
// Assert
expectEqualWrites(
logger.stdout.write,
"✨ 2 rules replaced with their ESLint equivalents. ✨",
`✨ 2 rules replaced with their ESLint equivalents. ✨${EOL}` +
`📢 2 ESLint rules behave differently from their TSLint counterparts: 📢${EOL}` +
`* tslint-rule-one:${EOL}` +
` - 1${EOL}` +
` - 2${EOL}` +
`* tslint-rule-two:${EOL}` +
` - 3${EOL}` +
` - 4${EOL}`,
);
});

Expand Down
29 changes: 29 additions & 0 deletions src/reporting/reportConversionResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const reportConversionResults = (
) => {
if (ruleConversionResults.converted.size !== 0) {
logSuccessfulConversions(ruleConversionResults.converted, dependencies.logger);
logNotices(ruleConversionResults.converted, dependencies.logger);
}

if (ruleConversionResults.failed.length !== 0) {
Expand Down Expand Up @@ -93,3 +94,31 @@ const logMissingPlugins = (plugins: Set<string>, logger: Logger) => {
.join(""),
);
};

interface RuleWithNotices {
notices: any[];
ruleName: string;
}

const logNotices = (converted: Map<string, ESLintRuleOptions>, logger: Logger) => {
const rulesWithNotices = Array.from(converted.values()).filter(
ruleOptions => ruleOptions.notices && ruleOptions.notices.length >= 1,
) as RuleWithNotices[];

if (rulesWithNotices.length > 0) {
logger.stdout.write(chalk.yellowBright(`📢 ${rulesWithNotices.length} ESLint`));
logger.stdout.write(
chalk.yellowBright(rulesWithNotices.length == 1 ? ` rule behaves` : ` rules behave`),
);
logger.stdout.write(
chalk.yellowBright(` differently from their TSLint counterparts: 📢${EOL}`),
);

rulesWithNotices.forEach(rule => {
logger.stdout.write(chalk.yellow(`* ${rule.ruleName}:${EOL}`));
rule.notices.forEach(notice => {
logger.stdout.write(chalk.yellow(` - ${notice}${EOL}`));
});
});
}
};
1 change: 1 addition & 0 deletions src/rules/convertRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe("convertRules", () => {
ruleArguments: mergedArguments,
ruleName: "eslint-rule-a",
ruleSeverity: "error",
notices: [],
},
],
]),
Expand Down
4 changes: 4 additions & 0 deletions src/rules/convertRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ 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],
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/rules/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export type ConversionResult = {
* An ESLint rule equivalent to a previously enabled TSLint rule.
*/
export type ConvertedRuleChanges = {
/**
* Any notices associated with that ESLint rule.
*/
notices?: string[];

/**
* Any arguments for that ESLint rule.
*/
Expand Down
1 change: 1 addition & 0 deletions src/rules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TSLintRuleOptions = {
export type ESLintRuleSeverity = "warn" | "error" | "off";

export type ESLintRuleOptions = {
notices?: any[];
ruleArguments?: any[];
ruleName: string;
ruleSeverity: ESLintRuleSeverity;
Expand Down