Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
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 @@ -2815,10 +2815,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 @@ -3860,3 +3856,28 @@ func IsJSDocNameReferenceContext(node *Node) bool {
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
}
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
12 changes: 6 additions & 6 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4224,7 +4224,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 @@ -6479,7 +6479,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 @@ -6767,7 +6767,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 @@ -8325,7 +8325,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 @@ -18826,7 +18826,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 @@ -26746,7 +26746,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
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 @@ -2338,7 +2338,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 @@ -5815,7 +5815,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 @@ -6215,10 +6215,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