Skip to content

Commit b524c36

Browse files
authored
codemod: make parser more comptible (#71122)
1 parent f0c058d commit b524c36

24 files changed

+107
-42
lines changed

packages/next-codemod/lib/cra-to-next/global-css-transform.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import nodePath from 'path'
22
import type { API, FileInfo, Options } from 'jscodeshift'
3+
import { createParserFromPath } from '../parser'
34

45
export const globalCssContext = {
56
cssImports: new Set<string>(),
@@ -9,10 +10,10 @@ const globalStylesRegex = /(?<!\.module)\.(css|scss|sass)$/i
910

1011
export default function transformer(
1112
file: FileInfo,
12-
api: API,
13+
_api: API,
1314
options: Options
1415
) {
15-
const j = api.jscodeshift.withParser('tsx')
16+
const j = createParserFromPath(file.path)
1617
const root = j(file.source)
1718
let hasModifications = false
1819

packages/next-codemod/lib/cra-to-next/index-to-component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { API, FileInfo, JSXElement, Options } from 'jscodeshift'
2+
import { createParserFromPath } from '../parser'
23

34
export const indexContext = {
45
multipleRenderRoots: false,
@@ -7,10 +8,10 @@ export const indexContext = {
78

89
export default function transformer(
910
file: FileInfo,
10-
api: API,
11+
_api: API,
1112
options: Options
1213
) {
13-
const j = api.jscodeshift.withParser('tsx')
14+
const j = createParserFromPath(file.path)
1415
const root = j(file.source)
1516
let hasModifications = false
1617
let foundReactRender = 0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import j from 'jscodeshift'
2+
import babylonParse from 'jscodeshift/parser/babylon'
3+
import tsOptions from 'jscodeshift/parser/tsOptions'
4+
5+
const dtsOptions = {
6+
...tsOptions,
7+
plugins: [
8+
...tsOptions.plugins.filter((plugin) => plugin !== 'typescript'),
9+
['typescript', { dts: true }],
10+
],
11+
}
12+
13+
function createParserFromPath(filePath: string): j.JSCodeshift {
14+
const isDeclarationFile = /\.d\.(m|c)?ts$/.test(filePath)
15+
if (isDeclarationFile) {
16+
return j.withParser(babylonParse(dtsOptions))
17+
}
18+
19+
// jsx is allowed in .js files, feed them into the tsx parser.
20+
// tsx parser :.js, .jsx, .tsx
21+
// ts parser: .ts, .mts, .cts
22+
const isTsFile = /\.(m|c)?.ts$/.test(filePath)
23+
return isTsFile ? j.withParser('ts') : j.withParser('tsx')
24+
}
25+
26+
export { createParserFromPath }

packages/next-codemod/lib/run-jscodeshift.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export default function runJscodeshift(
1313
'**/node_modules/**',
1414
'**/.next/**',
1515
'**/build/**',
16-
// type files
17-
'**/*.d.ts',
18-
'**/*.d.cts',
19-
'**/*.d.mts',
2016
// test files
2117
'**/*.test.*',
2218
'**/*.spec.*',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { cookies } from 'next/headers'
2+
3+
export {
4+
cookies
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { cookies } from 'next/headers'
2+
3+
export {
4+
cookies
5+
}
File renamed without changes.
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { JSX } from 'react'
2+
import dynamic from 'next/dynamic'
3+
4+
const DynamicComponent = dynamic(
5+
() => import('./component').then(mod => {
6+
return mod.default;
7+
})
8+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { JSX } from 'react'
2+
import dynamic from 'next/dynamic'
3+
4+
const DynamicComponent = dynamic(
5+
() => import('./component').then(mod => {
6+
return {
7+
default: mod.default
8+
};
9+
})
10+
)

0 commit comments

Comments
 (0)