UNPKG

maestro-roku-bsc-plugin

Version:

Visual studio plugin for maestro brightscript development

110 lines (108 loc) 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const brighterscript_1 = require("brighterscript"); const Diagnostics_1 = require("../utils/Diagnostics"); const NodeClass_1 = require("./NodeClass"); /* Crude brighterscript class processor */ class NodeClassUtil { constructor(fileMap, fileFactory) { this.fileMap = fileMap; this.fileFactory = fileFactory; } addFile(file, mFile) { var _a, _b, _c, _d, _e; for (let nodeClass of this.fileMap.nodeClassesByPath[file.pathAbsolute] || []) { delete this.fileMap.nodeClasses[nodeClass.name]; delete mFile.nodeClasses[nodeClass.name]; } delete this.fileMap.nodeClassesByPath[file.pathAbsolute]; for (let cs of file.parser.references.classStatements) { let annotation = (_a = cs.annotations) === null || _a === void 0 ? void 0 : _a.find((a) => a.name.toLowerCase() === 'task' || a.name.toLowerCase() === 'node'); let nodeType = NodeClass_1.NodeClassType.none; if (annotation) { let lazyAnnotation = (_b = cs.annotations) === null || _b === void 0 ? void 0 : _b.find((a) => a.name.toLowerCase() === 'lazy'); let waitInitAnnotation = (_c = cs.annotations) === null || _c === void 0 ? void 0 : _c.find((a) => a.name.toLowerCase() === 'observerswaitinitialize'); nodeType = annotation.name.toLowerCase() === 'task' ? NodeClass_1.NodeClassType.task : NodeClass_1.NodeClassType.node; let args = annotation.getArguments(); let nodeName = args.length === 2 ? (_d = args[0]) === null || _d === void 0 ? void 0 : _d.trim() : undefined; let extendsName = args.length === 2 ? (_e = args[1]) === null || _e === void 0 ? void 0 : _e.trim() : undefined; if (args.length < 2 || !nodeName || !extendsName) { (0, Diagnostics_1.addNodeClassBadDeclaration)(file, '', annotation.range.start.line, annotation.range.start.character + 1); console.log(' bad class in ', file.pkgPath); } else if (this.fileMap.nodeClasses[nodeName]) { (0, Diagnostics_1.addNodeClassDuplicateName)(file, nodeName, annotation.range.start.line, annotation.range.start.character + 1); console.log(' duplicate node in ', file.pkgPath); } else { let isValid = true; if (nodeType === NodeClass_1.NodeClassType.node) { let newFunc = cs.memberMap.new; if (newFunc && newFunc.func.parameters.length !== 0) { (0, Diagnostics_1.addNodeClassWrongNewSignature)(file, annotation.range.start.line, annotation.range.start.character); isValid = false; console.log(' wrong sig in ', file.pkgPath); } } if (nodeType === NodeClass_1.NodeClassType.task) { let executeFunction = cs.memberMap.execute; if (!executeFunction || executeFunction.func.parameters.length !== 1) { (0, Diagnostics_1.addNodeClassNoExtendNodeFound)(file, nodeName, extendsName, annotation.range.start.line, annotation.range.start.character + 1); } } if (isValid) { //is valid let nodeClass = new NodeClass_1.NodeClass(nodeType, file, cs, nodeName, extendsName, annotation, this.fileMap, lazyAnnotation !== undefined, waitInitAnnotation !== undefined); this.fileMap.nodeClasses[nodeClass.generatedNodeName] = nodeClass; let nodeClasses = this.fileMap.nodeClassesByPath[file.pathAbsolute]; if (!nodeClasses) { nodeClasses = []; this.fileMap.nodeClassesByPath[file.pathAbsolute] = nodeClasses; } mFile.nodeClasses.set(file.pathAbsolute, nodeClass); nodeClasses.push(nodeClass); // eslint-disable-next-line @typescript-eslint/dot-notation cs['_isNodeClass'] = true; } else { console.log('not adding invalid class', cs.name.text); } } } } } generateTestCode(program) { let codeText = ` function tests_maestro_nodeClassUtils_createNodeClass(clazz, nodeTop = {}, nodeGlobal = {}) instance = invalid name = mc_getFunctionName(clazz) if name = invalid return invalid end if name = lcase(name) if false ? "maestro nodeclass test-utils"`; for (let nc of Object.values(this.fileMap.nodeClasses)) { codeText += `\n else if name = "${nc.classStatement.getName(brighterscript_1.ParseMode.BrightScript).toLowerCase()}" 'bs:disable-next-line instance = __${nc.classStatement.getName(brighterscript_1.ParseMode.BrightScript)}_builder() `; } codeText += ` end if if instance <> invalid instance.top = nodeTop instance.global = nodeGlobal instance.new() end if return instance end function `; let brsFile = this.fileFactory.addFile(`source/roku_modules/maestro/tests/TestUtils.brs`, codeText); brsFile.parser.invalidateReferences(); } } exports.default = NodeClassUtil; //# sourceMappingURL=NodeClassUtil.js.map