Skip to content

Commit ac7ef5e

Browse files
Emily GiurleoJosh Goldberg
authored andcommitted
Print Rule Notices (#87)
* initial attempt at logging notices * got one passing test * added notices to test multiple rules * small code change * clean up log statements in reportRuleConversions * Indent individual rule notices Co-Authored-By: Josh Goldberg <[email protected]> * just merge notices arrays and fix test * fix tests
1 parent e8f7115 commit ac7ef5e

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

src/reporting/reportConversionResults.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EOL } from "os";
12
import { ESLintRuleOptions } from "../rules/types";
23
import { reportConversionResults } from "./reportConversionResults";
34
import { createStubLogger, expectEqualWrites } from "../adapters/logger.stubs";
@@ -13,6 +14,7 @@ describe("reportConversionResults", () => {
1314
[
1415
"tslint-rule-one",
1516
{
17+
notices: ["1", "2"],
1618
ruleArguments: ["a", "b"],
1719
ruleName: "tslint-rule-one",
1820
ruleSeverity: "error",
@@ -27,7 +29,14 @@ describe("reportConversionResults", () => {
2729
reportConversionResults({ logger }, conversionResults);
2830

2931
// Assert
30-
expectEqualWrites(logger.stdout.write, "✨ 1 rule replaced with its ESLint equivalent. ✨");
32+
expectEqualWrites(
33+
logger.stdout.write,
34+
`✨ 1 rule replaced with its ESLint equivalent. ✨${EOL}` +
35+
`📢 1 ESLint rule behaves differently from their TSLint counterparts: 📢${EOL}` +
36+
`* tslint-rule-one:${EOL}` +
37+
` - 1${EOL}` +
38+
` - 2${EOL}`,
39+
);
3140
});
3241

3342
it("logs successful conversions when there are multiple converted rules", () => {
@@ -37,6 +46,7 @@ describe("reportConversionResults", () => {
3746
[
3847
"tslint-rule-one",
3948
{
49+
notices: ["1", "2"],
4050
ruleArguments: ["a", "b"],
4151
ruleName: "tslint-rule-one",
4252
ruleSeverity: "error",
@@ -45,6 +55,7 @@ describe("reportConversionResults", () => {
4555
[
4656
"tslint-rule-two",
4757
{
58+
notices: ["3", "4"],
4859
ruleArguments: ["c", "d"],
4960
ruleName: "tslint-rule-two",
5061
ruleSeverity: "warn",
@@ -61,7 +72,14 @@ describe("reportConversionResults", () => {
6172
// Assert
6273
expectEqualWrites(
6374
logger.stdout.write,
64-
"✨ 2 rules replaced with their ESLint equivalents. ✨",
75+
`✨ 2 rules replaced with their ESLint equivalents. ✨${EOL}` +
76+
`📢 2 ESLint rules behave differently from their TSLint counterparts: 📢${EOL}` +
77+
`* tslint-rule-one:${EOL}` +
78+
` - 1${EOL}` +
79+
` - 2${EOL}` +
80+
`* tslint-rule-two:${EOL}` +
81+
` - 3${EOL}` +
82+
` - 4${EOL}`,
6583
);
6684
});
6785

src/reporting/reportConversionResults.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const reportConversionResults = (
1717
) => {
1818
if (ruleConversionResults.converted.size !== 0) {
1919
logSuccessfulConversions(ruleConversionResults.converted, dependencies.logger);
20+
logNotices(ruleConversionResults.converted, dependencies.logger);
2021
}
2122

2223
if (ruleConversionResults.failed.length !== 0) {
@@ -93,3 +94,31 @@ const logMissingPlugins = (plugins: Set<string>, logger: Logger) => {
9394
.join(""),
9495
);
9596
};
97+
98+
interface RuleWithNotices {
99+
notices: any[];
100+
ruleName: string;
101+
}
102+
103+
const logNotices = (converted: Map<string, ESLintRuleOptions>, logger: Logger) => {
104+
const rulesWithNotices = Array.from(converted.values()).filter(
105+
ruleOptions => ruleOptions.notices && ruleOptions.notices.length >= 1,
106+
) as RuleWithNotices[];
107+
108+
if (rulesWithNotices.length > 0) {
109+
logger.stdout.write(chalk.yellowBright(`📢 ${rulesWithNotices.length} ESLint`));
110+
logger.stdout.write(
111+
chalk.yellowBright(rulesWithNotices.length == 1 ? ` rule behaves` : ` rules behave`),
112+
);
113+
logger.stdout.write(
114+
chalk.yellowBright(` differently from their TSLint counterparts: 📢${EOL}`),
115+
);
116+
117+
rulesWithNotices.forEach(rule => {
118+
logger.stdout.write(chalk.yellow(`* ${rule.ruleName}:${EOL}`));
119+
rule.notices.forEach(notice => {
120+
logger.stdout.write(chalk.yellow(` - ${notice}${EOL}`));
121+
});
122+
});
123+
}
124+
};

src/rules/convertRules.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ describe("convertRules", () => {
176176
ruleArguments: mergedArguments,
177177
ruleName: "eslint-rule-a",
178178
ruleSeverity: "error",
179+
notices: [],
179180
},
180181
],
181182
]),

src/rules/convertRules.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ export const convertRules = (
6969
),
7070
);
7171
} else {
72+
const existingNotices = existingConversion.notices || [];
73+
const newNotices = newConversion.notices || [];
74+
7275
converted.set(changes.ruleName, {
7376
...existingConversion,
7477
ruleArguments: merger(
7578
existingConversion.ruleArguments,
7679
newConversion.ruleArguments,
7780
),
81+
notices: [...existingNotices, ...newNotices],
7882
});
7983
}
8084
}

src/rules/converter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export type ConversionResult = {
3737
* An ESLint rule equivalent to a previously enabled TSLint rule.
3838
*/
3939
export type ConvertedRuleChanges = {
40+
/**
41+
* Any notices associated with that ESLint rule.
42+
*/
43+
notices?: string[];
44+
4045
/**
4146
* Any arguments for that ESLint rule.
4247
*/

src/rules/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type TSLintRuleOptions = {
99
export type ESLintRuleSeverity = "warn" | "error" | "off";
1010

1111
export type ESLintRuleOptions = {
12+
notices?: any[];
1213
ruleArguments?: any[];
1314
ruleName: string;
1415
ruleSeverity: ESLintRuleSeverity;

0 commit comments

Comments
 (0)