Skip to content

fix(1374): support declaration emit for expando functions #1399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 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
27 changes: 22 additions & 5 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,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 @@ -2808,10 +2808,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 @@ -2915,6 +2911,14 @@ func IsContextualKeyword(token Kind) bool {
return KindFirstContextualKeyword <= token && token <= KindLastContextualKeyword
}

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

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

func IsThisInTypeQuery(node *Node) bool {
if !IsThisIdentifier(node) {
return false
Expand Down Expand Up @@ -3624,3 +3628,16 @@ func GetSemanticJsxChildren(children []*JsxChild) []*JsxChild {
}
})
}

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
}
16 changes: 2 additions & 14 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,30 +1019,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
59 changes: 57 additions & 2 deletions internal/checker/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,43 @@ func (r *emitResolver) IsLiteralConstDeclaration(node *ast.Node) bool {
}

func (r *emitResolver) IsExpandoFunctionDeclaration(node *ast.Node) bool {
// node = r.emitContext.ParseNode(node)
// !!! TODO: expando function support
if !ast.IsParseTreeNode(node) {
return false
}

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

var declaration *ast.Node
if ast.IsVariableDeclaration(node) {
initializer := node.Initializer()
if node.Type() == nil && (ast.IsInJSFile(node) || ast.IsVarConstLike(node)) && ast.IsExpandoInitializer(initializer) {
declaration = initializer
}
}

if ast.IsFunctionDeclaration(node) {
declaration = node
}

if declaration == nil {
return false
}

symbol := r.checker.getSymbolOfDeclaration(declaration)
if symbol == nil || (symbol.Flags&(ast.SymbolFlagsFunction|ast.SymbolFlagsVariable)) == 0 {
return false
}

exports := r.checker.getExportsOfSymbol(symbol)
for _, p := range exports {
if p.ValueDeclaration == nil || p.Flags&ast.SymbolFlagsValue == 0 || p.Flags&ast.SymbolFlagsAssignment == 0 {
continue
}
if p.ValueDeclaration.Flags&ast.NodeFlagsAmbient == 0 {
return true
}
}
return false
}

Expand Down Expand Up @@ -847,6 +882,26 @@ func (r *emitResolver) GetReferencedValueDeclarations(node *ast.IdentifierNode)
return r.getReferenceResolver().GetReferencedValueDeclarations(node)
}

func (r *emitResolver) GetPropertiesOfContainerFunction(node *ast.Node) []*ast.Symbol {
props := []*ast.Symbol{}

if !ast.IsParseTreeNode(node) {
return props
}

if ast.IsFunctionDeclaration(node) {
r.checkerMu.Lock()
defer r.checkerMu.Unlock()

symbol := r.checker.getSymbolOfDeclaration(node)
if symbol == nil {
return props
}
props = r.checker.getPropertiesOfType(r.checker.getTypeOfSymbol(symbol))
}
return props
}

// 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
14 changes: 5 additions & 9 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,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 @@ -641,7 +641,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 @@ -659,7 +659,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 @@ -2376,7 +2376,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 @@ -6011,7 +6011,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 @@ -6411,10 +6411,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
1 change: 1 addition & 0 deletions internal/printer/emitresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type EmitResolver interface {
GetExternalModuleFileFromDeclaration(node *ast.Node) *ast.SourceFile
GetEffectiveDeclarationFlags(node *ast.Node, flags ast.ModifierFlags) ast.ModifierFlags
GetResolutionModeOverride(node *ast.Node) core.ResolutionMode
GetPropertiesOfContainerFunction(node *ast.Node) []*ast.Symbol

// JSX Emit
GetJsxFactoryEntity(location *ast.Node) *ast.Node
Expand Down
Loading
Loading