Skip to content

Commit ded072e

Browse files
dragomirtitianDanielRosenwasser
authored andcommitted
Fixed find all references for private identifiers. (#35887)
1 parent 75ca641 commit ded072e

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,6 @@ namespace ts {
26252625
case SyntaxKind.NumericLiteral:
26262626
if (isComputedPropertyName(parent)) return parent.parent;
26272627
// falls through
2628-
26292628
case SyntaxKind.Identifier:
26302629
if (isDeclaration(parent)) {
26312630
return parent.name === name ? parent : undefined;
@@ -2643,6 +2642,8 @@ namespace ts {
26432642
? binExp
26442643
: undefined;
26452644
}
2645+
case SyntaxKind.PrivateIdentifier:
2646+
return isDeclaration(parent) && parent.name === name ? parent : undefined;
26462647
default:
26472648
return undefined;
26482649
}

src/services/findAllReferences.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ namespace ts.FindAllReferences {
925925
// The other two forms seem to be handled downstream (e.g. in `skipPastExportOrImportSpecifier`), so special-casing the first form
926926
// here appears to be intentional).
927927
const {
928-
text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || getNonModuleSymbolOfMergedModuleSymbol(symbol) || symbol).escapedName)),
928+
text = stripQuotes(symbolName(getLocalSymbolForExportDefault(symbol) || getNonModuleSymbolOfMergedModuleSymbol(symbol) || symbol)),
929929
allSearchSymbols = [symbol],
930930
} = searchOptions;
931931
const escapedText = escapeLeadingUnderscores(text);
@@ -1087,7 +1087,7 @@ namespace ts.FindAllReferences {
10871087

10881088
// If this is private property or method, the scope is the containing class
10891089
if (flags & (SymbolFlags.Property | SymbolFlags.Method)) {
1090-
const privateDeclaration = find(declarations, d => hasModifier(d, ModifierFlags.Private));
1090+
const privateDeclaration = find(declarations, d => hasModifier(d, ModifierFlags.Private) || isPrivateIdentifierPropertyDeclaration(d));
10911091
if (privateDeclaration) {
10921092
return getAncestor(privateDeclaration, SyntaxKind.ClassDeclaration);
10931093
}
@@ -1231,9 +1231,9 @@ namespace ts.FindAllReferences {
12311231
function isValidReferencePosition(node: Node, searchSymbolName: string): boolean {
12321232
// Compare the length so we filter out strict superstrings of the symbol we are looking for
12331233
switch (node.kind) {
1234+
case SyntaxKind.PrivateIdentifier:
12341235
case SyntaxKind.Identifier:
1235-
return (node as Identifier).text.length === searchSymbolName.length;
1236-
1236+
return (node as PrivateIdentifier | Identifier).text.length === searchSymbolName.length;
12371237
case SyntaxKind.NoSubstitutionTemplateLiteral:
12381238
case SyntaxKind.StringLiteral: {
12391239
const str = node as StringLiteralLike;

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ namespace ts {
721721
* position >= start and (position < end or (position === end && token is literal or keyword or identifier))
722722
*/
723723
export function getTouchingPropertyName(sourceFile: SourceFile, position: number): Node {
724-
return getTouchingToken(sourceFile, position, n => isPropertyNameLiteral(n) || isKeyword(n.kind));
724+
return getTouchingToken(sourceFile, position, n => isPropertyNameLiteral(n) || isKeyword(n.kind) || isPrivateIdentifier(n));
725725
}
726726

727727
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////class C {
4+
//// [|[|{|"isDefinition": true, "isWriteAccess": true, "contextRangeIndex": 0 |}#foo|] = 10;|]
5+
//// constructor() {
6+
//// this.[|{|"isWriteAccess": true|}#foo|] = 20;
7+
//// }
8+
////}
9+
////class D extends C {
10+
//// constructor() {
11+
//// super()
12+
//// this.#foo = 20;
13+
//// }
14+
////}
15+
////class E {
16+
//// [|[|{|"isDefinition": true, "contextRangeIndex": 3 |}#foo|]: number;|]
17+
//// constructor() {
18+
//// this.[|{|"isWriteAccess": true|}#foo|] = 20;
19+
//// }
20+
////}
21+
22+
const [rC0Def, rC0, rC1, rE0Def, rE0, rE1] = test.ranges();
23+
verify.singleReferenceGroup("(property) C.#foo: number", [rC0, rC1]);
24+
verify.singleReferenceGroup("(property) E.#foo: number", [rE0, rE1]);

0 commit comments

Comments
 (0)