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

Function transformer

packages/next-codemod/transforms/remove-unstable-prefix.ts:16–357  ·  view source on GitHub ↗
(
  file: FileInfo,
  _api: API,
  options: Options
)

Source from the content-addressed store, hash-verified

14}
15
16export default function transformer(
17 file: FileInfo,
18 _api: API,
19 options: Options
20) {
21 const j = createParserFromPath(file.path)
22 const root = j(file.source)
23 let hasChanges = false
24
25 try {
26 // Track identifier renames that need to be applied
27 const identifierRenames: Array<{ oldName: string; newName: string }> = []
28 // Track variables assigned from next/cache imports/requires
29 const cacheVariables = new Set<string>()
30
31 // Handle ES6 imports: import { unstable_cacheTag } from 'next/cache'
32 root
33 .find(j.ImportDeclaration, { source: { value: 'next/cache' } })
34 .forEach((path) => {
35 path.node.specifiers?.forEach((specifier) => {
36 if (
37 specifier.type === 'ImportSpecifier' &&
38 specifier.imported?.type === 'Identifier' &&
39 shouldRenameProperty(specifier.imported.name)
40 ) {
41 const oldName = specifier.imported.name
42 const newName = UNSTABLE_TO_STABLE_MAPPING[oldName]
43
44 // Handle alias scenarios
45 if (specifier.local && specifier.local.name === newName) {
46 // Same alias name: { unstable_cacheTag as cacheTag } -> { cacheTag }
47 const newSpecifier = j.importSpecifier(j.identifier(newName))
48 const specifierIndex = path.node.specifiers.indexOf(specifier)
49 path.node.specifiers[specifierIndex] = newSpecifier
50 identifierRenames.push({ oldName, newName })
51 } else {
52 // Normal case: just update the imported name
53 specifier.imported = j.identifier(newName)
54 if (!specifier.local || specifier.local.name === oldName) {
55 // Not aliased or aliased with old name: add to identifier renames
56 identifierRenames.push({ oldName, newName })
57 }
58 }
59
60 hasChanges = true
61 } else if (specifier.type === 'ImportNamespaceSpecifier') {
62 // Handle namespace imports: import * as cache from 'next/cache'
63 cacheVariables.add(specifier.local.name)
64 }
65 })
66 })
67
68 // Handle export statements: export { unstable_cacheTag } from 'next/cache'
69 root
70 .find(j.ExportNamedDeclaration, { source: { value: 'next/cache' } })
71 .forEach((path) => {
72 path.node.specifiers?.forEach((specifier) => {
73 if (

Callers

nothing calls this directly

Calls 12

createParserFromPathFunction · 0.90
shouldRenamePropertyFunction · 0.85
pushMethod · 0.65
jFunction · 0.50
forEachMethod · 0.45
findMethod · 0.45
identifierMethod · 0.45
indexOfMethod · 0.45
addMethod · 0.45
hasMethod · 0.45
filterMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected