Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -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'; <DataList />;`,
},
{
code: `import { DataList } from '@patternfly/react-core'; <DataList onSelectDataListItem={() => onSelect()} />;`,
},
{
code: `import { DataList } from '@patternfly/react-core'; const onSelect = () => {}; <DataList onSelectDataListItem={onSelect} />;`,
},
{
code: `import { DataList } from '@patternfly/react-core'; function onSelect() {}; <DataList onSelectDataListItem={onSelect} />;`,
},
{
// No @patternfly/react-core import
code: `<DataList onSelectDataListItem />;`,
},
],
invalid: [
{
code: `import { DataList } from '@patternfly/react-core'; <DataList onSelectDataListItem={(id) => onSelect(id)} />;`,
output: `import { DataList } from '@patternfly/react-core'; <DataList onSelectDataListItem={(_event, id) => 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'; <DataList onSelectDataListItem={id => onSelect(id)} />;`,
output: `import { DataList } from '@patternfly/react-core'; <DataList onSelectDataListItem={(_event, id) => 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) => {}; <DataList onSelectDataListItem={onSelect} />;`,
output: `import { DataList } from '@patternfly/react-core'; const onSelect = (_event, id) => {}; <DataList onSelectDataListItem={onSelect} />;`,
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) {}; <DataList onSelectDataListItem={onSelect} />;`,
output: `import { DataList } from '@patternfly/react-core'; function onSelect(_event, id) {}; <DataList onSelectDataListItem={onSelect} />;`,
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'; <DataList onSelectDataListItem={this.onSelect} />;`,
output: `import { DataList } from '@patternfly/react-core'; <DataList onSelectDataListItem={this.onSelect} />;`,
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'; <PFDataList onSelectDataListItem={(id) => onSelect(id)} />;`,
output: `import { DataList as PFDataList } from '@patternfly/react-core'; <PFDataList onSelectDataListItem={(_event, id) => 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");
Original file line number Diff line number Diff line change
@@ -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'; <DataListCheck />;`,
},
{
code: `import { DataListCheck } from '@patternfly/react-core'; <DataListCheck onChange={() => onChange()} />;`,
},
{
code: `import { DataListCheck } from '@patternfly/react-core'; const onChange = () => {}; <DataListCheck onChange={onChange} />;`,
},
{
code: `import { DataListCheck } from '@patternfly/react-core'; function onChange() {}; <DataListCheck onChange={onChange} />;`,
},
{
// No @patternfly/react-core import
code: `<DataListCheck onChange />;`,
},
],
invalid: [
{
code: `import { DataListCheck } from '@patternfly/react-core'; <DataListCheck onChange={(checked) => onChange()} />;`,
output: `import { DataListCheck } from '@patternfly/react-core'; <DataListCheck onChange={(_event, checked) => 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) => {}; <DataListCheck onChange={onChange} />;`,
output: `import { DataListCheck } from '@patternfly/react-core'; const onChange = (_event, checked) => {}; <DataListCheck onChange={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'; function onChange (checked) {}; <DataListCheck onChange={onChange} />;`,
output: `import { DataListCheck } from '@patternfly/react-core'; function onChange (_event, checked) {}; <DataListCheck onChange={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'; <DataListCheck onChange={(checked, event) => onChange()} />;`,
output: `import { DataListCheck } from '@patternfly/react-core'; <DataListCheck onChange={(checked, event) => 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'; <PFdlc onChange={(checked) => onChange()} />;`,
output: `import { DataListCheck as PFdlc } from '@patternfly/react-core'; <PFdlc onChange={(_event, checked) => 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')
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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');
115 changes: 115 additions & 0 deletions packages/eslint-plugin-pf-codemods/test/testHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const ruleTester = require("./ruletester");

function getAddCallbackParamMessage(componentName, propName, newParamName) {
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) {
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'; <PF${componentName} ${propName}={(id) => handler(id)} />;`,
output: `import { ${componentName} as PF${componentName} } from '@patternfly/react-core'; <PF${componentName} ${propName}={(${newParamName}, id) => 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
};