ui5plugin-parser
Version:
66 lines (65 loc) • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSReferenceFinder = void 0;
/* eslint-disable @typescript-eslint/indent */
const path = require("path");
const ts_morph_1 = require("ts-morph");
const CustomTSClass_1 = require("../../ui5class/ts/CustomTSClass");
const CustomTSObject_1 = require("../../ui5class/ts/CustomTSObject");
const RangeAdapter_1 = require("../range/adapters/RangeAdapter");
const ReferenceFinderBase_1 = require("./ReferenceFinderBase");
class TSReferenceFinder extends ReferenceFinderBase_1.default {
getReferenceLocations(member) {
const locations = [];
const UIClass = this._parser.classFactory.getUIClass(member.owner);
if (UIClass instanceof CustomTSClass_1.CustomTSClass || UIClass instanceof CustomTSObject_1.CustomTSObject) {
this._addLocationsFromUIClass(member, UIClass, locations);
if (member.name !== "constructor") {
const viewsAndFragments = this._parser.classFactory.getViewsAndFragmentsOfControlHierarchically(UIClass, [], true, true, true);
const viewAndFragmentArray = [...viewsAndFragments.fragments, ...viewsAndFragments.views];
viewAndFragmentArray.forEach(XMLDoc => {
this._addLocationsFromXMLDocument(XMLDoc, member, locations);
});
}
}
return locations;
}
_addLocationsFromUIClass(member, UIClass, locations) {
const cache = UIClass.getCache("referenceCodeLensCache") || {};
if (cache[member.owner]?.[`_${member.name}`]) {
locations.push(...cache[member.owner][`_${member.name}`]);
return;
}
const references = member.node?.findReferences().flatMap(reference => reference.getReferences());
const currentLocations = references
?.filter(reference => {
const notAReferenceToItself = path.resolve(reference.getSourceFile().getFilePath()) !== UIClass.fsPath ||
(!member.node?.isKind(ts_morph_1.ts.SyntaxKind.Constructor) &&
reference.getNode().getStart() !==
member.node.getNameNode().getStart()) ||
(member.node?.isKind(ts_morph_1.ts.SyntaxKind.Constructor) &&
reference.getNode().getStart() !== member.node.getStart());
return notAReferenceToItself;
})
.map(reference => {
const range = RangeAdapter_1.RangeAdapter.offsetsRange(reference.getSourceFile().getFullText(), reference.getTextSpan().getStart(), reference.getTextSpan().getEnd());
let referenceData;
if (range) {
referenceData = [range, path.resolve(reference.getSourceFile().getFilePath())];
}
return referenceData;
})
.filter(rangeData => !!rangeData)
.map(rangeData => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return { filePath: rangeData[1] || "", range: rangeData[0] };
}) ?? [];
if (!cache[member.owner]) {
cache[member.owner] = {};
}
cache[member.owner][`_${member.name}`] = currentLocations;
UIClass.setCache("referenceCodeLensCache", cache);
locations.push(...currentLocations);
}
}
exports.TSReferenceFinder = TSReferenceFinder;