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

Function transformer

packages/next-codemod/lib/cra-to-next/index-to-component.ts:9–102  ·  view source on GitHub ↗
(
  file: FileInfo,
  _api: API,
  options: Options
)

Source from the content-addressed store, hash-verified

7}
8
9export default function transformer(
10 file: FileInfo,
11 _api: API,
12 options: Options
13) {
14 const j = createParserFromPath(file.path)
15 const root = j(file.source)
16 let hasModifications = false
17 let foundReactRender = 0
18 let hasRenderImport = false
19 let defaultReactDomImport: string | undefined
20
21 root.find(j.ImportDeclaration).forEach((path) => {
22 if (path.node.source.value === 'react-dom') {
23 return path.node.specifiers.forEach((specifier) => {
24 if (specifier.local.name === 'render') {
25 hasRenderImport = true
26 }
27 if (specifier.type === 'ImportDefaultSpecifier') {
28 defaultReactDomImport = specifier.local.name
29 }
30 })
31 }
32 return false
33 })
34
35 root
36 .find(j.CallExpression)
37 .filter((path) => {
38 const { node } = path
39 let found = false
40
41 if (
42 defaultReactDomImport &&
43 node.callee.type === 'MemberExpression' &&
44 (node.callee.object as any).name === defaultReactDomImport &&
45 (node.callee.property as any).name === 'render'
46 ) {
47 found = true
48 }
49
50 if (hasRenderImport && (node.callee as any).name === 'render') {
51 found = true
52 }
53
54 if (found) {
55 foundReactRender++
56 hasModifications = true
57
58 if (!Array.isArray(path.parentPath?.parentPath?.value)) {
59 indexContext.nestedRender = true
60 return false
61 }
62
63 const newNode = j.exportDefaultDeclaration(
64 j.functionDeclaration(
65 j.identifier('NextIndexWrapper'),
66 [],

Callers

nothing calls this directly

Calls 9

createParserFromPathFunction · 0.90
isArrayMethod · 0.80
insertBeforeMethod · 0.80
jFunction · 0.50
forEachMethod · 0.45
findMethod · 0.45
removeMethod · 0.45
filterMethod · 0.45
identifierMethod · 0.45

Tested by

no test coverage detected