From 583ed43a86faf14f60cc07bd1f169d717115fc7c Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sun, 6 Oct 2019 14:06:36 -0500 Subject: [PATCH 1/5] Implement variable-name converter --- src/rules/converters.ts | 3 +- .../converters/tests/variable-name.test.ts | 261 ++++++++++++++++++ src/rules/converters/variable-name.ts | 116 ++++++++ 3 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 src/rules/converters/tests/variable-name.test.ts create mode 100644 src/rules/converters/variable-name.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index 78b2b9e94..232665192 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -108,6 +108,7 @@ import { convertUnifiedSignatures } from "./converters/unified-signatures"; import { convertUnnecessaryBind } from "./converters/unnecessary-bind"; import { convertUnnecessaryConstructor } from "./converters/unnecessary-constructor"; import { convertUseIsnan } from "./converters/use-isnan"; +import { convertVariableName } from "./converters/variable-name"; import { convertQuotemark } from "./converters/quotemark"; import { convertTripleEquals } from "./converters/triple-equals"; @@ -227,6 +228,7 @@ export const converters = new Map([ ["unnecessary-bind", convertUnnecessaryBind], ["unnecessary-constructor", convertUnnecessaryConstructor], ["use-isnan", convertUseIsnan], + ["variable-name", convertVariableName], // These converters are all for rules that need more complex option conversions. // Some of them will likely need to have notices about changed lint behaviors... @@ -240,7 +242,6 @@ export const converters = new Map([ // ["no-shadowed-variable", convertNoShadowedVariable], // no-shadow // ["no-unused-expression", convertNoUnusedExpression], // no-unused-expressions // ["space-within-parens", convertSpaceWithinParens], // space-in-parens - // ["variable-name", convertVariableName], // a bunch of rules... // tslint-microsoft-contrib rules: // ["max-func-body-length", convertMaxFuncBodyLength], diff --git a/src/rules/converters/tests/variable-name.test.ts b/src/rules/converters/tests/variable-name.test.ts new file mode 100644 index 000000000..dd3ca2829 --- /dev/null +++ b/src/rules/converters/tests/variable-name.test.ts @@ -0,0 +1,261 @@ +import { convertVariableName } from "../variable-name"; + +describe(convertVariableName, () => { + test("conversion without arguments", () => { + const result = convertVariableName({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with require-const-for-all-caps argument", () => { + const result = convertVariableName({ + ruleArguments: ["require-const-for-all-caps"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-pascal-case argument", () => { + const result = convertVariableName({ + ruleArguments: ["allow-pascal-case"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-snake-case argument", () => { + const result = convertVariableName({ + ruleArguments: ["allow-snake-case"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-leading-underscore without check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["allow-leading-underscore"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-leading-underscore with check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["check-format", "allow-leading-underscore"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: ["off"], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + notices: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with all arguments", () => { + const result = convertVariableName({ + ruleArguments: [ + "check-format", + "allow-leading-underscore", + "allow-pascal-case", + "allow-snake-case", + "allow-trailing-underscore", + "require-const-for-all-caps", + "ban-keywords", + ], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + ruleArguments: [], + notices: [ + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', + "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: ["off"], + notices: [ + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [ + "any", + "Number", + "number", + "String", + "string", + "Boolean", + "boolean", + "Undefined", + "undefined", + ], + notices: [ + 'If "ban-keywords" was provided, ESLint has to disallows the use of certain TypeScript keywords by using "id-blacklist" rule.', + ], + }, + { + ruleName: "id-match", + }, + ], + }); + }); +}); diff --git a/src/rules/converters/variable-name.ts b/src/rules/converters/variable-name.ts new file mode 100644 index 000000000..8277cb977 --- /dev/null +++ b/src/rules/converters/variable-name.ts @@ -0,0 +1,116 @@ +import { RuleConverter } from "../converter"; + +export const convertVariableName: RuleConverter = tslintRule => { + const getCamelCaseRuleOptions = () => { + const camelCaseOptionNotice: string[] = []; + camelCaseOptionNotice.push( + "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + ); + + if ( + tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments.includes("require-const-for-all-caps") + ) { + camelCaseOptionNotice.push( + 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', + ); + } + + if ( + tslintRule.ruleArguments.length !== 0 && + (tslintRule.ruleArguments.includes("allow-pascal-case") || + tslintRule.ruleArguments.includes("allow-snake-case")) + ) { + camelCaseOptionNotice.push( + "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + ); + } + + return { + arguments: [], + notices: camelCaseOptionNotice, + }; + }; + + const getUnderscoreDangleRuleOptions = () => { + const underscoreDangleOptionArguments: string[] = []; + const underscoreDangleOptionNotice: string[] = []; + underscoreDangleOptionNotice.push( + 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + ); + + if ( + tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments.includes("check-format") && + (tslintRule.ruleArguments.includes("allow-leading-underscore") || + tslintRule.ruleArguments.includes("allow-trailing-underscore")) + ) { + underscoreDangleOptionArguments.push("off"); + underscoreDangleOptionNotice.push( + 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + ); + } + + return { + arguments: underscoreDangleOptionArguments, + notices: underscoreDangleOptionNotice, + }; + }; + + const getBlackListRuleOptions = () => { + const blackListOptionArguments: string[] = []; + const blackListOptionNotice: string[] = []; + + if ( + tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments.includes("ban-keywords") + ) { + blackListOptionArguments.push( + "any", + "Number", + "number", + "String", + "string", + "Boolean", + "boolean", + "Undefined", + "undefined", + ); + blackListOptionNotice.push( + 'If "ban-keywords" was provided, ESLint has to disallows the use of certain TypeScript keywords by using "id-blacklist" rule.', + ); + } + + return { + arguments: blackListOptionArguments, + notices: blackListOptionNotice, + }; + }; + + const camelCaseOptions = getCamelCaseRuleOptions(); + const underscoreDangleOptions = getUnderscoreDangleRuleOptions(); + const idblackListOptions = getBlackListRuleOptions(); + + return { + rules: [ + { + ruleName: "camelcase", + ruleArguments: camelCaseOptions.arguments, + notices: camelCaseOptions.notices, + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: underscoreDangleOptions.arguments, + notices: underscoreDangleOptions.notices, + }, + { + ruleName: "id-blacklist", + ruleArguments: idblackListOptions.arguments, + notices: idblackListOptions.notices, + }, + { + ruleName: "id-match", + }, + ], + }; +}; From 4b58c1b2271cc3a9fe0176a6e634ea9ad097c55e Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sun, 6 Oct 2019 17:48:22 -0500 Subject: [PATCH 2/5] Feedback changes --- .../converters/tests/variable-name.test.ts | 57 ++++--------- src/rules/converters/variable-name.ts | 83 ++++++++++--------- 2 files changed, 62 insertions(+), 78 deletions(-) diff --git a/src/rules/converters/tests/variable-name.test.ts b/src/rules/converters/tests/variable-name.test.ts index dd3ca2829..3f59ebd03 100644 --- a/src/rules/converters/tests/variable-name.test.ts +++ b/src/rules/converters/tests/variable-name.test.ts @@ -10,22 +10,20 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + "Leading and trailing underscores (_) in variable names will now be ignored.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: [], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -43,23 +41,21 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', + "Leading and trailing underscores (_) in variable names will now be ignored.", + "ESLint's camel-case will throw a warning if const name is not uppercase.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: [], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -77,23 +73,21 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + "Leading and trailing underscores (_) in variable names will now be ignored.", + "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: [], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -111,23 +105,21 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + "Leading and trailing underscores (_) in variable names will now be ignored.", + "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: [], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -145,22 +137,20 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", + "Leading and trailing underscores (_) in variable names will now be ignored.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: [], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -178,23 +168,18 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], - notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - ], + notices: ["Leading undescores in variable names will now be ignored."], }, { ruleName: "no-underscore-dangle", ruleArguments: ["off"], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', - 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + "Leading and trailing underscores (_) on identifiers will now be ignored.", ], }, { ruleName: "id-blacklist", ruleArguments: [], - notices: [], }, { ruleName: "id-match", @@ -220,19 +205,16 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - ruleArguments: [], notices: [ - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', - "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + "ESLint's camel-case will throw a warning if const name is not uppercase.", + "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", ], }, { ruleName: "no-underscore-dangle", ruleArguments: ["off"], notices: [ - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', - 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + "Leading and trailing underscores (_) on identifiers will now be ignored.", ], }, { @@ -248,9 +230,6 @@ describe(convertVariableName, () => { "Undefined", "undefined", ], - notices: [ - 'If "ban-keywords" was provided, ESLint has to disallows the use of certain TypeScript keywords by using "id-blacklist" rule.', - ], }, { ruleName: "id-match", diff --git a/src/rules/converters/variable-name.ts b/src/rules/converters/variable-name.ts index 8277cb977..8bc1d3c5e 100644 --- a/src/rules/converters/variable-name.ts +++ b/src/rules/converters/variable-name.ts @@ -3,31 +3,51 @@ import { RuleConverter } from "../converter"; export const convertVariableName: RuleConverter = tslintRule => { const getCamelCaseRuleOptions = () => { const camelCaseOptionNotice: string[] = []; - camelCaseOptionNotice.push( - "By default, this rule looks for any underscores (_) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name.", - ); + if (tslintRule.ruleArguments.includes("check-format")) { + if ( + !tslintRule.ruleArguments.includes("allow-leading-underscore") && + !tslintRule.ruleArguments.includes("allow-trailing-underscore") + ) { + camelCaseOptionNotice.push( + "Leading and trailing underscores (_) in variable names will now be ignored.", + ); + } else if ( + tslintRule.ruleArguments.includes("allow-leading-underscore") && + !tslintRule.ruleArguments.includes("allow-trailing-underscore") + ) { + camelCaseOptionNotice.push( + "Leading undescores in variable names will now be ignored.", + ); + } else if ( + !tslintRule.ruleArguments.includes("allow-leading-underscore") && + tslintRule.ruleArguments.includes("allow-trailing-underscore") + ) { + camelCaseOptionNotice.push( + "Trailing undescores in variable names will now be ignored.", + ); + } + } else { + camelCaseOptionNotice.push( + "Leading and trailing underscores (_) in variable names will now be ignored.", + ); + } - if ( - tslintRule.ruleArguments.length !== 0 && - tslintRule.ruleArguments.includes("require-const-for-all-caps") - ) { + if (tslintRule.ruleArguments.includes("require-const-for-all-caps")) { camelCaseOptionNotice.push( - 'The argument "require-const-for-all-caps" is not needed as ESlint will decide if a variable is a constant (all uppercase). If not, a warning will be thrown.', + "ESLint's camel-case will throw a warning if const name is not uppercase.", ); } if ( - tslintRule.ruleArguments.length !== 0 && - (tslintRule.ruleArguments.includes("allow-pascal-case") || - tslintRule.ruleArguments.includes("allow-snake-case")) + tslintRule.ruleArguments.includes("allow-pascal-case") || + tslintRule.ruleArguments.includes("allow-snake-case") ) { camelCaseOptionNotice.push( - "This rule does not allow pascal neither snake case to variable names. Those are reserved for class names and static methods.", + "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", ); } return { - arguments: [], notices: camelCaseOptionNotice, }; }; @@ -35,36 +55,32 @@ export const convertVariableName: RuleConverter = tslintRule => { const getUnderscoreDangleRuleOptions = () => { const underscoreDangleOptionArguments: string[] = []; const underscoreDangleOptionNotice: string[] = []; - underscoreDangleOptionNotice.push( - 'By default, "no-underscore-dangle" will disallows dangling underscores in identifiers.', - ); if ( - tslintRule.ruleArguments.length !== 0 && tslintRule.ruleArguments.includes("check-format") && (tslintRule.ruleArguments.includes("allow-leading-underscore") || tslintRule.ruleArguments.includes("allow-trailing-underscore")) ) { underscoreDangleOptionArguments.push("off"); underscoreDangleOptionNotice.push( - 'If either "allow-leading-underscore" or "allow-trailing-underscore" are provided, "no-underscore-dangle" will be turned off.', + "Leading and trailing underscores (_) on identifiers will now be ignored.", + ); + } else { + underscoreDangleOptionNotice.push( + "Leading or trailing underscores (_) on identifiers will now be forbidden.", ); } return { - arguments: underscoreDangleOptionArguments, + ruleArguments: underscoreDangleOptionArguments, notices: underscoreDangleOptionNotice, }; }; const getBlackListRuleOptions = () => { const blackListOptionArguments: string[] = []; - const blackListOptionNotice: string[] = []; - if ( - tslintRule.ruleArguments.length !== 0 && - tslintRule.ruleArguments.includes("ban-keywords") - ) { + if (tslintRule.ruleArguments.includes("ban-keywords")) { blackListOptionArguments.push( "any", "Number", @@ -76,37 +92,26 @@ export const convertVariableName: RuleConverter = tslintRule => { "Undefined", "undefined", ); - blackListOptionNotice.push( - 'If "ban-keywords" was provided, ESLint has to disallows the use of certain TypeScript keywords by using "id-blacklist" rule.', - ); } return { - arguments: blackListOptionArguments, - notices: blackListOptionNotice, + ruleArguments: blackListOptionArguments, }; }; - const camelCaseOptions = getCamelCaseRuleOptions(); - const underscoreDangleOptions = getUnderscoreDangleRuleOptions(); - const idblackListOptions = getBlackListRuleOptions(); - return { rules: [ { ruleName: "camelcase", - ruleArguments: camelCaseOptions.arguments, - notices: camelCaseOptions.notices, + ...getCamelCaseRuleOptions(), }, { ruleName: "no-underscore-dangle", - ruleArguments: underscoreDangleOptions.arguments, - notices: underscoreDangleOptions.notices, + ...getUnderscoreDangleRuleOptions(), }, { ruleName: "id-blacklist", - ruleArguments: idblackListOptions.arguments, - notices: idblackListOptions.notices, + ...getBlackListRuleOptions(), }, { ruleName: "id-match", From 14fa169f9eea81bb0448658b815807010e2cf736 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Sun, 6 Oct 2019 17:57:43 -0500 Subject: [PATCH 3/5] Add more test cases --- .../converters/tests/variable-name.test.ts | 126 +++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/src/rules/converters/tests/variable-name.test.ts b/src/rules/converters/tests/variable-name.test.ts index 3f59ebd03..1898f029a 100644 --- a/src/rules/converters/tests/variable-name.test.ts +++ b/src/rules/converters/tests/variable-name.test.ts @@ -159,7 +159,69 @@ describe(convertVariableName, () => { }); }); - test("conversion with allow-leading-underscore with check-format argument", () => { + test("conversion with allow-trailing-underscore without check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["allow-trailing-underscore"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + notices: [ + "Leading and trailing underscores (_) in variable names will now be ignored.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + "Leading or trailing underscores (_) on identifiers will now be forbidden.", + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["check-format"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + notices: [ + "Leading and trailing underscores (_) in variable names will now be ignored.", + ], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: [], + notices: [ + "Leading or trailing underscores (_) on identifiers will now be forbidden.", + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-leading-underscore and check-format argument", () => { const result = convertVariableName({ ruleArguments: ["check-format", "allow-leading-underscore"], }); @@ -188,6 +250,68 @@ describe(convertVariableName, () => { }); }); + test("conversion with allow-trailing-underscore and check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["check-format", "allow-trailing-underscore"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + notices: ["Trailing undescores in variable names will now be ignored."], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: ["off"], + notices: [ + "Leading and trailing underscores (_) on identifiers will now be ignored.", + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with allow-leading-underscore, allow-trailing-underscore and check-format argument", () => { + const result = convertVariableName({ + ruleArguments: [ + "check-format", + "allow-leading-underscore", + "allow-trailing-underscore", + ], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "camelcase", + notices: [], + }, + { + ruleName: "no-underscore-dangle", + ruleArguments: ["off"], + notices: [ + "Leading and trailing underscores (_) on identifiers will now be ignored.", + ], + }, + { + ruleName: "id-blacklist", + ruleArguments: [], + }, + { + ruleName: "id-match", + }, + ], + }); + }); + test("conversion with all arguments", () => { const result = convertVariableName({ ruleArguments: [ From b54afeed0550b20b926e9342f714f639e3f1c51e Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Mon, 7 Oct 2019 18:55:17 -0500 Subject: [PATCH 4/5] Set const values to improve readability and order converters rules --- src/rules/converters.ts | 6 +- .../converters/tests/variable-name.test.ts | 95 ++++++------------- src/rules/converters/variable-name.ts | 88 ++++++++--------- 3 files changed, 75 insertions(+), 114 deletions(-) diff --git a/src/rules/converters.ts b/src/rules/converters.ts index 360cfb9f7..0ce0b08f8 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -12,7 +12,6 @@ import { convertClassName } from "./converters/class-name"; import { convertCurly } from "./converters/curly"; import { convertCyclomaticComplexity } from "./converters/cyclomatic-complexity"; import { convertEofline } from "./converters/eofline"; -import { convertMemberAccess } from "./converters/member-access"; import { convertFileNameCasing } from "./converters/file-name-casing"; import { convertForin } from "./converters/forin"; import { convertFunctionConstructor } from "./converters/function-constructor"; @@ -25,6 +24,7 @@ import { convertLinebreakStyle } from "./converters/linebreak-style"; import { convertMaxClassesPerFile } from "./converters/max-classes-per-file"; import { convertMaxFileLineCount } from "./converters/max-file-line-count"; import { convertMaxLineLength } from "./converters/max-line-length"; +import { convertMemberAccess } from "./converters/member-access"; import { convertMemberOrdering } from "./converters/member-ordering"; import { convertNewlineBeforeReturn } from "./converters/newline-before-return"; import { convertNewlinePerChainedCall } from "./converters/newline-per-chained-call"; @@ -105,12 +105,14 @@ import { convertPreferObjectSpread } from "./converters/prefer-object-spread"; import { convertPreferReadonly } from "./converters/prefer-readonly"; import { convertPreferTemplate } from "./converters/prefer-template"; import { convertPromiseFunctionAsync } from "./converters/promise-function-async"; +import { convertQuotemark } from "./converters/quotemark"; import { convertRadix } from "./converters/radix"; import { convertRestrictPlusOperands } from "./converters/restrict-plus-operands"; import { convertSemicolon } from "./converters/semicolon"; import { convertSpaceBeforeFunctionParen } from "./converters/space-before-function-paren"; import { convertSpaceWithinParens } from "./converters/space-within-parens"; import { convertSwitchDefault } from "./converters/switch-default"; +import { convertTripleEquals } from "./converters/triple-equals"; import { convertTypedefWhitespace } from "./converters/typedef-whitespace"; import { convertTypeLiteralDelimiter } from "./converters/type-literal-delimiter"; import { convertTypeofCompare } from "./converters/typeof-compare"; @@ -120,8 +122,6 @@ import { convertUnnecessaryConstructor } from "./converters/unnecessary-construc import { convertUseDefaultTypeParameter } from "./converters/use-default-type-parameter"; import { convertUseIsnan } from "./converters/use-isnan"; import { convertVariableName } from "./converters/variable-name"; -import { convertQuotemark } from "./converters/quotemark"; -import { convertTripleEquals } from "./converters/triple-equals"; /** * Keys TSLint rule names to their ESLint rule converters. diff --git a/src/rules/converters/tests/variable-name.test.ts b/src/rules/converters/tests/variable-name.test.ts index 1898f029a..e98201c20 100644 --- a/src/rules/converters/tests/variable-name.test.ts +++ b/src/rules/converters/tests/variable-name.test.ts @@ -1,4 +1,13 @@ -import { convertVariableName } from "../variable-name"; +import { + convertVariableName, + IgnoreLeadingTrailingUnderscoreMsg, + ForbiddenLeadingTrailingIdentifierMsg, + IgnoreLeadingTrailingIdentifierMsg, + ForbiddenPascalSnakeMsg, + ConstRequiredForAllCapsMsg, + IgnoreOnlyLeadingUnderscoreMsg, + IgnoreOnlyTrailingUnderscoreMsg, +} from "../variable-name"; describe(convertVariableName, () => { test("conversion without arguments", () => { @@ -10,16 +19,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -41,17 +46,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - "ESLint's camel-case will throw a warning if const name is not uppercase.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg, ConstRequiredForAllCapsMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -73,17 +73,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -105,17 +100,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -137,16 +127,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -168,16 +154,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -199,16 +181,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "Leading and trailing underscores (_) in variable names will now be ignored.", - ], + notices: [IgnoreLeadingTrailingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: [], - notices: [ - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ], + notices: [ForbiddenLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -230,14 +208,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: ["Leading undescores in variable names will now be ignored."], + notices: [IgnoreOnlyLeadingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: ["off"], - notices: [ - "Leading and trailing underscores (_) on identifiers will now be ignored.", - ], + notices: [IgnoreLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -259,14 +235,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: ["Trailing undescores in variable names will now be ignored."], + notices: [IgnoreOnlyTrailingUnderscoreMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: ["off"], - notices: [ - "Leading and trailing underscores (_) on identifiers will now be ignored.", - ], + notices: [IgnoreLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -297,9 +271,7 @@ describe(convertVariableName, () => { { ruleName: "no-underscore-dangle", ruleArguments: ["off"], - notices: [ - "Leading and trailing underscores (_) on identifiers will now be ignored.", - ], + notices: [IgnoreLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", @@ -329,17 +301,12 @@ describe(convertVariableName, () => { rules: [ { ruleName: "camelcase", - notices: [ - "ESLint's camel-case will throw a warning if const name is not uppercase.", - "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", - ], + notices: [ConstRequiredForAllCapsMsg, ForbiddenPascalSnakeMsg], }, { ruleName: "no-underscore-dangle", ruleArguments: ["off"], - notices: [ - "Leading and trailing underscores (_) on identifiers will now be ignored.", - ], + notices: [IgnoreLeadingTrailingIdentifierMsg], }, { ruleName: "id-blacklist", diff --git a/src/rules/converters/variable-name.ts b/src/rules/converters/variable-name.ts index 8bc1d3c5e..90af66ddb 100644 --- a/src/rules/converters/variable-name.ts +++ b/src/rules/converters/variable-name.ts @@ -1,50 +1,52 @@ import { RuleConverter } from "../converter"; +export const IgnoreLeadingTrailingUnderscoreMsg = + "Leading and trailing underscores (_) in variable names will now be ignored."; +export const IgnoreOnlyLeadingUnderscoreMsg = + "Leading undescores in variable names will now be ignored."; +export const IgnoreOnlyTrailingUnderscoreMsg = + "Trailing undescores in variable names will now be ignored."; +export const ConstRequiredForAllCapsMsg = + "ESLint's camel-case will throw a warning if const name is not uppercase."; +export const ForbiddenPascalSnakeMsg = + "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods."; +export const IgnoreLeadingTrailingIdentifierMsg = + "Leading and trailing underscores (_) on identifiers will now be ignored."; +export const ForbiddenLeadingTrailingIdentifierMsg = + "Leading or trailing underscores (_) on identifiers will now be forbidden."; + export const convertVariableName: RuleConverter = tslintRule => { + const hasCheckFormat = tslintRule.ruleArguments.includes("check-format"); + const allowedLeadingUnderscore = tslintRule.ruleArguments.includes("allow-leading-underscore"); + const allowedTrailingUnderscore = tslintRule.ruleArguments.includes( + "allow-trailing-underscore", + ); + const constRequiredForAllCaps = tslintRule.ruleArguments.includes("require-const-for-all-caps"); + const allowPascalSnakeCase = + tslintRule.ruleArguments.includes("allow-pascal-case") || + tslintRule.ruleArguments.includes("allow-snake-case"); + const getCamelCaseRuleOptions = () => { const camelCaseOptionNotice: string[] = []; - if (tslintRule.ruleArguments.includes("check-format")) { - if ( - !tslintRule.ruleArguments.includes("allow-leading-underscore") && - !tslintRule.ruleArguments.includes("allow-trailing-underscore") - ) { - camelCaseOptionNotice.push( - "Leading and trailing underscores (_) in variable names will now be ignored.", - ); - } else if ( - tslintRule.ruleArguments.includes("allow-leading-underscore") && - !tslintRule.ruleArguments.includes("allow-trailing-underscore") - ) { - camelCaseOptionNotice.push( - "Leading undescores in variable names will now be ignored.", - ); - } else if ( - !tslintRule.ruleArguments.includes("allow-leading-underscore") && - tslintRule.ruleArguments.includes("allow-trailing-underscore") - ) { - camelCaseOptionNotice.push( - "Trailing undescores in variable names will now be ignored.", - ); + + if (hasCheckFormat) { + if (!allowedLeadingUnderscore && !allowedTrailingUnderscore) { + camelCaseOptionNotice.push(IgnoreLeadingTrailingUnderscoreMsg); + } else if (allowedLeadingUnderscore && !allowedTrailingUnderscore) { + camelCaseOptionNotice.push(IgnoreOnlyLeadingUnderscoreMsg); + } else if (!allowedLeadingUnderscore && allowedTrailingUnderscore) { + camelCaseOptionNotice.push(IgnoreOnlyTrailingUnderscoreMsg); } } else { - camelCaseOptionNotice.push( - "Leading and trailing underscores (_) in variable names will now be ignored.", - ); + camelCaseOptionNotice.push(IgnoreLeadingTrailingUnderscoreMsg); } - if (tslintRule.ruleArguments.includes("require-const-for-all-caps")) { - camelCaseOptionNotice.push( - "ESLint's camel-case will throw a warning if const name is not uppercase.", - ); + if (constRequiredForAllCaps) { + camelCaseOptionNotice.push(ConstRequiredForAllCapsMsg); } - if ( - tslintRule.ruleArguments.includes("allow-pascal-case") || - tslintRule.ruleArguments.includes("allow-snake-case") - ) { - camelCaseOptionNotice.push( - "ESLint's camel-case rule does not allow pascal or snake case variable names. Those cases are reserved for class names and static methods.", - ); + if (allowPascalSnakeCase) { + camelCaseOptionNotice.push(ForbiddenPascalSnakeMsg); } return { @@ -56,19 +58,11 @@ export const convertVariableName: RuleConverter = tslintRule => { const underscoreDangleOptionArguments: string[] = []; const underscoreDangleOptionNotice: string[] = []; - if ( - tslintRule.ruleArguments.includes("check-format") && - (tslintRule.ruleArguments.includes("allow-leading-underscore") || - tslintRule.ruleArguments.includes("allow-trailing-underscore")) - ) { + if (hasCheckFormat && (allowedLeadingUnderscore || allowedTrailingUnderscore)) { underscoreDangleOptionArguments.push("off"); - underscoreDangleOptionNotice.push( - "Leading and trailing underscores (_) on identifiers will now be ignored.", - ); + underscoreDangleOptionNotice.push(IgnoreLeadingTrailingIdentifierMsg); } else { - underscoreDangleOptionNotice.push( - "Leading or trailing underscores (_) on identifiers will now be forbidden.", - ); + underscoreDangleOptionNotice.push(ForbiddenLeadingTrailingIdentifierMsg); } return { From 1c692c903e15e62cc5e26f575e385d06c4cb2564 Mon Sep 17 00:00:00 2001 From: KingDarBoja Date: Mon, 7 Oct 2019 18:58:31 -0500 Subject: [PATCH 5/5] Optimize rules array on variable name --- src/rules/converters/variable-name.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/rules/converters/variable-name.ts b/src/rules/converters/variable-name.ts index 90af66ddb..1a10009a3 100644 --- a/src/rules/converters/variable-name.ts +++ b/src/rules/converters/variable-name.ts @@ -50,6 +50,7 @@ export const convertVariableName: RuleConverter = tslintRule => { } return { + ruleName: "camelcase", notices: camelCaseOptionNotice, }; }; @@ -66,6 +67,7 @@ export const convertVariableName: RuleConverter = tslintRule => { } return { + ruleName: "no-underscore-dangle", ruleArguments: underscoreDangleOptionArguments, notices: underscoreDangleOptionNotice, }; @@ -89,24 +91,16 @@ export const convertVariableName: RuleConverter = tslintRule => { } return { + ruleName: "id-blacklist", ruleArguments: blackListOptionArguments, }; }; return { rules: [ - { - ruleName: "camelcase", - ...getCamelCaseRuleOptions(), - }, - { - ruleName: "no-underscore-dangle", - ...getUnderscoreDangleRuleOptions(), - }, - { - ruleName: "id-blacklist", - ...getBlackListRuleOptions(), - }, + getCamelCaseRuleOptions(), + getUnderscoreDangleRuleOptions(), + getBlackListRuleOptions(), { ruleName: "id-match", },