rooibos-roku
Version:
simple, flexible, fun brightscript test framework for roku scenegraph apps - roku brighterscript plugin
120 lines • 5.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getScopeForSuite = exports.getPathValuePartAsString = exports.getStringPathFromDottedGet = exports.getRootObjectFromDottedGet = exports.getAllDottedGetParts = exports.functionRequiresReturnValue = exports.sanitizeBsJsonString = exports.addOverriddenMethod = void 0;
const brighterscript_1 = require("brighterscript");
const brighterscript = require("brighterscript");
const Diagnostics_1 = require("../utils/Diagnostics");
function addOverriddenMethod(file, annotation, target, name, source, editor) {
var _a;
let functionSource = `
function ${name}()
${source}
end function
`;
let { statements, diagnostics } = brighterscript.Parser.parse(functionSource, { mode: brighterscript.ParseMode.BrighterScript });
let error = '';
if (statements && statements.length > 0) {
let statement = statements[0];
if (statement.func.body.statements.length > 0) {
let p = brighterscript.createToken(brighterscript.TokenKind.Public, 'public', target.range);
let o = brighterscript.createToken(brighterscript.TokenKind.Override, 'override', target.range);
let n = brighterscript.createIdentifier(name, target.range);
let method = new brighterscript.ClassMethodStatement(p, n, statement.func, o);
//bsc has a quirk where it auto-adds a `new` method if missing. That messes with our AST editing, so
//trigger that functionality BEFORE performing AstEditor operations. TODO remove this whenever bsc stops doing this.
// eslint-disable-next-line @typescript-eslint/dot-notation
(_a = target['ensureConstructorFunctionExists']) === null || _a === void 0 ? void 0 : _a.call(target);
editor.addToArray(target.body, target.body.length, method);
return true;
}
}
error = (diagnostics === null || diagnostics === void 0 ? void 0 : diagnostics.length) > 0 ? diagnostics[0].message : 'unknown error';
(0, Diagnostics_1.diagnosticCorruptTestProduced)(file, annotation, error, functionSource);
return false;
}
exports.addOverriddenMethod = addOverriddenMethod;
function sanitizeBsJsonString(text) {
return `"${text ? text.replace(/"/g, '\'') : ''}"`;
}
exports.sanitizeBsJsonString = sanitizeBsJsonString;
function functionRequiresReturnValue(statement) {
const returnTypeToken = statement.func.returnTypeToken;
const functionType = statement.func.functionType;
return !(((functionType === null || functionType === void 0 ? void 0 : functionType.kind) === brighterscript_1.TokenKind.Sub && (returnTypeToken === undefined || (returnTypeToken === null || returnTypeToken === void 0 ? void 0 : returnTypeToken.kind) === brighterscript_1.TokenKind.Void)) || (returnTypeToken === null || returnTypeToken === void 0 ? void 0 : returnTypeToken.kind) === brighterscript_1.TokenKind.Void);
}
exports.functionRequiresReturnValue = functionRequiresReturnValue;
function getAllDottedGetParts(dg) {
var _a, _b;
let parts = [(_a = dg === null || dg === void 0 ? void 0 : dg.name) === null || _a === void 0 ? void 0 : _a.text];
let nextPart = dg.obj;
while (brighterscript.isDottedGetExpression(nextPart) || brighterscript.isVariableExpression(nextPart)) {
parts.push((_b = nextPart === null || nextPart === void 0 ? void 0 : nextPart.name) === null || _b === void 0 ? void 0 : _b.text);
nextPart = brighterscript.isDottedGetExpression(nextPart) ? nextPart.obj : undefined;
}
return parts.reverse();
}
exports.getAllDottedGetParts = getAllDottedGetParts;
function getRootObjectFromDottedGet(value) {
let root;
if (brighterscript.isDottedGetExpression(value) || brighterscript.isIndexedGetExpression(value)) {
root = value.obj;
while (root.obj) {
root = root.obj;
}
}
else {
root = value;
}
return root;
}
exports.getRootObjectFromDottedGet = getRootObjectFromDottedGet;
function getStringPathFromDottedGet(value) {
let parts = [getPathValuePartAsString(value)];
let root;
root = value.obj;
while (root) {
if (brighterscript.isCallExpression(root) || brighterscript.isCallfuncExpression(root)) {
return undefined;
}
parts.push(`${getPathValuePartAsString(root)}`);
root = root.obj;
}
let joinedParts = parts.reverse().join('.');
return joinedParts === '' ? undefined : brighterscript.createStringLiteral(joinedParts);
}
exports.getStringPathFromDottedGet = getStringPathFromDottedGet;
function getPathValuePartAsString(expr) {
if (brighterscript.isCallExpression(expr) || brighterscript.isCallfuncExpression(expr)) {
return undefined;
}
if (brighterscript.isVariableExpression(expr)) {
return expr.name.text;
}
if (!expr) {
return undefined;
}
if (brighterscript.isDottedGetExpression(expr)) {
return expr.name.text;
}
else if (brighterscript.isIndexedGetExpression(expr)) {
if (brighterscript.isLiteralExpression(expr.index)) {
return `${expr.index.token.text.replace(/^"/, '').replace(/"$/, '')}`;
}
else if (brighterscript.isVariableExpression(expr.index)) {
return `${expr.index.name.text}`;
}
}
}
exports.getPathValuePartAsString = getPathValuePartAsString;
function getScopeForSuite(testSuite) {
if (testSuite.isNodeTest) {
return testSuite.file.program.getScopesForFile(testSuite.file).find((scope) => {
return (0, brighterscript_1.isXmlScope)(scope) && scope.xmlFile.componentName.text === testSuite.generatedNodeName;
});
}
else {
return testSuite.file.program.getFirstScopeForFile(testSuite.file);
}
}
exports.getScopeForSuite = getScopeForSuite;
//# sourceMappingURL=Utils.js.map