UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

323 lines (322 loc) • 12 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var types_1 = require("../../../../types"); var utils_1 = require("../../../../utils"); var base_1 = require("./base"); base_1.ezra.expression = function (type) { var exp; this.outerspace(); switch (true) { case this.eat("/*"): case this.eat("//"): this.skip(); break; case /"|'/.test(this.text[this.i]): return this.reparse(this.stringLiteral()); case /`/.test(this.text[this.i]): return this.reparse(this.templateLiteral()); case this.text[this.i] === ":": if (!(type === "ternary" || type === "case")) this.raise("JS_UNEXPECTED_TOKEN"); case this.text[this.i] === undefined: case this.text[this.i] === ";": return; case this.eat("<"): if (this.options.parseJSX) { var JSXStart = this.i - 1; this.outerspace(); if (this.eat(">")) return this.reparse(this.jsxFragment(JSXStart)); else return this.reparse(this.jsxElement(JSXStart)); } else this.raise("JS_UNEXPECTED_TOKEN", "<"); case this.eat("/"): return this.reparse(this.regexLiteral()); case this.eat("("): return this.reparse(this.group("expression")); case this.eat("["): return this.reparse(this.arrayExpression()); case this.eat("{"): return this.reparse(this.objectExpression()); case this.eat("++"): case this.eat("--"): case this.eat("!"): case this.eat("~"): case this.eat("+"): case this.eat("-"): case this.match("typeof"): case this.match("void"): case this.match("delete"): return this.reparse(this.unaryExpression()); case this.match("await"): return this.reparse(this.awaitExpression()); case this.eat("..."): if (this.allowSpread()) return this.spreadElement(); else this.raise("EXPRESSION_EXPECTED"); case this.eat("."): return this.reparse(this.numberLiteral()); case this.match("new"): return this.reparse(this.newExpression()); case this.match("null"): return this.reparse(this.nullLiteral()); case this.match("this"): return this.reparse(this.thisExpression()); case this.match("true"): case this.match("false"): return this.reparse(this.booleanLiteral()); case this.match("async"): return this.reparse(this.maybeAsyncExpression()); case (0, utils_1.isDigit)(this.text[this.i]): return this.reparse(this.numberLiteral(), "number"); case this.match("class"): return this.reparse(this.classExpression()); case this.match("yield"): return this.reparse(this.yieldExpression()); case this.match("super"): return this.reparse(this.super()); case this.match("import"): return this.reparse(this.importExpression()); case this.match("function"): return this.reparse(this.functionExpression()); case (0, utils_1.isValidIdentifierCharacter)(this.text[this.i]): return this.reparse(this.identifier()); default: this.raise("JS_UNEXPECTED_TOKEN"); } return exp; }; base_1.ezra.memberExpression = function (object) { var _a; var memexp = new types_1.MemberExpression(this.i); this.outerspace(); memexp.object = object; var query = this.belly.top(); if (query === "[") { memexp.property = this.group(); if (memexp.property === undefined) this.raise("EXPRESSION_EXPECTED"); memexp.computed = true; } else if (this.text[this.i] === "#") { var arr = __assign({}, this.contexts).arr; if (arr.includes("class_body")) { this.i++; memexp.property = this.privateIdentifier(); } else this.raise("JS_ILLEGAL_PRIV_IDENT"); } else memexp.property = this.identifier(true); memexp.loc.start = memexp.object.loc.start; memexp.loc.end = (_a = memexp.property) === null || _a === void 0 ? void 0 : _a.loc.end; if (query === "?.") memexp.optional = true; this.belly.pop(); return this.reparse(memexp, "."); }; base_1.ezra.chainExpression = function (exp) { var chainexp = new types_1.ChainExpression(exp.loc.start); chainexp.expression = exp; chainexp.loc.end = exp.loc.end; return chainexp; }; base_1.ezra.thisExpression = function () { var thisexp = new types_1.ThisExpression(this.i - this.belly.pop().length); thisexp.loc.end = this.i; return thisexp; }; base_1.ezra.callExpression = function (callee) { var _a; this.belly.push(this.text[this.i++]); var callexp = new types_1.CallExpression(callee.loc.start); callexp.callee = callee; callexp.arguments = (_a = this.group("call")) !== null && _a !== void 0 ? _a : []; callexp.loc.end = this.i; return this.reparse(callexp); }; base_1.ezra.importExpression = function () { var _a; var importexp = new types_1.ImportExpression(this.i - 6); this.outerspace(); if (!this.eat("(")) this.raise("EXPECTED", "("); var source = (_a = this.group("call")) !== null && _a !== void 0 ? _a : []; if (source.length !== 1) this.raise("JS_ILLEGAL_IMPORT_EXP"); importexp.source = source[0]; importexp.loc.end = this.i; return this.reparse(importexp); }; base_1.ezra.arguments = function () { var args = []; while (!(this.text[this.i] === undefined) && this.text[this.i] !== ")") { args.push(this.expression()); if (this.text[this.i] === ",") this.i++; } return args; }; base_1.ezra.newExpression = function () { var _a, _b; this.outerspace(); var newexp = new types_1.NewExpression(this.i); this.contexts.push("new"); this.operators.push("new"); newexp.callee = this.expression(); if (this.eat("(")) newexp.arguments = (_a = this.group("call")) !== null && _a !== void 0 ? _a : []; else newexp.arguments = []; this.operators.pop(); this.contexts.pop(); newexp.loc.end = (_b = newexp.callee) === null || _b === void 0 ? void 0 : _b.loc.end; return this.reparse(newexp); }; base_1.ezra.updateExpression = function (argument, prefix) { if (!(0, types_1.isValidReference)(argument)) this.raise(prefix ? "JS_INVALID_LHS_PREFIX" : "JS_INVALID_LHS_POFTIX"); var upexp = new types_1.UpdateExpression(argument.loc.start); upexp.operator = this.belly.top(); upexp.argument = argument; upexp.loc.end = this.i; upexp.prefix = prefix ? true : false; if (prefix) upexp.loc.start = upexp.loc.start - 2; return this.reparse(upexp, prefix ? "prefix" : "postfix"); }; base_1.ezra.unaryExpression = function () { var unexp = new types_1.UnaryExpression(this.i - this.belly.top().length); unexp.operator = this.belly.top(); this.operators.push(unexp.operator); unexp.argument = this.expression(); unexp.loc.end = this.i; this.operators.pop(); if (/\-\-|\+\+/.test(unexp.operator)) return this.updateExpression(unexp.argument, true); return this.reparse(unexp); }; base_1.ezra.awaitExpression = function () { var awexp = new types_1.AwaitExpression(this.i - 5); this.operators.push(this.belly.top()); awexp.argument = this.expression(); this.operators.pop(); awexp.loc.end = awexp.argument.loc.end; return this.reparse(awexp); }; base_1.ezra.binaryExpression = function (left) { if (this.lowerPrecedence()) return left; var binexp = new types_1.BinaryExpression(left.loc.start); binexp.left = left; binexp.operator = this.belly.top(); this.operators.push(binexp.operator); binexp.right = this.expression(); binexp.loc.end = this.i; this.operators.pop(); return this.reparse(binexp); }; base_1.ezra.logicalExpression = function (left) { if (this.lowerPrecedence()) return left; var logexp = new types_1.LogicalExpression(left.loc.start); logexp.left = left; logexp.operator = this.belly.top(); this.operators.push(logexp.operator); this.outerspace(); logexp.right = this.expression(); logexp.loc.end = this.i; this.operators.pop(); return this.reparse(logexp); }; base_1.ezra.conditionalExpression = function (test) { if (this.lowerPrecedence()) return test; this.belly.pop(); var condexp = new types_1.ConditionalExpression(test.loc.start); condexp.test = test; condexp.consequent = this.expression("ternary"); this.outerspace(); if (!this.eat(":")) this.raise("COLON_EXPECTED"); condexp.alternate = this.expression(); condexp.loc.end = this.i; return this.reparse(condexp); }; base_1.ezra.assignmentExpression = function (left) { if (this.lowerPrecedence()) return left; if (!(0, types_1.isValidReference)(left) && !/ArrayExpression|ObjectExpression/.test(left.type)) this.raise("JS_INVALID_LHS_ASSIGN"); var assignexp = new types_1.AssignmentExpression(left.loc.start); assignexp.left = left; assignexp.operator = this.belly.top(); this.operators.push(this.belly.top()); assignexp.right = this.expression(); this.operators.pop(); assignexp.loc.end = this.i; return this.reparse(assignexp); }; base_1.ezra.sequenceExpression = function (left) { if (this.lowerPrecedence()) return left; var seqexp = new types_1.SequenceExpression(left.loc.start); this.operators.push(","); if (left.type === "SequenceExpression") { var expressions = left.expressions; for (var i = 0; expressions[i]; i++) { seqexp.expressions.push(expressions[i]); } } else seqexp.expressions.push(left); seqexp.expressions.push(this.expression()); this.operators.pop(); seqexp.loc.end = this.i; return this.reparse(seqexp); }; base_1.ezra.arrayExpression = function () { var array = new types_1.ArrayExpression(this.i - 1); array.elements = this.group("array").flat(1); array.loc.end = this.i; return array; }; base_1.ezra.objectExpression = function () { var _a; var object = new types_1.ObjectExpression(this.i - 1); object.properties = (_a = this.group("object")) !== null && _a !== void 0 ? _a : []; object.loc.end = this.i; return object; }; base_1.ezra.yieldExpression = function () { var yieldexp = new types_1.YieldExpression(this.i - 6); this.outerspace(); if (this.text[this.i] == "*") { this.i++; yieldexp.delegate = true; this.outerspace(); } else yieldexp.delegate = false; yieldexp.argument = this.expression(); this.operators.push(this.belly.top()); this.operators.pop(); yieldexp.loc.end = yieldexp.argument.loc.end; return this.reparse(yieldexp); };