ui5plugin-parser
Version:
75 lines (74 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReusableMethods = void 0;
const CustomJSClass_1 = require("./parsing/ui5class/js/CustomJSClass");
class ReusableMethods {
constructor(documentTransformer) {
this._documentTransformer = documentTransformer;
}
getPositionOfTheLastUIDefine(document) {
let position;
const UIClass = this._documentTransformer.toCustomUIClass(document);
if (UIClass && UIClass instanceof CustomJSClass_1.CustomJSClass) {
const mainFunction = UIClass.fileContent?.body[0]?.expression;
const definePaths = mainFunction?.arguments[0]?.elements;
let insertPosition = 0;
if (definePaths?.length) {
const lastDefinePath = definePaths[definePaths.length - 1];
insertPosition = lastDefinePath.end;
}
else {
insertPosition = mainFunction?.arguments[0]?.start;
}
if (insertPosition) {
position = insertPosition;
}
}
return position;
}
getIfPositionIsInTheLastOrAfterLastMember(UIClass, position) {
const currentMethod = UIClass.methods.find(method => method.node?.start < position && method.node?.end > position);
const positionIsInMethod = !!currentMethod;
const positionIsAfterLastMethod = positionIsInMethod
? false
: this._getIfPositionIsAfterLastMember(UIClass, position);
return positionIsInMethod || positionIsAfterLastMethod;
}
_getIfPositionIsAfterLastMember(UIClass, position) {
let isPositionAfterLastMethod = false;
const properties = UIClass.acornClassBody?.properties || [];
const lastProperty = properties[properties.length - 1];
if (lastProperty) {
isPositionAfterLastMethod = lastProperty.end <= position && UIClass.acornClassBody.end > position;
}
return isPositionAfterLastMethod;
}
getIfMethodIsLastOne(UIClass, method) {
let currentMethodIsLastMethod = false;
const propertyValues = UIClass.acornClassBody?.properties?.map((node) => node.value);
if (propertyValues) {
const methodsInClassBody = UIClass.methods.filter(method => {
return propertyValues.includes(method.node);
});
currentMethodIsLastMethod = methodsInClassBody.indexOf(method) === methodsInClassBody.length - 1;
}
return currentMethodIsLastMethod;
}
getIfPositionIsInPropertyName(UIClass, position) {
let bPositionIsInPropertyName = true;
const positionIsBetweenProperties = !!UIClass.acornClassBody.properties?.find((node, index) => {
let correctNode = false;
const nextNode = UIClass.acornClassBody.properties[index + 1];
if (nextNode && node.end < position && nextNode.start > position) {
correctNode = true;
}
return correctNode;
});
const positionIsInPropertyKey = !!UIClass.acornClassBody.properties?.find((node) => {
return node.key?.start <= position && node.key?.end >= position;
});
bPositionIsInPropertyName = positionIsBetweenProperties || positionIsInPropertyKey;
return bPositionIsInPropertyName;
}
}
exports.ReusableMethods = ReusableMethods;