Skip to content
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

chore(useClickableProps): Codemod for changing dataTestId to data-testid #2406

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,115 @@
import transform from "../use-clickable-props-hook-migration";
import { defineInlineTest } from "jscodeshift/src/testUtils";

function prependImport(source: string): string {
return `
import { useClickableProps } from "monday-ui-react-core";
${source}
`;
}

describe("useClickableProps prop migration", () => {
defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
onClick: onClickCallback,
onMouseDown,
disabled,
id,
dataTestId: componentDataTestId,
ariaLabel: overrideAriaLabel,
ariaHidden: false,
ariaHasPopup: false,
ariaExpanded: false
});
`),
prependImport(`
useClickableProps({
onClick: onClickCallback,
onMouseDown,
disabled,
id,
"data-testid": componentDataTestId,
ariaLabel: overrideAriaLabel,
ariaHidden: false,
ariaHasPopup: false,
ariaExpanded: false
});
`),
"should rename 'dataTestId' to 'data-testid'"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
"data-testid": componentDataTestId,
onClick: onClickCallback
});
`),
"should rename 'dataTestId' to 'data-testid' when it's the first property"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
"data-testid": componentDataTestId,
onClick: onClickCallback
});
`),
"should rename 'dataTestId' to 'data-testid' when it's the first property"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
onClick: onClickCallback
});
`),
"should not change anything when 'dataTestId' is not present"
);

defineInlineTest(
transform,
{},
`
import { useClickableProps } from "other-library";
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`,
`
import { useClickableProps } from "other-library";
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`,
"should not rename 'dataTestId' to 'data-testid' when not a vibe component"
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getComponentNameOrAliasFromImports, getCoreImportsForFile, wrap } from "../../../src/utils";
import { TransformationContext } from "../../../types";
import { Property } from "jscodeshift";

/**
* 1. Update the 'dataTestId' prop to 'data-testid'
*/
function transform({ j, root }: TransformationContext) {
const imports = getCoreImportsForFile(root);
const componentName = getComponentNameOrAliasFromImports(j, imports, "useClickableProps");
if (!componentName) return;
rivka-ungar marked this conversation as resolved.
Show resolved Hide resolved

root
.find(j.CallExpression, {
callee: {
type: "Identifier",
name: "useClickableProps"
}
})
.forEach(path => {
Copy link
Contributor

@YossiSaadi YossiSaadi Sep 1, 2024

Choose a reason for hiding this comment

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

would using JSCodeshift's findVariableDeclarators(hookName) help here?
and then you can maybe find the ObjectExpression, with Property of key of { type: 'Identifier' and name: 'dataTestId }

something like:

findVariableDeclarators(hookName)
  .find(ObjectExpression)
  .find(Property, {
    key: {
      type: 'Identifier',
      name: 'dataTestId',
    }
  })

this can save you all the code from line 21 to 28 I believe
the rest can be changed with iterating over the Collection been made with forEach on each path.
something like:

path.node.key = j.literal('data-testid');

those two can turn into a generic util for changing a Callee argument name

const args = path.node.arguments;

if (args.length > 0 && args[0].type === "ObjectExpression") {
const objectExpression = args[0];

objectExpression.properties.forEach(property => {
const prop = property as Property;
if (prop.key?.type === "Identifier" && prop.key?.name === "dataTestId") {
prop.key = j.literal("data-testid");
}
});
}
});
}

export default wrap(transform);
2 changes: 1 addition & 1 deletion packages/core/docs/vibe-3-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ codemod: `color-picker-content-import-migration`

### useClickableProps

- `dataTestId` -> `data-testid` [codemod]
- `dataTestId` -> `data-testid` [codemod]

## monday-ui-style

Expand Down