From 3fe103c5914a12ad3ae59e785a9eb7e00f7fc84a Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Tue, 22 Aug 2023 18:57:16 +0300 Subject: [PATCH] fix: disable `removeDefinitionFromReferences` in js class prop definitions fixes #165 --- typescript/src/references.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/typescript/src/references.ts b/typescript/src/references.ts index ef56036..62f7200 100644 --- a/typescript/src/references.ts +++ b/typescript/src/references.ts @@ -1,18 +1,34 @@ import { GetConfig } from './types' -import { findChildContainingPositionMaxDepth, approveCast } from './utils' +import { findChildContainingPositionMaxDepth, approveCast, findChildContainingExactPosition, matchParents } from './utils' export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => { proxy.findReferences = (fileName, position) => { let prior = languageService.findReferences(fileName, position) if (!prior) return + const program = languageService.getProgram()! if (c('removeDefinitionFromReferences')) { - prior = prior.map(({ references, ...other }) => ({ - ...other, - references: references.filter(({ isDefinition }) => !isDefinition), - })) + const sourceFile = program.getSourceFile(fileName) + const node = findChildContainingExactPosition(sourceFile!, position) + let filterDefs = true + if ( + node && + node.flags & ts.NodeFlags.JavaScriptFile && + matchParents(node, ['Identifier', 'PropertyAccessExpression'])?.expression.kind === ts.SyntaxKind.ThisKeyword + ) { + // https://github.com/zardoy/typescript-vscode-plugins/issues/165 + filterDefs = false + } + + if (filterDefs) { + prior = prior.map(({ references, ...other }) => ({ + ...other, + references: references.filter(({ isDefinition, textSpan, fileName }) => { + return !isDefinition + }), + })) + } } if (c('removeImportsFromReferences')) { - const program = languageService.getProgram()! const refCountPerFileName: Record< string, {