Skip to content

[Feature] Add ban rule converter #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 67 additions & 0 deletions src/rules/converters/ban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { RuleConverter } from "../converter";

type BanRule = {
object?: string;
property?: string;
message?: string;
};

export const convertBan: RuleConverter = tslintRule => {
let bannedObjMethod: BanRule[] = [];

for (const rule of tslintRule.ruleArguments) {
switch (rule.constructor.name) {
case "String":
bannedObjMethod.push({
property: rule,
});
Comment on lines +15 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this'll need some work. TSLint's ban rule bans both globals and properties. ESLint splits those between no-restricted-globals and no-restricted-properties.

For example, this TSLint configuration:

{
    "rules": {
        "ban": [
            true,
            "eval",
        ]
    }
}

...is, from my understanding, equivalent to this ESLint configuration:

{
    "rules": {
        "no-restricted-globals": [
            "error",
            "eval"
        ]
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused as the roadmap states that it should be only the no-restricted-properties rule. ESLint rule

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KingDarBoja perhaps the roadmap is incorrect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be outdated but should be good if someone check that.

break;
case "Array":
if (rule.length == 1) {
bannedObjMethod.push({
property: rule[0],
});
} else {
bannedObjMethod.push({
object: rule[0],
property: rule[1],
...{ message: rule[2] ? rule[2] : undefined },
});
}
break;
case "Object":
if (Array.isArray(rule.name)) {
if (rule.name.length == 2) {
// TODO: Fix iteration if multiple properties.
bannedObjMethod.push({
...{ object: rule.name[0] !== "*" ? rule.name[0] : undefined },
property: rule.name[1],
...{ message: rule.message ? rule.message : undefined },
});
} else {
// Don't know how to deal with nested functions.
}
} else {
bannedObjMethod.push({
property: rule.name,
...{ message: rule.message ? rule.message : undefined },
});
}
break;
default:
break;
}
}

const ruleArguments =
tslintRule.ruleArguments.length === 0 ? undefined : [2, ...bannedObjMethod];

return {
rules: [
{
ruleName: "no-restricted-properties",
...{ ruleArguments },
},
],
};
};
142 changes: 142 additions & 0 deletions src/rules/converters/tests/ban.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { convertBan } from "../ban";

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

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
},
],
});
});

test("conversion with single string argument", () => {
const result = convertBan({
ruleArguments: [true, "eval"],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
property: "eval",
},
],
},
],
});
});

test("conversion with single array argument", () => {
const result = convertBan({
ruleArguments: [true, ["map"]],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
property: "map",
},
],
},
],
});
});

test("conversion with array argument but no message", () => {
const result = convertBan({
ruleArguments: [true, ["describe", "only"]],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
object: "describe",
property: "only",
},
],
},
],
});
});

test("conversion with array argument and message", () => {
const result = convertBan({
ruleArguments: [true, ["describe", "only", "use another method"]],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
object: "describe",
property: "only",
message: "use another method",
},
],
},
],
});
});

test("conversion with object argument and property name as string but no message", () => {
const result = convertBan({
ruleArguments: [true, { name: "$" }],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
property: "$",
},
],
},
],
});
});

test("conversion with object argument and property name as array and message", () => {
const result = convertBan({
ruleArguments: [true, { name: ["it", "only"], message: "do not focus tests" }],
});

expect(result).toEqual({
rules: [
{
ruleName: "no-restricted-properties",
ruleArguments: [
2,
{
object: "it",
property: "only",
message: "do not focus tests",
},
],
},
],
});
});
});
3 changes: 2 additions & 1 deletion src/rules/rulesConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { convertArrayType } from "./converters/array-type";
import { convertArrowParens } from "./converters/arrow-parens";
import { convertArrowReturnShorthand } from "./converters/arrow-return-shorthand";
import { convertAwaitPromise } from "./converters/await-promise";
import { convertBan } from "./converters/ban";
import { convertBanCommaOperator } from "./converters/ban-comma-operator";
import { convertBanTsIgnore } from "./converters/ban-ts-ignore";
import { convertBanTypes } from "./converters/ban-types";
Expand Down Expand Up @@ -142,6 +143,7 @@ export const rulesConverters = new Map([
["arrow-parens", convertArrowParens],
["arrow-return-shorthand", convertArrowReturnShorthand],
["await-promise", convertAwaitPromise],
["ban", convertBan], // no-restricted-properties
["ban-comma-operator", convertBanCommaOperator],
["ban-ts-ignore", convertBanTsIgnore],
["ban-types", convertBanTypes],
Expand Down Expand Up @@ -276,7 +278,6 @@ export const rulesConverters = new Map([
// As these are enabled, they should be added in sorted order to the list above.

// TSLint core rules:
// ["ban", convertBan], // no-restricted-properties

// tslint-microsoft-contrib rules:
// ["max-func-body-length", convertMaxFuncBodyLength],
Expand Down