Skip to content

findAllReferences inital port #882

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

Merged
merged 18 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
158 changes: 151 additions & 7 deletions internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,26 @@ func IsStatic(node *Node) bool {
return IsClassElement(node) && HasStaticModifier(node) || IsClassStaticBlockDeclaration(node)
}

func CanHaveSymbol(node *Node) bool {
switch node.Kind {
case KindArrowFunction, KindBinaryExpression, KindBindingElement, KindCallExpression, KindCallSignature,
KindClassDeclaration, KindClassExpression, KindClassStaticBlockDeclaration, KindConstructor, KindConstructorType,
KindConstructSignature, KindElementAccessExpression, KindEnumDeclaration, KindEnumMember, KindExportAssignment,
KindExportDeclaration, KindExportSpecifier, KindFunctionDeclaration, KindFunctionExpression, KindFunctionType,
KindGetAccessor, KindIdentifier, KindImportClause, KindImportEqualsDeclaration, KindImportSpecifier,
KindIndexSignature, KindInterfaceDeclaration, KindJSDocCallbackTag,
KindJSDocParameterTag, KindJSDocPropertyTag, KindJSDocSignature, KindJSDocTypedefTag, KindJSDocTypeLiteral,
KindJsxAttribute, KindJsxAttributes, KindJsxSpreadAttribute, KindMappedType, KindMethodDeclaration,
KindMethodSignature, KindModuleDeclaration, KindNamedTupleMember, KindNamespaceExport, KindNamespaceExportDeclaration,
KindNamespaceImport, KindNewExpression, KindNoSubstitutionTemplateLiteral, KindNumericLiteral, KindObjectLiteralExpression,
KindParameter, KindPropertyAccessExpression, KindPropertyAssignment, KindPropertyDeclaration, KindPropertySignature,
KindSetAccessor, KindShorthandPropertyAssignment, KindSourceFile, KindSpreadAssignment, KindStringLiteral,
KindTypeAliasDeclaration, KindTypeLiteral, KindTypeParameter, KindVariableDeclaration:
return true
}
return false
}

func CanHaveIllegalDecorators(node *Node) bool {
switch node.Kind {
case KindPropertyAssignment, KindShorthandPropertyAssignment,
Expand Down Expand Up @@ -1249,6 +1269,10 @@ func IsExternalModuleImportEqualsDeclaration(node *Node) bool {
return node.Kind == KindImportEqualsDeclaration && node.AsImportEqualsDeclaration().ModuleReference.Kind == KindExternalModuleReference
}

func IsModuleOrEnumDeclaration(node *Node) bool {
return node.Kind == KindModuleDeclaration || node.Kind == KindEnumDeclaration
}

func IsLiteralImportTypeNode(node *Node) bool {
return IsImportTypeNode(node) && IsLiteralTypeNode(node.AsImportTypeNode().Argument) && IsStringLiteral(node.AsImportTypeNode().Argument.AsLiteralTypeNode().Literal)
}
Expand Down Expand Up @@ -1290,6 +1314,37 @@ func IsThisParameter(node *Node) bool {
return IsParameter(node) && node.Name() != nil && IsThisIdentifier(node.Name())
}

func IsBindableObjectDefinePropertyCall(expr *Node) bool {
return len(expr.Arguments()) == 3 &&
IsPropertyAccessExpression(expr.Expression()) &&
IsIdentifier(expr.Expression().Expression()) &&
// IdText(expr.Expression().Expression()) == "Object" &&
// IdText(expr.Expression().Name()) == "defineProperty" &&
IsStringOrNumericLiteralLike(expr.Arguments()[1]) &&
IsBindableStaticNameExpression(expr.Arguments()[0] /*excludeThisKeyword*/, true)
}

func IsBindableStaticAccessExpression(node *Node, excludeThisKeyword bool) bool {
return IsPropertyAccessExpression(node) &&
(!excludeThisKeyword && node.Expression().Kind == KindThisKeyword || IsIdentifier(node.Name()) && IsBindableStaticNameExpression(node.Expression() /*excludeThisKeyword*/, true)) ||
IsBindableStaticElementAccessExpression(node, excludeThisKeyword)
}

func IsBindableStaticElementAccessExpression(node *Node, excludeThisKeyword bool) bool {
return IsLiteralLikeElementAccess(node) &&
((!excludeThisKeyword && node.Expression().Kind == KindThisKeyword) ||
IsEntityNameExpression(node.Expression()) ||
IsBindableStaticAccessExpression(node.Expression() /*excludeThisKeyword*/, true))
}

func IsLiteralLikeElementAccess(node *Node) bool {
return IsElementAccessExpression(node) && IsStringOrNumericLiteralLike(node.AsElementAccessExpression().ArgumentExpression)
}

func IsBindableStaticNameExpression(node *Node, excludeThisKeyword bool) bool {
return IsEntityNameExpression(node) || IsBindableStaticAccessExpression(node, excludeThisKeyword)
}

// Does not handle signed numeric names like `a[+0]` - handling those would require handling prefix unary expressions
// throughout late binding handling as well, which is awkward (but ultimately probably doable if there is demand)
func GetElementOrPropertyAccessArgumentExpressionOrName(node *Node) *Node {
Expand All @@ -1314,6 +1369,13 @@ func GetElementOrPropertyAccessName(node *Node) string {
return name.Text()
}

func GetInitializerOfBinaryExpression(expr *BinaryExpression) *Expression {
for IsBinaryExpression(expr.Right) {
expr = expr.Right.AsBinaryExpression()
}
return expr.Right.Expression()
}

func IsExpressionWithTypeArgumentsInClassExtendsClause(node *Node) bool {
return TryGetClassExtendingExpressionWithTypeArguments(node) != nil
}
Expand Down Expand Up @@ -1417,6 +1479,19 @@ const (
JSDeclarationKindThisProperty
/// F.name = expr, F[name] = expr
JSDeclarationKindProperty

// PropertyAccessKinds
// F.prototype = { ... }
JSDeclarationKindPrototype
// Object.defineProperty(x, 'name', { value: any, writable?: boolean (false by default) });
// Object.defineProperty(x, 'name', { get: Function, set: Function });
// Object.defineProperty(x, 'name', { get: Function });
// Object.defineProperty(x, 'name', { set: Function });
JSDeclarationKindObjectDefinePropertyValue
// Object.defineProperty(exports || module.exports, 'name', ...);
JSDeclarationKindObjectDefinePropertyExports
// Object.defineProperty(Foo.prototype, 'name', ...);
JSDeclarationKindObjectDefinePrototypeProperty
)

func GetAssignmentDeclarationKind(bin *BinaryExpression) JSDeclarationKind {
Expand Down Expand Up @@ -1444,6 +1519,37 @@ func hasJSBindableName(node *Node) bool {
return IsIdentifier(name) || IsStringLiteralLike(name)
}

func GetAssignmentDeclarationPropertyAccessKind(lhs *Node) JSDeclarationKind {
if lhs.Expression().Kind == KindThisKeyword {
return JSDeclarationKindThisProperty
} else if IsModuleExportsAccessExpression(lhs) {
// module.exports = expr
return JSDeclarationKindModuleExports
} else if IsBindableStaticNameExpression(lhs.Expression() /*excludeThisKeyword*/, true) {
if IsPrototypeAccess(lhs.Expression()) {
// F.G....prototype.x = expr
return JSDeclarationKindPrototypeProperty
}

nextToLast := lhs
for nextToLast.Expression().Kind != KindIdentifier {
nextToLast = nextToLast.Expression()
}
idText := nextToLast.Expression().AsIdentifier().Text
if (idText == "exports" || idText == "module" && GetElementOrPropertyAccessName(nextToLast) == "exports") &&
// ExportsProperty does not support binding with computed names
IsBindableStaticAccessExpression(lhs, false) {
// exports.name = expr OR module.exports.name = expr OR exports["name"] = expr ...
return JSDeclarationKindExportsProperty
}
if IsBindableStaticNameExpression(lhs /*excludeThisKeyword*/, true) || (IsElementAccessExpression(lhs) && IsDynamicName(lhs)) {
// F.G...x = expr
return JSDeclarationKindProperty
}
}
return JSDeclarationKindNone
}

/**
* A declaration has a dynamic name if all of the following are true:
* 1. The declaration has a computed property name.
Expand Down Expand Up @@ -1562,14 +1668,14 @@ func GetImplementsHeritageClauseElements(node *Node) []*ExpressionWithTypeArgume
}

func GetHeritageElements(node *Node, kind Kind) []*Node {
clause := getHeritageClause(node, kind)
clause := GetHeritageClause(node, kind)
if clause != nil {
return clause.AsHeritageClause().Types.Nodes
}
return nil
}

func getHeritageClause(node *Node, kind Kind) *Node {
func GetHeritageClause(node *Node, kind Kind) *Node {
clauses := getHeritageClauses(node)
if clauses != nil {
for _, clause := range clauses.Nodes {
Expand Down Expand Up @@ -1658,6 +1764,40 @@ func GetThisContainer(node *Node, includeArrowFunctions bool, includeClassComput
}
}

func GetSuperContainer(node *Node, stopOnFunctions bool) *Node {
for {
node = node.Parent
if node == nil {
return nil
}
switch node.Kind {
case KindComputedPropertyName:
node = node.Parent
break
case KindFunctionDeclaration, KindFunctionExpression, KindArrowFunction:
if !stopOnFunctions {
continue
}
// falls through

case KindPropertyDeclaration, KindPropertySignature, KindMethodDeclaration, KindMethodSignature, KindConstructor, KindGetAccessor, KindSetAccessor, KindClassStaticBlockDeclaration:
return node
case KindDecorator:
// Decorators are always applied outside of the body of a class or method.
if node.Parent.Kind == KindParameter && IsClassElement(node.Parent.Parent) {
// If the decorator's parent is a Parameter, we resolve the this container from
// the grandparent class declaration.
node = node.Parent.Parent
} else if IsClassElement(node.Parent) {
// If the decorator's parent is a class element, we resolve the 'this' container
// from the parent class declaration.
node = node.Parent
}
break
}
}
}

func GetImmediatelyInvokedFunctionExpression(fn *Node) *Node {
if IsFunctionExpressionOrArrowFunction(fn) {
prev := fn
Expand Down Expand Up @@ -1766,13 +1906,13 @@ func IsExpressionNode(node *Node) bool {
for node.Parent.Kind == KindQualifiedName {
node = node.Parent
}
return IsTypeQueryNode(node.Parent) || isJSDocLinkLike(node.Parent) || isJSXTagName(node)
return IsTypeQueryNode(node.Parent) || IsJSDocLinkLike(node.Parent) || isJSXTagName(node)
case KindJSDocMemberName:
return IsTypeQueryNode(node.Parent) || isJSDocLinkLike(node.Parent) || isJSXTagName(node)
return IsTypeQueryNode(node.Parent) || IsJSDocLinkLike(node.Parent) || isJSXTagName(node)
case KindPrivateIdentifier:
return IsBinaryExpression(node.Parent) && node.Parent.AsBinaryExpression().Left == node && node.Parent.AsBinaryExpression().OperatorToken.Kind == KindInKeyword
case KindIdentifier:
if IsTypeQueryNode(node.Parent) || isJSDocLinkLike(node.Parent) || isJSXTagName(node) {
if IsTypeQueryNode(node.Parent) || IsJSDocLinkLike(node.Parent) || isJSXTagName(node) {
return true
}
fallthrough
Expand Down Expand Up @@ -1915,7 +2055,7 @@ func isPartOfTypeExpressionWithTypeArguments(node *Node) bool {
return IsHeritageClause(parent) && (!IsClassLike(parent.Parent) || parent.AsHeritageClause().Token == KindImplementsKeyword)
}

func isJSDocLinkLike(node *Node) bool {
func IsJSDocLinkLike(node *Node) bool {
return NodeKindIs(node, KindJSDocLink, KindJSDocLinkCode, KindJSDocLinkPlain)
}

Expand Down Expand Up @@ -1978,7 +2118,7 @@ func IsJSDocCommentContainingNode(node *Node) bool {
node.Kind == KindJSDocText ||
node.Kind == KindJSDocTypeLiteral ||
node.Kind == KindJSDocSignature ||
isJSDocLinkLike(node) ||
IsJSDocLinkLike(node) ||
IsJSDocTag(node)
}

Expand Down Expand Up @@ -2456,6 +2596,10 @@ func GetDeclarationContainer(node *Node) *Node {
}).Parent
}

func IsPrototypeAccess(node *Node) bool {
return IsBindableStaticAccessExpression(node, false) && GetElementOrPropertyAccessName(node) == "prototype"
}

// Indicates that a symbol is an alias that does not merge with a local declaration.
// OR Is a JSContainer which may merge an alias with a local declaration
func IsNonLocalAlias(symbol *Symbol, excludes SymbolFlags) bool {
Expand Down
4 changes: 4 additions & 0 deletions internal/astnav/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func GetTouchingPropertyName(sourceFile *ast.SourceFile, position int) *ast.Node
})
}

func GetTouchingToken(sourceFile *ast.SourceFile, position int) *ast.Node {
return getTokenAtPosition(sourceFile, position, false /*allowPositionInLeadingTrivia*/, nil)
}

func GetTokenAtPosition(sourceFile *ast.SourceFile, position int) *ast.Node {
return getTokenAtPosition(sourceFile, position, true /*allowPositionInLeadingTrivia*/, nil)
}
Expand Down
10 changes: 3 additions & 7 deletions internal/binder/nameresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ loop:
// (it refers to the constant type of the expression instead)
return nil
}
if isModuleOrEnumDeclaration(location) && lastLocation != nil && location.Name() == lastLocation {
if ast.IsModuleOrEnumDeclaration(location) && lastLocation != nil && location.Name() == lastLocation {
// If lastLocation is the name of a namespace or enum, skip the parent since it will have is own locals that could
// conflict.
lastLocation = location
Expand Down Expand Up @@ -99,7 +99,7 @@ loop:
// name of that export default matches.
result = moduleExports[ast.InternalSymbolNameDefault]
if result != nil {
localSymbol := getLocalSymbolForExportDefault(result)
localSymbol := GetLocalSymbolForExportDefault(result)
if localSymbol != nil && result.Flags&meaning != 0 && localSymbol.Name == name {
break loop
}
Expand Down Expand Up @@ -448,11 +448,7 @@ func (r *NameResolver) argumentsSymbol() *ast.Symbol {
return r.ArgumentsSymbol
}

func isModuleOrEnumDeclaration(node *ast.Node) bool {
return node.Kind == ast.KindModuleDeclaration || node.Kind == ast.KindEnumDeclaration
}

func getLocalSymbolForExportDefault(symbol *ast.Symbol) *ast.Symbol {
func GetLocalSymbolForExportDefault(symbol *ast.Symbol) *ast.Symbol {
if !isExportDefaultSymbol(symbol) || len(symbol.Declarations) == 0 {
return nil
}
Expand Down
Loading