rsnext/packages/next-codemod/transforms/new-link.ts

113 lines
3.3 KiB
TypeScript
Raw Normal View History

Rework <Link> behavior (backwards compatible) (#36436) Fixes https://github.com/vercel/next.js/discussions/32233 :warning: If you're looking at this PR please read the complete description including the part about incremental adoption. ## TLDR: Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled. ## Full context ### Changes to `<Link>` - Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default - When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element - When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>` ### Incremental Adoption / Codemod The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning. This codemod will auto-fix the usage where possible. For example: - When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>` - When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>` - For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop. **This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.** Some code samples of what changed: ```jsx const CustomComponent = () => <strong>Hello</strong> // Legacy behavior: `<a>` has to be nested otherwise it's excluded // Renders: <a href="/about">About</a>. `<a>` has to be nested. <Link href="/about"> <a>About</a> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> // -------------------------------------------------- // New behavior: `<Link>` always renders `<a>` // Renders: <a href="/about">About</a>. `<a>` no longer has to be nested. <Link href="/about"> About </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> ``` --- ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-26 00:01:30 +02:00
import { API, FileInfo } from 'jscodeshift'
export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift.withParser('tsx')
Rework <Link> behavior (backwards compatible) (#36436) Fixes https://github.com/vercel/next.js/discussions/32233 :warning: If you're looking at this PR please read the complete description including the part about incremental adoption. ## TLDR: Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled. ## Full context ### Changes to `<Link>` - Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default - When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element - When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>` ### Incremental Adoption / Codemod The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning. This codemod will auto-fix the usage where possible. For example: - When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>` - When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>` - For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop. **This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.** Some code samples of what changed: ```jsx const CustomComponent = () => <strong>Hello</strong> // Legacy behavior: `<a>` has to be nested otherwise it's excluded // Renders: <a href="/about">About</a>. `<a>` has to be nested. <Link href="/about"> <a>About</a> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> // -------------------------------------------------- // New behavior: `<Link>` always renders `<a>` // Renders: <a href="/about">About</a>. `<a>` no longer has to be nested. <Link href="/about"> About </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> ``` --- ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-26 00:01:30 +02:00
const $j = j(file.source)
return $j
.find(j.ImportDeclaration, { source: { value: 'next/link' } })
.forEach((path) => {
const defaultImport = j(path).find(j.ImportDefaultSpecifier)
if (defaultImport.size() === 0) {
return
}
const variableName = j(path)
.find(j.ImportDefaultSpecifier)
.find(j.Identifier)
.get('name').value
if (!variableName) {
return
}
const linkElements = $j.findJSXElements(variableName)
const hasStylesJSX = $j.findJSXElements('style').some((stylePath) => {
const $style = j(stylePath)
const hasJSXProp =
$style.find(j.JSXAttribute, { name: { name: 'jsx' } }).size() !== 0
return hasJSXProp
})
Rework <Link> behavior (backwards compatible) (#36436) Fixes https://github.com/vercel/next.js/discussions/32233 :warning: If you're looking at this PR please read the complete description including the part about incremental adoption. ## TLDR: Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled. ## Full context ### Changes to `<Link>` - Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default - When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element - When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>` ### Incremental Adoption / Codemod The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning. This codemod will auto-fix the usage where possible. For example: - When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>` - When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>` - For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop. **This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.** Some code samples of what changed: ```jsx const CustomComponent = () => <strong>Hello</strong> // Legacy behavior: `<a>` has to be nested otherwise it's excluded // Renders: <a href="/about">About</a>. `<a>` has to be nested. <Link href="/about"> <a>About</a> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> // -------------------------------------------------- // New behavior: `<Link>` always renders `<a>` // Renders: <a href="/about">About</a>. `<a>` no longer has to be nested. <Link href="/about"> About </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> ``` --- ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-26 00:01:30 +02:00
linkElements.forEach((linkPath) => {
const $link = j(linkPath).filter((childPath) => {
// Exclude links with `legacybehavior` prop from modification
return (
j(childPath)
.find(j.JSXAttribute, { name: { name: 'legacyBehavior' } })
.size() === 0
)
})
if ($link.size() === 0) {
return
}
// If file has <style jsx> enable legacyBehavior
// and keep <a> to stay on the safe side
if (hasStylesJSX) {
$link
.get('attributes')
.push(j.jsxAttribute(j.jsxIdentifier('legacyBehavior')))
return
}
const linkChildrenNodes = $link.get('children')
// Text-only link children are already correct with the new behavior
// `next/link` would previously auto-wrap typeof 'string' children already
if (
linkChildrenNodes.value &&
linkChildrenNodes.value.length === 1 &&
linkChildrenNodes.value[0].type === 'JSXText'
) {
return
}
Rework <Link> behavior (backwards compatible) (#36436) Fixes https://github.com/vercel/next.js/discussions/32233 :warning: If you're looking at this PR please read the complete description including the part about incremental adoption. ## TLDR: Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled. ## Full context ### Changes to `<Link>` - Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default - When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element - When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>` ### Incremental Adoption / Codemod The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning. This codemod will auto-fix the usage where possible. For example: - When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>` - When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>` - For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop. **This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.** Some code samples of what changed: ```jsx const CustomComponent = () => <strong>Hello</strong> // Legacy behavior: `<a>` has to be nested otherwise it's excluded // Renders: <a href="/about">About</a>. `<a>` has to be nested. <Link href="/about"> <a>About</a> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> // -------------------------------------------------- // New behavior: `<Link>` always renders `<a>` // Renders: <a href="/about">About</a>. `<a>` no longer has to be nested. <Link href="/about"> About </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> ``` --- ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-26 00:01:30 +02:00
// Direct child elements referenced
const $childrenElements = $link.childElements()
const $childrenWithA = $childrenElements.filter((childPath) => {
return (
j(childPath).find(j.JSXOpeningElement).get('name').get('name')
.value === 'a'
)
})
// No <a> as child to <Link> so the old behavior is used
if ($childrenWithA.size() !== 1) {
$link
.get('attributes')
.push(j.jsxAttribute(j.jsxIdentifier('legacyBehavior')))
return
}
const props = $childrenWithA.get('attributes').value
const hasProps = props.length > 0
if (hasProps) {
// 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)
Rework <Link> behavior (backwards compatible) (#36436) Fixes https://github.com/vercel/next.js/discussions/32233 :warning: If you're looking at this PR please read the complete description including the part about incremental adoption. ## TLDR: Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled. ## Full context ### Changes to `<Link>` - Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default - When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element - When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>` ### Incremental Adoption / Codemod The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning. This codemod will auto-fix the usage where possible. For example: - When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>` - When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>` - For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop. **This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.** Some code samples of what changed: ```jsx const CustomComponent = () => <strong>Hello</strong> // Legacy behavior: `<a>` has to be nested otherwise it's excluded // Renders: <a href="/about">About</a>. `<a>` has to be nested. <Link href="/about"> <a>About</a> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> // -------------------------------------------------- // New behavior: `<Link>` always renders `<a>` // Renders: <a href="/about">About</a>. `<a>` no longer has to be nested. <Link href="/about"> About </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <strong>Hello</strong> </Link> // Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included. <Link href="/about"> <CustomComponent /> </Link> ``` --- ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-26 00:01:30 +02:00
// Remove props from <a>
props.length = 0
}
const childrenProps = $childrenWithA.get('children')
$childrenWithA.replaceWith(childrenProps.value)
})
})
.toSource()
}