ts-budgie
Version:
Converts TypeScript code to Budgie.
83 lines • 4.61 kB
JavaScript
;
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 unsupported_1 = require("../../output/unsupported");
var visitor_1 = require("../visitor");
var ObjectLiteralExpressionVisitor = /** @class */ (function (_super) {
__extends(ObjectLiteralExpressionVisitor, _super);
function ObjectLiteralExpressionVisitor() {
return _super !== null && _super.apply(this, arguments) || this;
}
ObjectLiteralExpressionVisitor.prototype.visit = function (node) {
if (node.parent === undefined) {
return [transformation_1.Transformation.fromNode(node, this.sourceFile, [unsupported_1.createUnsupportedTypeBudgieLine()])];
}
var parentTypePair = this.getParentTypePair(node.parent);
if (parentTypePair instanceof budgieLine_1.BudgieLine) {
return [transformation_1.Transformation.fromNode(node, this.sourceFile, [parentTypePair])];
}
var typeKeys = parentTypePair[0], typeValues = parentTypePair[1];
return node.properties.length === 0
? this.returnForBlankDictionary(node, typeKeys, typeValues)
: this.returnForPopulatingDictionary(node, typeKeys, typeValues);
};
ObjectLiteralExpressionVisitor.prototype.getParentTypePair = function (node) {
if (ts.isBinaryExpression(node)) {
return this.getDirectNodeTypePair(node.left);
}
if (ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)) {
return this.getDirectNodeTypePair(node);
}
return unsupported_1.createUnsupportedTypeBudgieLine();
};
ObjectLiteralExpressionVisitor.prototype.getDirectNodeTypePair = function (node) {
var typeName = this.aliaser.getFriendlyTypeName(node);
if (!(typeName instanceof budgieLine_1.BudgieLine) || typeName.command !== budgie_1.CommandNames.DictionaryType) {
return unsupported_1.createUnsupportedTypeBudgieLine();
}
return typeName.args;
};
ObjectLiteralExpressionVisitor.prototype.returnForBlankDictionary = function (node, typeKeys, typeValues) {
return [transformation_1.Transformation.fromNode(node, this.sourceFile, [new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryNew, typeKeys, typeValues)])];
};
ObjectLiteralExpressionVisitor.prototype.returnForPopulatingDictionary = function (node, typeKeys, typeValues) {
var _this = this;
var typeLine = new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryType, typeKeys, typeValues);
this.context.setTypeCoercion(typeLine);
var bodyNodes = node.properties.map(function (bodyNode) {
return transformation_1.Transformation.fromNode(bodyNode, _this.sourceFile, _this.router.recurseIntoNode(bodyNode));
});
this.context.exitTypeCoercion();
return __spreadArrays([
transformation_1.Transformation.fromNodeStart(node, this.sourceFile, [new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryNewStart, typeKeys, typeValues)])
], bodyNodes, [
transformation_1.Transformation.fromNodeEnd(node, [new budgieLine_1.BudgieLine(budgie_1.CommandNames.DictionaryNewEnd)]),
]);
};
return ObjectLiteralExpressionVisitor;
}(visitor_1.NodeVisitor));
exports.ObjectLiteralExpressionVisitor = ObjectLiteralExpressionVisitor;
//# sourceMappingURL=objectLiteralExpressionVisitor.js.map