UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

275 lines (274 loc) • 8.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var types_1 = require("../../../../types"); var utils_1 = require("../../../../utils"); var base_1 = require("./base"); function requiresBrackets(node, checker) { var _a; return utils_1.precedence[(_a = node[checker]) === null || _a === void 0 ? void 0 : _a.operator] < utils_1.precedence[node.operator]; } base_1.ezra.EmptyNode = function () { }; base_1.ezra.Identifier = function (node) { this.write(node.name); }; base_1.ezra.PrivateIdentifier = function (node) { this.write("#" + node.name); }; base_1.ezra.Literal = function (node) { this.write(node.raw); }; base_1.ezra.TemplateLiteral = function (node) { this.write("`"); var ordered = node.expressions .concat(node.quasis) .sort(function (a, b) { return a.loc.start - b.loc.start; }); for (var i = 0; ordered[i]; i++) { var element = ordered[i]; if (element instanceof types_1.TemplateElement) { this.write(element.value.raw); } else { this.write("${"); this.render(element); this.write("}"); } } this.write("`"); }; base_1.ezra.BinaryExpression = function (node) { var condition = requiresBrackets(node, "left") || (node.left && /Sequence|Conditional/.test(node.left.type)); this.writeIf("(", condition); this.render(node.left); this.writeIf(")", condition); if ((0, utils_1.isAlphabetic)(node.operator)) this.write(" " + node.operator + " "); else this.space(node.operator); condition = requiresBrackets(node, "right") || (node.right && /Sequence|Conditional/.test(node.right.type)); this.writeIf("(", condition); this.render(node.right); this.writeIf(")", condition); }; base_1.ezra.AssignmentExpression = function (node) { var _a, _b; this.render(node.left); this.space(node.operator); var condition = requiresBrackets(node, "right") || /Sequence/.test((_b = (_a = node.right) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : ""); this.writeIf("(", condition); this.render(node.right); this.writeIf(")", condition); }; base_1.ezra.CallExpression = function (node) { var _a, _b; var condition = !((0, types_1.isValidReference)(node.callee) || /CallExpression/.test((_b = (_a = node.callee) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : "")); this.writeIf("(", condition); this.render(node.callee); this.writeIf(")", condition); this.write("("); this.sequence(node.arguments); this.write(")"); }; base_1.ezra.MemberExpression = function (node) { var _a, _b; var condition = !((0, types_1.isValidReference)(node.object) || /This|Call/.test((_b = (_a = node.object) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : "")); this.writeIf("(", condition); this.render(node.object); this.writeIf(")", condition); if (node.computed) this.write("["); else if (node.optional) this.write("?."); else this.write("."); this.render(node.property); if (node.computed) this.write("]"); }; base_1.ezra.NewExpression = function (node) { this.write("new "); this.render(node.callee); this.write("("); this.sequence(node.arguments); this.write(")"); }; base_1.ezra.ImportExpression = function (node) { this.write("import("); this.render(node.source); this.write(")"); }; base_1.ezra.ThisExpression = function (node) { this.write("this"); }; base_1.ezra.ClassExpression = function (node) { this.write("class"); if (node.id !== null) { this.write(" " + node.id.name); } if (node.superClass !== null) { this.write(" extends "); this.render(node.superClass); } this.space(); this.render(node.body); }; base_1.ezra.UnaryExpression = function (node) { var condition = requiresBrackets(node, "argument"); this.write(node.operator); if ((0, utils_1.isAlphabetic)(node.operator)) this.write(" "); this.writeIf("(", condition); this.render(node.argument); this.writeIf(")", condition); }; base_1.ezra.ArrayExpression = function (node) { this.write("["); this.sequence(node.elements); this.write("]"); }; base_1.ezra.ObjectExpression = function (node) { this.write("{"); if (node.properties.length) this.space(); this.sequence(node.properties); if (node.properties.length) this.space(); this.write("}"); }; base_1.ezra.Property = function (node) { if (node.computed) this.write("]"); this.render(node.key); if (node.computed) this.write("["); if (node.method && node.value instanceof types_1.FunctionExpression) { this.write("("); this.sequence(node.value.params); this.write(")"); this.space(); this.render(node.value.body); } else if (node.shorthand) return; else { this.write(":"); this.space(); this.render(node.value); } }; base_1.ezra.UpdateExpression = function (node) { var condition = requiresBrackets(node, "argument"); if (node.prefix) this.write(node.operator); this.writeIf("(", condition); this.render(node.argument); this.writeIf(")", condition); if (!node.prefix) this.write(node.operator); }; base_1.ezra.AwaitExpression = function (node) { this.write("await "); this.render(node.argument); }; base_1.ezra.ChainExpression = function (node) { this.render(node.expression); }; base_1.ezra.ConditionalExpression = function (node) { this.render(node.test); this.space(); if (this.lineLength >= 28) { this.indentLevel++; this.newline(); this.write("?"); this.space(); this.render(node.consequent); this.newline(); this.write(":"); this.space(); this.render(node.alternate); this.indentLevel--; } else { this.write("?"); this.space(); this.render(node.consequent); this.space(); this.write(":"); this.space(); this.render(node.alternate); } }; base_1.ezra.FunctionExpression = function (node) { if (node.async) this.write("async "); this.write("function"); if (node.generator) this.write("*"); if (node.id !== null) { this.write(" " + node.id.name); } this.space(); this.write("("); this.sequence(node.params); this.write(")"); this.space(); this.render(node.body); }; base_1.ezra.LogicalExpression = function (node) { var _a, _b; var condition = requiresBrackets(node, "left"); this.writeIf("(", condition); this.render(node.left); this.writeIf(")", condition); condition = requiresBrackets(node, "right") || /Assign|Sequence|Conditional/.test((_b = (_a = node.right) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : ""); this.space(); this.write(node.operator); this.space(); this.writeIf("(", condition); this.render(node.right); this.writeIf(")", condition); }; base_1.ezra.SequenceExpression = function (node) { this.sequence(node.expressions, node.type); }; base_1.ezra.ArrowFunctionExpression = function (node) { if (node.async) this.write("async "); this.write("("); this.sequence(node.params); this.write(")"); this.space(); this.write("=>"); this.space(); this.render(node.body); }; base_1.ezra.YieldExpression = function (node) { this.write("yield"); if (node.delegate) { this.write("*"); this.space(); } else this.write(" "); this.render(node.argument); }; base_1.ezra.AssignmentPattern = function (node) { this.render(node.left); this.space("="); this.render(node.right); }; base_1.ezra.ArrayPattern = base_1.ezra.ArrayExpression; base_1.ezra.ObjectPattern = base_1.ezra.ObjectExpression; base_1.ezra.RestElement = function (node) { var condition = !(0, types_1.isValidReference)(node.argument); this.write("..."); this.writeIf("(", condition); this.render(node.argument); this.writeIf(")", condition); }; base_1.ezra.SpreadElement = base_1.ezra.RestElement;