From 96afca204505b5560a47574e421283357363153d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 12 Dec 2020 15:17:57 -0500 Subject: [PATCH 1/2] Added converter for jsx-ban-props --- .../lintConfigs/rules/ruleConverters.ts | 2 + .../eslint-plugin-react/jsx-ban-props.ts | 18 ++++++ .../tests/jsx-ban-props.test.ts | 62 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts create mode 100644 src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts diff --git a/src/converters/lintConfigs/rules/ruleConverters.ts b/src/converters/lintConfigs/rules/ruleConverters.ts index 4baf906e4..cf0b85f27 100644 --- a/src/converters/lintConfigs/rules/ruleConverters.ts +++ b/src/converters/lintConfigs/rules/ruleConverters.ts @@ -176,6 +176,7 @@ import { convertUsePipeDecorator } from "./ruleConverters/codelyzer/use-pipe-dec import { convertUsePipeTransformInterface } from "./ruleConverters/codelyzer/use-pipe-transform-interface"; // ESLint-React converters +import { convertJsxBanProps } from "./ruleConverters/eslint-plugin-react/jsx-ban-props"; import { convertJsxBooleanValue } from "./ruleConverters/eslint-plugin-react/jsx-boolean-value"; import { convertJsxCurlySpacing } from "./ruleConverters/eslint-plugin-react/jsx-curly-spacing"; import { convertJsxEqualsSpacing } from "./ruleConverters/eslint-plugin-react/jsx-equals-spacing"; @@ -237,6 +238,7 @@ export const ruleConverters = new Map([ ["interface-name", convertInterfaceName], ["interface-over-type-literal", convertInterfaceOverTypeLiteral], ["jsdoc-format", convertJSDocFormat], + ["jsx-ban-props", convertJsxBanProps], ["jsx-boolean-value", convertJsxBooleanValue], ["jsx-curly-spacing", convertJsxCurlySpacing], ["jsx-equals-spacing", convertJsxEqualsSpacing], diff --git a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts new file mode 100644 index 000000000..7b39541b1 --- /dev/null +++ b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts @@ -0,0 +1,18 @@ +import { RuleConverter } from "../../ruleConverter"; + +export const convertJsxBanProps: RuleConverter = (tslintRule) => { + return { + rules: [ + { + ...(tslintRule.ruleArguments.length !== 0 && { + ruleArguments: tslintRule.ruleArguments.map((ruleArgument) => ({ + ...(ruleArgument.length === 2 && { message: ruleArgument[1] }), + propName: ruleArgument[0], + })), + }), + ruleName: "react/forbid-component-props", + }, + ], + plugins: ["eslint-plugin-react"], + }; +}; diff --git a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts new file mode 100644 index 000000000..ff07eb797 --- /dev/null +++ b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts @@ -0,0 +1,62 @@ +import { convertJsxBanProps } from "../jsx-ban-props"; + +describe(convertJsxBanProps, () => { + test("conversion without arguments", () => { + const result = convertJsxBanProps({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "react/forbid-component-props", + }, + ], + plugins: ["eslint-plugin-react"], + }); + }); + + test("conversion with a single prop", () => { + const result = convertJsxBanProps({ + ruleArguments: [["someProp"]], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: [ + { + propName: "someProp", + }, + ], + ruleName: "react/forbid-component-props", + }, + ], + plugins: ["eslint-plugin-react"], + }); + }); + + test("conversion with a message", () => { + const result = convertJsxBanProps({ + ruleArguments: [["someProp"], ["anotherProp", "Optional explanation"]], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: [ + { + propName: "someProp", + }, + { + message: "Optional explanation", + propName: "anotherProp", + }, + ], + ruleName: "react/forbid-component-props", + }, + ], + plugins: ["eslint-plugin-react"], + }); + }); +}); From 9a073565d5d8e25f0db778e9de45e7f62cdb4580 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sat, 12 Dec 2020 16:25:42 -0500 Subject: [PATCH 2/2] Nest inside a forbid member --- .../eslint-plugin-react/jsx-ban-props.ts | 9 +++++++-- .../eslint-plugin-react/tests/jsx-ban-props.test.ts | 10 ++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts index 7b39541b1..d66dae7e6 100644 --- a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts +++ b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/jsx-ban-props.ts @@ -6,8 +6,13 @@ export const convertJsxBanProps: RuleConverter = (tslintRule) => { { ...(tslintRule.ruleArguments.length !== 0 && { ruleArguments: tslintRule.ruleArguments.map((ruleArgument) => ({ - ...(ruleArgument.length === 2 && { message: ruleArgument[1] }), - propName: ruleArgument[0], + forbid: + ruleArgument.length === 1 + ? ruleArgument[0] + : { + message: ruleArgument[1], + propName: ruleArgument[0], + }, })), }), ruleName: "react/forbid-component-props", diff --git a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts index ff07eb797..ed9559684 100644 --- a/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts +++ b/src/converters/lintConfigs/rules/ruleConverters/eslint-plugin-react/tests/jsx-ban-props.test.ts @@ -26,7 +26,7 @@ describe(convertJsxBanProps, () => { { ruleArguments: [ { - propName: "someProp", + forbid: "someProp", }, ], ruleName: "react/forbid-component-props", @@ -46,11 +46,13 @@ describe(convertJsxBanProps, () => { { ruleArguments: [ { - propName: "someProp", + forbid: "someProp", }, { - message: "Optional explanation", - propName: "anotherProp", + forbid: { + message: "Optional explanation", + propName: "anotherProp", + }, }, ], ruleName: "react/forbid-component-props",