Unverified Commit d3e576db authored by Steven's avatar Steven Committed by GitHub

fix: codemod should preserve type only import (#46512)

This PR is similar to PR #46460, but applies the patch to all our codemods.
parent 474b115e
// @ts-nocheck
/* eslint-disable */
import { Oswald } from "@next/font/google"; import { Oswald } from "@next/font/google";
import localFont1 from "@next/font/local"; import localFont1 from "@next/font/local";
import type { AdjustFontFallback } from "@next/font"
const oswald = Oswald({ subsets: ["latin"] }); const oswald = Oswald({ subsets: ["latin"] });
const myFont = localFont1({ const myFont = localFont1({
......
// @ts-nocheck
/* eslint-disable */
import { Oswald } from "next/font/google"; import { Oswald } from "next/font/google";
import localFont1 from "next/font/local"; import localFont1 from "next/font/local";
import type { AdjustFontFallback } from "next/font"
const oswald = Oswald({ subsets: ["latin"] }); const oswald = Oswald({ subsets: ["latin"] });
const myFont = localFont1({ const myFont = localFont1({
......
// @ts-nocheck
/* eslint-disable */
import Image from "next/legacy/image"; import Image from "next/legacy/image";
import Named, { Blarg } from "next/legacy/image"; import Named, { Blarg } from "next/legacy/image";
import type { ImageProps } from "next/legacy/image";
import { type ImageLoaderProps } from "next/legacy/image";
import Foo from "foo"; import Foo from "foo";
import img from "../public/img.jpg"; import img from "../public/img.jpg";
...@@ -30,4 +34,4 @@ export default function Home() { ...@@ -30,4 +34,4 @@ export default function Home() {
<Image src="/test.dot" width="200" height="300" layout="responsive" style={{...myStyle}} /> <Image src="/test.dot" width="200" height="300" layout="responsive" style={{...myStyle}} />
</div> </div>
); );
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
import Image from "next/image"; import Image from "next/image";
import Named, { Blarg } from "next/image"; import Named, { Blarg } from "next/image";
import type { ImageProps } from "next/image";
import { type ImageLoaderProps } from "next/image";
import Foo from "foo"; import Foo from "foo";
import img from "../public/img.jpg"; import img from "../public/img.jpg";
...@@ -99,4 +103,4 @@ export default function Home() { ...@@ -99,4 +103,4 @@ export default function Home() {
}} /> }} />
</div> </div>
); );
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const Image = require("next/legacy/image"); const Image = require("next/legacy/image");
const Named = require("next/legacy/image"); const Named = require("next/legacy/image");
const Foo = require("foo"); const Foo = require("foo");
...@@ -11,4 +13,4 @@ export default function Home() { ...@@ -11,4 +13,4 @@ export default function Home() {
<Foo /> <Foo />
</div> </div>
); );
} }
\ No newline at end of file
// @ts-nocheck
/* eslint-disable */
const Image = require("next/image"); const Image = require("next/image");
const Named = require("next/image"); const Named = require("next/image");
const Foo = require("foo"); const Foo = require("foo");
...@@ -11,4 +13,4 @@ export default function Home() { ...@@ -11,4 +13,4 @@ export default function Home() {
<Foo /> <Foo />
</div> </div>
); );
} }
\ No newline at end of file
...@@ -7,6 +7,8 @@ import { type ImageLoaderProps } from "next/image"; ...@@ -7,6 +7,8 @@ import { type ImageLoaderProps } from "next/image";
import Foo from "foo"; import Foo from "foo";
import Future1 from "next/future/image"; import Future1 from "next/future/image";
import Future2 from "next/future/image"; import Future2 from "next/future/image";
import type { ImageProps as ImageFutureProps } from "next/future/image";
import { type ImageLoaderProps as ImageFutureLoaderProps } from "next/future/image";
export default function Home() { export default function Home() {
return (<div> return (<div>
......
...@@ -7,6 +7,8 @@ import { type ImageLoaderProps } from "next/legacy/image"; ...@@ -7,6 +7,8 @@ import { type ImageLoaderProps } from "next/legacy/image";
import Foo from "foo"; import Foo from "foo";
import Future1 from "next/image"; import Future1 from "next/image";
import Future2 from "next/image"; import Future2 from "next/image";
import type { ImageProps as ImageFutureProps } from "next/image";
import { type ImageLoaderProps as ImageFutureLoaderProps } from "next/image";
export default function Home() { export default function Home() {
return (<div> return (<div>
......
...@@ -7,10 +7,10 @@ const { join } = require('path') ...@@ -7,10 +7,10 @@ const { join } = require('path')
const fixtureDir = 'built-in-next-font' const fixtureDir = 'built-in-next-font'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath) const fixtures = readdirSync(fixtureDirPath)
.filter(file => file.endsWith('.input.js')) .filter(file => file.endsWith('.input.tsx'))
.map(file => file.replace('.input.js', '')) .map(file => file.replace('.input.tsx', ''))
for (const fixture of fixtures) { for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`; const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix) defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' });
} }
...@@ -7,10 +7,10 @@ const { join } = require('path') ...@@ -7,10 +7,10 @@ const { join } = require('path')
const fixtureDir = 'next-image-experimental' const fixtureDir = 'next-image-experimental'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir) const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath) const fixtures = readdirSync(fixtureDirPath)
.filter(file => file.endsWith('.input.js')) .filter(file => file.endsWith('.input.tsx'))
.map(file => file.replace('.input.js', '')) .map(file => file.replace('.input.tsx', ''))
for (const fixture of fixtures) { for (const fixture of fixtures) {
const prefix = `${fixtureDir}/${fixture}`; const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix) defineTest(__dirname, fixtureDir, null, prefix, { parser: 'tsx' })
} }
...@@ -8,19 +8,24 @@ export default function transformer( ...@@ -8,19 +8,24 @@ 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)
// Before: import { ... } from '@next/font'
// After: import { ... } from 'next/font'
root
.find(j.ImportDeclaration, {
source: { value: '@next/font' },
})
.forEach((fontImport) => {
fontImport.node.source = j.stringLiteral('next/font')
})
// Before: import { ... } from '@next/font/google' // Before: import { ... } from '@next/font/google'
// After: import { ... } from 'next/font/google' // After: import { ... } from 'next/font/google'
root root
.find(j.ImportDeclaration, { .find(j.ImportDeclaration, {
source: { value: '@next/font/google' }, source: { value: '@next/font/google' },
}) })
.forEach((imageImport) => { .forEach((fontImport) => {
j(imageImport).replaceWith( fontImport.node.source = j.stringLiteral('next/font/google')
j.importDeclaration(
imageImport.node.specifiers,
j.stringLiteral('next/font/google')
)
)
}) })
// Before: import localFont from '@next/font/local' // Before: import localFont from '@next/font/local'
...@@ -29,13 +34,8 @@ export default function transformer( ...@@ -29,13 +34,8 @@ export default function transformer(
.find(j.ImportDeclaration, { .find(j.ImportDeclaration, {
source: { value: '@next/font/local' }, source: { value: '@next/font/local' },
}) })
.forEach((imageImport) => { .forEach((fontImport) => {
j(imageImport).replaceWith( fontImport.node.source = j.stringLiteral('next/font/local')
j.importDeclaration(
imageImport.node.specifiers,
j.stringLiteral('next/font/local')
)
)
}) })
return root.toSource(options) return root.toSource(options)
......
...@@ -284,14 +284,8 @@ export default function transformer( ...@@ -284,14 +284,8 @@ export default function transformer(
(node) => node.type === 'ImportDefaultSpecifier' (node) => node.type === 'ImportDefaultSpecifier'
) as ImportDefaultSpecifier | undefined ) as ImportDefaultSpecifier | undefined
const tagName = defaultSpecifier?.local?.name const tagName = defaultSpecifier?.local?.name
imageImport.node.source = j.stringLiteral('next/image')
if (tagName) { if (tagName) {
j(imageImport).replaceWith(
j.importDeclaration(
imageImport.node.specifiers,
j.stringLiteral('next/image')
)
)
findAndReplaceProps(j, root, tagName) findAndReplaceProps(j, root, tagName)
} }
}) })
......
...@@ -38,12 +38,7 @@ export default function transformer( ...@@ -38,12 +38,7 @@ export default function transformer(
source: { value: 'next/future/image' }, source: { value: 'next/future/image' },
}) })
.forEach((imageFutureImport) => { .forEach((imageFutureImport) => {
j(imageFutureImport).replaceWith( imageFutureImport.node.source = j.stringLiteral('next/image')
j.importDeclaration(
imageFutureImport.node.specifiers,
j.stringLiteral('next/image')
)
)
}) })
// Before: const Image = await import("next/future/image") // Before: const Image = await import("next/future/image")
......
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