fix static method reference non-static (#38730)

* fix static method reference non-static

* fix contextRangeIndex

* fix lint.

* fix style.

* update according to suggestion.

* continue fix.

* use every rather than foreach

* format

* fix

* add comment.

* fix according to review suggestions.

* Update src/services/findAllReferences.ts

* Update src/services/findAllReferences.ts

* Update src/services/findAllReferences.ts

* Update src/services/findAllReferences.ts

* Update src/services/findAllReferences.ts

Co-authored-by: Song Gao <song.gao@laserfiche.com>
Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
This commit is contained in:
ShuiRuTian
2020-07-10 03:21:12 +08:00
committed by GitHub
parent 8e9de9bed2
commit bf1ea6508b
3 changed files with 113 additions and 7 deletions

View File

@@ -1906,13 +1906,28 @@ namespace ts.FindAllReferences {
function populateSearchSymbolSet(symbol: Symbol, location: Node, checker: TypeChecker, isForRename: boolean, providePrefixAndSuffixText: boolean, implementations: boolean): Symbol[] {
const result: Symbol[] = [];
forEachRelatedSymbol<void>(symbol, location, checker, isForRename, !(isForRename && providePrefixAndSuffixText),
(sym, root, base) => { result.push(base || root || sym); },
/*allowBaseTypes*/ () => !implementations);
(sym, root, base) => {
// static method/property and instance method/property might have the same name. Only include static or only include instance.
if (base) {
if (isStatic(symbol) !== isStatic(base)) {
base = undefined;
}
}
result.push(base || root || sym);
},
// when try to find implementation, implementations is true, and not allowed to find base class
/*allowBaseTypes*/() => !implementations);
return result;
}
/**
* @param allowBaseTypes return true means it would try to find in base class or interface.
*/
function forEachRelatedSymbol<T>(
symbol: Symbol, location: Node, checker: TypeChecker, isForRenamePopulateSearchSymbolSet: boolean, onlyIncludeBindingElementAtReferenceLocation: boolean,
/**
* @param baseSymbol This symbol means one property/mehtod from base class or interface when it is not null or undefined,
*/
cbSymbol: (symbol: Symbol, rootSymbol?: Symbol, baseSymbol?: Symbol, kind?: NodeEntryKind) => T | undefined,
allowBaseTypes: (rootSymbol: Symbol) => boolean,
): T | undefined {
@@ -2031,16 +2046,33 @@ namespace ts.FindAllReferences {
readonly symbol: Symbol;
readonly kind: NodeEntryKind | undefined;
}
function isStatic(symbol: Symbol): boolean {
if (!symbol.valueDeclaration) { return false; }
const modifierFlags = getEffectiveModifierFlags(symbol.valueDeclaration);
return !!(modifierFlags & ModifierFlags.Static);
}
function getRelatedSymbol(search: Search, referenceSymbol: Symbol, referenceLocation: Node, state: State): RelatedSymbol | undefined {
const { checker } = state;
return forEachRelatedSymbol(referenceSymbol, referenceLocation, checker, /*isForRenamePopulateSearchSymbolSet*/ false,
/*onlyIncludeBindingElementAtReferenceLocation*/ state.options.use !== FindReferencesUse.Rename || !!state.options.providePrefixAndSuffixTextForRename,
(sym, rootSymbol, baseSymbol, kind): RelatedSymbol | undefined => search.includes(baseSymbol || rootSymbol || sym)
// For a base type, use the symbol for the derived type. For a synthetic (e.g. union) property, use the union symbol.
? { symbol: rootSymbol && !(getCheckFlags(sym) & CheckFlags.Synthetic) ? rootSymbol : sym, kind }
: undefined,
(sym, rootSymbol, baseSymbol, kind): RelatedSymbol | undefined => {
// check whether the symbol used to search itself is just the searched one.
if (baseSymbol) {
// static method/property and instance method/property might have the same name. Only check static or only check instance.
if (isStatic(referenceSymbol) !== isStatic(baseSymbol)) {
baseSymbol = undefined;
}
}
return search.includes(baseSymbol || rootSymbol || sym)
// For a base type, use the symbol for the derived type. For a synthetic (e.g. union) property, use the union symbol.
? { symbol: rootSymbol && !(getCheckFlags(sym) & CheckFlags.Synthetic) ? rootSymbol : sym, kind }
: undefined;
},
/*allowBaseTypes*/ rootSymbol =>
!(search.parents && !search.parents.some(parent => explicitlyInheritsFrom(rootSymbol.parent!, parent, state.inheritsFromCache, checker))));
!(search.parents && !search.parents.some(parent => explicitlyInheritsFrom(rootSymbol.parent!, parent, state.inheritsFromCache, checker)))
);
}
/**