Skip to content

Commit 20e5fed

Browse files
Handle styled-jsx in newLinkBehavior codemod (#36628)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 9e53af8 commit 20e5fed

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Link from 'next/link';
2+
3+
const CustomLink = ({
4+
href,
5+
title,
6+
children,
7+
}) => {
8+
return (
9+
<span className="link-container">
10+
<Link href={href}>
11+
<a className="link" title={title}>
12+
{children}
13+
</a>
14+
</Link>
15+
<style jsx>{`
16+
.link {
17+
text-decoration: none;
18+
color: var(--geist-foreground);
19+
font-weight: 500;
20+
}
21+
`}</style>
22+
</span>
23+
);
24+
};
25+
26+
export default CustomLink;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Link from 'next/link';
2+
3+
const CustomLink = ({
4+
href,
5+
title,
6+
children,
7+
}) => {
8+
return (
9+
<span className="link-container">
10+
<Link href={href} legacyBehavior>
11+
<a className="link" title={title}>
12+
{children}
13+
</a>
14+
</Link>
15+
<style jsx>{`
16+
.link {
17+
text-decoration: none;
18+
color: var(--geist-foreground);
19+
font-weight: 500;
20+
}
21+
`}</style>
22+
</span>
23+
);
24+
};
25+
26+
export default CustomLink;

packages/next-codemod/transforms/__tests__/new-link.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const fixtures = [
99
'excludes-links-with-legacybehavior-prop',
1010
'children-interpolation',
1111
'spread-props',
12-
'link-string'
12+
'link-string',
13+
'styled-jsx',
1314
]
1415

1516
for (const fixture of fixtures) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export default function transformer(file: FileInfo, api: API) {
2222
}
2323

2424
const linkElements = $j.findJSXElements(variableName)
25+
const hasStylesJSX = $j.findJSXElements('style').some((stylePath) => {
26+
const $style = j(stylePath)
27+
const hasJSXProp =
28+
$style.find(j.JSXAttribute, { name: { name: 'jsx' } }).size() !== 0
29+
30+
return hasJSXProp
31+
})
2532

2633
linkElements.forEach((linkPath) => {
2734
const $link = j(linkPath).filter((childPath) => {
@@ -37,6 +44,15 @@ export default function transformer(file: FileInfo, api: API) {
3744
return
3845
}
3946

47+
// If file has <style jsx> enable legacyBehavior
48+
// and keep <a> to stay on the safe side
49+
if (hasStylesJSX) {
50+
$link
51+
.get('attributes')
52+
.push(j.jsxAttribute(j.jsxIdentifier('legacyBehavior')))
53+
return
54+
}
55+
4056
const linkChildrenNodes = $link.get('children')
4157

4258
// Text-only link children are already correct with the new behavior

0 commit comments

Comments
 (0)