MCPcopy
hub / github.com/vercel/next.js / transformer

Function transformer

packages/next-codemod/transforms/next-image-experimental.ts:258–328  ·  view source on GitHub ↗
(
  file: FileInfo,
  _api: API,
  options: Options
)

Source from the content-addressed store, hash-verified

256}
257
258export default function transformer(
259 file: FileInfo,
260 _api: API,
261 options: Options
262) {
263 const j = createParserFromPath(file.path)
264 const root = j(file.source)
265
266 const isConfig = isNextConfigFile(file)
267
268 if (isConfig) {
269 const fileDir = parse(file.path).dir
270 const result = nextConfigTransformer(j, root, fileDir)
271 return result.toSource(options)
272 }
273
274 // Before: import Image from "next/legacy/image"
275 // After: import Image from "next/image"
276 root
277 .find(j.ImportDeclaration, {
278 source: { value: 'next/legacy/image' },
279 })
280 .forEach((imageImport) => {
281 const defaultSpecifier = imageImport.node.specifiers?.find(
282 (node) => node.type === 'ImportDefaultSpecifier'
283 ) as ImportDefaultSpecifier | undefined
284 const tagName = defaultSpecifier?.local?.name
285 imageImport.node.source = j.stringLiteral('next/image')
286 if (tagName) {
287 findAndReplaceProps(j, root, tagName)
288 }
289 })
290 // Before: const Image = await import("next/legacy/image")
291 // After: const Image = await import("next/image")
292 root.find(j.AwaitExpression).forEach((awaitExp) => {
293 const arg = awaitExp.value.argument
294 if (arg?.type === 'CallExpression' && arg.callee.type === 'Import') {
295 if (
296 arg.arguments[0].type === 'StringLiteral' &&
297 arg.arguments[0].value === 'next/legacy/image'
298 ) {
299 arg.arguments[0] = j.stringLiteral('next/image')
300 }
301 }
302 })
303
304 // Before: const Image = require("next/legacy/image")
305 // After: const Image = require("next/image")
306 root.find(j.CallExpression).forEach((requireExp) => {
307 if (
308 requireExp?.value?.callee?.type === 'Identifier' &&
309 requireExp.value.callee.name === 'require'
310 ) {
311 let firstArg = requireExp.value.arguments[0]
312 if (
313 firstArg &&
314 firstArg.type === 'StringLiteral' &&
315 firstArg.value === 'next/legacy/image'

Callers

nothing calls this directly

Calls 8

createParserFromPathFunction · 0.90
isNextConfigFileFunction · 0.90
parseFunction · 0.90
nextConfigTransformerFunction · 0.85
findAndReplacePropsFunction · 0.85
jFunction · 0.50
forEachMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected