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 4 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
212 changes: 203 additions & 9 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 @@ -1354,7 +1416,7 @@ func GetNonAssignedNameOfDeclaration(declaration *Node) *Node {
switch declaration.Kind {
case KindBinaryExpression:
bin := declaration.AsBinaryExpression()
kind := GetAssignmentDeclarationKind(bin)
kind := GetJSDocAssignmentDeclarationKind(bin)
if kind == JSDeclarationKindProperty || kind == JSDeclarationKindThisProperty {
return GetElementOrPropertyAccessArgumentExpressionOrName(bin.Left)
}
Expand Down Expand Up @@ -1419,7 +1481,7 @@ const (
JSDeclarationKindProperty
)

func GetAssignmentDeclarationKind(bin *BinaryExpression) JSDeclarationKind {
func GetJSDocAssignmentDeclarationKind(bin *BinaryExpression) JSDeclarationKind {
if bin.OperatorToken.Kind != KindEqualsToken || !IsAccessExpression(bin.Left) {
return JSDeclarationKindNone
}
Expand Down Expand Up @@ -1562,14 +1624,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 +1720,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 +1862,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 +2011,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 +2074,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 +2552,104 @@ func GetDeclarationContainer(node *Node) *Node {
}).Parent
}

type AssignmentDeclarationKind = int32

const (
AssignmentDeclarationKindNone = AssignmentDeclarationKind(iota)
/// exports.name = expr
/// module.exports.name = expr
AssignmentDeclarationKindExportsProperty
/// module.exports = expr
AssignmentDeclarationKindModuleExports
/// className.prototype.name = expr
AssignmentDeclarationKindPrototypeProperty
/// this.name = expr
AssignmentDeclarationKindThisProperty
// F.name = expr
AssignmentDeclarationKindProperty
// F.prototype = { ... }
AssignmentDeclarationKindPrototype
// 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 });
AssignmentDeclarationKindObjectDefinePropertyValue
// Object.defineProperty(exports || module.exports, 'name', ...);
AssignmentDeclarationKindObjectDefinePropertyExports
// Object.defineProperty(Foo.prototype, 'name', ...);
AssignmentDeclarationKindObjectDefinePrototypeProperty
)

// / Given a BinaryExpression, returns SpecialPropertyAssignmentKind for the various kinds of property
// / assignments we treat as special in the binder
func GetAssignmentDeclarationKind(expr *Expression /*BinaryExpression | CallExpression*/) AssignmentDeclarationKind {
var special AssignmentDeclarationKind
switch expr.Kind {
case KindCallExpression:
if !IsBindableObjectDefinePropertyCall(expr) {
special = AssignmentDeclarationKindNone
} else {
entityName := expr.Arguments()[0]
if IsExportsIdentifier(entityName) || IsModuleExportsAccessExpression(entityName) {
special = AssignmentDeclarationKindObjectDefinePropertyExports
} else if IsBindableStaticAccessExpression(entityName, false) && GetElementOrPropertyAccessName(entityName) == "prototype" {
special = AssignmentDeclarationKindObjectDefinePrototypeProperty
} else {
special = AssignmentDeclarationKindObjectDefinePropertyValue
}
}
default: // KindBinaryExpression
binExpr := expr.AsBinaryExpression()
if binExpr.OperatorToken.Kind != KindEqualsToken || !IsAccessExpression(binExpr.Left) || isVoidZero(GetRightMostAssignedExpression(expr)) {
special = AssignmentDeclarationKindNone
} else if IsBindableStaticNameExpression(binExpr.Left.Expression() /*excludeThisKeyword*/, true) &&
GetElementOrPropertyAccessName(binExpr.Left) == "prototype" &&
IsObjectLiteralExpression(GetInitializerOfBinaryExpression(binExpr)) {
// F.prototype = { ... }
special = AssignmentDeclarationKindPrototype
} else {
special = GetAssignmentDeclarationPropertyAccessKind(binExpr.Left)
}
}
return core.IfElse(special == AssignmentDeclarationKindProperty || IsInJSFile(expr), special, AssignmentDeclarationKindNone)
}

func GetAssignmentDeclarationPropertyAccessKind(lhs *Node) AssignmentDeclarationKind {
if lhs.Expression().Kind == KindThisKeyword {
return AssignmentDeclarationKindThisProperty
} else if IsModuleExportsAccessExpression(lhs) {
// module.exports = expr
return AssignmentDeclarationKindModuleExports
} else if IsBindableStaticNameExpression(lhs.Expression() /*excludeThisKeyword*/, true) {
if IsPrototypeAccess(lhs.Expression()) {
// F.G....prototype.x = expr
return AssignmentDeclarationKindPrototypeProperty
}

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 AssignmentDeclarationKindExportsProperty
}
if IsBindableStaticNameExpression(lhs /*excludeThisKeyword*/, true) || (IsElementAccessExpression(lhs) && IsDynamicName(lhs)) {
// F.G...x = expr
return AssignmentDeclarationKindProperty
}
}

return AssignmentDeclarationKindNone
}

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
2 changes: 1 addition & 1 deletion internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (b *Binder) bind(node *ast.Node) bool {
setFlowNode(node, b.currentFlow)
}
case ast.KindBinaryExpression:
switch ast.GetAssignmentDeclarationKind(node.AsBinaryExpression()) {
switch ast.GetJSDocAssignmentDeclarationKind(node.AsBinaryExpression()) {
case ast.JSDeclarationKindProperty:
b.bindFunctionPropertyAssignment(node)
case ast.JSDeclarationKindThisProperty:
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