UNPKG

siphon-cli

Version:

Simple bundler for web applications. 📦🔧🧡

216 lines (215 loc) • 8.15 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.jsxElement = function (start) { var _a; var elem = new types_1.JSXElement(start); this.outerspace(); if (!(0, utils_1.isValidIdentifierCharacter)(this.text[this.i])) { this.raise("JS_UNEXPECTED_TOKEN", this.text[this.i]); } elem.openingElement = this.jsxOpeningElement(start); if (elem.openingElement.selfClosing) { elem.closingElement = null; return elem; } elem.children = []; while (!(this.text[this.i] === undefined)) { this.outerspace(); if (this.eat("<")) { start = this.i - 1; this.outerspace(); if (this.text[this.i] === "/") { this.i++; elem.closingElement = this.jsxClosingElement(start); if (elem.closingElement.tagName !== elem.openingElement.tagName) this.raise("JSX_NO_CLOSE", elem.openingElement.tagName, elem.openingElement.loc.end); break; } else if (this.eat(">")) elem.children.push(this.jsxFragment(start)); else elem.children.push(this.jsxElement(start)); } else if (this.text[this.i] === "{") { elem.children.push(this.jsxExpressionContainer()); } else elem.children.push(this.jsxText()); } if (this.text[this.i] === undefined && elem.closingElement === undefined) this.raise("JSX_NO_CLOSE", elem.openingElement.tagName, elem.openingElement.loc.end); elem.loc.end = (_a = elem.closingElement) === null || _a === void 0 ? void 0 : _a.loc.end; return elem; }; base_1.ezra.jsxText = function () { var text = new types_1.JSXText(this.i); text.raw = ""; while (!(this.text[this.i] === undefined || /\<|\{/.test(this.text[this.i]))) { text.raw += this.text[this.i]; this.i++; } text.value = text.raw.replace(/\r|\n/g, ""); text.loc.end = this.i; return text; }; base_1.ezra.jsxAttribute = function () { var _a, _b; if (/\//.test(this.text[this.i])) return; var attrib = new types_1.JSXAttribute(this.i); var attribName = this.identifier(true, true); attrib.name = new types_1.JSXIdentifier(this.i); attrib.name.name = attribName.name; attrib.name.loc = attribName.loc; this.outerspace(); if (this.taste("=")) { switch (this.text[this.i]) { case '"': case "'": attrib.value = this.stringLiteral(); break; case "{": attrib.value = this.jsxExpressionContainer(); break; default: this.raise("EXPECTED", "{"); } } else attrib.value = null; attrib.loc.end = (_b = (_a = attrib.value) === null || _a === void 0 ? void 0 : _a.loc.end) !== null && _b !== void 0 ? _b : this.i; return attrib; }; base_1.ezra.jsxIdentifier = function () { var jsxident = new types_1.JSXIdentifier(this.i); var identifier = this.identifier(true, true); if (this.text[this.i] === ":") return this.JSXNamespacedName(identifier); else if (this.text[this.i] === ".") return this.JSXMemberExpression(identifier); jsxident.loc = identifier.loc; jsxident.name = identifier.name; return jsxident; }; base_1.ezra.JSXReparse = function (node) { this.outerspace(); switch (this.text[this.i]) { case ":": return this.JSXNamespacedName(node); case ".": return this.JSXMemberExpression(node); default: return node; } }; base_1.ezra.JSXMemberExpression = function (object) { this.i++; var prop = this.identifier(true, true); var jsxmem = new types_1.JSXMemberExpression(object.loc.start); jsxmem.object = object; jsxmem.property = new types_1.JSXIdentifier(prop.loc.start); jsxmem.property.name = prop.name; jsxmem.property.loc.end = prop.loc.end; return this.JSXReparse(jsxmem); }; base_1.ezra.JSXNamespacedName = function (namespace) { this.i++; var ident = this.jsxIdentifier(); var namsp = new types_1.JSXNamespacedName(namespace.loc.start); namsp.namespace = namespace; if (ident instanceof types_1.JSXIdentifier) namsp.name = ident; else this.raise("EXPECTED", ">", ident.loc.start); return namsp; }; base_1.ezra.jsxOpeningElement = function () { var open = new types_1.JSXOpeningElement(this.i - 1); open.name = this.jsxIdentifier(); open.attributes = this.group("JSX_attribute"); if (this.text[this.i] === ">") open.selfClosing = false; else if (this.taste("/")) { this.outerspace(); if (this.text[this.i] === ">") { open.selfClosing = true; } } this.i++; if (open.name instanceof types_1.JSXIdentifier) { open.tagName = open.name.name; } else if (open.name instanceof types_1.JSXMemberExpression) { open.tagName = "".concat(open.name.object.name, ".").concat(open.name.property.name); } else if (open.name instanceof types_1.JSXNamespacedName) { open.tagName = "".concat(open.name.namespace.name, ":").concat(open.name.name.name); } open.loc.end = this.i; return open; }; base_1.ezra.jsxClosingElement = function (start) { this.belly.pop(); var close = new types_1.JSXClosingElement(start); close.name = this.jsxIdentifier(); this.outerspace(); if (!this.taste(">")) this.raise("JS_UNEXPECTED_TOKEN", this.text[this.i]); this.outerspace(); if (close.name instanceof types_1.JSXIdentifier) { close.tagName = close.name.name; } else if (close.name instanceof types_1.JSXMemberExpression) { close.tagName = "".concat(close.name.object.name, ".").concat(close.name.property.name); } else if (close.name instanceof types_1.JSXNamespacedName) { close.tagName = "".concat(close.name.namespace.name, ":").concat(close.name.name.name); } close.loc.end = this.i; return close; }; base_1.ezra.jsxExpressionContainer = function () { var container = new types_1.JSXExpressionContainer(this.i); this.eat("{"); container.expression = this.group("expression"); container.loc.end = this.i; return container; }; base_1.ezra.jsxFragment = function (start) { var fragment = new types_1.JSXFragment(start); fragment.openingFragment = new types_1.JSXOpeningFragment(start); fragment.openingFragment.loc.end = this.i; fragment.children = []; while (!(this.text[this.i] === undefined)) { this.outerspace(); if (this.eat("<")) { start = this.i - 1; this.outerspace(); if (this.eat("/")) { this.outerspace(); if (this.eat(">")) { fragment.closingFragment = new types_1.JSXClosingFragment(start); fragment.closingFragment.loc.end = this.i; } else this.raise("JSX_FRAGMENT_NO_CLOSE", undefined, fragment.openingFragment.loc.end); break; } else if (this.eat(">")) fragment.children.push(this.jsxFragment(start)); else fragment.children.push(this.jsxElement(start)); } else if (this.text[this.i] === "{") { fragment.children.push(this.jsxExpressionContainer()); } else fragment.children.push(this.jsxText()); } if (this.text[this.i] === undefined && fragment.closingFragment === undefined) this.raise("JSX_FRAGMENT_NO_CLOSE", undefined, fragment.openingFragment.loc.end); return fragment; };