Unverified Commit 20e5fed1 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle styled-jsx in newLinkBehavior codemod (#36628)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 9e53af8e
import Link from 'next/link';
const CustomLink = ({
href,
title,
children,
}) => {
return (
<span className="link-container">
<Link href={href}>
<a className="link" title={title}>
{children}
</a>
</Link>
<style jsx>{`
.link {
text-decoration: none;
color: var(--geist-foreground);
font-weight: 500;
}
`}</style>
</span>
);
};
export default CustomLink;
\ No newline at end of file
import Link from 'next/link';
const CustomLink = ({
href,
title,
children,
}) => {
return (
<span className="link-container">
<Link href={href} legacyBehavior>
<a className="link" title={title}>
{children}
</a>
</Link>
<style jsx>{`
.link {
text-decoration: none;
color: var(--geist-foreground);
font-weight: 500;
}
`}</style>
</span>
);
};
export default CustomLink;
\ No newline at end of file
...@@ -9,7 +9,8 @@ const fixtures = [ ...@@ -9,7 +9,8 @@ const fixtures = [
'excludes-links-with-legacybehavior-prop', 'excludes-links-with-legacybehavior-prop',
'children-interpolation', 'children-interpolation',
'spread-props', 'spread-props',
'link-string' 'link-string',
'styled-jsx',
] ]
for (const fixture of fixtures) { for (const fixture of fixtures) {
......
...@@ -22,6 +22,13 @@ export default function transformer(file: FileInfo, api: API) { ...@@ -22,6 +22,13 @@ export default function transformer(file: FileInfo, api: API) {
} }
const linkElements = $j.findJSXElements(variableName) 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
})
linkElements.forEach((linkPath) => { linkElements.forEach((linkPath) => {
const $link = j(linkPath).filter((childPath) => { const $link = j(linkPath).filter((childPath) => {
...@@ -37,6 +44,15 @@ export default function transformer(file: FileInfo, api: API) { ...@@ -37,6 +44,15 @@ export default function transformer(file: FileInfo, api: API) {
return 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') const linkChildrenNodes = $link.get('children')
// Text-only link children are already correct with the new behavior // Text-only link children are already correct with the new behavior
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment