@aurahelper/languages
Version:
Language Libraries to work with XML, Aura, Apex... files. tokenizers, parsers, system classes and much more
338 lines • 18.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BundleAnalyzer = void 0;
var core_1 = require("@aurahelper/core");
var apex_1 = require("../apex");
var javascript_1 = require("../javascript");
var system_1 = require("../system");
var parser_1 = require("./parser");
var baseComponentsDetail = system_1.System.getAuraComponentDetails();
/**
* Class to analize an entire Aura Bundle (including Apex Controllers)
*/
var BundleAnalyzer = /** @class */ (function () {
/**
* Create new BundleAnalyzer instance to analize Aura Bundles
* @param {string} file File path to analyze
* @param {ParserData} [systemData] Parser Data object with data from Project and Salesforce to identify tokens with more precission
*/
function BundleAnalyzer(file, systemData) {
this._file = file;
this._systemData = systemData;
this._fileName;
this._activeFile = this._file;
this._content;
this._component;
this._tabSize = 4;
}
/**
* Method to set file path
* @param {string} file File path value
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setFile = function (file) {
this._file = file;
return this;
};
/**
* Method to set file content
* @param {string} content File content value
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setContent = function (content) {
this._content = content;
return this;
};
/**
* Method to set Parser Data
* @param {ParserData} systemData Parser Data object
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setSystemData = function (systemData) {
this._systemData = systemData;
return this;
};
/**
* Method to set active file path
* @param {string} activeFile active file path value
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setActiveFile = function (activeFile) {
this._activeFile = activeFile;
return this;
};
/**
* Method to set file name
* @param {string} fileName File name value
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setFileName = function (fileName) {
this._fileName = fileName;
return this;
};
/**
* Method to set file tab size
* @param {number} tabSize File tab size value
* @returns {BundleAnalyzer} Return the BundleAnalyzer instance
*/
BundleAnalyzer.prototype.setTabSize = function (tabSize) {
this._tabSize = tabSize;
return this;
};
/**
* Method to analize bundle and get all Aura component info
* @param {Position} [position] Position to get data about cursor position on file
* @returns {AuraComponent | AuraApplication | AuraEvent | undefined} Return the analized Component or undefined if not exists
*/
BundleAnalyzer.prototype.analize = function (position) {
if (this._file && !this._component) {
this._component = new parser_1.AuraParser(this._file, this._fileName).setTabSize(this._tabSize).setContent(this._content).setCursorPosition((core_1.FileChecker.isAuraFile(this._activeFile)) ? position : undefined).parse();
}
else {
this._file = this._component.file;
}
if (!this._component) {
return undefined;
}
var componentName = this._component.fileName || this._fileName;
if (!componentName || !this._file) {
return undefined;
}
if (!this._component.attributes) {
this._component.attributes = [];
}
for (var _i = 0, _a = baseComponentsDetail['root']['component']; _i < _a.length; _i++) {
var rootDetail = _a[_i];
var newAttribute = new core_1.AuraAttribute('');
for (var _b = 0, _c = Object.keys(rootDetail); _b < _c.length; _b++) {
var field = _c[_b];
if (field === 'name' || field === 'type' || field === 'description' || field === 'access' || field === 'required' || field === 'default') {
var data = {
name: new core_1.Token(core_1.AuraTokenTypes.ENTITY.TAG.ATTRIBUTE, field, 0, 0),
value: new core_1.Token(core_1.AuraTokenTypes.ENTITY.TAG.ATTRIBUTE_VALUE, rootDetail[field].toString(), 0, 0),
};
newAttribute[field] = data;
}
}
this._component.attributes.push(newAttribute);
}
var jsController = getController(this._file, (core_1.FileChecker.isAuraControllerJS(this._activeFile)) ? position : undefined);
var helperController = getHelper(this._file, (core_1.FileChecker.isAuraHelperJS(this._activeFile)) ? position : undefined);
if (jsController && jsController.positionData) {
this._component.positionData = jsController.positionData;
}
if (helperController && helperController.positionData) {
this._component.positionData = helperController.positionData;
}
if (this._component instanceof core_1.AuraComponent) {
this._component.controllerFunctions = (jsController) ? jsController.methods : [];
this._component.helperFunctions = (helperController) ? helperController.methods : [];
if (this._component.controller && this._component.controller.value.textToLower) {
var apexNode = void 0;
if (this._systemData && this._systemData.userClassesData && this._systemData.userClassesData[this._component.controller.value.textToLower]) {
apexNode = new apex_1.ApexParser().setSystemData(this._systemData).resolveReferences(this._systemData.userClassesData[this._component.controller.value.textToLower]);
}
else {
var classPath = this._file.replace('aura/' + componentName + '/' + componentName + '.cmp', 'classes/' + this._component.controller.value.text + '.cls');
apexNode = new apex_1.ApexParser(classPath, this._systemData).resolveReferences();
}
if (apexNode instanceof core_1.ApexClass) {
this._component.apexFunctions = apexNode.methods;
}
}
var parentComponent = this._component;
while (parentComponent.extends) {
var parentComponentName = parentComponent.extends.value.text.replace('c:', '');
var parentFile = this._file.replace(new RegExp(componentName, 'g'), parentComponentName);
if (!core_1.FileChecker.isExists(parentFile)) {
break;
}
parentComponent = new parser_1.AuraParser(parentFile).parse();
var jsController_1 = getController(parentFile);
var helperController_1 = getHelper(parentFile);
parentComponent.controllerFunctions = (jsController_1) ? jsController_1.methods : [];
parentComponent.helperFunctions = (helperController_1) ? helperController_1.methods : [];
if (parentComponent.controller && parentComponent.controller.value.textToLower) {
var apexNode = void 0;
if (this._systemData && this._systemData.userClassesData && this._systemData.userClassesData[parentComponent.controller.value.textToLower]) {
apexNode = new apex_1.ApexParser().setSystemData(this._systemData).resolveReferences(this._systemData.userClassesData[parentComponent.controller.value.textToLower]);
}
else {
var classPath = parentFile.replace('aura/' + componentName + '/' + componentName + '.cmp', 'classes\/' + parentComponent.controller.value.text + '.cls');
classPath = classPath.replace('aura/' + componentName + '/' + componentName + '.app', 'classes\/' + parentComponent.controller.value.text + '.cls');
classPath = classPath.replace('aura/' + componentName + '/' + componentName + '.evt', 'classes\/' + parentComponent.controller.value.text + '.cls');
apexNode = new apex_1.ApexParser(classPath, this._systemData).resolveReferences();
}
if (apexNode && apexNode instanceof core_1.ApexClass) {
for (var _d = 0, _e = Object.keys(apexNode.methods); _d < _e.length; _d++) {
var methodKey = _e[_d];
if (this._component && this._component.apexFunctions && !this._component.apexFunctions[methodKey]) {
this._component.apexFunctions[methodKey] = apexNode.methods[methodKey];
}
}
}
}
if (parentComponent.attributes) {
for (var _f = 0, _g = parentComponent.attributes; _f < _g.length; _f++) {
var element = _g[_f];
var exists = false;
for (var _h = 0, _j = this._component.attributes; _h < _j.length; _h++) {
var existingElement = _j[_h];
if (element.name === existingElement.name) {
exists = true;
break;
}
}
if (!exists) {
this._component.attributes.push(element);
}
}
}
if (parentComponent.implements) {
var implementValues = parentComponent.implements.value.text.split(',');
for (var i = 0; i < implementValues.length; i++) {
var value = implementValues[i].trim();
if (this._component && this._component.implementsValues && !this._component.implementsValues.includes(value)) {
this._component.implementsValues.push(value);
}
}
}
if (parentComponent.events) {
for (var _k = 0, _l = parentComponent.events; _k < _l.length; _k++) {
var element = _l[_k];
var existing = false;
for (var _m = 0, _o = this._component.events; _m < _o.length; _m++) {
var existingElement = _o[_m];
if (existingElement === element) {
existing = true;
break;
}
}
if (!existing) {
this._component.events.push(element);
}
}
}
if (parentComponent.handlers) {
for (var _p = 0, _q = parentComponent.handlers; _p < _q.length; _p++) {
var element = _q[_p];
var existing = false;
for (var _r = 0, _s = this._component.handlers; _r < _s.length; _r++) {
var existingElement = _s[_r];
if (existingElement === element) {
existing = true;
break;
}
}
if (!existing) {
this._component.handlers.push(element);
}
}
}
if (parentComponent.controllerFunctions) {
for (var _t = 0, _u = parentComponent.controllerFunctions; _t < _u.length; _t++) {
var element = _u[_t];
var existing = false;
if (!this._component.controllerFunctions) {
this._component.controllerFunctions = [];
}
for (var _v = 0, _w = this._component.controllerFunctions; _v < _w.length; _v++) {
var existingElement = _w[_v];
if (existingElement === element) {
existing = true;
break;
}
}
if (!existing) {
this._component.controllerFunctions.push(element);
}
}
}
if (parentComponent.helperFunctions) {
for (var _x = 0, _y = parentComponent.helperFunctions; _x < _y.length; _x++) {
var element = _y[_x];
var existing = false;
if (!this._component.helperFunctions) {
this._component.helperFunctions = [];
}
for (var _z = 0, _0 = this._component.helperFunctions; _z < _0.length; _z++) {
var existingElement = _0[_z];
if (existingElement === element) {
existing = true;
break;
}
}
if (!existing) {
this._component.helperFunctions.push(element);
}
}
}
}
if (this._component.implementsValues && this._component.implementsValues.length > 0) {
for (var _1 = 0, _2 = this._component.implementsValues; _1 < _2.length; _1++) {
var implement = _2[_1];
var interfaceToCheck = implement;
if (interfaceToCheck.indexOf('lightning:isUrlAddressable') !== -1) {
interfaceToCheck = 'lightning:hasPageReference';
}
var splits = interfaceToCheck.split(':');
var ns = splits[0];
var componentName_1 = splits[1];
var interfaceNS = baseComponentsDetail[ns];
if (interfaceNS) {
var attributes = interfaceNS[componentName_1];
if (attributes && attributes.length > 0) {
for (var _3 = 0, attributes_1 = attributes; _3 < attributes_1.length; _3++) {
var attribute = attributes_1[_3];
var existing = false;
for (var _4 = 0, _5 = this._component.attributes; _4 < _5.length; _4++) {
var existingAttr = _5[_4];
if (attribute.name === existingAttr.name) {
existing = true;
break;
}
}
if (!existing) {
var newAttribute = new core_1.AuraAttribute('');
for (var _6 = 0, _7 = Object.keys(attribute); _6 < _7.length; _6++) {
var field = _7[_6];
if (field === 'name' || field === 'type' || field === 'description' || field === 'access' || field === 'required' || field === 'default') {
var data = {
name: new core_1.Token(core_1.AuraTokenTypes.ENTITY.TAG.ATTRIBUTE, field, 0, 0),
value: new core_1.Token(core_1.AuraTokenTypes.ENTITY.TAG.ATTRIBUTE_VALUE, attribute[field].toString(), 0, 0),
};
newAttribute[field] = data;
}
}
newAttribute.namespace = ns;
newAttribute.tagName = componentName_1;
this._component.attributes.push(newAttribute);
}
}
}
}
}
}
}
return this._component;
};
return BundleAnalyzer;
}());
exports.BundleAnalyzer = BundleAnalyzer;
function getController(componentPath, position) {
var controllerPath = componentPath.replace('.cmp', 'Controller.js').replace('.app', 'Controller.js').replace('.evt', 'Controller.js');
if (core_1.FileChecker.isExists(controllerPath) && core_1.FileChecker.isAuraControllerJS(controllerPath)) {
var jsFileData = new javascript_1.JSParser(controllerPath).setCursorPosition(position).parse();
return jsFileData;
}
return undefined;
}
function getHelper(componentPath, position) {
var helperPath = componentPath.replace('.cmp', 'Helper.js').replace('.app', 'Helper.js').replace('.evt', 'Helper.js');
if (core_1.FileChecker.isExists(helperPath) && core_1.FileChecker.isAuraHelperJS(helperPath)) {
var jsFileData = new javascript_1.JSParser(helperPath).setCursorPosition(position).parse();
return jsFileData;
}
return undefined;
}
//# sourceMappingURL=bundleAnalyzer.js.map