From b7a0aae3c0611d369f20cebf6a7dfda0d8ea2825 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Tue, 15 Oct 2019 21:34:19 -0500 Subject: [PATCH 1/4] Add comment-format converter --- src/rules/converters.ts | 2 + src/rules/converters/comment-format.ts | 56 ++++++++ .../converters/tests/comment-format.test.ts | 133 ++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 src/rules/converters/comment-format.ts create mode 100644 src/rules/converters/tests/comment-format.test.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index d21e68bea..723ebb317 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -9,6 +9,7 @@ import { convertBanTypes } from "./converters/ban-types"; import { convertBinaryExpressionOperandOrder } from "./converters/binary-expression-operand-order"; import { convertCallableTypes } from "./converters/callable-types"; import { convertClassName } from "./converters/class-name"; +import { convertCommentFormat } from "./converters/comment-format"; import { convertCurly } from "./converters/curly"; import { convertCyclomaticComplexity } from "./converters/cyclomatic-complexity"; import { convertEofline } from "./converters/eofline"; @@ -139,6 +140,7 @@ export const converters = new Map([ ["binary-expression-operand-order", convertBinaryExpressionOperandOrder], ["callable-types", convertCallableTypes], ["class-name", convertClassName], + ["comment-format", convertCommentFormat], ["curly", convertCurly], ["cyclomatic-complexity", convertCyclomaticComplexity], ["eofline", convertEofline], diff --git a/src/rules/converters/comment-format.ts b/src/rules/converters/comment-format.ts new file mode 100644 index 000000000..6c1d4532d --- /dev/null +++ b/src/rules/converters/comment-format.ts @@ -0,0 +1,56 @@ +import { RuleConverter } from "../converter"; + +export const CheckTrailingLowercaseMessage: string = + "Only first trailing comment can be configured."; +export const CapitalizedIgnoreMessage: string = + "Only accepts a single string pattern to be ignored."; + +export const convertCommentFormat: RuleConverter = tslintRule => { + const capitalizedRuleArguments: string[] = []; + const spaceCommentRuleArguments: string[] = []; + const capitalizedNotices: string[] = []; + + const hasCheckSpace = tslintRule.ruleArguments.includes("check-space"); + const hasCheckLowercase = tslintRule.ruleArguments.includes("check-lowercase"); + const hasCheckUppercase = tslintRule.ruleArguments.includes("check-uppercase"); + const hasCheckTrailingLowercase = tslintRule.ruleArguments.includes("allow-trailing-lowercase"); + + if (!hasCheckSpace) { + spaceCommentRuleArguments.push("never"); + } + + if (hasCheckUppercase) { + capitalizedRuleArguments.push("always"); + } else if (hasCheckLowercase) { + capitalizedRuleArguments.push("never"); + } + + if (hasCheckTrailingLowercase) { + capitalizedNotices.push(CheckTrailingLowercaseMessage); + } + + if (typeof tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1] == "object") { + const objectArgument: Object = + tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1]; + if ( + objectArgument.hasOwnProperty("ignore-words") || + objectArgument.hasOwnProperty("ignore-pattern") + ) { + capitalizedNotices.push(CapitalizedIgnoreMessage); + } + } + + return { + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: capitalizedRuleArguments, + notices: capitalizedNotices, + }, + { + ruleName: "spaced-comment", + ruleArguments: spaceCommentRuleArguments, + }, + ], + }; +}; diff --git a/src/rules/converters/tests/comment-format.test.ts b/src/rules/converters/tests/comment-format.test.ts new file mode 100644 index 000000000..ee08f9b34 --- /dev/null +++ b/src/rules/converters/tests/comment-format.test.ts @@ -0,0 +1,133 @@ +import { + convertCommentFormat, + CheckTrailingLowercaseMessage, + CapitalizedIgnoreMessage, +} from "../comment-format"; + +describe(convertCommentFormat, () => { + test("conversion without arguments", () => { + const result = convertCommentFormat({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: [], + notices: [], + }, + { + ruleName: "spaced-comment", + ruleArguments: ["never"], + }, + ], + }); + }); + + test("conversion with check-space argument", () => { + const result = convertCommentFormat({ + ruleArguments: ["check-space"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: [], + notices: [], + }, + { + ruleName: "spaced-comment", + ruleArguments: [], + }, + ], + }); + }); + + test("conversion with check-lowercase arguments", () => { + const result = convertCommentFormat({ + ruleArguments: ["check-lowercase"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: ["never"], + notices: [], + }, + { + ruleName: "spaced-comment", + ruleArguments: ["never"], + }, + ], + }); + }); + + test("conversion with ignore-pattern argument", () => { + const result = convertCommentFormat({ + ruleArguments: [{ "ignore-pattern": "STD\\w{2,3}\\b" }], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: [], + notices: [CapitalizedIgnoreMessage], + }, + { + ruleName: "spaced-comment", + ruleArguments: ["never"], + }, + ], + }); + }); + + test("conversion with ignore-words argument", () => { + const result = convertCommentFormat({ + ruleArguments: [{ "ignore-words": ["TODO", "HACK"] }], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: [], + notices: [CapitalizedIgnoreMessage], + }, + { + ruleName: "spaced-comment", + ruleArguments: ["never"], + }, + ], + }); + }); + + test("conversion with all arguments", () => { + const result = convertCommentFormat({ + ruleArguments: [ + "check-space", + "check-lowercase", + "check-uppercase", + "allow-trailing-lowercase", + { "ignore-words": ["TODO", "HACK"], "ignore-pattern": "STD\\w{2,3}\\b" }, + ], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: ["always"], + notices: [CheckTrailingLowercaseMessage, CapitalizedIgnoreMessage], + }, + { + ruleName: "spaced-comment", + ruleArguments: [], + }, + ], + }); + }); +}); From c30ab050fa1a4b6e444cc9be58618fe5c74f3d51 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sat, 19 Oct 2019 18:44:36 -0500 Subject: [PATCH 2/4] Fix lint error and provide type to comment format options --- src/rules/converters/comment-format.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/rules/converters/comment-format.ts b/src/rules/converters/comment-format.ts index 6c1d4532d..2a55c7122 100644 --- a/src/rules/converters/comment-format.ts +++ b/src/rules/converters/comment-format.ts @@ -1,9 +1,12 @@ import { RuleConverter } from "../converter"; -export const CheckTrailingLowercaseMessage: string = - "Only first trailing comment can be configured."; -export const CapitalizedIgnoreMessage: string = - "Only accepts a single string pattern to be ignored."; +type CommentFormatOptions = { + "ignore-words": string[]; + "ignore-pattern": string; +}; + +export const CheckTrailingLowercaseMessage = "Only first trailing comment can be configured."; +export const CapitalizedIgnoreMessage = "Only accepts a single string pattern to be ignored."; export const convertCommentFormat: RuleConverter = tslintRule => { const capitalizedRuleArguments: string[] = []; @@ -29,13 +32,10 @@ export const convertCommentFormat: RuleConverter = tslintRule => { capitalizedNotices.push(CheckTrailingLowercaseMessage); } - if (typeof tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1] == "object") { - const objectArgument: Object = + if (typeof tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1] === "object") { + const objectArgument: CommentFormatOptions = tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1]; - if ( - objectArgument.hasOwnProperty("ignore-words") || - objectArgument.hasOwnProperty("ignore-pattern") - ) { + if (objectArgument["ignore-words"] || objectArgument["ignore-pattern"]) { capitalizedNotices.push(CapitalizedIgnoreMessage); } } From 66c2028dc781a241b503b0ca6855aba252fa617f Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sun, 20 Oct 2019 11:00:51 -0500 Subject: [PATCH 3/4] Remove unnecessary check at comment-format converter --- src/rules/converters/comment-format.ts | 11 ++++------- src/rules/converters/tests/comment-format.test.ts | 8 ++------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/rules/converters/comment-format.ts b/src/rules/converters/comment-format.ts index 2a55c7122..faf8b14ed 100644 --- a/src/rules/converters/comment-format.ts +++ b/src/rules/converters/comment-format.ts @@ -5,7 +5,6 @@ type CommentFormatOptions = { "ignore-pattern": string; }; -export const CheckTrailingLowercaseMessage = "Only first trailing comment can be configured."; export const CapitalizedIgnoreMessage = "Only accepts a single string pattern to be ignored."; export const convertCommentFormat: RuleConverter = tslintRule => { @@ -16,7 +15,6 @@ export const convertCommentFormat: RuleConverter = tslintRule => { const hasCheckSpace = tslintRule.ruleArguments.includes("check-space"); const hasCheckLowercase = tslintRule.ruleArguments.includes("check-lowercase"); const hasCheckUppercase = tslintRule.ruleArguments.includes("check-uppercase"); - const hasCheckTrailingLowercase = tslintRule.ruleArguments.includes("allow-trailing-lowercase"); if (!hasCheckSpace) { spaceCommentRuleArguments.push("never"); @@ -28,14 +26,13 @@ export const convertCommentFormat: RuleConverter = tslintRule => { capitalizedRuleArguments.push("never"); } - if (hasCheckTrailingLowercase) { - capitalizedNotices.push(CheckTrailingLowercaseMessage); - } - if (typeof tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1] === "object") { const objectArgument: CommentFormatOptions = tslintRule.ruleArguments[tslintRule.ruleArguments.length - 1]; - if (objectArgument["ignore-words"] || objectArgument["ignore-pattern"]) { + if ( + (objectArgument["ignore-words"] && objectArgument["ignore-words"].length) || + objectArgument["ignore-pattern"] + ) { capitalizedNotices.push(CapitalizedIgnoreMessage); } } diff --git a/src/rules/converters/tests/comment-format.test.ts b/src/rules/converters/tests/comment-format.test.ts index ee08f9b34..490ac906c 100644 --- a/src/rules/converters/tests/comment-format.test.ts +++ b/src/rules/converters/tests/comment-format.test.ts @@ -1,8 +1,4 @@ -import { - convertCommentFormat, - CheckTrailingLowercaseMessage, - CapitalizedIgnoreMessage, -} from "../comment-format"; +import { convertCommentFormat, CapitalizedIgnoreMessage } from "../comment-format"; describe(convertCommentFormat, () => { test("conversion without arguments", () => { @@ -121,7 +117,7 @@ describe(convertCommentFormat, () => { { ruleName: "capitalized-comments", ruleArguments: ["always"], - notices: [CheckTrailingLowercaseMessage, CapitalizedIgnoreMessage], + notices: [CapitalizedIgnoreMessage], }, { ruleName: "spaced-comment", From c96c1c743fa27524ea4bccfb90f099431fea9223 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sun, 20 Oct 2019 17:04:51 -0500 Subject: [PATCH 4/4] Provide extra test to ignore-words on comment-format --- .../converters/tests/comment-format.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/rules/converters/tests/comment-format.test.ts b/src/rules/converters/tests/comment-format.test.ts index 490ac906c..539e5a675 100644 --- a/src/rules/converters/tests/comment-format.test.ts +++ b/src/rules/converters/tests/comment-format.test.ts @@ -81,6 +81,26 @@ describe(convertCommentFormat, () => { }); }); + test("conversion with empty ignore-words argument", () => { + const result = convertCommentFormat({ + ruleArguments: [{ "ignore-words": [] }], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "capitalized-comments", + ruleArguments: [], + notices: [], + }, + { + ruleName: "spaced-comment", + ruleArguments: ["never"], + }, + ], + }); + }); + test("conversion with ignore-words argument", () => { const result = convertCommentFormat({ ruleArguments: [{ "ignore-words": ["TODO", "HACK"] }],