siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
169 lines (168 loc) • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("../../../../types");
var utils_1 = require("../../../../utils");
var base_1 = require("./base");
base_1.ezra.super = function () {
var sup = new types_1.Super(this.i - 5);
sup.loc.end = this.i;
this.outerspace();
if (!this.eat("("))
this.raise("EXPECTED", "(");
return this.callExpression(sup);
};
var getOrSet = { get: true, set: true };
base_1.ezra.classDeclaration = function () {
var cl = new types_1.ClassDeclaration(this.i - 5);
this.outerspace();
cl.id = this.identifier();
this.outerspace();
if (this.match("extends")) {
this.contexts.push("super_class");
cl.superClass = this.expression();
this.contexts.pop();
this.outerspace();
}
if (!this.match("{"))
this.raise("EXPECTED", "{");
cl.body = new types_1.ClassBody(this.i);
cl.body.body = this.group("class_body");
if (cl.body.body.filter(function (def) { return def.kind === "constructor"; }).length > 1) {
this.raise("JS_DUPLICATE_CONSTRUCTORS");
}
cl.loc.end = cl.body.loc.end = this.i;
this.outerspace();
this.eat(";");
return cl;
};
base_1.ezra.definition = function () {
if (this.eat(";"))
return;
var start = this.i, definition, kind, isStatic = false;
if (this.match("static")) {
isStatic = true;
this.outerspace();
}
var _a = this.definitionKey(), key = _a.key, isComputed = _a.isComputed;
if (key instanceof types_1.Identifier) {
if (key.name === "constructor" && this.text[this.i] !== "(")
this.raise("RESERVED", "constructor");
if (getOrSet[key.name] === true &&
!(isComputed || /;|\(|\=/.test(this.text[this.i]))) {
kind = key.name;
var actual = this.definitionKey();
key = actual.key;
isComputed = actual.isComputed;
if (this.text[this.i] !== "(")
this.raise("EXPECTED", "(");
}
}
if (this.text[this.i] === "(") {
definition = new types_1.MethodDefinition(start);
definition.computed = isComputed;
definition.static = isStatic;
switch (key.name) {
case "constructor":
if (isStatic)
this.raise("JS_STATIC_CONSTRUCTOR", undefined, key.loc.start - 1);
else
definition.kind = "constructor";
break;
default:
definition.kind = kind !== null && kind !== void 0 ? kind : "method";
}
definition.key = key;
definition.value = this.functionExpression(true);
if (kind === "set" && definition.value.params.length !== 1) {
this.raise("JS_INVALID_SETTER_PARAMS", undefined, key.loc.end);
}
else if (kind === "get" && definition.value.params.length !== 0) {
this.raise("JS_INVALID_GETTER_PARAMS", undefined, key.loc.end);
}
definition.loc.end = this.i;
}
else {
definition = new types_1.PropertyDefinition(start);
definition.computed = isComputed;
definition.static = isStatic;
definition.key = key;
this.outerspace();
if (this.text[this.i] === "=") {
this.i++;
definition.value = this.expression();
if (definition.value === undefined)
this.raise("EXPRESSION_EXPECTED");
}
else if (!/}|;/.test(this.text[this.i])) {
if (/\n/.test(this.text.slice(key.loc.end, this.i))) {
this.recede();
definition.value = null;
}
else
this.raise("JS_UNEXP_KEYWORD_OR_IDENTIFIER");
}
else
definition.value = null;
definition.loc.end = this.i;
}
this.outerspace();
this.eat(";");
return definition;
};
base_1.ezra.definitionKey = function () {
this.outerspace();
var key, isComputed = false;
switch (true) {
case this.eat("["):
key = this.group("property");
isComputed = true;
break;
case (0, utils_1.isDigit)(this.text[this.i]):
key = this.numberLiteral();
break;
case this.eat("#"):
key = this.privateIdentifier();
break;
case /'|"/.test(this.text[this.i]):
key = this.stringLiteral();
break;
default:
key = this.identifier(true);
}
this.outerspace();
return { key: key, isComputed: isComputed };
};
base_1.ezra.privateIdentifier = function () {
var priv = new types_1.PrivateIdentifier(this.i);
var identifier = this.identifier(true);
if (identifier.name === "constructor")
this.raise("RESERVED", "constructor");
priv.name = identifier.name;
priv.loc.end = identifier.loc.end;
return priv;
};
base_1.ezra.classExpression = function () {
var classexp = new types_1.ClassExpression(this.i - 5);
this.outerspace();
if (this.text[this.i] !== "{") {
if (!this.match("extends"))
classexp.id = this.identifier();
this.outerspace();
if (this.belly.top() === "extends" || this.match("extends")) {
this.contexts.push("super_class");
classexp.superClass = this.expression();
this.contexts.pop();
}
this.outerspace();
}
if (!this.eat("{"))
this.raise("EXPECTED", "{");
classexp.body = new types_1.ClassBody(this.i);
classexp.body.body = this.group("class_body");
if (classexp.body.body.filter(function (def) { return def.kind === "constructor"; }).length >
1) {
this.raise("JS_DUPLICATE_CONSTRUCTORS");
}
classexp.loc.end = classexp.body.loc.end = this.i;
return this.reparse(classexp);
};