UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

173 lines (172 loc) • 6.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var types_1 = require("../../../../types"); var utils_1 = require("../../../../utils"); var base_1 = require("./base"); base_1.ezra.maybeAsync = function () { var pos = this.i - this.belly.pop().length; try { var argument = this.statement(); if (argument.type === "ExpressionStatement" && argument.expression.type === "ArrowFunctionExpression") { argument.expression.async = true; return argument; } if (argument.type === "FunctionDeclaration") { argument.async = true; return argument; } } catch (e) { } this.i = pos; return this.tryExpressionStatement(); }; var isAsync = { FunctionExpression: true, ArrowFunctionExpression: true }; base_1.ezra.maybeAsyncExpression = function () { var pos = this.i - this.belly.pop().length; try { var argument = this.expression(); if (isAsync[argument.type] === true) { argument.async = true; return argument; } } catch (_a) { } this.i = pos; return this.reparse(this.identifier()); }; base_1.ezra.functionDeclaration = function () { var func = new types_1.FunctionDeclaration(this.i - 8); this.contexts.push("function"); this.outerspace(); if (this.text[this.i] === "*") { func.generator = true; this.i++; this.outerspace(); } func.id = this.identifier(); this.outerspace(); if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); func.params = this.group("parameters"); this.outerspace(); if (!this.eat("{")) this.raise("OPEN_CURLY_EXPECTED"); else func.body = this.blockStatement(); this.contexts.pop(); func.loc.end = this.i; return func; }; base_1.ezra.parameter = function () { if (this.eat("...")) return this.restElement(); if (this.eat("{")) { var exp = this.objectExpression(); var objpattern = new types_1.ObjectPattern(this.i - 1); objpattern.properties = exp.properties; objpattern.loc.end = exp.loc.end; return objpattern; } var name = this.identifier(); this.outerspace(); if (this.text[this.i] === "," || this.text[this.i] === ")") { if (this.text[this.i] === ",") this.i++; return name; } if (!(this.text[this.i] === "=")) this.raise("JS_PARAM_DEC_EXPECTED"); this.i++; var defaultValue = this.expression(); if (this.text[this.i] === ",") this.i++; var pattern = new types_1.AssignmentPattern(name.loc.start); pattern.left = name; pattern.right = defaultValue; pattern.loc.end = defaultValue.loc.end; return pattern; }; base_1.ezra.functionExpression = function (shouldReturn) { if (shouldReturn === void 0) { shouldReturn = false; } this.contexts.push("function"); var func = new types_1.FunctionExpression(this.i - 8); this.outerspace(); if (this.text[this.i] === "*") { func.generator = true; this.i++; this.outerspace(); } if ((0, utils_1.isValidIdentifierCharacter)(this.text[this.i])) { func.id = this.identifier(); this.outerspace(); } else func.id = null; if (!this.eat("(")) this.raise("OPEN_BRAC_EXPECTED"); else func.params = this.group("parameters"); this.outerspace(); if (!this.eat("{")) this.raise("EXPECTED", "{"); else func.body = this.blockStatement(false); func.loc.end = this.i; this.contexts.pop(); return shouldReturn ? func : this.reparse(func); }; base_1.ezra.arrowFunctionExpression = function (params, startAt) { var _a; if (this.lowerPrecedence()) return params; var arrowfunc = new types_1.ArrowFunctionExpression((_a = startAt !== null && startAt !== void 0 ? startAt : params === null || params === void 0 ? void 0 : params.loc.start) !== null && _a !== void 0 ? _a : 0); arrowfunc.params = params ? this.parameterize(params) : []; this.outerspace(); arrowfunc.id = null; if (this.eat("{")) { this.contexts.push("function"); arrowfunc.body = this.blockStatement(false); this.contexts.pop(); } else arrowfunc.body = this.expression(); arrowfunc.loc.end = this.i; return this.reparse(arrowfunc); }; base_1.ezra.parameterize = function (params) { var parameterArray = [], parameterNames = {}; if (params === undefined) return parameterArray; if (params instanceof types_1.SequenceExpression) { var expressions = params.expressions; for (var i = 0; expressions[i]; i++) { if (!(0, types_1.isValidParameter)(expressions[i])) this.raise("JS_PARAM_DEC_EXPECTED"); if (expressions[i] instanceof types_1.Identifier) { parameterNames[expressions[i].name] === true ? this.raise("JS_PARAM_CLASH", expressions[i].name) : (parameterNames[expressions[i].name] = true); parameterArray.push(expressions[i]); } else { parameterNames[expressions[i].left.name] ? this.raise("JS_PARAM_CLASH", expressions[i].name) : parameterNames[expressions[i].left.name] === true; if (expressions[i].operator !== "=") this.raise("JS_PARAM_DEC_EXPECTED"); var pattern = new types_1.AssignmentPattern(expressions[i].loc.start); pattern.left = expressions[i].left; pattern.right = expressions[i].right; pattern.loc.end = expressions[i].loc.end; parameterArray.push(pattern); } } } else { if (!(0, types_1.isValidParameter)(params)) this.raise("JS_PARAM_DEC_EXPECTED"); parameterArray.push(params); } return parameterArray; };