UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

441 lines (440 loc) • 15.7 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"); var identifiers_1 = require("./identifiers"); var statementScope = { block: true, global: true, case: true, labelled: true, }; base_1.ezra.statement = function () { var _a; this.outerspace(); var context = (_a = {}, _a[this.contexts.top()] = true, _a); switch (true) { case context.class_body: return this.definition(); case context.object: return this.property(); case context.parameters: return this.parameter(); case context.array: return this.elements(); case context.import: return this.importSpecifier(); case context.export: return this.exportSpecifier(); case context.expression: return this.expression(); case context.call: return this.arguments(); case context.JSX_attribute: return this.jsxAttribute(); case context.switch_block: if (this.text[this.i] === "}") return; if (!(this.match("case") || this.match("default")) && !(this.text[this.i] === undefined)) { this.raise("JS_CASE_EXPECTED"); } else { return this.caseStatement(this.belly.top() === "default"); } default: break; } switch (true) { case this.text[this.i] === undefined: return; case this.eat("/*"): case this.eat("//"): this.skip(); break; case this.text[this.i] === "(": return this.tryExpressionStatement(); case this.text[this.i] === "{": if (statementScope[this.contexts.top()] === undefined) { return this.tryExpressionStatement(); } else { this.belly.push(this.text[this.i++]); return this.blockStatement(); } case this.match("do"): return this.doWhileStatement(); case this.eat(";"): return this.emptyStatement(); case this.match("if"): return this.ifStatement(); case this.match("for"): return this.forStatement(); case this.match("try"): return this.tryStatement(); case this.match("while"): return this.whileStatement(); case this.match("break"): return this.breakStatement(); case this.match("const"): case this.match("var"): case this.match("let"): return this.variableDeclaration(); case this.match("async"): return this.maybeAsync(); case this.match("class"): return this.classDeclaration(); case this.match("throw"): return this.throwStatement(); case this.match("switch"): return this.switchStatement(); case this.match("continue"): return this.continueStatement(); case this.match("function"): return this.functionDeclaration(); case this.match("return"): return this.returnStatement(); case this.match("import"): var pos = this.i - 6; this.outerspace(); if (this.text[this.i] === "(") { this.belly.pop(); this.i = pos; return this.tryExpressionStatement(); } else return this.importDeclaration(); case this.match("else"): this.raise("JS_ILLEGAL_ELSE"); case this.match("case"): this.raise("JS_ILLEGAL_CASE"); case this.match("export"): return this.exportDeclaration(); case this.match("default"): this.raise("JS_EXPORT_EXPECTED"); default: return this.tryExpressionStatement(); } }; base_1.ezra.tryExpressionStatement = function () { if ((0, utils_1.isValidIdentifierCharacter)(this.text[this.i]) && !(0, utils_1.isDigit)(this.text[this.i])) { var pos = this.i; var label = this.identifier(true); this.outerspace(); if (this.text[this.i] === ":") { this.i++; return this.labeledStatement(label); } else this.i = pos; } var expstat = new types_1.ExpressionStatement(this.i); this.operators.push("none"); expstat.expression = this.expression(); if (expstat.expression === undefined) return; expstat.loc.start = expstat.expression.loc.start; expstat.loc.end = expstat.expression.loc.end; this.operators.pop(); if (this.eat(";")) expstat.loc.end = this.i; return expstat; }; base_1.ezra.blockStatement = function (eatComma) { var blockstat = new types_1.BlockStatement(this.i - 1); blockstat.body = this.group("block"); blockstat.loc.end = this.i; if (eatComma) { this.outerspace(); this.eat(";"); } return blockstat; }; base_1.ezra.ifStatement = function () { this.outerspace(); if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); var ifstat = new types_1.IfStatement(this.i - 2); ifstat.test = this.group("expression"); if (ifstat.test === undefined) this.raise("EXPRESSION_EXPECTED"); this.outerspace(); ifstat.consequent = this.statement(); if (ifstat.consequent === undefined) this.raise("EXPRESSION_EXPECTED"); this.outerspace(); if (this.match("else")) ifstat.alternate = this.statement(); else ifstat.alternate = null; return ifstat; }; base_1.ezra.emptyStatement = function () { var empty = new types_1.EmptyStatement(this.i - 1); empty.loc.end = this.i; return empty; }; base_1.ezra.whileStatement = function () { var whilestat = new types_1.WhileStatement(this.i - 5); this.outerspace(); if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); whilestat.test = this.group("expression"); this.outerspace(); whilestat.body = this.statement(); if (whilestat.body === undefined) this.raise("EXPRESSION_EXPECTED"); whilestat.loc.end = this.i; return whilestat; }; base_1.ezra.doWhileStatement = function () { var dwstat = new types_1.DoWhileStatement(this.i - 2); this.outerspace(); dwstat.body = this.statement(); this.outerspace(); if (!this.eat("while")) this.raise("JS_WHILE_EXPECTED"); this.outerspace(); if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); dwstat.test = this.group(); dwstat.loc.end = this.i; this.outerspace(); this.eat(";"); return dwstat; }; base_1.ezra.switchStatement = function () { var switchstat = new types_1.SwitchStatement(this.i - 6); this.outerspace(); if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); switchstat.discriminant = this.group("expression"); this.outerspace(); if (!this.eat("{")) this.raise("OPEN_CURLY_EXPECTED"); switchstat.cases = this.group("switch_block"); switchstat.loc.end = this.i; return switchstat; }; base_1.ezra.caseStatement = function (isDefault) { var _a; var switchcase = new types_1.SwitchCase(this.i - 4); switchcase.test = this.expression("case"); if (isDefault) switchcase.test = null; else if (switchcase.test === undefined) this.raise("EXPRESSION_EXPECTED"); if (!this.eat(":")) this.raise("COLON_EXPECTED"); this.outerspace(); this.contexts.push("case"); while (!(this.text[this.i] === undefined) && this.text[this.i] !== "}" && !this.isNewCaseStatement()) { var statement = this.statement(); switchcase.consequent.push(statement); this.outerspace(); } this.contexts.pop(); switchcase.loc.end = (_a = switchcase.consequent[switchcase.consequent.length - 1]) === null || _a === void 0 ? void 0 : _a.loc.end; return switchcase; }; base_1.ezra.breakStatement = function () { var breakstat = new types_1.BreakStatement(this.i - 5); this.outerspace(); this.eat(";"); breakstat.loc.end = this.i; return breakstat; }; base_1.ezra.continueStatement = function () { var pos = this.i; var continuestat = new types_1.ContinueStatement(this.i - 8); this.outerspace(); if (this.text[this.i] === ";" || /\n/.test(this.text.slice(pos, this.i))) { continuestat.label = null; } else { continuestat.label = this.identifier(); var arr = __assign({}, this.contexts).arr; if (!arr.find(function (a) { var _a; return a.label === ((_a = continuestat.label) === null || _a === void 0 ? void 0 : _a.name); })) { this.raise("JS_ILLEGAL_CONTINUE"); } } this.outerspace(); this.eat(";"); return continuestat; }; base_1.ezra.throwStatement = function () { var _a; var throwstat = new types_1.ThrowStatement(this.i - 5); var pos = this.i; this.outerspace(); if (/\n|;/.test(this.text.slice(pos, this.i)) || this.text[this.i] === "}") this.raise("EXPRESSION_EXPECTED"); else throwstat.argument = this.expression(); this.eat(";"); throwstat.loc.end = (_a = throwstat.argument) === null || _a === void 0 ? void 0 : _a.loc.end; return throwstat; }; base_1.ezra.tryStatement = function () { var trystat = new types_1.TryStatement(this.i - 3); this.outerspace(); if (!this.eat("{")) this.raise("OPEN_CURLY_EXPECTED"); trystat.block = this.blockStatement(); this.outerspace(); if (this.match("catch")) { trystat.handler = new types_1.CatchClause(this.i - 5); this.outerspace(); if (this.eat("(")) { var param = this.group("parameters"); if (param.length > 1) this.raise("CATCH_NEW_PARAM"); if (param[0] === undefined) this.raise("IDENTIFIER_EXPECTED"); if (!(param[0] instanceof types_1.Identifier)) { this.raise("CATCH_ASSIGN"); } trystat.handler.param = param[0]; } else trystat.handler.param = null; this.outerspace(); if (!this.eat("{")) this.raise("OPEN_CURLY_EXPECTED"); trystat.handler.body = this.blockStatement(); trystat.handler.loc.end = trystat.handler.body.loc.end; this.outerspace(); } else trystat.handler = null; if (this.match("finally")) { this.outerspace(); if (!this.eat("{")) this.raise("OPEN_CURLY_EXPECTED"); trystat.finalizer = this.blockStatement(); } else trystat.finalizer = null; if (trystat.handler === null && trystat.finalizer === null) { this.raise("EXPECTED", "catch"); } trystat.loc.end = this.i - 1; return trystat; }; base_1.ezra.returnStatement = function () { var _a; var arr = __assign({}, this.contexts).arr; if (!arr.includes("function")) this.raise("JS_ILLEGAL_RETURN"); var retstat = new types_1.ReturnStatement(this.i - 6); var pos = this.i; this.outerspace(); if (/;|\}/.test(this.text[this.i]) || /\n/.test(this.text.slice(pos, this.i))) { retstat.argument = null; } else retstat.argument = (_a = this.expression()) !== null && _a !== void 0 ? _a : null; if (retstat.argument instanceof types_1.SequenceExpression) { var exp = retstat.argument; exp.__isreturn = true; } this.eat(";"); retstat.loc.end = this.i; return retstat; }; base_1.ezra.labeledStatement = function (label) { if (identifiers_1.keywords[label.name] === true) this.raise("JS_UNEXPECTED_TOKEN", ":"); var labelstat = new types_1.LabeledStatement(label.loc.start); labelstat.label = label; this.contexts.push({ label: label.name }); this.contexts.push("labelled"); labelstat.body = this.statement(); this.contexts.pop(); this.contexts.pop(); return labelstat; }; base_1.ezra.variableDeclaration = function () { var kind = this.belly.pop(), vardec = new types_1.VariableDeclaration(this.i - kind.length); vardec.kind = kind; this.outerspace(); this.contexts.push("declaration"); do { vardec.declarations.push(this.declarator(kind)); } while (this.text[this.i] === "," ? (this.i++, true) : false); this.contexts.pop(); vardec.loc.end = this.i; this.outerspace(); this.eat(";"); return vardec; }; base_1.ezra.declarator = function (kind) { var _a; var _this = this; var _b; var declarator = new types_1.VariableDeclarator(this.i), decExp = this.expression(); if (decExp === undefined) this.raise("VARIABLE_DECLARATION_EXPECTED"); var forLoopInit = function () { var arr = __assign({}, _this.contexts).arr; return (decExp.type === "BinaryExpression" && (decExp.operator === "of" || decExp.operator === "in") && arr.includes("for_params")); }; if (decExp.type === "AssignmentExpression" || forLoopInit()) { if (decExp.type === "AssignmentExpression" && decExp.operator !== "=") { this.raise("JS_UNEXPECTED_TOKEN", decExp.operator, (_b = decExp.left) === null || _b === void 0 ? void 0 : _b.loc.end); } var type = (_a = {}, _a[decExp.left.type] = true, _a); switch (true) { case type.Identifier: declarator.id = decExp.left; break; case type.ArrayExpression: declarator.id = new types_1.ArrayPattern(decExp.left.loc.start); declarator.id.elements = decExp.left.elements; break; case type.ObjectExpression: declarator.id = new types_1.ObjectPattern(decExp.left.loc.start); declarator.id.properties = decExp.left.properties; break; default: this.raise("IDENTIFIER_EXPECTED", undefined, decExp.left.loc.end); } declarator.id.loc.end = decExp.left.loc.end; if (forLoopInit()) { declarator[decExp.operator] = true; declarator["right_val"] = decExp.right; declarator.init = null; } else declarator.init = decExp.right; } else if (decExp.type === "Identifier") { if (kind === "const") this.raise("CONST_INIT"); declarator.id = decExp; declarator.init = null; } else this.raise("VARIABLE_DECLARATION_EXPECTED"); declarator.loc.end = decExp.loc.end; return declarator; };