Skip to content

Commit

Permalink
Implement variable-name rule converter (#222)
Browse files Browse the repository at this point in the history
* Implement variable-name converter

* Feedback changes

* Add more test cases

* Set const values to improve readability and order converters rules

* Optimize rules array on variable name
  • Loading branch information
KingDarBoja authored and Josh Goldberg committed Oct 10, 2019
1 parent 6ae5bb7 commit bd694e7
Show file tree
Hide file tree
Showing 3 changed files with 445 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/rules/converters.ts
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -119,8 +121,7 @@ import { convertUnnecessaryBind } from "./converters/unnecessary-bind";
import { convertUnnecessaryConstructor } from "./converters/unnecessary-constructor";
import { convertUseDefaultTypeParameter } from "./converters/use-default-type-parameter";
import { convertUseIsnan } from "./converters/use-isnan";
import { convertQuotemark } from "./converters/quotemark";
import { convertTripleEquals } from "./converters/triple-equals";
import { convertVariableName } from "./converters/variable-name";

/**
* Keys TSLint rule names to their ESLint rule converters.
Expand Down Expand Up @@ -249,6 +250,7 @@ export const converters = new Map([
["unnecessary-constructor", convertUnnecessaryConstructor],
["use-default-type-parameter", convertUseDefaultTypeParameter],
["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...
Expand All @@ -262,7 +264,6 @@ export const converters = new Map([
// ["no-void-expression", convertNoVoidExpression], // (no exact equivalent)
// ["quotemark", convertQuotemark], // quotes
// ["triple-equals", convertTripleEquals], // eqeqeq
// ["variable-name", convertVariableName], // a bunch of rules...

// tslint-microsoft-contrib rules:
// ["max-func-body-length", convertMaxFuncBodyLength],
Expand Down
331 changes: 331 additions & 0 deletions src/rules/converters/tests/variable-name.test.ts
@@ -0,0 +1,331 @@
import {
convertVariableName,
IgnoreLeadingTrailingUnderscoreMsg,
ForbiddenLeadingTrailingIdentifierMsg,
IgnoreLeadingTrailingIdentifierMsg,
ForbiddenPascalSnakeMsg,
ConstRequiredForAllCapsMsg,
IgnoreOnlyLeadingUnderscoreMsg,
IgnoreOnlyTrailingUnderscoreMsg,
} from "../variable-name";

describe(convertVariableName, () => {
test("conversion without arguments", () => {
const result = convertVariableName({
ruleArguments: [],
});

expect(result).toEqual({
rules: [
{
ruleName: "camelcase",
notices: [IgnoreLeadingTrailingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
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",
notices: [IgnoreLeadingTrailingUnderscoreMsg, ConstRequiredForAllCapsMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
ruleName: "id-match",
},
],
});
});

test("conversion with allow-pascal-case argument", () => {
const result = convertVariableName({
ruleArguments: ["allow-pascal-case"],
});

expect(result).toEqual({
rules: [
{
ruleName: "camelcase",
notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
ruleName: "id-match",
},
],
});
});

test("conversion with allow-snake-case argument", () => {
const result = convertVariableName({
ruleArguments: ["allow-snake-case"],
});

expect(result).toEqual({
rules: [
{
ruleName: "camelcase",
notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
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",
notices: [IgnoreLeadingTrailingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
ruleName: "id-match",
},
],
});
});

test("conversion with allow-trailing-underscore without check-format argument", () => {
const result = convertVariableName({
ruleArguments: ["allow-trailing-underscore"],
});

expect(result).toEqual({
rules: [
{
ruleName: "camelcase",
notices: [IgnoreLeadingTrailingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
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: [IgnoreLeadingTrailingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: [],
notices: [ForbiddenLeadingTrailingIdentifierMsg],
},
{
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"],
});

expect(result).toEqual({
rules: [
{
ruleName: "camelcase",
notices: [IgnoreOnlyLeadingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: ["off"],
notices: [IgnoreLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
ruleName: "id-match",
},
],
});
});

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: [IgnoreOnlyTrailingUnderscoreMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: ["off"],
notices: [IgnoreLeadingTrailingIdentifierMsg],
},
{
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: [IgnoreLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [],
},
{
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",
notices: [ConstRequiredForAllCapsMsg, ForbiddenPascalSnakeMsg],
},
{
ruleName: "no-underscore-dangle",
ruleArguments: ["off"],
notices: [IgnoreLeadingTrailingIdentifierMsg],
},
{
ruleName: "id-blacklist",
ruleArguments: [
"any",
"Number",
"number",
"String",
"string",
"Boolean",
"boolean",
"Undefined",
"undefined",
],
},
{
ruleName: "id-match",
},
],
});
});
});

0 comments on commit bd694e7

Please sign in to comment.