-
Notifications
You must be signed in to change notification settings - Fork 631
findAllReferences inital port #882
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR ports the findAllReferences functionality and updates various components across the codebase. Key changes include:
- Adding new APIs and handlers (e.g. ResolveTripleslashReference in the path package and handleReferences in the LSP server) to support reference lookup.
- Replacing usage of older APIs (like GetAssignmentDeclarationKind) with new ones (GetJSDocAssignmentDeclarationKind) and renaming several utility functions for better export visibility.
- Introducing new testing files for findAllReferences and new core utilities (FlatMap and CoalesceList).
Reviewed Changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
internal/tspath/path.go | Added ResolveTripleslashReference for triple-slash path resolution. |
internal/parser/reparser.go | Updated assignment declaration kind calls to use GetJSDocAssignmentDeclarationKind. |
internal/lsp/server.go | Added handling for ReferenceParams and a new handleReferences function. |
internal/ls/findallreferencesexport_test.go | New test file for export reference lookups. |
internal/ls/findallreferences_test.go | New tests covering various findAllReferences scenarios. |
internal/core/core.go | Introduced FlatMap and CoalesceList utility functions. |
internal/compiler/program.go | Added functions for module resolution improvements and extension handling. |
internal/checker/utilities.go | Renamed internal helper functions (e.g. HasModifier, GetSingleVariableOfVariableStatement). |
internal/checker/services.go | Added GetSymbolsOfParameterPropertyDeclaration for parameter-property handling. |
internal/checker/printer.go | Updated modifier check call in expression printing. |
internal/checker/exports.go | Added exported helper functions for type operations and symbol merging. |
internal/checker/checker.go | Replaced legacy modifier and assignment kind calls with their updated versions. |
internal/binder/nameresolver.go | Updated internal module/enum checks and exported GetLocalSymbolForExportDefault. |
internal/binder/binder.go | Switched to use GetJSDocAssignmentDeclarationKind in assignment binding logic. |
internal/astnav/tokens.go | Added GetTouchingToken as an alternative for token retrieval in AST. |
internal/ast/utilities.go | Introduced several new AST helper functions and renamed utility methods. |
@@ -1029,6 +1029,26 @@ func IsStatic(node *Node) bool { | |||
return IsClassElement(node) && HasStaticModifier(node) || IsClassStaticBlockDeclaration(node) | |||
} | |||
|
|||
func CanHaveSymbol(node *Node) bool { | |||
switch node.Kind { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the JSDoc tags should be dropped from here, replaced with JSExportAssignment JSTypeAliasDeclaration, CommonJSExport (I think).
Also I'm pretty sure Identifier is JS-only and unsupported in Corsa, so can be dropped as well.
@@ -1290,6 +1314,37 @@ func IsThisParameter(node *Node) bool { | |||
return IsParameter(node) && node.Name() != nil && IsThisIdentifier(node.Name()) | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lot of this syntax isn't supported, or supported yet, in Corsa. Merge fom main and use GetAssignmentDeclarationKind as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't read findallreferences.go yet but here are my comments for the rest. I focussed on changes needed for JS. I'll follow up with comments for the big file tomorrow.
|
||
// PropertyAccessKinds | ||
// F.prototype = { ... } | ||
JSDeclarationKindPrototype |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSDeclarationKindPrototype isn't supported in Corsa, and Object.defineProperty is at the very bottom of my TODO list. Same advice as above.
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to wonder if I came up with the name IsJSDocLinkLike
about the time I was reading Fox in Socks' "Luke Luck likes lakes, Luke's duck licks lakes" twice a day.
@@ -2456,6 +2596,10 @@ func GetDeclarationContainer(node *Node) *Node { | |||
}).Parent | |||
} | |||
|
|||
func IsPrototypeAccess(node *Node) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same advice as above
@@ -48,6 +48,17 @@ func (s *Set[T]) AddIfAbsent(key T) bool { | |||
return true | |||
} | |||
|
|||
// Returns true if the key is arleady in the set. Adds the key and returns false otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Returns true if the key is arleady in the set. Adds the key and returns false otherwise. | |
// Returns true if the key is already in the set. Adds the key and returns false otherwise. |
@@ -13673,7 +13673,7 @@ func (c *Checker) getSymbolOfPartOfRightHandSideOfImportEquals(entityName *ast.N | |||
// import a = |b|; // Namespace | |||
// import a = |b.c|; // Value, type, namespace | |||
// import a = |b.c|.d; // Namespace | |||
if entityName.Kind == ast.KindIdentifier && isRightSideOfQualifiedNameOrPropertyAccess(entityName) { | |||
if entityName.Kind == ast.KindIdentifier && IsRightSideOfQualifiedNameOrPropertyAccess(entityName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love the way changing accessibility of a thing requires changing all references to it everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the gift that keeps on giving 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this package, just declare the exported one in exports.go
.
|
||
for meaning != lastIterationMeaning { | ||
// The result is order-sensitive, for instance if initialMeaning == Namespace, and declarations = [class, instantiated module] | ||
// we need to consider both as they initialMeaning intersects with the module in the namespace space, and the module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// we need to consider both as they initialMeaning intersects with the module in the namespace space, and the module | |
// we need to consider both as the initialMeaning intersects with the module in the namespace space, and the module |
return res | ||
} | ||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Go format for doc comments is just //
. Not certain though.
* @param result an array of symbol of found property symbols | ||
* @param previousIterationSymbolsCache a cache of symbol from previous iterations of calling this function to prevent infinite revisiting of the same symbol. | ||
* The value of previousIterationSymbol is undefined when the function is first called. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these don't appear to match the current parameters
} | ||
|
||
res := core.MapNonNil( | ||
core.IfElse(lhsType.Flags() != 0, lhsType.Types(), core.IfElse(lhsType.Symbol() == symbol.Parent, nil, []*checker.Type{lhsType})), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feels like it does too much work for a pair of IfElses, though it would be much longer to write out as if/elses.
return nil | ||
} | ||
|
||
func getSourceOfDefaultedAssignment(node *ast.Node) *ast.Node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed because this is called from unneeded code.
also, defaulted assignments (window.x = window.x || "thing"
) are not supported in Corsa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments on findallreferences.go, nothing substantive.
if ast.IsInJSFile(node) { | ||
binaryExpression := core.IfElse(node.Parent.Kind == ast.KindBinaryExpression, | ||
node.Parent, | ||
core.IfElse(ast.IsAccessExpression(node.Parent) && node.Parent.Parent.Kind == ast.KindBinaryExpression && node.Parent.Parent.AsBinaryExpression().Left == node.Parent, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this branch might not be needed, but I can't tell. better to wait until we have tests to find out.
return nil | ||
} | ||
|
||
if !ast.IsDeclaration(node.Parent) && node.Parent.Kind != ast.KindExportAssignment { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure this is needed, but:
if !ast.IsDeclaration(node.Parent) && node.Parent.Kind != ast.KindExportAssignment { | |
if !ast.IsDeclaration(node.Parent) && node.Parent.Kind != ast.KindExportAssignment && node.Parent.Kind != ast.KindJSExportAssignment { |
// 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aside--if this port didn't skip any Strada jsdoc checks for @private
, then Corsa's going to be more a lot more accurate for JS, since my next PR is to convert @private
into ModifierFlagsPrivate. (Nothing for you to do here, just an observation.)
} | ||
|
||
scope = container | ||
if scope.Kind == ast.KindFunctionExpression { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later: pretty sure this isn't needed.
searchSpaceNode = searchSpaceNode.Parent // re-assign to be the owning class | ||
break | ||
case ast.KindSourceFile: | ||
if ast.IsExternalModule(searchSpaceNode.AsSourceFile()) || isParameterName(thisOrSuperKeyword) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how GetThisContainer
could return a SourceFile for a parameter name but also this
never fails to surprise me.
// Use the parent symbol if the location is commonjs require syntax on javascript files only. | ||
if ast.IsInJSFile(referenceLocation) && referenceLocation.Parent.Kind == ast.KindBindingElement && | ||
ast.IsVariableDeclarationInitializedToRequire(referenceLocation.Parent.Parent.Parent) { | ||
// !!! when findAllReferences has been fully implemented, check if the behavior is the same since isVariableDeclarationInitializedToBareOrAccessedRequire has been removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only the tests that use a property access on a require should fail, eg const x = require('mod').x
This port focuses on the logic needed to resolving symbols within a single file.
Todos after this PR:
lstestutil
capabilities.