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