Skip to content

Commit 7d71d08

Browse files
authored
Reparse @import as synthetic type-only import declaration (#831)
1 parent bc3195e commit 7d71d08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+170
-763
lines changed

_packages/ast/src/syntaxKind.enum.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ export enum SyntaxKind {
344344
JSDocSatisfiesTag,
345345
JSDocImportTag,
346346
SyntaxList,
347+
JSTypeAliasDeclaration,
348+
JSExportAssignment,
349+
CommonJSExport,
350+
JSImportDeclaration,
347351
NotEmittedStatement,
348352
PartiallyEmittedExpression,
349353
CommaListExpression,

_packages/ast/src/syntaxKind.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,13 @@ export var SyntaxKind: any;
347347
SyntaxKind[SyntaxKind["JSDocSatisfiesTag"] = 342] = "JSDocSatisfiesTag";
348348
SyntaxKind[SyntaxKind["JSDocImportTag"] = 343] = "JSDocImportTag";
349349
SyntaxKind[SyntaxKind["SyntaxList"] = 344] = "SyntaxList";
350-
SyntaxKind[SyntaxKind["NotEmittedStatement"] = 345] = "NotEmittedStatement";
351-
SyntaxKind[SyntaxKind["PartiallyEmittedExpression"] = 346] = "PartiallyEmittedExpression";
352-
SyntaxKind[SyntaxKind["CommaListExpression"] = 347] = "CommaListExpression";
353-
SyntaxKind[SyntaxKind["SyntheticReferenceExpression"] = 348] = "SyntheticReferenceExpression";
354-
SyntaxKind[SyntaxKind["Count"] = 349] = "Count";
350+
SyntaxKind[SyntaxKind["JSTypeAliasDeclaration"] = 345] = "JSTypeAliasDeclaration";
351+
SyntaxKind[SyntaxKind["JSExportAssignment"] = 346] = "JSExportAssignment";
352+
SyntaxKind[SyntaxKind["CommonJSExport"] = 347] = "CommonJSExport";
353+
SyntaxKind[SyntaxKind["JSImportDeclaration"] = 348] = "JSImportDeclaration";
354+
SyntaxKind[SyntaxKind["NotEmittedStatement"] = 349] = "NotEmittedStatement";
355+
SyntaxKind[SyntaxKind["PartiallyEmittedExpression"] = 350] = "PartiallyEmittedExpression";
356+
SyntaxKind[SyntaxKind["CommaListExpression"] = 351] = "CommaListExpression";
357+
SyntaxKind[SyntaxKind["SyntheticReferenceExpression"] = 352] = "SyntheticReferenceExpression";
358+
SyntaxKind[SyntaxKind["Count"] = 353] = "Count";
355359
})(SyntaxKind || (SyntaxKind = {}));

internal/api/encoder/encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func getChildrenPropertyMask(node *ast.Node) uint8 {
459459
case ast.KindImportEqualsDeclaration:
460460
n := node.AsImportEqualsDeclaration()
461461
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.Name() != nil) << 1) | (boolToByte(n.ModuleReference != nil) << 2)
462-
case ast.KindImportDeclaration:
462+
case ast.KindImportDeclaration, ast.KindJSImportDeclaration:
463463
n := node.AsImportDeclaration()
464464
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.ImportClause != nil) << 1) | (boolToByte(n.ModuleSpecifier != nil) << 2) | (boolToByte(n.Attributes != nil) << 3)
465465
case ast.KindImportSpecifier:
@@ -468,7 +468,7 @@ func getChildrenPropertyMask(node *ast.Node) uint8 {
468468
case ast.KindImportClause:
469469
n := node.AsImportClause()
470470
return (boolToByte(n.Name() != nil) << 0) | (boolToByte(n.NamedBindings != nil) << 1)
471-
case ast.KindExportAssignment:
471+
case ast.KindExportAssignment, ast.KindJSExportAssignment:
472472
n := node.AsExportAssignment()
473473
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.Expression != nil) << 1)
474474
case ast.KindNamespaceExportDeclaration:

internal/ast/ast.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,10 @@ func (n *Node) Children() *NodeList {
771771

772772
func (n *Node) ModuleSpecifier() *Expression {
773773
switch n.Kind {
774-
case KindImportDeclaration:
774+
case KindImportDeclaration, KindJSImportDeclaration:
775775
return n.AsImportDeclaration().ModuleSpecifier
776776
case KindExportDeclaration:
777777
return n.AsExportDeclaration().ModuleSpecifier
778-
case KindJSDocImportTag:
779-
return n.AsJSDocImportTag().ModuleSpecifier
780778
}
781779
panic("Unhandled case in Node.ModuleSpecifier: " + n.Kind.String())
782780
}
@@ -3689,7 +3687,7 @@ type TypeAliasDeclaration struct {
36893687
Type *TypeNode // TypeNode
36903688
}
36913689

3692-
func (f *NodeFactory) newTypeOrJSTypeAliasDeclaration(kind Kind, modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
3690+
func (f *NodeFactory) newTypeAliasOrJSTypeAliasDeclaration(kind Kind, modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
36933691
data := &TypeAliasDeclaration{}
36943692
data.modifiers = modifiers
36953693
data.name = name
@@ -3699,12 +3697,12 @@ func (f *NodeFactory) newTypeOrJSTypeAliasDeclaration(kind Kind, modifiers *Modi
36993697
}
37003698

37013699
func (f *NodeFactory) NewTypeAliasDeclaration(modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
3702-
return f.newTypeOrJSTypeAliasDeclaration(KindTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
3700+
return f.newTypeAliasOrJSTypeAliasDeclaration(KindTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
37033701
}
37043702

37053703
func (f *NodeFactory) UpdateTypeAliasDeclaration(node *TypeAliasDeclaration, modifiers *ModifierList, name *IdentifierNode, typeParameters *TypeParameterList, typeNode *TypeNode) *Node {
37063704
if modifiers != node.modifiers || name != node.name || typeParameters != node.TypeParameters || typeNode != node.Type {
3707-
return updateNode(f.NewTypeAliasDeclaration(modifiers, name, typeParameters, typeNode), node.AsNode(), f.hooks)
3705+
return updateNode(f.newTypeAliasOrJSTypeAliasDeclaration(node.Kind, modifiers, name, typeParameters, typeNode), node.AsNode(), f.hooks)
37083706
}
37093707
return node.AsNode()
37103708
}
@@ -3714,14 +3712,11 @@ func (node *TypeAliasDeclaration) ForEachChild(v Visitor) bool {
37143712
}
37153713

37163714
func (node *TypeAliasDeclaration) VisitEachChild(v *NodeVisitor) *Node {
3717-
if node.Kind == KindJSTypeAliasDeclaration {
3718-
return v.Factory.UpdateJSTypeAliasDeclaration(node, v.visitModifiers(node.modifiers), v.visitNode(node.name), v.visitNodes(node.TypeParameters), v.visitNode(node.Type))
3719-
}
37203715
return v.Factory.UpdateTypeAliasDeclaration(node, v.visitModifiers(node.modifiers), v.visitNode(node.name), v.visitNodes(node.TypeParameters), v.visitNode(node.Type))
37213716
}
37223717

37233718
func (node *TypeAliasDeclaration) Clone(f NodeFactoryCoercible) *Node {
3724-
return cloneNode(f.AsNodeFactory().NewTypeAliasDeclaration(node.Modifiers(), node.Name(), node.TypeParameters, node.Type), node.AsNode(), f.AsNodeFactory().hooks)
3719+
return cloneNode(f.AsNodeFactory().newTypeAliasOrJSTypeAliasDeclaration(node.Kind, node.Modifiers(), node.Name(), node.TypeParameters, node.Type), node.AsNode(), f.AsNodeFactory().hooks)
37253720
}
37263721

37273722
func (node *TypeAliasDeclaration) Name() *DeclarationName { return node.name }
@@ -3735,14 +3730,7 @@ func IsTypeOrJSTypeAliasDeclaration(node *Node) bool {
37353730
}
37363731

37373732
func (f *NodeFactory) NewJSTypeAliasDeclaration(modifiers *ModifierList, name *IdentifierNode, typeParameters *NodeList, typeNode *TypeNode) *Node {
3738-
return f.newTypeOrJSTypeAliasDeclaration(KindJSTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
3739-
}
3740-
3741-
func (f *NodeFactory) UpdateJSTypeAliasDeclaration(node *TypeAliasDeclaration, modifiers *ModifierList, name *IdentifierNode, typeParameters *TypeParameterList, typeNode *TypeNode) *Node {
3742-
if modifiers != node.modifiers || name != node.name || typeParameters != node.TypeParameters || typeNode != node.Type {
3743-
return updateNode(f.NewJSTypeAliasDeclaration(modifiers, name, typeParameters, typeNode), node.AsNode(), f.hooks)
3744-
}
3745-
return node.AsNode()
3733+
return f.newTypeAliasOrJSTypeAliasDeclaration(KindJSTypeAliasDeclaration, modifiers, name, typeParameters, typeNode)
37463734
}
37473735

37483736
func IsJSTypeAliasDeclaration(node *Node) bool {
@@ -4056,18 +4044,26 @@ type ImportDeclaration struct {
40564044
Attributes *ImportAttributesNode // ImportAttributesNode. Optional
40574045
}
40584046

4059-
func (f *NodeFactory) NewImportDeclaration(modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
4047+
func (f *NodeFactory) newImportOrJSImportDeclaration(kind Kind, modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
40604048
data := &ImportDeclaration{}
40614049
data.modifiers = modifiers
40624050
data.ImportClause = importClause
40634051
data.ModuleSpecifier = moduleSpecifier
40644052
data.Attributes = attributes
4065-
return f.newNode(KindImportDeclaration, data)
4053+
return f.newNode(kind, data)
4054+
}
4055+
4056+
func (f *NodeFactory) NewImportDeclaration(modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
4057+
return f.newImportOrJSImportDeclaration(KindImportDeclaration, modifiers, importClause, moduleSpecifier, attributes)
4058+
}
4059+
4060+
func (f *NodeFactory) NewJSImportDeclaration(modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
4061+
return f.newImportOrJSImportDeclaration(KindJSImportDeclaration, modifiers, importClause, moduleSpecifier, attributes)
40664062
}
40674063

40684064
func (f *NodeFactory) UpdateImportDeclaration(node *ImportDeclaration, modifiers *ModifierList, importClause *ImportClauseNode, moduleSpecifier *Expression, attributes *ImportAttributesNode) *Node {
40694065
if modifiers != node.modifiers || importClause != node.ImportClause || moduleSpecifier != node.ModuleSpecifier || attributes != node.Attributes {
4070-
return updateNode(f.NewImportDeclaration(modifiers, importClause, moduleSpecifier, attributes), node.AsNode(), f.hooks)
4066+
return updateNode(f.newImportOrJSImportDeclaration(node.Kind, modifiers, importClause, moduleSpecifier, attributes), node.AsNode(), f.hooks)
40714067
}
40724068
return node.AsNode()
40734069
}
@@ -4081,7 +4077,7 @@ func (node *ImportDeclaration) VisitEachChild(v *NodeVisitor) *Node {
40814077
}
40824078

40834079
func (node *ImportDeclaration) Clone(f NodeFactoryCoercible) *Node {
4084-
return cloneNode(f.AsNodeFactory().NewImportDeclaration(node.Modifiers(), node.ImportClause, node.ModuleSpecifier, node.Attributes), node.AsNode(), f.AsNodeFactory().hooks)
4080+
return cloneNode(f.AsNodeFactory().newImportOrJSImportDeclaration(node.Kind, node.Modifiers(), node.ImportClause, node.ModuleSpecifier, node.Attributes), node.AsNode(), f.AsNodeFactory().hooks)
40854081
}
40864082

40874083
func (node *ImportDeclaration) computeSubtreeFacts() SubtreeFacts {

internal/ast/kind.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ const (
381381
KindJSTypeAliasDeclaration
382382
KindJSExportAssignment
383383
KindCommonJSExport
384+
KindJSImportDeclaration
384385
// Transformation nodes
385386
KindNotEmittedStatement
386387
KindPartiallyEmittedExpression

0 commit comments

Comments
 (0)