Skip to content
Draft
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
2 changes: 1 addition & 1 deletion packages/typescript/src/api/node/protocol.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const childProperties: Readonly<Partial<Record<SyntaxKind, readonly (stri
[SyntaxKind.JSDocSignature]: ["typeParameters", "parameters", "type"],
[SyntaxKind.JSDocNameReference]: ["name"],
[SyntaxKind.SourceFile]: ["statements", "endOfFileToken"],
[SyntaxKind.ModuleDeclaration]: ["modifiers", "name", "body"],
[SyntaxKind.ModuleDeclaration]: ["modifiers", "name", "attributes", "body"],
[SyntaxKind.ImportEqualsDeclaration]: ["modifiers", "name", "moduleReference"],
[SyntaxKind.ExportDeclaration]: ["modifiers", "exportClause", "moduleSpecifier", "attributes"],
[SyntaxKind.ImportType]: ["argument", "attributes", "qualifier", "typeArguments"],
Expand Down
1 change: 1 addition & 0 deletions packages/typescript/src/ast/ast.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ export interface ModuleDeclaration extends StatementBase, DeclarationBase, Modif
readonly kind: SyntaxKind.ModuleDeclaration;
readonly keyword: SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword;
readonly name: ModuleName;
readonly attributes?: TypeLiteralNode;
readonly body?: ModuleBody;
}
export interface ImportEqualsDeclaration extends StatementBase, DeclarationBase, ModifiersBase {
Expand Down
10 changes: 6 additions & 4 deletions packages/typescript/src/ast/factory.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ function cloneNodeData(node: Node): any {
case SyntaxKind.JSDocNameReference:
return { name: n.name };
case SyntaxKind.ModuleDeclaration:
return { modifiers: n.modifiers, keyword: n.keyword, name: n.name, body: n.body };
return { modifiers: n.modifiers, keyword: n.keyword, name: n.name, attributes: n.attributes, body: n.body };
case SyntaxKind.ImportEqualsDeclaration:
return { modifiers: n.modifiers, isTypeOnly: n.isTypeOnly, name: n.name, moduleReference: n.moduleReference };
case SyntaxKind.ExportDeclaration:
Expand Down Expand Up @@ -1605,6 +1605,7 @@ const forEachChildTable: Record<number, ForEachChildFunction> = {
[SyntaxKind.ModuleDeclaration]: (data, cbNode, cbNodes) =>
visitNodes(cbNode, cbNodes, data.modifiers) ||
visitNode(cbNode, data.name) ||
visitNode(cbNode, data.attributes) ||
visitNode(cbNode, data.body),
[SyntaxKind.ImportEqualsDeclaration]: (data, cbNode, cbNodes) =>
visitNodes(cbNode, cbNodes, data.modifiers) ||
Expand Down Expand Up @@ -2955,11 +2956,12 @@ export function createJSDocNameReference(name: EntityName): JSDocNameReference {
}) as unknown as JSDocNameReference;
}

export function createModuleDeclaration(modifiers: readonly ModifierLike[] | undefined, keyword: SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword, name: ModuleName, body?: ModuleBody): ModuleDeclaration {
export function createModuleDeclaration(modifiers: readonly ModifierLike[] | undefined, keyword: SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword, name: ModuleName, attributes?: TypeLiteralNode, body?: ModuleBody): ModuleDeclaration {
return new NodeObject(SyntaxKind.ModuleDeclaration, {
modifiers: modifiers ? createNodeArray(modifiers) : undefined,
keyword,
name,
attributes,
body,
}) as unknown as ModuleDeclaration;
}
Expand Down Expand Up @@ -3726,8 +3728,8 @@ export function updateJSDocNameReference(node: JSDocNameReference, name: EntityN
return node.name !== name ? createJSDocNameReference(name) : node;
}

export function updateModuleDeclaration(node: ModuleDeclaration, modifiers: readonly ModifierLike[] | undefined, name: ModuleName, body?: ModuleBody): ModuleDeclaration {
return node.modifiers !== modifiers || node.name !== name || node.body !== body ? createModuleDeclaration(modifiers, node.keyword, name, body) : node;
export function updateModuleDeclaration(node: ModuleDeclaration, modifiers: readonly ModifierLike[] | undefined, name: ModuleName, attributes?: TypeLiteralNode, body?: ModuleBody): ModuleDeclaration {
return node.modifiers !== modifiers || node.name !== name || node.attributes !== attributes || node.body !== body ? createModuleDeclaration(modifiers, node.keyword, name, attributes, body) : node;
}

export function updateImportEqualsDeclaration(node: ImportEqualsDeclaration, modifiers: readonly ModifierLike[] | undefined, name: Identifier, moduleReference: ModuleReference): ImportEqualsDeclaration {
Expand Down
4 changes: 3 additions & 1 deletion packages/typescript/src/ast/visitor.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ import {
isTemplateHead,
isTemplateLiteral,
isTemplateMiddleOrTail,
isTypeLiteralNode,
isTypeNode,
isTypeParameterDeclaration,
isTypePredicateParameterName,
Expand Down Expand Up @@ -1342,8 +1343,9 @@ const visitEachChildTable: Record<number, VisitEachChildFunction> = {
[SyntaxKind.ModuleDeclaration]: (node: ModuleDeclaration, visitor: Visitor): ModuleDeclaration => {
const _modifiers = visitNodes(node.modifiers, visitor);
const _name = visitNode(node.name, visitor, isModuleName);
const _attributes = visitNode(node.attributes, visitor, isTypeLiteralNode);
const _body = visitNode(node.body, visitor, isModuleBody);
return updateModuleDeclaration(node, _modifiers, _name, _body);
return updateModuleDeclaration(node, _modifiers, _name, _attributes, _body);
},
[SyntaxKind.ImportEqualsDeclaration]: (node: ImportEqualsDeclaration, visitor: Visitor): ImportEqualsDeclaration => {
const _modifiers = visitNodes(node.modifiers, visitor);
Expand Down
5 changes: 5 additions & 0 deletions tools/scripts/tsc/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -4807,6 +4807,11 @@
"type": "ModuleName",
"private": true
},
{
"name": "Attributes",
"type": "TypeLiteralNode",
"optional": true
},
{
"name": "Body",
"inherited": true,
Expand Down
5 changes: 3 additions & 2 deletions tsc/internal/api/encoder/decoder_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tsc/internal/api/encoder/encoder_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 12 additions & 25 deletions tsc/internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/microsoft/TypeScript/tsc/internal/collections"
"github.com/microsoft/TypeScript/tsc/internal/core"
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
"github.com/microsoft/TypeScript/tsc/internal/spanmap"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
"github.com/zeebo/xxh3"
Expand Down Expand Up @@ -968,6 +969,8 @@ func (n *Node) Attributes() *Node {
return n.AsJsxOpeningElement().Attributes
case KindJsxSelfClosingElement:
return n.AsJsxSelfClosingElement().Attributes
case KindModuleDeclaration:
return n.AsModuleDeclaration().Attributes
}
panic("Unhandled case in Node.Attributes: " + n.Kind.String())
}
Expand Down Expand Up @@ -2211,44 +2214,28 @@ func (node *ExpressionWithTypeArguments) computeSubtreeFacts() SubtreeFacts {
propagateEraseableSyntaxListSubtreeFacts(node.TypeArguments)
}

func (node *ImportAttributesNode) GetResolutionModeOverride( /* !!! grammarErrorOnNode?: (node: Node, diagnostic: DiagnosticMessage) => void*/ ) (core.ResolutionMode, bool) {
func (node *ImportAttributesNode) GetResolutionModeOverride(grammarErrorOnNode func(node *Node, message *diagnostics.Message, args ...any) bool) (core.ResolutionMode, bool) {
if node == nil {
return core.ResolutionModeNone, false
}

attributes := node.AsImportAttributes().Attributes

if len(attributes.Nodes) != 1 {
// !!!
// grammarErrorOnNode?.(
// node,
// node.token === SyntaxKind.WithKeyword
// ? Diagnostics.Type_import_attributes_should_have_exactly_one_key_resolution_mode_with_value_import_or_require
// : Diagnostics.Type_import_assertions_should_have_exactly_one_key_resolution_mode_with_value_import_or_require,
// );
attribute := core.Find(attributes.Nodes, func(attribute *Node) bool {
return attribute.Name().Text() == "resolution-mode"
})
if attribute == nil {
return core.ResolutionModeNone, false
}

elem := attributes.Nodes[0].AsImportAttribute()
if !IsStringLiteralLike(elem.Name()) {
return core.ResolutionModeNone, false
}
if elem.Name().Text() != "resolution-mode" {
// !!!
// grammarErrorOnNode?.(
// elem.name,
// node.token === SyntaxKind.WithKeyword
// ? Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_attributes
// : Diagnostics.resolution_mode_is_the_only_valid_key_for_type_import_assertions,
// );
return core.ResolutionModeNone, false
}
elem := attribute.AsImportAttribute()
if !IsStringLiteralLike(elem.Value) {
return core.ResolutionModeNone, false
}
if elem.Value.Text() != "import" && elem.Value.Text() != "require" {
// !!!
// grammarErrorOnNode?.(elem.value, Diagnostics.resolution_mode_should_be_either_require_or_import);
if grammarErrorOnNode != nil {
grammarErrorOnNode(elem.Value, diagnostics.X_resolution_mode_should_be_either_require_or_import)
}
return core.ResolutionModeNone, false
}
if elem.Value.Text() == "import" {
Expand Down
23 changes: 14 additions & 9 deletions tsc/internal/ast/ast_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions tsc/internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1941,14 +1941,24 @@ func GetExternalModuleName(node *Node) *Expression {
panic("Unhandled case in getExternalModuleName")
}

func HasImportAttributes(node *Node) bool {
switch node.Kind {
case KindImportDeclaration, KindJSImportDeclaration, KindExportDeclaration, KindImportType:
return true
}
return false
}

func GetImportAttributes(node *Node) *Node {
switch node.Kind {
case KindImportDeclaration, KindJSImportDeclaration:
return node.AsImportDeclaration().Attributes
case KindExportDeclaration:
return node.AsExportDeclaration().Attributes
case KindImportType:
return node.AsImportTypeNode().Attributes
}
panic("Unhandled case in getImportAttributes")
panic("Unhandled case in getImportAttributes: " + node.Kind.String())
}

func getImportTypeNodeLiteral(node *Node) *Node {
Expand Down Expand Up @@ -3198,10 +3208,6 @@ func IsPartOfTypeOnlyImportOrExportDeclaration(node *Node) bool {
return FindAncestor(node, IsTypeOnlyImportOrExportDeclaration) != nil
}

func IsPartOfExclusivelyTypeOnlyImportOrExportDeclaration(node *Node) bool {
return FindAncestor(node, IsExclusivelyTypeOnlyImportOrExport) != nil
}

func IsEmittableImport(node *Node) bool {
switch node.Kind {
case KindImportDeclaration:
Expand Down Expand Up @@ -3239,7 +3245,7 @@ func HasResolutionModeOverride(node *Node) bool {
attributes = node.AsExportDeclaration().Attributes
}
if attributes != nil {
_, ok := attributes.GetResolutionModeOverride()
_, ok := attributes.GetResolutionModeOverride(nil)
return ok
}
return false
Expand Down Expand Up @@ -3521,6 +3527,7 @@ func ReplaceModifiers(factory *NodeFactory, node *Node, modifierArray *ModifierL
modifierArray,
node.AsModuleDeclaration().Keyword,
node.Name(),
node.Attributes(),
node.Body(),
)
case KindImportEqualsDeclaration:
Expand Down
8 changes: 8 additions & 0 deletions tsc/internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ func (b *Binder) getDeclarationName(node *ast.Node) string {
if ast.IsGlobalScopeAugmentation(node) {
return ast.InternalSymbolNameGlobal
}
if pattern := core.TryParsePattern(moduleName); pattern.IsValid() && pattern.StarIndex >= 0 {
if attributes := node.AsModuleDeclaration().Attributes; attributes != nil {
return "\"" + moduleName + "\"" + ast.InternalSymbolNamePrefix + "pattern@" + strconv.FormatUint(uint64(ast.GetNodeId(attributes)), 10)
}
}
return "\"" + moduleName + "\""
}
if ast.IsPrivateIdentifier(name) {
Expand Down Expand Up @@ -781,12 +786,15 @@ func (b *Binder) bindModuleDeclaration(node *ast.Node) {
symbol := b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsValueModule, ast.SymbolFlagsValueModuleExcludes)

if ast.IsStringLiteral(name) {
attributes := node.AsModuleDeclaration().Attributes
pattern := core.TryParsePattern(name.Text())
if !pattern.IsValid() {
// An invalid pattern - must have multiple wildcards.
b.errorOnFirstToken(name, diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, name.Text())
} else if pattern.StarIndex >= 0 {
b.file.PatternAmbientModules = append(b.file.PatternAmbientModules, &ast.PatternAmbientModule{Pattern: pattern, Symbol: symbol})
} else if attributes != nil {
b.errorOnNode(name, diagnostics.An_ambient_module_declaration_with_import_attributes_must_use_a_pattern_name_with_an_Asterisk_character)
}
}
}
Expand Down
Loading
Loading