Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rules/converters/class-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const convertClassName: RuleConverter = () => {
return {
rules: [
{
ruleName: "@typescript-eslint/class-name-casing",
ruleName: "@typescript-eslint/naming-convention",
},
],
};
Expand Down
2 changes: 1 addition & 1 deletion src/rules/converters/interface-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const convertInterfaceName: RuleConverter = () => {
return {
rules: [
{
ruleName: "@typescript-eslint/interface-name-prefix",
ruleName: "@typescript-eslint/naming-convention",
},
],
};
Expand Down
2 changes: 1 addition & 1 deletion src/rules/converters/tests/class-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe(convertClassName, () => {
expect(result).toEqual({
rules: [
{
ruleName: "@typescript-eslint/class-name-casing",
ruleName: "@typescript-eslint/naming-convention",
},
],
});
Expand Down
2 changes: 1 addition & 1 deletion src/rules/converters/tests/interface-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe(convertInterfaceName, () => {
expect(result).toEqual({
rules: [
{
ruleName: "@typescript-eslint/interface-name-prefix",
ruleName: "@typescript-eslint/naming-convention",
},
],
});
Expand Down
167 changes: 137 additions & 30 deletions src/rules/converters/tests/variable-name.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
convertVariableName,
IgnoreLeadingTrailingUnderscoreMsg,
ConstRequiredForAllCapsMsg,
ForbiddenLeadingTrailingIdentifierMsg,
IgnoreLeadingTrailingIdentifierMsg,
ForbiddenPascalSnakeMsg,
ConstRequiredForAllCapsMsg,
IgnoreOnlyLeadingUnderscoreMsg,
IgnoreOnlyTrailingUnderscoreMsg,
} from "../variable-name";

describe(convertVariableName, () => {
Expand All @@ -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],
Expand All @@ -35,16 +38,23 @@ 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"],
});

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],
Expand All @@ -60,16 +70,23 @@ 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"],
});

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],
Expand All @@ -85,16 +102,23 @@ 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"],
});

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],
Expand All @@ -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",
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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",
Expand Down
59 changes: 30 additions & 29 deletions src/rules/converters/variable-name.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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<string, unknown>[] = [];
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",
};
};

Expand Down