@@ -154,7 +154,7 @@ namespace ts.FindAllReferences {
154154 textSpan : getTextSpan ( node , sourceFile ) ,
155155 isWriteAccess : isWriteAccessForReference ( node ) ,
156156 isDefinition : node . kind === SyntaxKind . DefaultKeyword
157- || isAnyDeclarationName ( node )
157+ || ! ! getDeclarationFromName ( node )
158158 || isLiteralComputedPropertyDeclarationName ( node ) ,
159159 isInString,
160160 } ;
@@ -223,7 +223,65 @@ namespace ts.FindAllReferences {
223223
224224 /** A node is considered a writeAccess iff it is a name of a declaration or a target of an assignment */
225225 function isWriteAccessForReference ( node : Node ) : boolean {
226- return node . kind === SyntaxKind . DefaultKeyword || isAnyDeclarationName ( node ) || isWriteAccess ( node ) ;
226+ const decl = getDeclarationFromName ( node ) ;
227+ return ! ! decl && declarationIsWriteAccess ( decl ) || node . kind === SyntaxKind . DefaultKeyword || isWriteAccess ( node ) ;
228+ }
229+
230+ /**
231+ * True if 'decl' provides a value, as in `function f() {}`;
232+ * false if 'decl' is just a location for a future write, as in 'let x;'
233+ */
234+ function declarationIsWriteAccess ( decl : Declaration ) : boolean {
235+ // Consider anything in an ambient declaration to be a write access since it may be coming from JS.
236+ if ( ! ! ( decl . flags & NodeFlags . Ambient ) ) return true ;
237+
238+ switch ( decl . kind ) {
239+ case SyntaxKind . BinaryExpression :
240+ case SyntaxKind . BindingElement :
241+ case SyntaxKind . ClassDeclaration :
242+ case SyntaxKind . ClassExpression :
243+ case SyntaxKind . DefaultKeyword :
244+ case SyntaxKind . EnumDeclaration :
245+ case SyntaxKind . EnumMember :
246+ case SyntaxKind . ExportSpecifier :
247+ case SyntaxKind . ImportClause : // default import
248+ case SyntaxKind . ImportEqualsDeclaration :
249+ case SyntaxKind . ImportSpecifier :
250+ case SyntaxKind . InterfaceDeclaration :
251+ case SyntaxKind . JSDocCallbackTag :
252+ case SyntaxKind . JSDocTypedefTag :
253+ case SyntaxKind . JsxAttribute :
254+ case SyntaxKind . ModuleDeclaration :
255+ case SyntaxKind . NamespaceExportDeclaration :
256+ case SyntaxKind . NamespaceImport :
257+ case SyntaxKind . Parameter :
258+ case SyntaxKind . PropertyAssignment :
259+ case SyntaxKind . ShorthandPropertyAssignment :
260+ case SyntaxKind . TypeAliasDeclaration :
261+ case SyntaxKind . TypeParameter :
262+ return true ;
263+
264+ case SyntaxKind . FunctionDeclaration :
265+ case SyntaxKind . FunctionExpression :
266+ case SyntaxKind . Constructor :
267+ case SyntaxKind . MethodDeclaration :
268+ case SyntaxKind . GetAccessor :
269+ case SyntaxKind . SetAccessor :
270+ return ! ! ( decl as FunctionDeclaration | FunctionExpression | ConstructorDeclaration | MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration ) . body ;
271+
272+ case SyntaxKind . VariableDeclaration :
273+ case SyntaxKind . PropertyDeclaration :
274+ return ! ! ( decl as VariableDeclaration | PropertyDeclaration ) . initializer || isCatchClause ( decl . parent ) ;
275+
276+ case SyntaxKind . MethodSignature :
277+ case SyntaxKind . PropertySignature :
278+ case SyntaxKind . JSDocPropertyTag :
279+ case SyntaxKind . JSDocParameterTag :
280+ return false ;
281+
282+ default :
283+ return Debug . failBadSyntaxKind ( decl ) ;
284+ }
227285 }
228286}
229287
0 commit comments