diff --git a/src/rules/converters/class-name.ts b/src/rules/converters/class-name.ts index 93d1819ba..16c8c364f 100644 --- a/src/rules/converters/class-name.ts +++ b/src/rules/converters/class-name.ts @@ -4,7 +4,7 @@ export const convertClassName: RuleConverter = () => { return { rules: [ { - ruleName: "@typescript-eslint/class-name-casing", + ruleName: "@typescript-eslint/naming-convention", }, ], }; diff --git a/src/rules/converters/interface-name.ts b/src/rules/converters/interface-name.ts index 9cff4a345..a3a2949df 100644 --- a/src/rules/converters/interface-name.ts +++ b/src/rules/converters/interface-name.ts @@ -1,10 +1,23 @@ import { RuleConverter } from "../converter"; -export const convertInterfaceName: RuleConverter = () => { +export const convertInterfaceName: RuleConverter = (tslintRule) => { return { rules: [ { - ruleName: "@typescript-eslint/interface-name-prefix", + ruleName: "@typescript-eslint/naming-convention", + ...(tslintRule.ruleArguments.length !== 0 && { + rules: [ + { + selector: "interface", + format: ["PascalCase"], + custom: { + regex: "^I[A-Z]", + match: + tslintRule.ruleArguments[0] === "always-prefix" ? true : false, + }, + }, + ], + }), }, ], }; diff --git a/src/rules/converters/tests/class-name.test.ts b/src/rules/converters/tests/class-name.test.ts index 6bd03d8ab..427d6d17a 100644 --- a/src/rules/converters/tests/class-name.test.ts +++ b/src/rules/converters/tests/class-name.test.ts @@ -9,7 +9,7 @@ describe(convertClassName, () => { expect(result).toEqual({ rules: [ { - ruleName: "@typescript-eslint/class-name-casing", + ruleName: "@typescript-eslint/naming-convention", }, ], }); diff --git a/src/rules/converters/tests/interface-name.test.ts b/src/rules/converters/tests/interface-name.test.ts index 38cc99c7f..0d0137eb6 100644 --- a/src/rules/converters/tests/interface-name.test.ts +++ b/src/rules/converters/tests/interface-name.test.ts @@ -9,7 +9,55 @@ describe(convertInterfaceName, () => { expect(result).toEqual({ rules: [ { - ruleName: "@typescript-eslint/interface-name-prefix", + ruleName: "@typescript-eslint/naming-convention", + }, + ], + }); + }); + + test("conversion with 'always-prefix' argument", () => { + const result = convertInterfaceName({ + ruleArguments: ["always-prefix"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "interface", + format: ["PascalCase"], + custom: { + regex: "^I[A-Z]", + match: true, + }, + }, + ], + }, + ], + }); + }); + + test("conversion with 'never-prefix' argument", () => { + const result = convertInterfaceName({ + ruleArguments: ["never-prefix"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "interface", + format: ["PascalCase"], + custom: { + regex: "^I[A-Z]", + match: false, + }, + }, + ], }, ], }); diff --git a/src/rules/converters/tests/variable-name.test.ts b/src/rules/converters/tests/variable-name.test.ts index 9772176d6..b4c62fa05 100644 --- a/src/rules/converters/tests/variable-name.test.ts +++ b/src/rules/converters/tests/variable-name.test.ts @@ -1,12 +1,8 @@ import { convertVariableName, - IgnoreLeadingTrailingUnderscoreMsg, + ConstRequiredForAllCapsMsg, ForbiddenLeadingTrailingIdentifierMsg, IgnoreLeadingTrailingIdentifierMsg, - ForbiddenPascalSnakeMsg, - ConstRequiredForAllCapsMsg, - IgnoreOnlyLeadingUnderscoreMsg, - IgnoreOnlyTrailingUnderscoreMsg, } from "../variable-name"; describe(convertVariableName, () => { @@ -18,8 +14,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreLeadingTrailingUnderscoreMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -35,7 +38,7 @@ describe(convertVariableName, () => { }); }); - test("conversion with require-const-for-all-caps argument", () => { + test("conversion with require-const-for-all-caps argument without check-format argument", () => { const result = convertVariableName({ ruleArguments: ["require-const-for-all-caps"], }); @@ -43,8 +46,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreLeadingTrailingUnderscoreMsg, ConstRequiredForAllCapsMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -60,7 +70,7 @@ describe(convertVariableName, () => { }); }); - test("conversion with allow-pascal-case argument", () => { + test("conversion with allow-pascal-case argument without check-format argument", () => { const result = convertVariableName({ ruleArguments: ["allow-pascal-case"], }); @@ -68,8 +78,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -85,7 +102,7 @@ describe(convertVariableName, () => { }); }); - test("conversion with allow-snake-case argument", () => { + test("conversion with allow-snake-case argument without check-format argument", () => { const result = convertVariableName({ ruleArguments: ["allow-snake-case"], }); @@ -93,8 +110,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - ruleName: "camelcase", - notices: [IgnoreLeadingTrailingUnderscoreMsg, ForbiddenPascalSnakeMsg], + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -118,8 +142,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - ruleName: "camelcase", - notices: [IgnoreLeadingTrailingUnderscoreMsg], + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { ruleName: "no-underscore-dangle", @@ -143,8 +174,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreLeadingTrailingUnderscoreMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -168,8 +206,48 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreLeadingTrailingUnderscoreMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], + }, + { + notices: [ForbiddenLeadingTrailingIdentifierMsg], + ruleName: "no-underscore-dangle", + }, + { + ruleName: "id-blacklist", + }, + { + ruleName: "id-match", + }, + ], + }); + }); + + test("conversion with require-const-for-all-caps argument and check-format argument", () => { + const result = convertVariableName({ + ruleArguments: ["check-format", "require-const-for-all-caps"], + }); + + expect(result).toEqual({ + rules: [ + { + notices: [ConstRequiredForAllCapsMsg], + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }, + ], }, { notices: [ForbiddenLeadingTrailingIdentifierMsg], @@ -193,8 +271,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreOnlyLeadingUnderscoreMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "allow", + trailingUnderscore: "forbid", + }, + ], }, { notices: [IgnoreLeadingTrailingIdentifierMsg], @@ -219,8 +304,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [IgnoreOnlyTrailingUnderscoreMsg], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "allow", + }, + ], }, { notices: [IgnoreLeadingTrailingIdentifierMsg], @@ -249,8 +341,15 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - notices: [], - ruleName: "camelcase", + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "allow", + trailingUnderscore: "allow", + }, + ], }, { notices: [IgnoreLeadingTrailingIdentifierMsg], @@ -283,8 +382,16 @@ describe(convertVariableName, () => { expect(result).toEqual({ rules: [ { - ruleName: "camelcase", - notices: [ConstRequiredForAllCapsMsg, ForbiddenPascalSnakeMsg], + ruleName: "@typescript-eslint/naming-convention", + rules: [ + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "allow", + trailingUnderscore: "allow", + }, + ], + notices: [ConstRequiredForAllCapsMsg], }, { ruleName: "no-underscore-dangle", diff --git a/src/rules/converters/variable-name.ts b/src/rules/converters/variable-name.ts index 535ed5ebf..0fef98663 100644 --- a/src/rules/converters/variable-name.ts +++ b/src/rules/converters/variable-name.ts @@ -1,15 +1,7 @@ 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."; + "typescript-eslint does not enforce uppercase for const only."; export const IgnoreLeadingTrailingIdentifierMsg = "Leading and trailing underscores (_) on identifiers will now be ignored."; export const ForbiddenLeadingTrailingIdentifierMsg = @@ -22,36 +14,45 @@ export const convertVariableName: RuleConverter = (tslintRule) => { "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 allowPascalCase = tslintRule.ruleArguments.includes("allow-pascal-case"); + const allowSnakeCase = tslintRule.ruleArguments.includes("allow-snake-case"); const getCamelCaseRuleOptions = () => { - const camelCaseOptionNotice: string[] = []; + const camelCaseRules: Record[] = []; + const camelCaseOptionNotices: string[] = []; + const formats = ["camelCase", "UPPER_CASE"]; - 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(IgnoreLeadingTrailingUnderscoreMsg); + if (hasCheckFormat && allowPascalCase) { + formats.push("PascalCase"); + } + if (hasCheckFormat && allowSnakeCase) { + formats.push("snake_case"); } - if (constRequiredForAllCaps) { - camelCaseOptionNotice.push(ConstRequiredForAllCapsMsg); + if (!hasCheckFormat) { + camelCaseRules.push({ + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "forbid", + trailingUnderscore: "forbid", + }); + } else { + camelCaseRules.push({ + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: allowedLeadingUnderscore ? "allow" : "forbid", + trailingUnderscore: allowedTrailingUnderscore ? "allow" : "forbid", + }); } - if (allowPascalSnakeCase) { - camelCaseOptionNotice.push(ForbiddenPascalSnakeMsg); + if (hasCheckFormat && constRequiredForAllCaps) { + camelCaseOptionNotices.push(ConstRequiredForAllCapsMsg); } return { - notices: camelCaseOptionNotice, - ruleName: "camelcase", + ...(camelCaseOptionNotices.length !== 0 && { notices: camelCaseOptionNotices }), + ...(camelCaseRules.length !== 0 && { rules: camelCaseRules }), + ruleName: "@typescript-eslint/naming-convention", }; };