Skip to content

Commit f6f429b

Browse files
committed
more
1 parent 5c98631 commit f6f429b

File tree

55 files changed

+569
-210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+569
-210
lines changed

internal/fourslash/_scripts/failingTests.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ TestGetJavaScriptQuickInfo8
198198
TestGetJavaScriptSyntacticDiagnostics24
199199
TestGetOccurrencesIfElseBroken
200200
TestGetQuickInfoForIntersectionTypes
201-
TestGetRenameInfoTests1
202201
TestHoverOverComment
203202
TestImportCompletionsPackageJsonExportsSpecifierEndsInTs
204203
TestImportCompletionsPackageJsonExportsTrailingSlash1
@@ -434,7 +433,6 @@ TestReferencesForStatementKeywords
434433
TestReferencesInEmptyFile
435434
TestRegexDetection
436435
TestRenameForAliasingExport02
437-
TestRenameForDefaultExport04
438436
TestRenameFromNodeModulesDep1
439437
TestRenameFromNodeModulesDep2
440438
TestRenameFromNodeModulesDep3

internal/ls/findallreferences.go

Lines changed: 173 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,105 @@ func getReferenceEntriesForShorthandPropertyAssignment(node *ast.Node, checker *
13221322
}
13231323
}
13241324

1325+
func climbPastPropertyAccess(node *ast.Node) *ast.Node {
1326+
if isRightSideOfPropertyAccess(node) {
1327+
return node.Parent
1328+
}
1329+
return node
1330+
}
1331+
1332+
func isNewExpressionTarget(node *ast.Node) bool {
1333+
if node.Parent == nil {
1334+
return false
1335+
}
1336+
return node.Parent.Kind == ast.KindNewExpression && node.Parent.AsNewExpression().Expression == node
1337+
}
1338+
1339+
func isCallExpressionTarget(node *ast.Node) bool {
1340+
if node.Parent == nil {
1341+
return false
1342+
}
1343+
return node.Parent.Kind == ast.KindCallExpression && node.Parent.AsCallExpression().Expression == node
1344+
}
1345+
1346+
func isMethodOrAccessor(node *ast.Node) bool {
1347+
return node.Kind == ast.KindMethodDeclaration || node.Kind == ast.KindGetAccessor || node.Kind == ast.KindSetAccessor
1348+
}
1349+
1350+
func tryGetClassByExtendingIdentifier(node *ast.Node) *ast.ClassLikeDeclaration {
1351+
return ast.TryGetClassExtendingExpressionWithTypeArguments(climbPastPropertyAccess(node).Parent)
1352+
}
1353+
1354+
func getClassConstructorSymbol(classSymbol *ast.Symbol) *ast.Symbol {
1355+
if classSymbol.Members == nil {
1356+
return nil
1357+
}
1358+
return classSymbol.Members[ast.InternalSymbolNameConstructor]
1359+
}
1360+
1361+
func hasOwnConstructor(classDeclaration *ast.ClassLikeDeclaration) bool {
1362+
return getClassConstructorSymbol(classDeclaration.Symbol()) != nil
1363+
}
1364+
1365+
func findOwnConstructorReferences(classSymbol *ast.Symbol, sourceFile *ast.SourceFile, addNode func(*ast.Node)) {
1366+
constructorSymbol := getClassConstructorSymbol(classSymbol)
1367+
if constructorSymbol != nil && len(constructorSymbol.Declarations) > 0 {
1368+
for _, decl := range constructorSymbol.Declarations {
1369+
if decl.Kind == ast.KindConstructor {
1370+
if ctrKeyword := findChildOfKind(decl, ast.KindConstructorKeyword, sourceFile); ctrKeyword != nil {
1371+
addNode(ctrKeyword)
1372+
}
1373+
}
1374+
}
1375+
}
1376+
1377+
if classSymbol.Exports != nil {
1378+
for _, member := range classSymbol.Exports {
1379+
decl := member.ValueDeclaration
1380+
if decl != nil && decl.Kind == ast.KindMethodDeclaration {
1381+
body := decl.Body()
1382+
if body != nil {
1383+
forEachDescendantOfKind(body, ast.KindThisKeyword, func(thisKeyword *ast.Node) {
1384+
if isNewExpressionTarget(thisKeyword) {
1385+
addNode(thisKeyword)
1386+
}
1387+
})
1388+
}
1389+
}
1390+
}
1391+
}
1392+
}
1393+
1394+
func findSuperConstructorAccesses(classDeclaration *ast.ClassLikeDeclaration, addNode func(*ast.Node)) {
1395+
constructorSymbol := getClassConstructorSymbol(classDeclaration.Symbol())
1396+
if constructorSymbol == nil || len(constructorSymbol.Declarations) == 0 {
1397+
return
1398+
}
1399+
1400+
for _, decl := range constructorSymbol.Declarations {
1401+
if decl.Kind == ast.KindConstructor {
1402+
body := decl.Body()
1403+
if body != nil {
1404+
forEachDescendantOfKind(body, ast.KindSuperKeyword, func(node *ast.Node) {
1405+
if isCallExpressionTarget(node) {
1406+
addNode(node)
1407+
}
1408+
})
1409+
}
1410+
}
1411+
}
1412+
}
1413+
1414+
func forEachDescendantOfKind(node *ast.Node, kind ast.Kind, action func(*ast.Node)) {
1415+
node.ForEachChild(func(child *ast.Node) bool {
1416+
if child.Kind == kind {
1417+
action(child)
1418+
}
1419+
forEachDescendantOfKind(child, kind, action)
1420+
return false
1421+
})
1422+
}
1423+
13251424
func (state *refState) addImplementationReferences(refNode *ast.Node, addRef func(*ast.Node)) {
13261425
// Check if we found a function/propertyAssignment/method with an implementation or initializer
13271426
if ast.IsDeclarationName(refNode) && isImplementation(refNode.Parent) {
@@ -1483,11 +1582,9 @@ func (state *refState) getReferencesAtLocation(sourceFile *ast.SourceFile, posit
14831582
state.addReference(referenceLocation, relatedSymbol, relatedSymbolKind)
14841583
}
14851584
case "constructor":
1486-
// !!! not implemented
1487-
// state.addConstructorReferences(referenceLocation, sourceFile, search)
1585+
state.addConstructorReferences(referenceLocation, relatedSymbol, search, addReferencesHere)
14881586
case "class":
1489-
// !!! not implemented
1490-
// state.addClassStaticThisReferences(referenceLocation, search)
1587+
state.addClassStaticThisReferences(referenceLocation, relatedSymbol, search, addReferencesHere)
14911588
}
14921589

14931590
// Use the parent symbol if the location is commonjs require syntax on javascript files only.
@@ -1504,6 +1601,78 @@ func (state *refState) getReferencesAtLocation(sourceFile *ast.SourceFile, posit
15041601
state.getImportOrExportReferences(referenceLocation, referenceSymbol, search)
15051602
}
15061603

1604+
func (state *refState) addConstructorReferences(referenceLocation *ast.Node, symbol *ast.Symbol, search *refSearch, addReferencesHere bool) {
1605+
if isNewExpressionTarget(referenceLocation) && addReferencesHere {
1606+
state.addReference(referenceLocation, symbol, entryKindNone)
1607+
}
1608+
1609+
pusher := func() func(*ast.Node, entryKind) {
1610+
return state.referenceAdder(search.symbol)
1611+
}
1612+
1613+
if ast.IsClassLike(referenceLocation.Parent) {
1614+
// This is the class declaration containing the constructor.
1615+
sourceFile := ast.GetSourceFileOfNode(referenceLocation)
1616+
findOwnConstructorReferences(search.symbol, sourceFile, func(n *ast.Node) {
1617+
pusher()(n, entryKindNone)
1618+
})
1619+
} else {
1620+
// If this class appears in `extends C`, then the extending class' "super" calls are references.
1621+
if classExtending := tryGetClassByExtendingIdentifier(referenceLocation); classExtending != nil {
1622+
findSuperConstructorAccesses(classExtending, func(n *ast.Node) {
1623+
pusher()(n, entryKindNone)
1624+
})
1625+
state.findInheritedConstructorReferences(classExtending)
1626+
}
1627+
}
1628+
}
1629+
1630+
func (state *refState) addClassStaticThisReferences(referenceLocation *ast.Node, symbol *ast.Symbol, search *refSearch, addReferencesHere bool) {
1631+
if addReferencesHere {
1632+
state.addReference(referenceLocation, symbol, entryKindNone)
1633+
}
1634+
1635+
classLike := referenceLocation.Parent
1636+
if state.options.use == referenceUseRename || !ast.IsClassLike(classLike) {
1637+
return
1638+
}
1639+
1640+
addRef := state.referenceAdder(search.symbol)
1641+
members := classLike.Members()
1642+
if members == nil {
1643+
return
1644+
}
1645+
for _, member := range members {
1646+
if !(isMethodOrAccessor(member) && ast.HasStaticModifier(member)) {
1647+
continue
1648+
}
1649+
body := member.Body()
1650+
if body != nil {
1651+
var cb func(*ast.Node)
1652+
cb = func(node *ast.Node) {
1653+
if node.Kind == ast.KindThisKeyword {
1654+
addRef(node, entryKindNone)
1655+
} else if !ast.IsFunctionLike(node) && !ast.IsClassLike(node) {
1656+
node.ForEachChild(func(child *ast.Node) bool {
1657+
cb(child)
1658+
return false
1659+
})
1660+
}
1661+
}
1662+
cb(body)
1663+
}
1664+
}
1665+
}
1666+
1667+
func (state *refState) findInheritedConstructorReferences(classDeclaration *ast.ClassLikeDeclaration) {
1668+
if hasOwnConstructor(classDeclaration) {
1669+
return
1670+
}
1671+
classSymbol := classDeclaration.Symbol()
1672+
search := state.createSearch(nil, classSymbol, ImpExpKindUnknown, "", nil)
1673+
state.getReferencesInContainerOrFiles(classSymbol, search)
1674+
}
1675+
15071676
func (state *refState) getImportOrExportReferences(referenceLocation *ast.Node, referenceSymbol *ast.Symbol, search *refSearch) {
15081677
importOrExport := getImportOrExportSymbol(referenceLocation, referenceSymbol, state.checker, search.comingFrom == ImpExpKindExport)
15091678
if importOrExport == nil {

testdata/baselines/reference/fourslash/documentHighlights/documentHighlightInExport1.baseline.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// === documentHighlights ===
22
// === /documentHighlightInExport1.ts ===
3-
// class /*HIGHLIGHTS*/C {}
3+
// class /*HIGHLIGHTS*/[|C|] {}
44
// export { C as D };
55

66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// === documentHighlights ===
22
// === /B.ts ===
3-
// export default class /*HIGHLIGHTS*/C {
3+
// export default class /*HIGHLIGHTS*/[|C|] {
44
// test() {
55
// }
66
// }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// === findAllReferences ===
22
// === /constructorFindAllReferences3.ts ===
33
// export class C {
4-
// /*FIND ALL REFS*/constructor() { }
4+
// /*FIND ALL REFS*/[|constructor|]() { }
55
// public foo() { }
66
// }
77
//
8-
// new C().foo();
8+
// new [|C|]().foo();

testdata/baselines/reference/fourslash/findAllReferences/findAllReferencesLinkTag1.baseline.jsonc

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,41 @@
141141

142142
// === findAllReferences ===
143143
// === /findAllReferencesLinkTag1.ts ===
144-
// class C/*FIND ALL REFS*/ {
144+
// class [|C|]/*FIND ALL REFS*/ {
145145
// m() { }
146146
// n = 1
147147
// static s() { }
148-
// // --- (line: 5) skipped ---
148+
// /**
149+
// * {@link m}
150+
// * @see {m}
151+
// * {@link [|C|].m}
152+
// * @see {[|C|].m}
153+
// * {@link [|C|]#m}
154+
// * @see {[|C|]#m}
155+
// * {@link [|C|].prototype.m}
156+
// * @see {[|C|].prototype.m}
157+
// */
158+
// p() { }
159+
// /**
160+
// * {@link n}
161+
// * @see {n}
162+
// * {@link [|C|].n}
163+
// * @see {[|C|].n}
164+
// * {@link [|C|]#n}
165+
// * @see {[|C|]#n}
166+
// * {@link [|C|].prototype.n}
167+
// * @see {[|C|].prototype.n}
168+
// */
169+
// q() { }
170+
// /**
171+
// * {@link s}
172+
// * @see {s}
173+
// * {@link [|C|].s}
174+
// * @see {[|C|].s}
175+
// */
176+
// r() { }
177+
// }
178+
// // --- (line: 35) skipped ---
149179

150180

151181

testdata/baselines/reference/fourslash/findAllReferences/findAllReferencesLinkTag2.baseline.jsonc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,31 @@
8282
// === findAllReferences ===
8383
// === /findAllReferencesLinkTag2.ts ===
8484
// namespace NPR {
85-
// export class Consider/*FIND ALL REFS*/ {
85+
// export class [|Consider|]/*FIND ALL REFS*/ {
8686
// This = class {
8787
// show() { }
8888
// }
89-
// // --- (line: 6) skipped ---
89+
// m() { }
90+
// }
91+
// /**
92+
// * @see {[|Consider|].prototype.m}
93+
// * {@link [|Consider|]#m}
94+
// * @see {[|Consider|]#This#show}
95+
// * {@link [|Consider|].This.show}
96+
// * @see {NPR.[|Consider|]#This#show}
97+
// * {@link NPR.[|Consider|].This#show}
98+
// * @see {NPR.[|Consider|]#This.show} # doesn't parse trailing .
99+
// * @see {NPR.[|Consider|].This.show}
100+
// */
101+
// export function ref() { }
102+
// }
103+
// /**
104+
// * {@link NPR.[|Consider|]#This#show hello hello}
105+
// * {@link NPR.[|Consider|].This#show}
106+
// * @see {NPR.[|Consider|]#This.show} # doesn't parse trailing .
107+
// * @see {NPR.[|Consider|].This.show}
108+
// */
109+
// export function outerref() { }
90110

91111

92112

testdata/baselines/reference/fourslash/findAllReferences/findAllReferencesLinkTag3.baseline.jsonc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,31 @@
8282
// === findAllReferences ===
8383
// === /findAllReferencesLinkTag3.ts ===
8484
// namespace NPR {
85-
// export class Consider/*FIND ALL REFS*/ {
85+
// export class [|Consider|]/*FIND ALL REFS*/ {
8686
// This = class {
8787
// show() { }
8888
// }
89-
// // --- (line: 6) skipped ---
89+
// m() { }
90+
// }
91+
// /**
92+
// * {@linkcode [|Consider|].prototype.m}
93+
// * {@linkplain [|Consider|]#m}
94+
// * {@linkcode [|Consider|]#This#show}
95+
// * {@linkplain [|Consider|].This.show}
96+
// * {@linkcode NPR.[|Consider|]#This#show}
97+
// * {@linkplain NPR.[|Consider|].This#show}
98+
// * {@linkcode NPR.[|Consider|]#This.show} # doesn't parse trailing .
99+
// * {@linkcode NPR.[|Consider|].This.show}
100+
// */
101+
// export function ref() { }
102+
// }
103+
// /**
104+
// * {@linkplain NPR.[|Consider|]#This#show hello hello}
105+
// * {@linkplain NPR.[|Consider|].This#show}
106+
// * {@linkcode NPR.[|Consider|]#This.show} # doesn't parse trailing .
107+
// * {@linkcode NPR.[|Consider|].This.show}
108+
// */
109+
// export function outerref() { }
90110

91111

92112

0 commit comments

Comments
 (0)