Skip to content

Commit

Permalink
issue-41925 fix: skip duplicate props when transferring props from an…
Browse files Browse the repository at this point in the history
…chor to link (#42158)

Fixes issue #41925 by skipping duplicate props. I've tested the changes
locally - the duplicate prop is being skipped successfully. The
new-link.test.js suite is also passing.

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: Ishaq Ibrahim <ishaq.ibrahim@convertdigital.com.au>
  • Loading branch information
ishaqibrahimbot and Ishaq Ibrahim committed Oct 30, 2022
1 parent ab42da0 commit 29c5acd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
@@ -0,0 +1,9 @@
import Link from 'next/link'

export default function Page() {
return (
<Link href="/about">
<a href="/about" className="some-class">Link</a>
</Link>
);
}
@@ -0,0 +1,9 @@
import Link from 'next/link'

export default function Page() {
return (
<Link href="/about" className="some-class">
Link
</Link>
);
}
Expand Up @@ -11,6 +11,7 @@ const fixtures = [
'spread-props',
'link-string',
'styled-jsx',
'handle-duplicate-props'
]

for (const fixture of fixtures) {
Expand Down
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)
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

0 comments on commit 29c5acd

Please sign in to comment.