ui5plugin-parser
Version:
122 lines (121 loc) • 4.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSFileReader = void 0;
const path = require("path");
const ParserPool_1 = require("../../../../parser/pool/ParserPool");
const CustomJSClass_1 = require("../../ui5class/js/CustomJSClass");
const AbstractFileReader_1 = require("./AbstractFileReader");
class JSFileReader extends AbstractFileReader_1.AbstractFileReader {
setParser(parser) {
this._parser = parser;
}
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 = ".js";
const manifest = ParserPool_1.default.getManifestForClass(className);
if (manifest) {
if (isController) {
extension = ".controller.js";
}
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;
}
//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 CustomJSClass_1.CustomJSClass) {
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 CustomJSClass_1.CustomJSClass) {
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() {
this._readAllJSFiles();
}
_readAllJSFiles() {
const classPaths = this._readFilesInWorkspace("**/*.js");
const classNames = classPaths.map(path => this.getClassNameFromPath(path));
classNames.forEach(className => {
if (className) {
try {
this._classFactory.getUIClass(className);
}
catch (error) {
console.error(`Error parsing ${className}: ${error.message}`);
}
}
});
}
getAllJSClassNamesFromProject() {
let classNames = [];
const classPaths = this._readFilesInWorkspace("**/*.js");
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 CustomJSClass_1.CustomJSClass) {
UIClass.relatedViewsAndFragments = undefined;
this._classFactory.enrichTypesInCustomClass(UIClass);
}
});
}
}
exports.JSFileReader = JSFileReader;