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

issue-41925 fix: skip duplicate props when transferring props from anchor to link #42158

Merged
merged 2 commits into from Oct 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/next-codemod/transforms/new-link.ts
Expand Up @@ -86,13 +86,24 @@ export default function transformer(file: FileInfo, api: API) {
const hasProps = props.length > 0

if (hasProps) {
// Add props to <Link>
$link.get('attributes').value.push(...props)
// Add only unique props to <Link> (skip duplicate props)

Choose a reason for hiding this comment

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

I wonder if we can move this to some sort of a global helper somewhere? Seems like a nice common pattern we might want to share––just a thought. Thanks for the addition and solving my issue #41925 !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'll take a look at where it might be possible to add this as a global helper/util function. Thanks for the feedback!

Choose a reason for hiding this comment

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

Aw, missed our chance to merge maybe. @styfle think it's worth sharing this logic around the other codemods? Think we should be concerned we'll see this again with other mods?

Great stuff either way, thanks for addressing 💯

Copy link
Member

Choose a reason for hiding this comment

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

@andrewmartin Which other codemods would share it?

Copy link

Choose a reason for hiding this comment

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

@styfle I'm just speculating, and I am being admittedly only inquisitive (not very thorough), but I did see this:

So, wondering if, in theory, someone had put a style attribute on an existing JSX element here, would it be duplicated? Sorry for not testing myself, just throwing the idea out there first.

EDIT sorry here's a better reference:

Choose a reason for hiding this comment

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

Furthermore, as more codemods will be added, it'd be nice to have this logic compartmentalized. I'd imagine that any new codemod that modifies an existing element's props would want to have similar logic and avoid duping.

const linkPropNames = $link
.get('attributes')
.value.map((linkProp) => linkProp?.name?.name)
const uniqueProps = []

props.forEach((anchorProp) => {
if (!linkPropNames.includes(anchorProp?.name?.name)) {
uniqueProps.push(anchorProp)
}
})

$link.get('attributes').value.push(...uniqueProps)

// Remove props from <a>
props.length = 0
}

//
const childrenProps = $childrenWithA.get('children')
$childrenWithA.replaceWith(childrenProps.value)
})
Expand Down