ui5plugin-parser
Version:
122 lines (121 loc) • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSFileReader = void 0;
const glob = require("glob");
const path = require("path");
const ParserPool_1 = require("../../../../parser/pool/ParserPool");
const CustomTSClass_1 = require("../../ui5class/ts/CustomTSClass");
const AbstractFileReader_1 = require("./AbstractFileReader");
const escapedFileSeparator = "\\" + path.sep;
class TSFileReader extends AbstractFileReader_1.AbstractFileReader {
convertClassNameToFSPath(className, isController = false, isFragment = false, isView = false, isFolder = false) {
const parser = ParserPool_1.default.getParserForCustomClass(className);
if (parser !== this._parser) {
return parser?.fileReader.convertClassNameToFSPath(className, isController, isFragment, isView, isFolder);
}
let FSPath;
let extension = ".ts";
const manifest = ParserPool_1.default.getManifestForClass(className);
if (manifest) {
if (isController) {
extension = ".controller.ts";
}
else if (isFragment) {
extension = ".fragment.xml";
}
else if (isView) {
extension = ".view.xml";
}
else if (isFolder) {
extension = "";
}
const separator = path.sep;
FSPath = `${manifest.fsPath}${className
.replace(manifest.componentName, "")
.replace(/\./g, separator)
.trim()}${extension}`;
}
return FSPath;
}
_readFilesInWorkspace(path) {
const wsFolderFSPath = this._parser.workspaceFolder.fsPath.replace(new RegExp(`${escapedFileSeparator}`, "g"), "/");
const exclusions = this._configHandler.getExcludeFolderPatterns();
exclusions.push("**/*.d.ts");
exclusions.push("**/src-gen/**");
const exclusionPaths = exclusions.map(excludeString => {
return `${wsFolderFSPath}/${excludeString}`;
});
const filePaths = glob
.sync(`${wsFolderFSPath}/${path}`, {
ignore: exclusionPaths
})
.map(filePath => (0, AbstractFileReader_1.toNative)(filePath));
return filePaths;
}
//TODO: Refactor this
getClassNameFromView(controllerClassName, controlId) {
let className;
const view = this.getViewForController(controllerClassName);
if (view) {
className = this._getClassOfControlIdFromView(view, controlId);
if (!className) {
view.fragments.find(fragment => {
className = this._getClassOfControlIdFromView(fragment, controlId);
return !!className;
});
}
}
if (!className) {
const UIClass = this._classFactory.getUIClass(controllerClassName);
if (UIClass instanceof CustomTSClass_1.CustomTSClass) {
const fragmentsAndViews = this._classFactory.getViewsAndFragmentsOfControlHierarchically(UIClass);
const fragmentAndViewArray = [...fragmentsAndViews.views, ...fragmentsAndViews.fragments];
fragmentAndViewArray.find(view => {
className = this._getClassOfControlIdFromView(view, controlId);
return !!className;
});
}
}
return className;
}
getFragmentsMentionedInClass(className) {
let fragments = [];
const UIClass = this._classFactory.getUIClass(className);
if (UIClass instanceof CustomTSClass_1.CustomTSClass) {
fragments = ParserPool_1.default.getAllFragments().filter(fragment => {
return UIClass.classText.indexOf(`"${fragment.name}"`) > -1;
});
const fragmentsInFragment = [];
fragments.forEach(fragment => {
fragmentsInFragment.push(...this.getFragmentsInXMLFile(fragment));
});
fragments.push(...fragmentsInFragment);
}
return fragments;
}
readCustomClasses() {
//not needed, handled by ts-morph
}
getAllJSClassNamesFromProject() {
let classNames = [];
const classPaths = this._readFilesInWorkspace("**/*.ts");
classNames = classPaths.reduce((accumulator, viewPath) => {
const path = this.getClassNameFromPath(viewPath);
if (path) {
accumulator.push(path);
}
return accumulator;
}, []);
return classNames;
}
reEnrichAllCustomClasses() {
const UIClasses = this._classFactory.getAllCustomUIClasses();
UIClasses.forEach(UIClass => {
if (UIClass instanceof CustomTSClass_1.CustomTSClass) {
UIClass.relatedViewsAndFragments = undefined;
this._classFactory.enrichTypesInCustomClass(UIClass);
}
});
}
}
exports.TSFileReader = TSFileReader;