Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion _packages/api/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ export class RemoteNode extends RemoteNodeBase implements Node {
get isTypeOnly(): boolean | undefined {
switch (this.kind) {
case SyntaxKind.ImportSpecifier:
case SyntaxKind.ImportClause:
case SyntaxKind.ExportSpecifier:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ExportDeclaration:
Expand Down Expand Up @@ -921,6 +920,18 @@ export class RemoteNode extends RemoteNodeBase implements Node {
}
}

get phaseModifier(): SyntaxKind {
switch (this.kind) {
case SyntaxKind.ImportClause:
const flags = (this.data & (1 << 24 | 1 << 25)) >> 24;
if (flags & 1) return SyntaxKind.TypeKeyword;
if (flags & 2) return SyntaxKind.DeferKeyword;
// fallthrough
default:
return SyntaxKind.Unknown;
}
}

get token(): SyntaxKind | undefined {
switch (this.kind) {
case SyntaxKind.ImportAttributes:
Expand Down
7 changes: 6 additions & 1 deletion _packages/ast/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1992,11 +1992,16 @@ export type NamedExportBindings =
export interface ImportClause extends NamedDeclaration {
readonly kind: SyntaxKind.ImportClause;
readonly parent: ImportDeclaration | JSDocImportTag;
readonly isTypeOnly: boolean;
readonly phaseModifier: ImportPhaseModifierSyntaxKind;
readonly name?: Identifier; // Default binding
readonly namedBindings?: NamedImportBindings;
}

export type ImportPhaseModifierSyntaxKind =
| SyntaxKind.Unknown
| SyntaxKind.TypeKeyword
| SyntaxKind.DeferKeyword;

/** @deprecated */
export type AssertionKey = ImportAttributeName;

Expand Down
1 change: 1 addition & 0 deletions _packages/ast/src/syntaxKind.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export enum SyntaxKind {
BigIntKeyword,
OverrideKeyword,
OfKeyword,
DeferKeyword,
QualifiedName,
ComputedPropertyName,
TypeParameter,
Expand Down
377 changes: 189 additions & 188 deletions _packages/ast/src/syntaxKind.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/api/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func getNodeDefinedData(node *ast.Node) uint32 {
return uint32(boolToByte(n.IsTypeOnly)) << 24
case ast.KindImportClause:
n := node.AsImportClause()
return uint32(boolToByte(n.IsTypeOnly)) << 24
return uint32(boolToByte(n.PhaseModifier == ast.KindTypeKeyword))<<24 | uint32(boolToByte(n.PhaseModifier == ast.KindDeferKeyword))<<25
case ast.KindExportSpecifier:
n := node.AsExportSpecifier()
return uint32(boolToByte(n.IsTypeOnly)) << 24
Expand Down
22 changes: 12 additions & 10 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ func (n *Node) Text() string {
return n.AsNumericLiteral().Text
case KindBigIntLiteral:
return n.AsBigIntLiteral().Text
case KindMetaProperty:
return n.AsMetaProperty().Name().Text()
case KindNoSubstitutionTemplateLiteral:
return n.AsNoSubstitutionTemplateLiteral().Text
case KindTemplateHead:
Expand Down Expand Up @@ -894,7 +896,7 @@ func (n *Node) IsTypeOnly() bool {
case KindImportSpecifier:
return n.AsImportSpecifier().IsTypeOnly
case KindImportClause:
return n.AsImportClause().IsTypeOnly
return n.AsImportClause().PhaseModifier == KindTypeKeyword
case KindExportDeclaration:
return n.AsExportDeclaration().IsTypeOnly
case KindExportSpecifier:
Expand Down Expand Up @@ -4945,22 +4947,22 @@ type ImportClause struct {
DeclarationBase
ExportableBase
compositeNodeBase
IsTypeOnly bool
PhaseModifier Kind // KindTypeKeyword | KindDeferKeyword
NamedBindings *NamedImportBindings // NamedImportBindings. Optional, named bindings
name *IdentifierNode // IdentifierNode. Optional, default binding
}

func (f *NodeFactory) NewImportClause(isTypeOnly bool, name *IdentifierNode, namedBindings *NamedImportBindings) *Node {
func (f *NodeFactory) NewImportClause(phaseModifier Kind, name *IdentifierNode, namedBindings *NamedImportBindings) *Node {
data := &ImportClause{}
data.IsTypeOnly = isTypeOnly
data.PhaseModifier = phaseModifier
data.name = name
data.NamedBindings = namedBindings
return f.newNode(KindImportClause, data)
}

func (f *NodeFactory) UpdateImportClause(node *ImportClause, isTypeOnly bool, name *IdentifierNode, namedBindings *NamedImportBindings) *Node {
if isTypeOnly != node.IsTypeOnly || name != node.name || namedBindings != node.NamedBindings {
return updateNode(f.NewImportClause(isTypeOnly, name, namedBindings), node.AsNode(), f.hooks)
func (f *NodeFactory) UpdateImportClause(node *ImportClause, phaseModifier Kind, name *IdentifierNode, namedBindings *NamedImportBindings) *Node {
if phaseModifier != node.PhaseModifier || name != node.name || namedBindings != node.NamedBindings {
return updateNode(f.NewImportClause(phaseModifier, name, namedBindings), node.AsNode(), f.hooks)
}
return node.AsNode()
}
Expand All @@ -4970,19 +4972,19 @@ func (node *ImportClause) ForEachChild(v Visitor) bool {
}

func (node *ImportClause) VisitEachChild(v *NodeVisitor) *Node {
return v.Factory.UpdateImportClause(node, node.IsTypeOnly, v.visitNode(node.name), v.visitNode(node.NamedBindings))
return v.Factory.UpdateImportClause(node, node.PhaseModifier, v.visitNode(node.name), v.visitNode(node.NamedBindings))
}

func (node *ImportClause) Clone(f NodeFactoryCoercible) *Node {
return cloneNode(f.AsNodeFactory().NewImportClause(node.IsTypeOnly, node.Name(), node.NamedBindings), node.AsNode(), f.AsNodeFactory().hooks)
return cloneNode(f.AsNodeFactory().NewImportClause(node.PhaseModifier, node.Name(), node.NamedBindings), node.AsNode(), f.AsNodeFactory().hooks)
}

func (node *ImportClause) Name() *DeclarationName {
return node.name
}

func (node *ImportClause) computeSubtreeFacts() SubtreeFacts {
if node.IsTypeOnly {
if node.PhaseModifier == KindTypeKeyword {
return SubtreeContainsTypeScript
} else {
return propagateSubtreeFacts(node.name) |
Expand Down
7 changes: 4 additions & 3 deletions internal/ast/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ const (
KindGlobalKeyword
KindBigIntKeyword
KindOverrideKeyword
KindOfKeyword // LastKeyword and LastToken and LastContextualKeyword
KindOfKeyword
KindDeferKeyword // LastKeyword and LastToken and LastContextualKeyword
// Parse tree nodes
// Names
KindQualifiedName
Expand Down Expand Up @@ -399,7 +400,7 @@ const (
KindFirstReservedWord = KindBreakKeyword
KindLastReservedWord = KindWithKeyword
KindFirstKeyword = KindBreakKeyword
KindLastKeyword = KindOfKeyword
KindLastKeyword = KindDeferKeyword
KindFirstFutureReservedWord = KindImplementsKeyword
KindLastFutureReservedWord = KindYieldKeyword
KindFirstTypeNode = KindTypePredicate
Expand All @@ -422,7 +423,7 @@ const (
KindFirstJSDocTagNode = KindJSDocTag
KindLastJSDocTagNode = KindJSDocImportTag
KindFirstContextualKeyword = KindAbstractKeyword
KindLastContextualKeyword = KindOfKeyword
KindLastContextualKeyword = KindDeferKeyword
KindComment = KindSingleLineCommentTrivia | KindMultiLineCommentTrivia
KindFirstTriviaToken = KindSingleLineCommentTrivia
KindLastTriviaToken = KindConflictMarkerTrivia
Expand Down
Loading