UNPKG

ts-budgie

Version:

Converts TypeScript code to Budgie.

89 lines 4.66 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var budgie_1 = require("budgie"); var ts = require("typescript"); var budgieLine_1 = require("../../output/budgieLine"); var transformation_1 = require("../../output/transformation"); var aliases_1 = require("../../parsing/aliases"); var visitor_1 = require("../visitor"); var unknownOperatorComplaint = "Unknown operator kind."; var BinaryExpressionVisitor = /** @class */ (function (_super) { __extends(BinaryExpressionVisitor, _super); function BinaryExpressionVisitor() { return _super !== null && _super.apply(this, arguments) || this; } BinaryExpressionVisitor.prototype.visit = function (node) { var _this = this; if (node.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword) { return [ transformation_1.Transformation.fromNode(node, this.sourceFile, [ new budgieLine_1.BudgieLine(budgie_1.CommandNames.InstanceOf, node.left.getText(this.sourceFile), node.right.getText(this.sourceFile)), ]), ]; } if (node.operatorToken.kind === ts.SyntaxKind.InKeyword) { return [ transformation_1.Transformation.fromNode(node, this.sourceFile, [ new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryContainsKey, node.left.getText(this.sourceFile), node.right.getText(this.sourceFile)), ]), ]; } var contents = this.collectOperationContents(node).map(function (content) { return _this.recurseOnOperationContents(content); }); return [transformation_1.Transformation.fromNode(node, this.sourceFile, [new (budgieLine_1.BudgieLine.bind.apply(budgieLine_1.BudgieLine, __spreadArrays([void 0, budgie_1.CommandNames.Operation], contents)))()])]; }; BinaryExpressionVisitor.prototype.collectOperationContents = function (node) { var left = node.left, right = node.right; if (node.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword) { return [new budgieLine_1.BudgieLine(budgie_1.CommandNames.InstanceOf, left.getText(this.sourceFile), right.getText(this.sourceFile))]; } if (node.operatorToken.kind === ts.SyntaxKind.InKeyword) { return [new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryContainsKey, left.getText(this.sourceFile), right.getText(this.sourceFile))]; } var contents = []; if (ts.isBinaryExpression(left)) { contents.push.apply(contents, this.collectOperationContents(left)); } else { contents.push(this.router.recurseIntoValue(left)); } if (node.operatorToken.kind in aliases_1.operators) { contents.push(aliases_1.operators[node.operatorToken.kind]); } else { contents.push(new budgieLine_1.BudgieLine(budgie_1.CommandNames.Unsupported, unknownOperatorComplaint)); } if (ts.isBinaryExpression(right)) { contents.push.apply(contents, this.collectOperationContents(right)); } else { contents.push(this.router.recurseIntoValue(right)); } return contents; }; BinaryExpressionVisitor.prototype.recurseOnOperationContents = function (content) { return typeof content === "string" || content instanceof budgieLine_1.BudgieLine ? content : this.router.recurseIntoValue(content); }; return BinaryExpressionVisitor; }(visitor_1.NodeVisitor)); exports.BinaryExpressionVisitor = BinaryExpressionVisitor; //# sourceMappingURL=binaryExpressionVisitor.js.map