Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f8ba3fe
fix(1374): support declaration emit for expando functions
a-tarasyuk Oct 5, 2025
0005071
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 5, 2025
b832853
fix missing export
a-tarasyuk Oct 6, 2025
d567d73
fix formatting
a-tarasyuk Oct 6, 2025
ad033f6
fix expando prop type and keyword generated alias names
a-tarasyuk Oct 6, 2025
c771636
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 6, 2025
e6a6025
transform expando hosts
a-tarasyuk Oct 6, 2025
52f4ae6
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 6, 2025
a674799
cleanup
a-tarasyuk Oct 6, 2025
bb2acd9
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 7, 2025
a808aba
Merge branch 'main' into fix/1374
a-tarasyuk Oct 7, 2025
90a1027
resolve element access property names
a-tarasyuk Oct 9, 2025
9b5b919
Merge branch 'fix/1374' of https://github.com/a-tarasyuk/typescript-g…
a-tarasyuk Oct 9, 2025
5f6eb6e
exclude expando initializers
a-tarasyuk Oct 9, 2025
46513b5
Merge branch 'main' into fix/1374
a-tarasyuk Oct 9, 2025
ff68b7a
deduplicate emitted namespaces
a-tarasyuk Oct 9, 2025
cf3605d
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 9, 2025
2600771
Merge branch 'fix/1374' of https://github.com/a-tarasyuk/typescript-g…
a-tarasyuk Oct 9, 2025
fb94aa2
skip emitting function properties
a-tarasyuk Oct 9, 2025
4413f24
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Oct 9, 2025
8847dd7
use var instead of const
a-tarasyuk Oct 9, 2025
f7980aa
Merge branch 'main' into fix/1374
a-tarasyuk Oct 9, 2025
839d78a
Merge branch 'main' into fix/1374
a-tarasyuk Oct 10, 2025
b600315
Merge branch 'main' into fix/1374
a-tarasyuk Oct 10, 2025
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
31 changes: 26 additions & 5 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ func IsBindableStaticElementAccessExpression(node *Node, excludeThisKeyword bool
return IsLiteralLikeElementAccess(node) &&
((!excludeThisKeyword && node.Expression().Kind == KindThisKeyword) ||
IsEntityNameExpression(node.Expression()) ||
IsBindableStaticAccessExpression(node.Expression() /*excludeThisKeyword*/, true))
IsBindableStaticAccessExpression(node.Expression(), true /*excludeThisKeyword*/))
}

func IsLiteralLikeElementAccess(node *Node) bool {
Expand Down Expand Up @@ -2822,10 +2822,6 @@ func IsModuleExportsAccessExpression(node *Node) bool {
return false
}

func isLiteralLikeElementAccess(node *Node) bool {
return node.Kind == KindElementAccessExpression && IsStringOrNumericLiteralLike(node.AsElementAccessExpression().ArgumentExpression)
}

func IsCheckJSEnabledForFile(sourceFile *SourceFile, compilerOptions *core.CompilerOptions) bool {
if sourceFile.CheckJsDirective != nil {
return sourceFile.CheckJsDirective.Enabled
Expand Down Expand Up @@ -3866,6 +3862,31 @@ func IsImportOrImportEqualsDeclaration(node *Node) bool {
return IsImportDeclaration(node) || IsImportEqualsDeclaration(node)
}

func IsKeyword(token Kind) bool {
return KindFirstKeyword <= token && token <= KindLastKeyword
}

func IsNonContextualKeyword(token Kind) bool {
return IsKeyword(token) && !IsContextualKeyword(token)
}

func HasModifier(node *Node, flags ModifierFlags) bool {
return node.ModifierFlags()&flags != 0
}

func IsExpandoInitializer(initializer *Node) bool {
if initializer == nil {
return false
}
if IsFunctionExpressionOrArrowFunction(initializer) {
return true
}
if IsInJSFile(initializer) {
return IsClassExpression(initializer) || (IsObjectLiteralExpression(initializer) && len(initializer.AsObjectLiteralExpression().Properties.Nodes) == 0)
}
return false
}

func GetContainingFunction(node *Node) *Node {
return FindAncestor(node.Parent, IsFunctionLike)
}
16 changes: 2 additions & 14 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,30 +1018,18 @@ func getInitializerSymbol(symbol *ast.Symbol) *ast.Symbol {
case ast.IsVariableDeclaration(declaration) &&
(declaration.Parent.Flags&ast.NodeFlagsConst != 0 || ast.IsInJSFile(declaration)):
initializer := declaration.Initializer()
if isExpandoInitializer(initializer) {
if ast.IsExpandoInitializer(initializer) {
return initializer.Symbol()
}
case ast.IsBinaryExpression(declaration) && ast.IsInJSFile(declaration):
initializer := declaration.AsBinaryExpression().Right
if isExpandoInitializer(initializer) {
if ast.IsExpandoInitializer(initializer) {
return initializer.Symbol()
}
}
return nil
}

func isExpandoInitializer(initializer *ast.Node) bool {
if initializer == nil {
return false
}
if ast.IsFunctionExpressionOrArrowFunction(initializer) {
return true
} else if ast.IsInJSFile(initializer) {
return ast.IsClassExpression(initializer) || (ast.IsObjectLiteralExpression(initializer) && len(initializer.AsObjectLiteralExpression().Properties.Nodes) == 0)
}
return false
}

func (b *Binder) bindThisPropertyAssignment(node *ast.Node) {
if !ast.IsInJSFile(node) {
return
Expand Down
13 changes: 13 additions & 0 deletions internal/binder/referenceresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ReferenceResolver interface {
GetReferencedImportDeclaration(node *ast.IdentifierNode) *ast.Declaration
GetReferencedValueDeclaration(node *ast.IdentifierNode) *ast.Declaration
GetReferencedValueDeclarations(node *ast.IdentifierNode) []*ast.Declaration
GetElementAccessExpressionName(expression *ast.ElementAccessExpression) string
}

type ReferenceResolverHooks struct {
Expand All @@ -21,6 +22,7 @@ type ReferenceResolverHooks struct {
GetSymbolOfDeclaration func(*ast.Declaration) *ast.Symbol
GetTypeOnlyAliasDeclaration func(symbol *ast.Symbol, include ast.SymbolFlags) *ast.Declaration
GetExportSymbolOfValueSymbolIfExported func(*ast.Symbol) *ast.Symbol
GetElementAccessExpressionName func(*ast.ElementAccessExpression) (string, bool)
}

var _ ReferenceResolver = &referenceResolver{}
Expand Down Expand Up @@ -236,3 +238,14 @@ func (r *referenceResolver) GetReferencedValueDeclarations(node *ast.IdentifierN
}
return declarations
}

func (r *referenceResolver) GetElementAccessExpressionName(expression *ast.ElementAccessExpression) string {
if expression != nil {
if r.hooks.GetElementAccessExpressionName != nil {
if name, ok := r.hooks.GetElementAccessExpressionName(expression); ok {
return name
}
}
}
return ""
}
12 changes: 6 additions & 6 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4234,7 +4234,7 @@ func (c *Checker) checkBaseTypeAccessibility(t *Type, node *ast.Node) {
signatures := c.getSignaturesOfType(t, SignatureKindConstruct)
if len(signatures) != 0 {
declaration := signatures[0].declaration
if declaration != nil && HasModifier(declaration, ast.ModifierFlagsPrivate) {
if declaration != nil && ast.HasModifier(declaration, ast.ModifierFlagsPrivate) {
typeClassDeclaration := ast.GetClassLikeDeclarationOfSymbol(t.symbol)
if !c.isNodeWithinClass(node, typeClassDeclaration) {
c.error(node, diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, c.getFullyQualifiedName(t.symbol, nil))
Expand Down Expand Up @@ -6489,7 +6489,7 @@ func (c *Checker) checkAliasSymbol(node *ast.Node) {
name := node.PropertyNameOrName().Text()
c.addTypeOnlyDeclarationRelatedInfo(c.error(node, message, name), core.IfElse(isType, nil, typeOnlyAlias), name)
}
if isType && node.Kind == ast.KindImportEqualsDeclaration && HasModifier(node, ast.ModifierFlagsExport) {
if isType && node.Kind == ast.KindImportEqualsDeclaration && ast.HasModifier(node, ast.ModifierFlagsExport) {
c.error(node, diagnostics.Cannot_use_export_import_on_a_type_or_type_only_namespace_when_0_is_enabled, c.getIsolatedModulesLikeFlagName())
}
case ast.KindExportSpecifier:
Expand Down Expand Up @@ -6777,7 +6777,7 @@ func (c *Checker) checkUnusedClassMembers(node *ast.Node) {
break // Already would have reported an error on the getter.
}
symbol := c.getSymbolOfDeclaration(member)
if !c.isReferenced(symbol) && (HasModifier(member, ast.ModifierFlagsPrivate) || member.Name() != nil && ast.IsPrivateIdentifier(member.Name())) && member.Flags&ast.NodeFlagsAmbient == 0 {
if !c.isReferenced(symbol) && (ast.HasModifier(member, ast.ModifierFlagsPrivate) || member.Name() != nil && ast.IsPrivateIdentifier(member.Name())) && member.Flags&ast.NodeFlagsAmbient == 0 {
c.reportUnused(member, UnusedKindLocal, NewDiagnosticForNode(member.Name(), diagnostics.X_0_is_declared_but_its_value_is_never_read, c.symbolToString(symbol)))
}
case ast.KindConstructor:
Expand Down Expand Up @@ -8337,7 +8337,7 @@ func (c *Checker) resolveNewExpression(node *ast.Node, candidatesOutArray *[]*Si
}
if expressionType.symbol != nil {
valueDecl := ast.GetClassLikeDeclarationOfSymbol(expressionType.symbol)
if valueDecl != nil && HasModifier(valueDecl, ast.ModifierFlagsAbstract) {
if valueDecl != nil && ast.HasModifier(valueDecl, ast.ModifierFlagsAbstract) {
c.error(node, diagnostics.Cannot_create_an_instance_of_an_abstract_class)
return c.resolveErrorCall(node)
}
Expand Down Expand Up @@ -18912,7 +18912,7 @@ func (c *Checker) getIndexInfosOfIndexSymbol(indexSymbol *ast.Symbol, siblingSym
}
forEachType(c.getTypeFromTypeNode(typeNode), func(keyType *Type) {
if c.isValidIndexKeyType(keyType) && findIndexInfo(indexInfos, keyType) == nil {
indexInfo := c.newIndexInfo(keyType, valueType, HasModifier(declaration, ast.ModifierFlagsReadonly), declaration, nil)
indexInfo := c.newIndexInfo(keyType, valueType, ast.HasModifier(declaration, ast.ModifierFlagsReadonly), declaration, nil)
indexInfos = append(indexInfos, indexInfo)
}
})
Expand Down Expand Up @@ -26823,7 +26823,7 @@ func (c *Checker) markPropertyAsReferenced(prop *ast.Symbol, nodeForCheckWriteOn
if prop.Flags&ast.SymbolFlagsClassMember == 0 || prop.ValueDeclaration == nil {
return
}
hasPrivateModifier := HasModifier(prop.ValueDeclaration, ast.ModifierFlagsPrivate)
hasPrivateModifier := ast.HasModifier(prop.ValueDeclaration, ast.ModifierFlagsPrivate)
hasPrivateIdentifier := prop.ValueDeclaration.Name() != nil && ast.IsPrivateIdentifier(prop.ValueDeclaration.Name())
if !hasPrivateModifier && !hasPrivateIdentifier {
return
Expand Down
12 changes: 12 additions & 0 deletions internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ func (r *emitResolver) getReferenceResolver() binder.ReferenceResolver {
GetSymbolOfDeclaration: r.checker.getSymbolOfDeclaration,
GetTypeOnlyAliasDeclaration: r.checker.getTypeOnlyAliasDeclarationEx,
GetExportSymbolOfValueSymbolIfExported: r.checker.getExportSymbolOfValueSymbolIfExported,
GetElementAccessExpressionName: r.checker.tryGetElementAccessExpressionName,
})
}
return r.referenceResolver
Expand Down Expand Up @@ -879,6 +880,17 @@ func (r *emitResolver) GetReferencedValueDeclarations(node *ast.IdentifierNode)
return r.getReferenceResolver().GetReferencedValueDeclarations(node)
}

func (r *emitResolver) GetElementAccessExpressionName(expression *ast.ElementAccessExpression) string {
if !ast.IsParseTreeNode(expression.AsNode()) {
return ""
}

r.checkerMu.Lock()
defer r.checkerMu.Unlock()

return r.getReferenceResolver().GetElementAccessExpressionName(expression)
}

// TODO: the emit resolver being responsible for some amount of node construction is a very leaky abstraction,
// and requires giving it access to a lot of context it's otherwise not required to have, which also further complicates the API
// and likely reduces performance. There's probably some refactoring that could be done here to simplify this.
Expand Down
8 changes: 2 additions & 6 deletions internal/checker/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ func getSelectedModifierFlags(node *ast.Node, flags ast.ModifierFlags) ast.Modif
return node.ModifierFlags() & flags
}

func HasModifier(node *ast.Node, flags ast.ModifierFlags) bool {
return node.ModifierFlags()&flags != 0
}

func hasReadonlyModifier(node *ast.Node) bool {
return HasModifier(node, ast.ModifierFlagsReadonly)
return ast.HasModifier(node, ast.ModifierFlagsReadonly)
}

func isStaticPrivateIdentifierProperty(s *ast.Symbol) bool {
Expand Down Expand Up @@ -405,7 +401,7 @@ func declarationBelongsToPrivateAmbientMember(declaration *ast.Node) bool {
}

func isPrivateWithinAmbient(node *ast.Node) bool {
return (HasModifier(node, ast.ModifierFlagsPrivate) || ast.IsPrivateIdentifierClassElementDeclaration(node)) && node.Flags&ast.NodeFlagsAmbient != 0
return (ast.HasModifier(node, ast.ModifierFlagsPrivate) || ast.IsPrivateIdentifierClassElementDeclaration(node)) && node.Flags&ast.NodeFlagsAmbient != 0
}

func isTypeAssertion(node *ast.Node) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func getSymbolScope(symbol *ast.Symbol) *ast.Node {
// If this is private property or method, the scope is the containing class
if symbol.Flags&(ast.SymbolFlagsProperty|ast.SymbolFlagsMethod) != 0 {
privateDeclaration := core.Find(declarations, func(d *ast.Node) bool {
return checker.HasModifier(d, ast.ModifierFlagsPrivate) || ast.IsPrivateIdentifierClassElementDeclaration(d)
return ast.HasModifier(d, ast.ModifierFlagsPrivate) || ast.IsPrivateIdentifierClassElementDeclaration(d)
})
if privateDeclaration != nil {
return ast.FindAncestorKind(privateDeclaration, ast.KindClassDeclaration)
Expand Down
14 changes: 5 additions & 9 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (p *Parser) lookAhead(callback func(p *Parser) bool) bool {

func (p *Parser) nextToken() ast.Kind {
// if the keyword had an escape
if isKeyword(p.token) && (p.scanner.HasUnicodeEscape() || p.scanner.HasExtendedUnicodeEscape()) {
if ast.IsKeyword(p.token) && (p.scanner.HasUnicodeEscape() || p.scanner.HasExtendedUnicodeEscape()) {
// issue a parse error for the escape
p.parseErrorAtCurrentToken(diagnostics.Keywords_cannot_contain_escape_characters)
}
Expand Down Expand Up @@ -644,7 +644,7 @@ func (p *Parser) parsingContextErrors(context ParsingContext) {
case PCHeritageClauseElement:
p.parseErrorAtCurrentToken(diagnostics.Expression_expected)
case PCVariableDeclarations:
if isKeyword(p.token) {
if ast.IsKeyword(p.token) {
p.parseErrorAtCurrentToken(diagnostics.X_0_is_not_allowed_as_a_variable_declaration_name, scanner.TokenToString(p.token))
} else {
p.parseErrorAtCurrentToken(diagnostics.Variable_declaration_expected)
Expand All @@ -662,7 +662,7 @@ func (p *Parser) parsingContextErrors(context ParsingContext) {
case PCJSDocParameters:
p.parseErrorAtCurrentToken(diagnostics.Parameter_declaration_expected)
case PCParameters:
if isKeyword(p.token) {
if ast.IsKeyword(p.token) {
p.parseErrorAtCurrentToken(diagnostics.X_0_is_not_allowed_as_a_parameter_name, scanner.TokenToString(p.token))
} else {
p.parseErrorAtCurrentToken(diagnostics.Parameter_declaration_expected)
Expand Down Expand Up @@ -2352,7 +2352,7 @@ func (p *Parser) parseModuleExportName(disallowKeywords bool) (node *ast.Node, n
if p.token == ast.KindStringLiteral {
return p.parseLiteralExpression(false /*intern*/), nameOk
}
if disallowKeywords && isKeyword(p.token) && !p.isIdentifier() {
if disallowKeywords && ast.IsKeyword(p.token) && !p.isIdentifier() {
nameOk = false
}
return p.parseIdentifierName(), nameOk
Expand Down Expand Up @@ -5835,7 +5835,7 @@ func (p *Parser) scanClassMemberStart() bool {
// If we were able to get any potential identifier...
if idToken != ast.KindUnknown {
// If we have a non-keyword identifier, or if we have an accessor, then it's safe to parse.
if !isKeyword(idToken) || idToken == ast.KindSetKeyword || idToken == ast.KindGetKeyword {
if !ast.IsKeyword(idToken) || idToken == ast.KindSetKeyword || idToken == ast.KindGetKeyword {
return true
}
// If it *is* a keyword, but not an accessor, check a little farther along
Expand Down Expand Up @@ -6235,10 +6235,6 @@ func (p *Parser) skipRangeTrivia(textRange core.TextRange) core.TextRange {
return core.NewTextRange(scanner.SkipTrivia(p.sourceText, textRange.Pos()), textRange.End())
}

func isKeyword(token ast.Kind) bool {
return ast.KindFirstKeyword <= token && token <= ast.KindLastKeyword
}

func isReservedWord(token ast.Kind) bool {
return ast.KindFirstReservedWord <= token && token <= ast.KindLastReservedWord
}
Expand Down
Loading