From 61e980348e2b185b75a62d5f6fa97113b8f849e4 Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Thu, 9 Mar 2023 11:12:31 -0500 Subject: [PATCH 1/2] feat(Helpers): added test helper for callback param adding --- .../rules/v5/dataList-updated-callback.js | 86 +------------ .../rules/v5/onToggle-updated-paramaters.js | 76 +----------- .../test/testHelpers.js | 115 ++++++++++++++++++ 3 files changed, 119 insertions(+), 158 deletions(-) create mode 100644 packages/eslint-plugin-pf-codemods/test/testHelpers.js diff --git a/packages/eslint-plugin-pf-codemods/test/rules/v5/dataList-updated-callback.js b/packages/eslint-plugin-pf-codemods/test/rules/v5/dataList-updated-callback.js index cd2e57ce9..509f6c813 100644 --- a/packages/eslint-plugin-pf-codemods/test/rules/v5/dataList-updated-callback.js +++ b/packages/eslint-plugin-pf-codemods/test/rules/v5/dataList-updated-callback.js @@ -1,85 +1,3 @@ -const ruleTester = require("../../ruletester"); -const rule = require("../../../lib/rules/v5/dataList-updated-callback"); +const { addCallbackParamTester } = require("../../testHelpers"); -ruleTester.run("dataList-updated-callback", rule, { - valid: [ - { - code: `import { DataList } from '@patternfly/react-core'; ;`, - }, - { - code: `import { DataList } from '@patternfly/react-core'; onSelect()} />;`, - }, - { - code: `import { DataList } from '@patternfly/react-core'; const onSelect = () => {}; ;`, - }, - { - code: `import { DataList } from '@patternfly/react-core'; function onSelect() {}; ;`, - }, - { - // No @patternfly/react-core import - code: `;`, - }, - ], - invalid: [ - { - code: `import { DataList } from '@patternfly/react-core'; onSelect(id)} />;`, - output: `import { DataList } from '@patternfly/react-core'; onSelect(id)} />;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for DataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataList } from '@patternfly/react-core'; onSelect(id)} />;`, - output: `import { DataList } from '@patternfly/react-core'; onSelect(id)} />;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for DataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataList } from '@patternfly/react-core'; const onSelect = (id) => {}; ;`, - output: `import { DataList } from '@patternfly/react-core'; const onSelect = (_event, id) => {}; ;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for DataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataList } from '@patternfly/react-core'; function onSelect(id) {}; ;`, - output: `import { DataList } from '@patternfly/react-core'; function onSelect(_event, id) {}; ;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for DataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataList } from '@patternfly/react-core'; ;`, - output: `import { DataList } from '@patternfly/react-core'; ;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for DataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataList as PFDataList } from '@patternfly/react-core'; onSelect(id)} />;`, - output: `import { DataList as PFDataList } from '@patternfly/react-core'; onSelect(id)} />;`, - errors: [ - { - message: `The "onSelectDataListItem" prop for PFDataList has been updated so that the "_event" parameter is the first parameter. "onSelectDataListItem" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - ], -}); +addCallbackParamTester("dataList-updated-callback", "DataList", "onSelectDataListItem"); diff --git a/packages/eslint-plugin-pf-codemods/test/rules/v5/onToggle-updated-paramaters.js b/packages/eslint-plugin-pf-codemods/test/rules/v5/onToggle-updated-paramaters.js index 05fad267e..b1ffa80ae 100644 --- a/packages/eslint-plugin-pf-codemods/test/rules/v5/onToggle-updated-paramaters.js +++ b/packages/eslint-plugin-pf-codemods/test/rules/v5/onToggle-updated-paramaters.js @@ -1,5 +1,4 @@ -const ruleTester = require("../../ruletester"); -const rule = require("../../../lib/rules/v5/onToggle-updated-paramaters"); +const { addCallbackParamTester } = require("../../testHelpers"); const onToggleAPIUpdateList = [ "ApplicationLauncher", "BadgeToggle", @@ -10,75 +9,4 @@ const onToggleAPIUpdateList = [ "SelectToggle", ]; -ruleTester.run("onToggle-updated-paramaters", rule, { - valid: [ - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; <${component} />;`, - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={() => onToggle} />;`, - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; const onToggle = () => {}; <${component} onToggle={onToggle} />;`, - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; function onToggle() {}; <${component} onToggle={onToggle} />;`, - })), - // No @patternfly/react-core import - ...onToggleAPIUpdateList.map((component) => ({ - code: `<${component} onToggle />;`, - })), - ], - invalid: [ - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={(isOpen) => onToggle(isOpen)} />;`, - output: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={(_event, isOpen) => onToggle(isOpen)} />;`, - errors: [ - { - message: `The "onToggle" prop for ${component} has been updated so that the "_event" parameter is the first parameter. "onToggle" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={isOpen => onToggle(isOpen)} />;`, - output: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={(_event, isOpen) => onToggle(isOpen)} />;`, - errors: [ - { - message: `The "onToggle" prop for ${component} has been updated so that the "_event" parameter is the first parameter. "onToggle" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; const onToggle = (isOpen) => {}; <${component} onToggle={onToggle} />;`, - output: `import { ${component} } from '@patternfly/react-core'; const onToggle = (_event, isOpen) => {}; <${component} onToggle={onToggle} />;`, - errors: [ - { - message: `The "onToggle" prop for ${component} has been updated so that the "_event" parameter is the first parameter. "onToggle" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; function onToggle(isOpen) {}; <${component} onToggle={onToggle} />;`, - output: `import { ${component} } from '@patternfly/react-core'; function onToggle(_event, isOpen) {}; <${component} onToggle={onToggle} />;`, - errors: [ - { - message: `The "onToggle" prop for ${component} has been updated so that the "_event" parameter is the first parameter. "onToggle" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - })), - ...onToggleAPIUpdateList.map((component) => ({ - code: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={this.onToggle} />;`, - output: `import { ${component} } from '@patternfly/react-core'; <${component} onToggle={this.onToggle} />;`, - errors: [ - { - message: `The "onToggle" prop for ${component} has been updated so that the "_event" parameter is the first parameter. "onToggle" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - })), - ], -}); +addCallbackParamTester("onToggle-updated-paramaters", onToggleAPIUpdateList, 'onToggle'); diff --git a/packages/eslint-plugin-pf-codemods/test/testHelpers.js b/packages/eslint-plugin-pf-codemods/test/testHelpers.js new file mode 100644 index 000000000..0865097a7 --- /dev/null +++ b/packages/eslint-plugin-pf-codemods/test/testHelpers.js @@ -0,0 +1,115 @@ +const ruleTester = require("./ruletester"); + +function getAddCallbackParamMessage(componentName, propName, newParamName) { + return `The "${propName}" prop for ${componentName} has been updated to include the "${newParamName}" parameter as its first parameter. "${propName}" handlers may require an update.` +} + +function getValidAddCallbackParamTests(componentNameArray, propNameArray) { + let tests = []; + + componentNameArray.forEach((componentName) => { + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} />;`, + }); + propNameArray.forEach((propName) => { + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={() => handler()} />;`, + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; const handler = () => {}; <${componentName} ${propName}={handler} />;`, + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; function handler() {}; <${componentName} ${propName}={handler} />;`, + }); + tests.push({ code: `<${componentName} ${propName} />;` }); + }); + }); + return tests; +} + +function getInvalidAddCallbackParamTests(componentNameArray, propNameArray, newParamName, getCustomMessage) { + let tests = []; + + componentNameArray.forEach((componentName) => { + propNameArray.forEach((propName) => { + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={(id) => handler(id)} />;`, + output: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={(${newParamName}, id) => handler(id)} />;`, + errors: [ + { + message: getAddCallbackParamMessage(componentName, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={id => handler(id)} />;`, + output: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={(${newParamName}, id) => handler(id)} />;`, + errors: [ + { + message: getAddCallbackParamMessage(componentName, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; const handler = (id) => {}; <${componentName} ${propName}={handler} />;`, + output: `import { ${componentName} } from '@patternfly/react-core'; const handler = (${newParamName}, id) => {}; <${componentName} ${propName}={handler} />;`, + errors: [ + { + message: getAddCallbackParamMessage(componentName, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; function handler(id) {}; <${componentName} ${propName}={handler} />;`, + output: `import { ${componentName} } from '@patternfly/react-core'; function handler(${newParamName}, id) {}; <${componentName} ${propName}={handler} />;`, + errors: [ + { + message: getAddCallbackParamMessage(componentName, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + tests.push({ + code: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={this.handler} />;`, + output: `import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${propName}={this.handler} />;`, + errors: [ + { + message: getAddCallbackParamMessage(componentName, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + tests.push({ + code: `import { ${componentName} as PF${componentName} } from '@patternfly/react-core'; handler(id)} />;`, + output: `import { ${componentName} as PF${componentName} } from '@patternfly/react-core'; handler(id)} />;`, + errors: [ + { + message: getAddCallbackParamMessage(`PF${componentName}`, propName, newParamName), + type: "JSXOpeningElement", + }, + ], + }); + }); + }); + return tests; +} + +function addCallbackParamTester(ruleName, componentNames, propNames, newParamName = '_event') { + const rule = require(`../lib/rules/v5/${ruleName}`); + const componentNameArray = + typeof componentNames === "string" ? [componentNames] : componentNames; + const propNameArray = + typeof propNames === "string" ? [propNames] : propNames; + + ruleTester.run(ruleName, rule, { + valid: getValidAddCallbackParamTests(componentNameArray, propNameArray), + invalid: getInvalidAddCallbackParamTests(componentNameArray, propNameArray, newParamName), + }); +} + +module.exports = { + addCallbackParamTester +}; From 8de010536c762e499e85978f565a552974eeec41 Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Fri, 10 Mar 2023 13:05:29 -0500 Subject: [PATCH 2/2] chore(Helpers): updated test helper to match changes in rule helper --- .../v5/dataListCheck-updated-callback.js | 76 +------------------ .../test/testHelpers.js | 2 +- 2 files changed, 3 insertions(+), 75 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/test/rules/v5/dataListCheck-updated-callback.js b/packages/eslint-plugin-pf-codemods/test/rules/v5/dataListCheck-updated-callback.js index 5b54a83f5..bf5579bf5 100644 --- a/packages/eslint-plugin-pf-codemods/test/rules/v5/dataListCheck-updated-callback.js +++ b/packages/eslint-plugin-pf-codemods/test/rules/v5/dataListCheck-updated-callback.js @@ -1,75 +1,3 @@ -const ruleTester = require("../../ruletester"); -const rule = require("../../../lib/rules/v5/dataListCheck-updated-callback"); +const { addCallbackParamTester } = require("../../testHelpers"); -ruleTester.run("dataListCheck-updated-callback", rule, { - valid: [ - { - code: `import { DataListCheck } from '@patternfly/react-core'; ;`, - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; onChange()} />;`, - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; const onChange = () => {}; ;`, - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; function onChange() {}; ;`, - }, - { - // No @patternfly/react-core import - code: `;`, - }, - ], - invalid: [ - { - code: `import { DataListCheck } from '@patternfly/react-core'; onChange()} />;`, - output: `import { DataListCheck } from '@patternfly/react-core'; onChange()} />;`, - errors: [ - { - message: `The "onChange" prop for DataListCheck has been updated so that the "_event" parameter is the first parameter. "onChange" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; const onChange = (checked) => {}; ;`, - output: `import { DataListCheck } from '@patternfly/react-core'; const onChange = (_event, checked) => {}; ;`, - errors: [ - { - message: `The "onChange" prop for DataListCheck has been updated so that the "_event" parameter is the first parameter. "onChange" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; function onChange (checked) {}; ;`, - output: `import { DataListCheck } from '@patternfly/react-core'; function onChange (_event, checked) {}; ;`, - errors: [ - { - message: `The "onChange" prop for DataListCheck has been updated so that the "_event" parameter is the first parameter. "onChange" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataListCheck } from '@patternfly/react-core'; onChange()} />;`, - output: `import { DataListCheck } from '@patternfly/react-core'; onChange()} />;`, - errors: [ - { - message: `The "onChange" prop for DataListCheck has been updated so that the "_event" parameter is the first parameter. "onChange" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - { - code: `import { DataListCheck as PFdlc } from '@patternfly/react-core'; onChange()} />;`, - output: `import { DataListCheck as PFdlc } from '@patternfly/react-core'; onChange()} />;`, - errors: [ - { - message: `The "onChange" prop for PFdlc has been updated so that the "_event" parameter is the first parameter. "onChange" handlers may require an update.`, - type: "JSXOpeningElement", - }, - ], - }, - ], -}); +addCallbackParamTester('dataListCheck-updated-callback', 'DataListCheck', 'onChange') diff --git a/packages/eslint-plugin-pf-codemods/test/testHelpers.js b/packages/eslint-plugin-pf-codemods/test/testHelpers.js index 0865097a7..e0baa68ac 100644 --- a/packages/eslint-plugin-pf-codemods/test/testHelpers.js +++ b/packages/eslint-plugin-pf-codemods/test/testHelpers.js @@ -1,7 +1,7 @@ const ruleTester = require("./ruletester"); function getAddCallbackParamMessage(componentName, propName, newParamName) { - return `The "${propName}" prop for ${componentName} has been updated to include the "${newParamName}" parameter as its first parameter. "${propName}" handlers may require an update.` + return `The "${propName}" prop for ${componentName} has been updated so that the "${newParamName}" parameter is the first parameter. "${propName}" handlers may require an update.` } function getValidAddCallbackParamTests(componentNameArray, propNameArray) {