Unverified Commit 422b3b01 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Only run toSource in next/font codemod if there are changes (#46877)

Currently the codemod always runs `root.toSource()`, recreating the
source from the AST. But this can sometimes add unexpected semicolons to
files that actually didn't get updated by the codemod. This PR adds a
check to see if the AST has been updated before running
`root.toSource()`, otherwise it just returns the original source.
parent 4b8240b8
export const unaffected = (string) => {
return string
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
.toLowerCase()
}
\ No newline at end of file
export const unaffected = (string) => {
return string
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
.toLowerCase()
}
\ No newline at end of file
...@@ -7,6 +7,7 @@ export default function transformer( ...@@ -7,6 +7,7 @@ export default function transformer(
) { ) {
const j = api.jscodeshift.withParser('tsx') const j = api.jscodeshift.withParser('tsx')
const root = j(file.source) const root = j(file.source)
let hasChanges = false
// Before: import { ... } from '@next/font' // Before: import { ... } from '@next/font'
// After: import { ... } from 'next/font' // After: import { ... } from 'next/font'
...@@ -15,6 +16,7 @@ export default function transformer( ...@@ -15,6 +16,7 @@ export default function transformer(
source: { value: '@next/font' }, source: { value: '@next/font' },
}) })
.forEach((fontImport) => { .forEach((fontImport) => {
hasChanges = true
fontImport.node.source = j.stringLiteral('next/font') fontImport.node.source = j.stringLiteral('next/font')
}) })
...@@ -25,6 +27,7 @@ export default function transformer( ...@@ -25,6 +27,7 @@ export default function transformer(
source: { value: '@next/font/google' }, source: { value: '@next/font/google' },
}) })
.forEach((fontImport) => { .forEach((fontImport) => {
hasChanges = true
fontImport.node.source = j.stringLiteral('next/font/google') fontImport.node.source = j.stringLiteral('next/font/google')
}) })
...@@ -35,8 +38,9 @@ export default function transformer( ...@@ -35,8 +38,9 @@ export default function transformer(
source: { value: '@next/font/local' }, source: { value: '@next/font/local' },
}) })
.forEach((fontImport) => { .forEach((fontImport) => {
hasChanges = true
fontImport.node.source = j.stringLiteral('next/font/local') fontImport.node.source = j.stringLiteral('next/font/local')
}) })
return root.toSource(options) return hasChanges ? root.toSource(options) : 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