Skip to content

Commit 03ebf1b

Browse files
authored
feat: upgrade to Babel 8 (#165)
1 parent dd387af commit 03ebf1b

File tree

18 files changed

+204
-142
lines changed

18 files changed

+204
-142
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
}
7575
},
7676
"dependencies": {
77-
"@babel/generator": "^7.28.6",
78-
"@babel/parser": "^7.28.6",
79-
"@babel/types": "^7.28.6",
80-
"ast-kit": "^2.2.0",
77+
"@babel/generator": "8.0.0-beta.4",
78+
"@babel/parser": "8.0.0-beta.4",
79+
"@babel/types": "8.0.0-beta.4",
80+
"ast-kit": "^3.0.0-beta.1",
8181
"birpc": "^4.0.0",
8282
"dts-resolver": "^2.1.3",
8383
"get-tsconfig": "^4.13.0",
@@ -88,7 +88,6 @@
8888
"@sxzz/eslint-config": "^7.4.5",
8989
"@sxzz/prettier-config": "^2.2.6",
9090
"@sxzz/test-utils": "^0.5.15",
91-
"@types/babel__generator": "^7.27.0",
9291
"@types/node": "^25.0.9",
9392
"@typescript/native-preview": "7.0.0-dev.20260118.1",
9493
"@volar/typescript": "^2.4.27",

pnpm-lock.yaml

Lines changed: 66 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fake-js.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { generate } from '@babel/generator'
22
import { parse } from '@babel/parser'
3-
import t from '@babel/types'
3+
import * as t from '@babel/types'
44
import {
55
isDeclarationType,
66
isIdentifierOf,
@@ -36,7 +36,7 @@ type Dep = t.Expression & { replace?: (newNode: t.Node) => void }
3636
*/
3737
type TypeParams = Array<{
3838
name: string
39-
typeParams: t.TSTypeParameter[]
39+
typeParams: t.Identifier[]
4040
}>
4141

4242
interface DeclarationInfo {
@@ -316,7 +316,10 @@ export function createFakeJsPlugin({
316316
sourceFileName: id,
317317
})
318318

319-
return result
319+
return {
320+
code: result.code,
321+
map: result.map as any,
322+
}
320323
}
321324

322325
function renderChunk(code: string, chunk: RenderedChunk) {
@@ -436,7 +439,10 @@ export function createFakeJsPlugin({
436439
sourceFileName: chunk.fileName,
437440
})
438441

439-
return result
442+
return {
443+
code: result.code,
444+
map: result.map as any,
445+
}
440446
}
441447

442448
function getIdentifierIndex(name: string) {
@@ -463,19 +469,21 @@ export function createFakeJsPlugin({
463469
* dependency function.
464470
*/
465471
function collectParams(node: t.Node): TypeParams {
466-
const typeParams: t.TSTypeParameter[] = []
472+
const typeParams: t.Identifier[] = []
467473
walkAST(node, {
468474
leave(node) {
469475
if (
470476
'typeParameters' in node &&
471477
node.typeParameters?.type === 'TSTypeParameterDeclaration'
472478
) {
473-
typeParams.push(...node.typeParameters.params)
479+
typeParams.push(
480+
...node.typeParameters.params.map((param) => param.name),
481+
)
474482
}
475483
},
476484
})
477485

478-
const paramMap = new Map<string, t.TSTypeParameter[]>()
486+
const paramMap = new Map<string, t.Identifier[]>()
479487
for (const typeParam of typeParams) {
480488
const name = typeParam.name
481489
const group = paramMap.get(name)
@@ -534,17 +542,17 @@ export function createFakeJsPlugin({
534542
}
535543
} else if (node.type === 'TSInterfaceDeclaration' && node.extends) {
536544
for (const heritage of node.extends || []) {
537-
addDependency(TSEntityNameToRuntime(heritage.expression))
545+
addDependency(heritage.expression)
538546
}
539547
} else if (node.type === 'ClassDeclaration') {
540548
if (node.superClass) addDependency(node.superClass)
541549
if (node.implements) {
542550
for (const implement of node.implements) {
543-
addDependency(
544-
TSEntityNameToRuntime(
545-
(implement as t.TSExpressionWithTypeArguments).expression,
546-
),
547-
)
551+
if (implement.type === 'ClassImplements') {
552+
addDependency(implement.id)
553+
} else {
554+
addDependency(implement.expression)
555+
}
548556
}
549557
}
550558
} else if (
@@ -566,7 +574,6 @@ export function createFakeJsPlugin({
566574
switch (node.type) {
567575
case 'TSTypeReference': {
568576
addDependency(TSEntityNameToRuntime(node.typeName))
569-
570577
break
571578
}
572579
case 'TSTypeQuery': {
@@ -579,11 +586,10 @@ export function createFakeJsPlugin({
579586
}
580587
case 'TSImportType': {
581588
seen.add(node)
582-
const source = node.argument
583-
const imported = node.qualifier
589+
const { source, qualifier } = node
584590
const dep = importNamespace(
585591
node,
586-
imported,
592+
qualifier,
587593
source,
588594
namespaceStmts,
589595
)
@@ -628,13 +634,19 @@ export function createFakeJsPlugin({
628634

629635
if (imported) {
630636
const importedLeft = getIdFromTSEntityName(imported)
637+
if (
638+
imported.type === 'ThisExpression' ||
639+
importedLeft.type === 'ThisExpression'
640+
) {
641+
throw new Error('Cannot import `this` from module.')
642+
}
631643
overwriteNode(importedLeft, t.tsQualifiedName(local, { ...importedLeft }))
632644
local = imported
633645
}
634646

635647
let replacement: t.Node = node
636-
if (node.typeParameters) {
637-
overwriteNode(node, t.tsTypeReference(local, node.typeParameters))
648+
if (node.typeArguments) {
649+
overwriteNode(node, t.tsTypeReference(local, node.typeArguments))
638650
replacement = local
639651
} else {
640652
overwriteNode(node, local)
@@ -666,7 +678,7 @@ function collectInferredNames(node: t.Node) {
666678
walkAST(node, {
667679
enter(node) {
668680
if (node.type === 'TSInferType' && node.typeParameter) {
669-
inferred.push(node.typeParameter.name)
681+
inferred.push(node.typeParameter.name.name)
670682
}
671683
},
672684
})
@@ -785,22 +797,24 @@ function isRuntimeBindingArrayElements(
785797
function isThisExpression(node: t.Node): boolean {
786798
return (
787799
isIdentifierOf(node, 'this') ||
800+
node.type === 'ThisExpression' ||
788801
(node.type === 'MemberExpression' && isThisExpression(node.object))
789802
)
790803
}
791804

792805
function TSEntityNameToRuntime(
793806
node: t.TSEntityName,
794-
): t.MemberExpression | t.Identifier {
795-
if (node.type === 'Identifier') {
807+
): t.MemberExpression | t.Identifier | t.ThisExpression {
808+
if (node.type === 'Identifier' || node.type === 'ThisExpression') {
796809
return node
797810
}
811+
798812
const left = TSEntityNameToRuntime(node.left)
799813
return Object.assign(node, t.memberExpression(left, node.right))
800814
}
801815

802816
function getIdFromTSEntityName(node: t.TSEntityName) {
803-
if (node.type === 'Identifier') {
817+
if (node.type === 'Identifier' || node.type === 'ThisExpression') {
804818
return node
805819
}
806820
return getIdFromTSEntityName(node.left)

0 commit comments

Comments
 (0)