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 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
90 changes: 85 additions & 5 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,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 @@ -2817,10 +2817,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 @@ -2924,6 +2920,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 @@ -3633,3 +3637,79 @@ func GetSemanticJsxChildren(children []*JsxChild) []*JsxChild {
}
})
}

func IsExpandoPropertyDeclaration(node *Node) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the binder already has to detect these patterns, so you shouldn't need to port anything new.

edit: your explanation of why these are needed makes sense, but much of it can be simplified given how much less Corsa supports.

if node == nil {
return false
}
return IsPropertyAccessExpression(node) || IsElementAccessExpression(node) || IsBinaryExpression(node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expando declarations should only be binary expressions now.

/** @type {string} */
f.p

is no longer supported by Corsa, and access expressions don't have a Symbol field.

}

func GetExpandoInitializer(initializer *Node, isPrototypeAssignment bool) *Node {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prototype assignments aren't supported in Corsa
(C.prototype = { ... })

if initializer.Kind == KindCallExpression {
expr := SkipParentheses(initializer.Expression())
if expr.Kind == KindFunctionExpression || expr.Kind == KindArrowFunction {
return initializer
}
return nil
}

if initializer.Kind == KindFunctionExpression || initializer.Kind == KindCallExpression || initializer.Kind == KindArrowFunction {
return initializer
}

if initializer.Kind == KindObjectLiteralExpression && (len(initializer.Properties()) == 0 || isPrototypeAssignment) {
return initializer
}

return nil
}

func GetEffectiveInitializer(node *Node) *Expression {
if IsInJSFile(node) && node.Initializer() != nil && IsBinaryExpression(node.Initializer()) {
initializer := node.Initializer().AsBinaryExpression()
if initializer.OperatorToken.Kind == KindBarBarToken || initializer.OperatorToken.Kind == KindQuestionQuestionToken {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaulting assignments with x.p = x.p || function() {} are not supported in Corsa.

if node.Name() != nil && IsEntityNameExpressionEx(node.Name(), IsInJSFile(node)) && IsSameEntityName(node.Name(), initializer.Left) {
return initializer.Right
}
}
}
return node.Initializer()
}

func GetDeclaredExpandoInitializer(node *Node) *Expression {
initializer := GetEffectiveInitializer(node)
if initializer == nil {
return nil
}
return GetExpandoInitializer(initializer, IsPrototypeAccess(node.Name()))
}

func IsPrototypeAccess(node *Node) bool {
return IsBindableStaticAccessExpression(node, false /*excludeThisKeyword*/)
}

func IsLiteralLikeAccess(node *Node) bool {
return IsPropertyAccessExpression(node) || IsLiteralLikeElementAccess(node)
}

func GetNameOrArgument(node *Expression) *Expression {
if IsPropertyAccessExpression(node) {
return node.Name()
}
return node.AsElementAccessExpression().ArgumentExpression
}

func IsSameEntityName(name *Expression, initializer *Expression) bool {
if IsPropertyNameLiteral(name) && IsPropertyNameLiteral(initializer) {
return name.Text() == initializer.Text()
}
if IsMemberName(name) && IsLiteralLikeAccess(initializer) && (initializer.Expression().Kind == KindThisKeyword || IsIdentifier(initializer.Expression()) &&
(initializer.Expression().Text() == "window" || initializer.Expression().Text() == "self" || initializer.Expression().Text() == "global")) {
return IsSameEntityName(name, GetNameOrArgument(initializer))
}
if IsLiteralLikeAccess(name) && IsLiteralLikeAccess(initializer) {
return GetElementOrPropertyAccessName(name) == GetElementOrPropertyAccessName(initializer) && IsSameEntityName(name.Expression(), initializer.Expression())
}
return false
}
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 symbol *ast.Symbol
if ast.IsVariableDeclaration(node) {
if node.Type() != nil || (!ast.IsInJSFile(node) && !ast.IsVarConstLike(node)) {
return false
}
initializer := ast.GetDeclaredExpandoInitializer(node)
if initializer == nil || !ast.CanHaveSymbol(initializer) {
return false
}
symbol = r.checker.getSymbolOfDeclaration(initializer)
} else if ast.IsFunctionDeclaration(node) {
symbol = r.checker.getSymbolOfDeclaration(node)
}

if symbol == nil || (symbol.Flags&(ast.SymbolFlagsFunction|ast.SymbolFlagsVariable)) == 0 {
return false
}

exports := r.checker.getExportsOfSymbol(symbol)
for _, p := range exports {
if p.Flags&ast.SymbolFlagsValue == 0 {
continue
}
if p.ValueDeclaration == nil {
continue
}
if ast.IsExpandoPropertyDeclaration(p.ValueDeclaration) {
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