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

Handle styled-jsx in newLinkBehavior codemod #36628

Merged
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,26 @@
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;
@@ -0,0 +1,26 @@
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;
Expand Up @@ -9,7 +9,8 @@ const fixtures = [
'excludes-links-with-legacybehavior-prop',
'children-interpolation',
'spread-props',
'link-string'
'link-string',
'styled-jsx',
]

for (const fixture of fixtures) {
Expand Down
16 changes: 16 additions & 0 deletions packages/next-codemod/transforms/new-link.ts
Expand Up @@ -22,6 +22,13 @@ export default function transformer(file: FileInfo, api: API) {
}

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) => {
const $link = j(linkPath).filter((childPath) => {
Expand All @@ -37,6 +44,15 @@ export default function transformer(file: FileInfo, api: API) {
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
Expand Down