Skip to content

Commit ef81a41

Browse files
committed
fix/add tests for hasAnimations after cli split
Signed-off-by: gitdallas <[email protected]>
1 parent 132e660 commit ef81a41

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

packages/eslint-plugin-pf-codemods/src/rules/v6/enableAnimations/enable-animations.test.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ const valid = [
1818
];
1919

2020
const invalid = [
21+
// --- Without includeTable (default) ---
22+
{
23+
code: "import { Table } from '@patternfly/react-table'; <Table />",
24+
output: "import { Table } from '@patternfly/react-table'; <Table />",
25+
errors: [
26+
{
27+
message: "Consider adding hasAnimations prop to enable component animations.",
28+
type: "JSXOpeningElement"
29+
}
30+
]
31+
},
32+
// --- With includeTable: true ---
33+
{
34+
code: "import { Table } from '@patternfly/react-table'; <Table />",
35+
output: "import { Table } from '@patternfly/react-table'; <Table hasAnimations />",
36+
errors: [
37+
{
38+
message: "Consider adding hasAnimations prop to enable component animations.",
39+
type: "JSXOpeningElement"
40+
}
41+
],
42+
options: [{ includeTable: true }]
43+
},
44+
// --- Core components (unchanged) ---
2145
{
2246
code: "import { AlertGroup } from '@patternfly/react-core'; <AlertGroup />",
2347
output: "import { AlertGroup } from '@patternfly/react-core'; <AlertGroup hasAnimations />",
@@ -78,27 +102,11 @@ const invalid = [
78102
}
79103
]
80104
},
81-
{
82-
code: "import { Table } from '@patternfly/react-table'; <Table />",
83-
output: "import { Table } from '@patternfly/react-table'; <Table hasAnimations />",
84-
errors: [
85-
{
86-
message: "Consider adding hasAnimations prop to enable component animations.",
87-
type: "JSXOpeningElement"
88-
}
89-
]
90-
},
91105
{
92106
code: `import { AlertGroup, TreeView } from '@patternfly/react-core';
93-
<>
94-
<AlertGroup />
95-
<TreeView />
96-
</>`,
107+
<><AlertGroup /><TreeView /></>`,
97108
output: `import { AlertGroup, TreeView } from '@patternfly/react-core';
98-
<>
99-
<AlertGroup hasAnimations />
100-
<TreeView hasAnimations />
101-
</>`,
109+
<><AlertGroup hasAnimations /><TreeView hasAnimations /></>`,
102110
errors: [
103111
{
104112
message: "Consider adding hasAnimations prop to enable component animations.",

packages/eslint-plugin-pf-codemods/src/rules/v6/enableAnimations/enable-animations.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Rule } from "eslint";
22
import { JSXOpeningElement, JSXAttribute } from "estree-jsx";
3-
import { getFromPackage, checkMatchingJSXOpeningElement } from "../../helpers";
3+
import { getFromPackage, checkMatchingJSXOpeningElement, getNodeName } from "../../helpers";
44
import { getAttribute, getAttributeValue } from "../../helpers/JSXAttributes";
55

66
// Rule to add hasAnimations prop to components that support animations
@@ -40,12 +40,10 @@ module.exports = {
4040
targetComponents.includes(specifier.imported.name)
4141
);
4242

43-
// Only include Table if option is set
44-
const targetTableImports = includeTable
45-
? tableImports.filter((specifier) =>
46-
tableComponents.includes(specifier.imported.name)
47-
)
48-
: [];
43+
// Always include Table imports for detection, but only add hasAnimations if includeTable is true
44+
const targetTableImports = tableImports.filter((specifier) =>
45+
tableComponents.includes(specifier.imported.name)
46+
);
4947

5048
const allTargetImports = [...targetCoreImports, ...targetTableImports];
5149

@@ -77,20 +75,14 @@ module.exports = {
7775
return true;
7876
}
7977

80-
// Helper function to get component name from node
81-
function getComponentName(node: JSXOpeningElement): string | null {
82-
if (node.name.type === "JSXIdentifier") {
83-
return node.name.name;
84-
}
85-
return null;
86-
}
78+
8779

8880
return allTargetImports.length === 0
8981
? {}
9082
: {
9183
JSXOpeningElement(node: JSXOpeningElement) {
9284
if (checkMatchingJSXOpeningElement(node, allTargetImports)) {
93-
const componentName = getComponentName(node);
85+
const componentName = getNodeName(node);
9486

9587
// Special handling for DualListSelector - only add hasAnimations if isTree is true
9688
if (componentName === "DualListSelector" && !hasValidIsTreeProp(node)) {
@@ -107,10 +99,13 @@ module.exports = {
10799

108100
// Only add prop if it doesn't already exist
109101
if (!hasAnimationsAttribute) {
102+
// For Table, only apply the fix if includeTable is true
103+
const shouldApplyFix = componentName !== "Table" || includeTable;
104+
110105
context.report({
111106
node,
112107
message,
113-
fix(fixer) {
108+
fix: shouldApplyFix ? (fixer) => {
114109
// Insert hasAnimations at the end of existing attributes
115110
if (node.attributes.length > 0) {
116111
const lastAttribute = node.attributes[node.attributes.length - 1];
@@ -119,7 +114,7 @@ module.exports = {
119114
// No existing attributes, insert after component name
120115
return fixer.insertTextAfter(node.name, " hasAnimations");
121116
}
122-
},
117+
} : undefined,
123118
});
124119
}
125120
}

0 commit comments

Comments
 (0)