Unverified Commit 070ae8a7 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[codemod] Only change files when `new-link` transform makes changes (#83112)

parent d574c6a6
...@@ -9,10 +9,10 @@ export default function transformer(file: FileInfo, _api: API) { ...@@ -9,10 +9,10 @@ export default function transformer(file: FileInfo, _api: API) {
const j = createParserFromPath(file.path) const j = createParserFromPath(file.path)
const $j = j(file.source) const $j = j(file.source)
let hasChanges = false
return $j $j.find(j.ImportDeclaration, { source: { value: 'next/link' } }).forEach(
.find(j.ImportDeclaration, { source: { value: 'next/link' } }) (path) => {
.forEach((path) => {
const defaultImport = j(path).find(j.ImportDefaultSpecifier) const defaultImport = j(path).find(j.ImportDefaultSpecifier)
if (defaultImport.size() === 0) { if (defaultImport.size() === 0) {
return return
...@@ -35,17 +35,17 @@ export default function transformer(file: FileInfo, _api: API) { ...@@ -35,17 +35,17 @@ export default function transformer(file: FileInfo, _api: API) {
return return
} }
const $legacyBehaviorProp = $link const $legacyBehaviorProps = $link.find(j.JSXAttribute, {
.find(j.JSXAttribute, { name: { type: 'JSXIdentifier', name: 'legacyBehavior' },
name: { type: 'JSXIdentifier', name: 'legacyBehavior' }, })
}) $legacyBehaviorProps.remove()
.remove() hasChanges ||= $legacyBehaviorProps.size() > 0
$link const $passHrefProps = $link.find(j.JSXAttribute, {
.find(j.JSXAttribute, { name: { type: 'JSXIdentifier', name: 'passHref' },
name: { type: 'JSXIdentifier', name: 'passHref' }, })
}) $passHrefProps.remove()
.remove() hasChanges ||= $passHrefProps.size() > 0
const linkChildrenNodes = $link.get('children') const linkChildrenNodes = $link.get('children')
...@@ -69,7 +69,7 @@ export default function transformer(file: FileInfo, _api: API) { ...@@ -69,7 +69,7 @@ export default function transformer(file: FileInfo, _api: API) {
}) })
if ($childrenWithA.length === 0) { if ($childrenWithA.length === 0) {
if ($legacyBehaviorProp.length > 0) { if ($legacyBehaviorProps.length > 0) {
linkPath.node.children.unshift( linkPath.node.children.unshift(
j.jsxText('\n'), j.jsxText('\n'),
j.jsxExpressionContainer.from({ j.jsxExpressionContainer.from({
...@@ -82,6 +82,7 @@ export default function transformer(file: FileInfo, _api: API) { ...@@ -82,6 +82,7 @@ export default function transformer(file: FileInfo, _api: API) {
}), }),
}) })
) )
hasChanges = true
} }
} else { } else {
const props = $childrenWithA.get('attributes').value const props = $childrenWithA.get('attributes').value
...@@ -104,12 +105,19 @@ export default function transformer(file: FileInfo, _api: API) { ...@@ -104,12 +105,19 @@ export default function transformer(file: FileInfo, _api: API) {
// Remove props from <a> // Remove props from <a>
props.length = 0 props.length = 0
hasChanges = true
} }
const childrenProps = $childrenWithA.get('children') const childrenProps = $childrenWithA.get('children')
$childrenWithA.replaceWith(childrenProps.value) $childrenWithA.replaceWith(childrenProps.value)
hasChanges = true
} }
}) })
}) }
.toSource() )
if (hasChanges) {
return $j.toSource()
}
return file.source
} }
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