coffee-script
Version:
Unfancy JavaScript
1,546 lines (1,297 loc) • 130 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Access, Arr, Assign, Base, Block, BooleanLiteral, Call, Class, Code, CodeFragment, Comment, Existence, Expansion, ExportAllDeclaration, ExportDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, ExportSpecifier, ExportSpecifierList, Extends, For, IdentifierLiteral, If, ImportClause, ImportDeclaration, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, ImportSpecifierList, In, Index, InfinityLiteral, JS_FORBIDDEN, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, ModuleDeclaration, ModuleSpecifier, ModuleSpecifierList, NEGATE, NO, NaNLiteral, NullLiteral, NumberLiteral, Obj, Op, Param, Parens, PassthroughLiteral, PropertyName, Range, RegexLiteral, RegexWithInterpolations, Return, SIMPLENUM, Scope, Slice, Splat, StatementLiteral, StringLiteral, StringWithInterpolations, SuperCall, Switch, TAB, THIS, TaggedTemplateCall, ThisLiteral, Throw, Try, UTILITIES, UndefinedLiteral, Value, While, YES, YieldReturn, addLocationDataFn, compact, del, ends, extend, flatten, fragmentsToText, isComplexOrAssignable, isLiteralArguments, isLiteralThis, isUnassignable, locationDataToString, merge, multident, ref1, ref2, some, starts, throwSyntaxError, unfoldSoak, utility,
extend1 = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
slice = [].slice;
Error.stackTraceLimit = 2e308;
Scope = require('./scope').Scope;
ref1 = require('./lexer'), isUnassignable = ref1.isUnassignable, JS_FORBIDDEN = ref1.JS_FORBIDDEN;
ref2 = require('./helpers'), compact = ref2.compact, flatten = ref2.flatten, extend = ref2.extend, merge = ref2.merge, del = ref2.del, starts = ref2.starts, ends = ref2.ends, some = ref2.some, addLocationDataFn = ref2.addLocationDataFn, locationDataToString = ref2.locationDataToString, throwSyntaxError = ref2.throwSyntaxError;
exports.extend = extend;
exports.addLocationDataFn = addLocationDataFn;
YES = function() {
return true;
};
NO = function() {
return false;
};
THIS = function() {
return this;
};
NEGATE = function() {
this.negated = !this.negated;
return this;
};
exports.CodeFragment = CodeFragment = (function() {
function CodeFragment(parent, code) {
var ref3;
this.code = "" + code;
this.locationData = parent != null ? parent.locationData : void 0;
this.type = (parent != null ? (ref3 = parent.constructor) != null ? ref3.name : void 0 : void 0) || 'unknown';
}
CodeFragment.prototype.toString = function() {
return "" + this.code + (this.locationData ? ": " + locationDataToString(this.locationData) : '');
};
return CodeFragment;
})();
fragmentsToText = function(fragments) {
var fragment;
return ((function() {
var j, len1, results;
results = [];
for (j = 0, len1 = fragments.length; j < len1; j++) {
fragment = fragments[j];
results.push(fragment.code);
}
return results;
})()).join('');
};
exports.Base = Base = (function() {
function Base() {}
Base.prototype.compile = function(o, lvl) {
return fragmentsToText(this.compileToFragments(o, lvl));
};
Base.prototype.compileToFragments = function(o, lvl) {
var node;
o = extend({}, o);
if (lvl) {
o.level = lvl;
}
node = this.unfoldSoak(o) || this;
node.tab = o.indent;
if (o.level === LEVEL_TOP || !node.isStatement(o)) {
return node.compileNode(o);
} else {
return node.compileClosure(o);
}
};
Base.prototype.compileClosure = function(o) {
var args, argumentsNode, func, jumpNode, meth, parts, ref3;
if (jumpNode = this.jumps()) {
jumpNode.error('cannot use a pure statement in an expression');
}
o.sharedScope = true;
func = new Code([], Block.wrap([this]));
args = [];
if ((argumentsNode = this.contains(isLiteralArguments)) || this.contains(isLiteralThis)) {
args = [new ThisLiteral];
if (argumentsNode) {
meth = 'apply';
args.push(new IdentifierLiteral('arguments'));
} else {
meth = 'call';
}
func = new Value(func, [new Access(new PropertyName(meth))]);
}
parts = (new Call(func, args)).compileNode(o);
if (func.isGenerator || ((ref3 = func.base) != null ? ref3.isGenerator : void 0)) {
parts.unshift(this.makeCode("(yield* "));
parts.push(this.makeCode(")"));
}
return parts;
};
Base.prototype.cache = function(o, level, isComplex) {
var complex, ref, sub;
complex = isComplex != null ? isComplex(this) : this.isComplex();
if (complex) {
ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
sub = new Assign(ref, this);
if (level) {
return [sub.compileToFragments(o, level), [this.makeCode(ref.value)]];
} else {
return [sub, ref];
}
} else {
ref = level ? this.compileToFragments(o, level) : this;
return [ref, ref];
}
};
Base.prototype.cacheToCodeFragments = function(cacheValues) {
return [fragmentsToText(cacheValues[0]), fragmentsToText(cacheValues[1])];
};
Base.prototype.makeReturn = function(res) {
var me;
me = this.unwrapAll();
if (res) {
return new Call(new Literal(res + ".push"), [me]);
} else {
return new Return(me);
}
};
Base.prototype.contains = function(pred) {
var node;
node = void 0;
this.traverseChildren(false, function(n) {
if (pred(n)) {
node = n;
return false;
}
});
return node;
};
Base.prototype.lastNonComment = function(list) {
var i;
i = list.length;
while (i--) {
if (!(list[i] instanceof Comment)) {
return list[i];
}
}
return null;
};
Base.prototype.toString = function(idt, name) {
var tree;
if (idt == null) {
idt = '';
}
if (name == null) {
name = this.constructor.name;
}
tree = '\n' + idt + name;
if (this.soak) {
tree += '?';
}
this.eachChild(function(node) {
return tree += node.toString(idt + TAB);
});
return tree;
};
Base.prototype.eachChild = function(func) {
var attr, child, j, k, len1, len2, ref3, ref4;
if (!this.children) {
return this;
}
ref3 = this.children;
for (j = 0, len1 = ref3.length; j < len1; j++) {
attr = ref3[j];
if (this[attr]) {
ref4 = flatten([this[attr]]);
for (k = 0, len2 = ref4.length; k < len2; k++) {
child = ref4[k];
if (func(child) === false) {
return this;
}
}
}
}
return this;
};
Base.prototype.traverseChildren = function(crossScope, func) {
return this.eachChild(function(child) {
var recur;
recur = func(child);
if (recur !== false) {
return child.traverseChildren(crossScope, func);
}
});
};
Base.prototype.invert = function() {
return new Op('!', this);
};
Base.prototype.unwrapAll = function() {
var node;
node = this;
while (node !== (node = node.unwrap())) {
continue;
}
return node;
};
Base.prototype.children = [];
Base.prototype.isStatement = NO;
Base.prototype.jumps = NO;
Base.prototype.isComplex = YES;
Base.prototype.isChainable = NO;
Base.prototype.isAssignable = NO;
Base.prototype.isNumber = NO;
Base.prototype.unwrap = THIS;
Base.prototype.unfoldSoak = NO;
Base.prototype.assigns = NO;
Base.prototype.updateLocationDataIfMissing = function(locationData) {
if (this.locationData) {
return this;
}
this.locationData = locationData;
return this.eachChild(function(child) {
return child.updateLocationDataIfMissing(locationData);
});
};
Base.prototype.error = function(message) {
return throwSyntaxError(message, this.locationData);
};
Base.prototype.makeCode = function(code) {
return new CodeFragment(this, code);
};
Base.prototype.wrapInBraces = function(fragments) {
return [].concat(this.makeCode('('), fragments, this.makeCode(')'));
};
Base.prototype.joinFragmentArrays = function(fragmentsList, joinStr) {
var answer, fragments, i, j, len1;
answer = [];
for (i = j = 0, len1 = fragmentsList.length; j < len1; i = ++j) {
fragments = fragmentsList[i];
if (i) {
answer.push(this.makeCode(joinStr));
}
answer = answer.concat(fragments);
}
return answer;
};
return Base;
})();
exports.Block = Block = (function(superClass1) {
extend1(Block, superClass1);
function Block(nodes) {
this.expressions = compact(flatten(nodes || []));
}
Block.prototype.children = ['expressions'];
Block.prototype.push = function(node) {
this.expressions.push(node);
return this;
};
Block.prototype.pop = function() {
return this.expressions.pop();
};
Block.prototype.unshift = function(node) {
this.expressions.unshift(node);
return this;
};
Block.prototype.unwrap = function() {
if (this.expressions.length === 1) {
return this.expressions[0];
} else {
return this;
}
};
Block.prototype.isEmpty = function() {
return !this.expressions.length;
};
Block.prototype.isStatement = function(o) {
var exp, j, len1, ref3;
ref3 = this.expressions;
for (j = 0, len1 = ref3.length; j < len1; j++) {
exp = ref3[j];
if (exp.isStatement(o)) {
return true;
}
}
return false;
};
Block.prototype.jumps = function(o) {
var exp, j, jumpNode, len1, ref3;
ref3 = this.expressions;
for (j = 0, len1 = ref3.length; j < len1; j++) {
exp = ref3[j];
if (jumpNode = exp.jumps(o)) {
return jumpNode;
}
}
};
Block.prototype.makeReturn = function(res) {
var expr, len;
len = this.expressions.length;
while (len--) {
expr = this.expressions[len];
if (!(expr instanceof Comment)) {
this.expressions[len] = expr.makeReturn(res);
if (expr instanceof Return && !expr.expression) {
this.expressions.splice(len, 1);
}
break;
}
}
return this;
};
Block.prototype.compileToFragments = function(o, level) {
if (o == null) {
o = {};
}
if (o.scope) {
return Block.__super__.compileToFragments.call(this, o, level);
} else {
return this.compileRoot(o);
}
};
Block.prototype.compileNode = function(o) {
var answer, compiledNodes, fragments, index, j, len1, node, ref3, top;
this.tab = o.indent;
top = o.level === LEVEL_TOP;
compiledNodes = [];
ref3 = this.expressions;
for (index = j = 0, len1 = ref3.length; j < len1; index = ++j) {
node = ref3[index];
node = node.unwrapAll();
node = node.unfoldSoak(o) || node;
if (node instanceof Block) {
compiledNodes.push(node.compileNode(o));
} else if (top) {
node.front = true;
fragments = node.compileToFragments(o);
if (!node.isStatement(o)) {
fragments.unshift(this.makeCode("" + this.tab));
fragments.push(this.makeCode(";"));
}
compiledNodes.push(fragments);
} else {
compiledNodes.push(node.compileToFragments(o, LEVEL_LIST));
}
}
if (top) {
if (this.spaced) {
return [].concat(this.joinFragmentArrays(compiledNodes, '\n\n'), this.makeCode("\n"));
} else {
return this.joinFragmentArrays(compiledNodes, '\n');
}
}
if (compiledNodes.length) {
answer = this.joinFragmentArrays(compiledNodes, ', ');
} else {
answer = [this.makeCode("void 0")];
}
if (compiledNodes.length > 1 && o.level >= LEVEL_LIST) {
return this.wrapInBraces(answer);
} else {
return answer;
}
};
Block.prototype.compileRoot = function(o) {
var exp, fragments, i, j, len1, name, prelude, preludeExps, ref3, ref4, rest;
o.indent = o.bare ? '' : TAB;
o.level = LEVEL_TOP;
this.spaced = true;
o.scope = new Scope(null, this, null, (ref3 = o.referencedVars) != null ? ref3 : []);
ref4 = o.locals || [];
for (j = 0, len1 = ref4.length; j < len1; j++) {
name = ref4[j];
o.scope.parameter(name);
}
prelude = [];
if (!o.bare) {
preludeExps = (function() {
var k, len2, ref5, results;
ref5 = this.expressions;
results = [];
for (i = k = 0, len2 = ref5.length; k < len2; i = ++k) {
exp = ref5[i];
if (!(exp.unwrap() instanceof Comment)) {
break;
}
results.push(exp);
}
return results;
}).call(this);
rest = this.expressions.slice(preludeExps.length);
this.expressions = preludeExps;
if (preludeExps.length) {
prelude = this.compileNode(merge(o, {
indent: ''
}));
prelude.push(this.makeCode("\n"));
}
this.expressions = rest;
}
fragments = this.compileWithDeclarations(o);
if (o.bare) {
return fragments;
}
return [].concat(prelude, this.makeCode("(function() {\n"), fragments, this.makeCode("\n}).call(this);\n"));
};
Block.prototype.compileWithDeclarations = function(o) {
var assigns, declars, exp, fragments, i, j, len1, post, ref3, ref4, ref5, rest, scope, spaced;
fragments = [];
post = [];
ref3 = this.expressions;
for (i = j = 0, len1 = ref3.length; j < len1; i = ++j) {
exp = ref3[i];
exp = exp.unwrap();
if (!(exp instanceof Comment || exp instanceof Literal)) {
break;
}
}
o = merge(o, {
level: LEVEL_TOP
});
if (i) {
rest = this.expressions.splice(i, 9e9);
ref4 = [this.spaced, false], spaced = ref4[0], this.spaced = ref4[1];
ref5 = [this.compileNode(o), spaced], fragments = ref5[0], this.spaced = ref5[1];
this.expressions = rest;
}
post = this.compileNode(o);
scope = o.scope;
if (scope.expressions === this) {
declars = o.scope.hasDeclarations();
assigns = scope.hasAssignments;
if (declars || assigns) {
if (i) {
fragments.push(this.makeCode('\n'));
}
fragments.push(this.makeCode(this.tab + "var "));
if (declars) {
fragments.push(this.makeCode(scope.declaredVariables().join(', ')));
}
if (assigns) {
if (declars) {
fragments.push(this.makeCode(",\n" + (this.tab + TAB)));
}
fragments.push(this.makeCode(scope.assignedVariables().join(",\n" + (this.tab + TAB))));
}
fragments.push(this.makeCode(";\n" + (this.spaced ? '\n' : '')));
} else if (fragments.length && post.length) {
fragments.push(this.makeCode("\n"));
}
}
return fragments.concat(post);
};
Block.wrap = function(nodes) {
if (nodes.length === 1 && nodes[0] instanceof Block) {
return nodes[0];
}
return new Block(nodes);
};
return Block;
})(Base);
exports.Literal = Literal = (function(superClass1) {
extend1(Literal, superClass1);
function Literal(value1) {
this.value = value1;
}
Literal.prototype.isComplex = NO;
Literal.prototype.assigns = function(name) {
return name === this.value;
};
Literal.prototype.compileNode = function(o) {
return [this.makeCode(this.value)];
};
Literal.prototype.toString = function() {
return " " + (this.isStatement() ? Literal.__super__.toString.apply(this, arguments) : this.constructor.name) + ": " + this.value;
};
return Literal;
})(Base);
exports.NumberLiteral = NumberLiteral = (function(superClass1) {
extend1(NumberLiteral, superClass1);
function NumberLiteral() {
return NumberLiteral.__super__.constructor.apply(this, arguments);
}
return NumberLiteral;
})(Literal);
exports.InfinityLiteral = InfinityLiteral = (function(superClass1) {
extend1(InfinityLiteral, superClass1);
function InfinityLiteral() {
return InfinityLiteral.__super__.constructor.apply(this, arguments);
}
InfinityLiteral.prototype.compileNode = function() {
return [this.makeCode('2e308')];
};
return InfinityLiteral;
})(NumberLiteral);
exports.NaNLiteral = NaNLiteral = (function(superClass1) {
extend1(NaNLiteral, superClass1);
function NaNLiteral() {
NaNLiteral.__super__.constructor.call(this, 'NaN');
}
NaNLiteral.prototype.compileNode = function(o) {
var code;
code = [this.makeCode('0/0')];
if (o.level >= LEVEL_OP) {
return this.wrapInBraces(code);
} else {
return code;
}
};
return NaNLiteral;
})(NumberLiteral);
exports.StringLiteral = StringLiteral = (function(superClass1) {
extend1(StringLiteral, superClass1);
function StringLiteral() {
return StringLiteral.__super__.constructor.apply(this, arguments);
}
return StringLiteral;
})(Literal);
exports.RegexLiteral = RegexLiteral = (function(superClass1) {
extend1(RegexLiteral, superClass1);
function RegexLiteral() {
return RegexLiteral.__super__.constructor.apply(this, arguments);
}
return RegexLiteral;
})(Literal);
exports.PassthroughLiteral = PassthroughLiteral = (function(superClass1) {
extend1(PassthroughLiteral, superClass1);
function PassthroughLiteral() {
return PassthroughLiteral.__super__.constructor.apply(this, arguments);
}
return PassthroughLiteral;
})(Literal);
exports.IdentifierLiteral = IdentifierLiteral = (function(superClass1) {
extend1(IdentifierLiteral, superClass1);
function IdentifierLiteral() {
return IdentifierLiteral.__super__.constructor.apply(this, arguments);
}
IdentifierLiteral.prototype.isAssignable = YES;
return IdentifierLiteral;
})(Literal);
exports.PropertyName = PropertyName = (function(superClass1) {
extend1(PropertyName, superClass1);
function PropertyName() {
return PropertyName.__super__.constructor.apply(this, arguments);
}
PropertyName.prototype.isAssignable = YES;
return PropertyName;
})(Literal);
exports.StatementLiteral = StatementLiteral = (function(superClass1) {
extend1(StatementLiteral, superClass1);
function StatementLiteral() {
return StatementLiteral.__super__.constructor.apply(this, arguments);
}
StatementLiteral.prototype.isStatement = YES;
StatementLiteral.prototype.makeReturn = THIS;
StatementLiteral.prototype.jumps = function(o) {
if (this.value === 'break' && !((o != null ? o.loop : void 0) || (o != null ? o.block : void 0))) {
return this;
}
if (this.value === 'continue' && !(o != null ? o.loop : void 0)) {
return this;
}
};
StatementLiteral.prototype.compileNode = function(o) {
return [this.makeCode("" + this.tab + this.value + ";")];
};
return StatementLiteral;
})(Literal);
exports.ThisLiteral = ThisLiteral = (function(superClass1) {
extend1(ThisLiteral, superClass1);
function ThisLiteral() {
ThisLiteral.__super__.constructor.call(this, 'this');
}
ThisLiteral.prototype.compileNode = function(o) {
var code, ref3;
code = ((ref3 = o.scope.method) != null ? ref3.bound : void 0) ? o.scope.method.context : this.value;
return [this.makeCode(code)];
};
return ThisLiteral;
})(Literal);
exports.UndefinedLiteral = UndefinedLiteral = (function(superClass1) {
extend1(UndefinedLiteral, superClass1);
function UndefinedLiteral() {
UndefinedLiteral.__super__.constructor.call(this, 'undefined');
}
UndefinedLiteral.prototype.compileNode = function(o) {
return [this.makeCode(o.level >= LEVEL_ACCESS ? '(void 0)' : 'void 0')];
};
return UndefinedLiteral;
})(Literal);
exports.NullLiteral = NullLiteral = (function(superClass1) {
extend1(NullLiteral, superClass1);
function NullLiteral() {
NullLiteral.__super__.constructor.call(this, 'null');
}
return NullLiteral;
})(Literal);
exports.BooleanLiteral = BooleanLiteral = (function(superClass1) {
extend1(BooleanLiteral, superClass1);
function BooleanLiteral() {
return BooleanLiteral.__super__.constructor.apply(this, arguments);
}
return BooleanLiteral;
})(Literal);
exports.Return = Return = (function(superClass1) {
extend1(Return, superClass1);
function Return(expression) {
this.expression = expression;
}
Return.prototype.children = ['expression'];
Return.prototype.isStatement = YES;
Return.prototype.makeReturn = THIS;
Return.prototype.jumps = THIS;
Return.prototype.compileToFragments = function(o, level) {
var expr, ref3;
expr = (ref3 = this.expression) != null ? ref3.makeReturn() : void 0;
if (expr && !(expr instanceof Return)) {
return expr.compileToFragments(o, level);
} else {
return Return.__super__.compileToFragments.call(this, o, level);
}
};
Return.prototype.compileNode = function(o) {
var answer;
answer = [];
answer.push(this.makeCode(this.tab + ("return" + (this.expression ? " " : ""))));
if (this.expression) {
answer = answer.concat(this.expression.compileToFragments(o, LEVEL_PAREN));
}
answer.push(this.makeCode(";"));
return answer;
};
return Return;
})(Base);
exports.YieldReturn = YieldReturn = (function(superClass1) {
extend1(YieldReturn, superClass1);
function YieldReturn() {
return YieldReturn.__super__.constructor.apply(this, arguments);
}
YieldReturn.prototype.compileNode = function(o) {
if (o.scope.parent == null) {
this.error('yield can only occur inside functions');
}
return YieldReturn.__super__.compileNode.apply(this, arguments);
};
return YieldReturn;
})(Return);
exports.Value = Value = (function(superClass1) {
extend1(Value, superClass1);
function Value(base, props, tag) {
if (!props && base instanceof Value) {
return base;
}
this.base = base;
this.properties = props || [];
if (tag) {
this[tag] = true;
}
return this;
}
Value.prototype.children = ['base', 'properties'];
Value.prototype.add = function(props) {
this.properties = this.properties.concat(props);
return this;
};
Value.prototype.hasProperties = function() {
return !!this.properties.length;
};
Value.prototype.bareLiteral = function(type) {
return !this.properties.length && this.base instanceof type;
};
Value.prototype.isArray = function() {
return this.bareLiteral(Arr);
};
Value.prototype.isRange = function() {
return this.bareLiteral(Range);
};
Value.prototype.isComplex = function() {
return this.hasProperties() || this.base.isComplex();
};
Value.prototype.isAssignable = function() {
return this.hasProperties() || this.base.isAssignable();
};
Value.prototype.isNumber = function() {
return this.bareLiteral(NumberLiteral);
};
Value.prototype.isString = function() {
return this.bareLiteral(StringLiteral);
};
Value.prototype.isRegex = function() {
return this.bareLiteral(RegexLiteral);
};
Value.prototype.isUndefined = function() {
return this.bareLiteral(UndefinedLiteral);
};
Value.prototype.isNull = function() {
return this.bareLiteral(NullLiteral);
};
Value.prototype.isBoolean = function() {
return this.bareLiteral(BooleanLiteral);
};
Value.prototype.isAtomic = function() {
var j, len1, node, ref3;
ref3 = this.properties.concat(this.base);
for (j = 0, len1 = ref3.length; j < len1; j++) {
node = ref3[j];
if (node.soak || node instanceof Call) {
return false;
}
}
return true;
};
Value.prototype.isNotCallable = function() {
return this.isNumber() || this.isString() || this.isRegex() || this.isArray() || this.isRange() || this.isSplice() || this.isObject() || this.isUndefined() || this.isNull() || this.isBoolean();
};
Value.prototype.isStatement = function(o) {
return !this.properties.length && this.base.isStatement(o);
};
Value.prototype.assigns = function(name) {
return !this.properties.length && this.base.assigns(name);
};
Value.prototype.jumps = function(o) {
return !this.properties.length && this.base.jumps(o);
};
Value.prototype.isObject = function(onlyGenerated) {
if (this.properties.length) {
return false;
}
return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
};
Value.prototype.isSplice = function() {
var lastProp, ref3;
ref3 = this.properties, lastProp = ref3[ref3.length - 1];
return lastProp instanceof Slice;
};
Value.prototype.looksStatic = function(className) {
var ref3;
return this.base.value === className && this.properties.length === 1 && ((ref3 = this.properties[0].name) != null ? ref3.value : void 0) !== 'prototype';
};
Value.prototype.unwrap = function() {
if (this.properties.length) {
return this;
} else {
return this.base;
}
};
Value.prototype.cacheReference = function(o) {
var base, bref, name, nref, ref3;
ref3 = this.properties, name = ref3[ref3.length - 1];
if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
return [this, this];
}
base = new Value(this.base, this.properties.slice(0, -1));
if (base.isComplex()) {
bref = new IdentifierLiteral(o.scope.freeVariable('base'));
base = new Value(new Parens(new Assign(bref, base)));
}
if (!name) {
return [base, bref];
}
if (name.isComplex()) {
nref = new IdentifierLiteral(o.scope.freeVariable('name'));
name = new Index(new Assign(nref, name.index));
nref = new Index(nref);
}
return [base.add(name), new Value(bref || base.base, [nref || name])];
};
Value.prototype.compileNode = function(o) {
var fragments, j, len1, prop, props;
this.base.front = this.front;
props = this.properties;
fragments = this.base.compileToFragments(o, (props.length ? LEVEL_ACCESS : null));
if (props.length && SIMPLENUM.test(fragmentsToText(fragments))) {
fragments.push(this.makeCode('.'));
}
for (j = 0, len1 = props.length; j < len1; j++) {
prop = props[j];
fragments.push.apply(fragments, prop.compileToFragments(o));
}
return fragments;
};
Value.prototype.unfoldSoak = function(o) {
return this.unfoldedSoak != null ? this.unfoldedSoak : this.unfoldedSoak = (function(_this) {
return function() {
var fst, i, ifn, j, len1, prop, ref, ref3, ref4, snd;
if (ifn = _this.base.unfoldSoak(o)) {
(ref3 = ifn.body.properties).push.apply(ref3, _this.properties);
return ifn;
}
ref4 = _this.properties;
for (i = j = 0, len1 = ref4.length; j < len1; i = ++j) {
prop = ref4[i];
if (!prop.soak) {
continue;
}
prop.soak = false;
fst = new Value(_this.base, _this.properties.slice(0, i));
snd = new Value(_this.base, _this.properties.slice(i));
if (fst.isComplex()) {
ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
fst = new Parens(new Assign(ref, fst));
snd.base = ref;
}
return new If(new Existence(fst), snd, {
soak: true
});
}
return false;
};
})(this)();
};
return Value;
})(Base);
exports.Comment = Comment = (function(superClass1) {
extend1(Comment, superClass1);
function Comment(comment1) {
this.comment = comment1;
}
Comment.prototype.isStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o, level) {
var code, comment;
comment = this.comment.replace(/^(\s*)#(?=\s)/gm, "$1 *");
code = "/*" + (multident(comment, this.tab)) + (indexOf.call(comment, '\n') >= 0 ? "\n" + this.tab : '') + " */";
if ((level || o.level) === LEVEL_TOP) {
code = o.indent + code;
}
return [this.makeCode("\n"), this.makeCode(code)];
};
return Comment;
})(Base);
exports.Call = Call = (function(superClass1) {
extend1(Call, superClass1);
function Call(variable1, args1, soak1) {
this.variable = variable1;
this.args = args1 != null ? args1 : [];
this.soak = soak1;
this.isNew = false;
if (this.variable instanceof Value && this.variable.isNotCallable()) {
this.variable.error("literal is not a function");
}
}
Call.prototype.children = ['variable', 'args'];
Call.prototype.updateLocationDataIfMissing = function(locationData) {
var base, ref3;
if (this.locationData && this.needsUpdatedStartLocation) {
this.locationData.first_line = locationData.first_line;
this.locationData.first_column = locationData.first_column;
base = ((ref3 = this.variable) != null ? ref3.base : void 0) || this.variable;
if (base.needsUpdatedStartLocation) {
this.variable.locationData.first_line = locationData.first_line;
this.variable.locationData.first_column = locationData.first_column;
base.updateLocationDataIfMissing(locationData);
}
delete this.needsUpdatedStartLocation;
}
return Call.__super__.updateLocationDataIfMissing.apply(this, arguments);
};
Call.prototype.newInstance = function() {
var base, ref3;
base = ((ref3 = this.variable) != null ? ref3.base : void 0) || this.variable;
if (base instanceof Call && !base.isNew) {
base.newInstance();
} else {
this.isNew = true;
}
this.needsUpdatedStartLocation = true;
return this;
};
Call.prototype.unfoldSoak = function(o) {
var call, ifn, j, left, len1, list, ref3, ref4, rite;
if (this.soak) {
if (this instanceof SuperCall) {
left = new Literal(this.superReference(o));
rite = new Value(left);
} else {
if (ifn = unfoldSoak(o, this, 'variable')) {
return ifn;
}
ref3 = new Value(this.variable).cacheReference(o), left = ref3[0], rite = ref3[1];
}
rite = new Call(rite, this.args);
rite.isNew = this.isNew;
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
return new If(left, new Value(rite), {
soak: true
});
}
call = this;
list = [];
while (true) {
if (call.variable instanceof Call) {
list.push(call);
call = call.variable;
continue;
}
if (!(call.variable instanceof Value)) {
break;
}
list.push(call);
if (!((call = call.variable.base) instanceof Call)) {
break;
}
}
ref4 = list.reverse();
for (j = 0, len1 = ref4.length; j < len1; j++) {
call = ref4[j];
if (ifn) {
if (call.variable instanceof Call) {
call.variable = ifn;
} else {
call.variable.base = ifn;
}
}
ifn = unfoldSoak(o, call, 'variable');
}
return ifn;
};
Call.prototype.compileNode = function(o) {
var arg, argIndex, compiledArgs, compiledArray, fragments, j, len1, preface, ref3, ref4;
if ((ref3 = this.variable) != null) {
ref3.front = this.front;
}
compiledArray = Splat.compileSplattedArray(o, this.args, true);
if (compiledArray.length) {
return this.compileSplat(o, compiledArray);
}
compiledArgs = [];
ref4 = this.args;
for (argIndex = j = 0, len1 = ref4.length; j < len1; argIndex = ++j) {
arg = ref4[argIndex];
if (argIndex) {
compiledArgs.push(this.makeCode(", "));
}
compiledArgs.push.apply(compiledArgs, arg.compileToFragments(o, LEVEL_LIST));
}
fragments = [];
if (this instanceof SuperCall) {
preface = this.superReference(o) + (".call(" + (this.superThis(o)));
if (compiledArgs.length) {
preface += ", ";
}
fragments.push(this.makeCode(preface));
} else {
if (this.isNew) {
fragments.push(this.makeCode('new '));
}
fragments.push.apply(fragments, this.variable.compileToFragments(o, LEVEL_ACCESS));
fragments.push(this.makeCode("("));
}
fragments.push.apply(fragments, compiledArgs);
fragments.push(this.makeCode(")"));
return fragments;
};
Call.prototype.compileSplat = function(o, splatArgs) {
var answer, base, fun, idt, name, ref;
if (this instanceof SuperCall) {
return [].concat(this.makeCode((this.superReference(o)) + ".apply(" + (this.superThis(o)) + ", "), splatArgs, this.makeCode(")"));
}
if (this.isNew) {
idt = this.tab + TAB;
return [].concat(this.makeCode("(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return Object(result) === result ? result : child;\n" + this.tab + "})("), this.variable.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), splatArgs, this.makeCode(", function(){})"));
}
answer = [];
base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('ref');
answer = answer.concat(this.makeCode("(" + ref + " = "), base.compileToFragments(o, LEVEL_LIST), this.makeCode(")"), name.compileToFragments(o));
} else {
fun = base.compileToFragments(o, LEVEL_ACCESS);
if (SIMPLENUM.test(fragmentsToText(fun))) {
fun = this.wrapInBraces(fun);
}
if (name) {
ref = fragmentsToText(fun);
fun.push.apply(fun, name.compileToFragments(o));
} else {
ref = 'null';
}
answer = answer.concat(fun);
}
return answer = answer.concat(this.makeCode(".apply(" + ref + ", "), splatArgs, this.makeCode(")"));
};
return Call;
})(Base);
exports.SuperCall = SuperCall = (function(superClass1) {
extend1(SuperCall, superClass1);
function SuperCall(args) {
SuperCall.__super__.constructor.call(this, null, args != null ? args : [new Splat(new IdentifierLiteral('arguments'))]);
this.isBare = args != null;
}
SuperCall.prototype.superReference = function(o) {
var accesses, base, bref, klass, method, name, nref, variable;
method = o.scope.namedMethod();
if (method != null ? method.klass : void 0) {
klass = method.klass, name = method.name, variable = method.variable;
if (klass.isComplex()) {
bref = new IdentifierLiteral(o.scope.parent.freeVariable('base'));
base = new Value(new Parens(new Assign(bref, klass)));
variable.base = base;
variable.properties.splice(0, klass.properties.length);
}
if (name.isComplex() || (name instanceof Index && name.index.isAssignable())) {
nref = new IdentifierLiteral(o.scope.parent.freeVariable('name'));
name = new Index(new Assign(nref, name.index));
variable.properties.pop();
variable.properties.push(name);
}
accesses = [new Access(new PropertyName('__super__'))];
if (method["static"]) {
accesses.push(new Access(new PropertyName('constructor')));
}
accesses.push(nref != null ? new Index(nref) : name);
return (new Value(bref != null ? bref : klass, accesses)).compile(o);
} else if (method != null ? method.ctor : void 0) {
return method.name + ".__super__.constructor";
} else {
return this.error('cannot call super outside of an instance method.');
}
};
SuperCall.prototype.superThis = function(o) {
var method;
method = o.scope.method;
return (method && !method.klass && method.context) || "this";
};
return SuperCall;
})(Call);
exports.RegexWithInterpolations = RegexWithInterpolations = (function(superClass1) {
extend1(RegexWithInterpolations, superClass1);
function RegexWithInterpolations(args) {
if (args == null) {
args = [];
}
RegexWithInterpolations.__super__.constructor.call(this, new Value(new IdentifierLiteral('RegExp')), args, false);
}
return RegexWithInterpolations;
})(Call);
exports.TaggedTemplateCall = TaggedTemplateCall = (function(superClass1) {
extend1(TaggedTemplateCall, superClass1);
function TaggedTemplateCall(variable, arg, soak) {
if (arg instanceof StringLiteral) {
arg = new StringWithInterpolations(Block.wrap([new Value(arg)]));
}
TaggedTemplateCall.__super__.constructor.call(this, variable, [arg], soak);
}
TaggedTemplateCall.prototype.compileNode = function(o) {
o.inTaggedTemplateCall = true;
return this.variable.compileToFragments(o, LEVEL_ACCESS).concat(this.args[0].compileToFragments(o, LEVEL_LIST));
};
return TaggedTemplateCall;
})(Call);
exports.Extends = Extends = (function(superClass1) {
extend1(Extends, superClass1);
function Extends(child1, parent1) {
this.child = child1;
this.parent = parent1;
}
Extends.prototype.children = ['child', 'parent'];
Extends.prototype.compileToFragments = function(o) {
return new Call(new Value(new Literal(utility('extend', o))), [this.child, this.parent]).compileToFragments(o);
};
return Extends;
})(Base);
exports.Access = Access = (function(superClass1) {
extend1(Access, superClass1);
function Access(name1, tag) {
this.name = name1;
this.soak = tag === 'soak';
}
Access.prototype.children = ['name'];
Access.prototype.compileToFragments = function(o) {
var name, node, ref3;
name = this.name.compileToFragments(o);
node = this.name.unwrap();
if (node instanceof PropertyName) {
if (ref3 = node.value, indexOf.call(JS_FORBIDDEN, ref3) >= 0) {
return [this.makeCode('["')].concat(slice.call(name), [this.makeCode('"]')]);
} else {
return [this.makeCode('.')].concat(slice.call(name));
}
} else {
return [this.makeCode('[')].concat(slice.call(name), [this.makeCode(']')]);
}
};
Access.prototype.isComplex = NO;
return Access;
})(Base);
exports.Index = Index = (function(superClass1) {
extend1(Index, superClass1);
function Index(index1) {
this.index = index1;
}
Index.prototype.children = ['index'];
Index.prototype.compileToFragments = function(o) {
return [].concat(this.makeCode("["), this.index.compileToFragments(o, LEVEL_PAREN), this.makeCode("]"));
};
Index.prototype.isComplex = function() {
return this.index.isComplex();
};
return Index;
})(Base);
exports.Range = Range = (function(superClass1) {
extend1(Range, superClass1);
Range.prototype.children = ['from', 'to'];
function Range(from1, to1, tag) {
this.from = from1;
this.to = to1;
this.exclusive = tag === 'exclusive';
this.equals = this.exclusive ? '' : '=';
}
Range.prototype.compileVariables = function(o) {
var isComplex, ref3, ref4, ref5, step;
o = merge(o, {
top: true
});
isComplex = del(o, 'isComplex');
ref3 = this.cacheToCodeFragments(this.from.cache(o, LEVEL_LIST, isComplex)), this.fromC = ref3[0], this.fromVar = ref3[1];
ref4 = this.cacheToCodeFragments(this.to.cache(o, LEVEL_LIST, isComplex)), this.toC = ref4[0], this.toVar = ref4[1];
if (step = del(o, 'step')) {
ref5 = this.cacheToCodeFragments(step.cache(o, LEVEL_LIST, isComplex)), this.step = ref5[0], this.stepVar = ref5[1];
}
this.fromNum = this.from.isNumber() ? Number(this.fromVar) : null;
this.toNum = this.to.isNumber() ? Number(this.toVar) : null;
return this.stepNum = (step != null ? step.isNumber() : void 0) ? Number(this.stepVar) : null;
};
Range.prototype.compileNode = function(o) {
var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, ref3, ref4, stepPart, to, varPart;
if (!this.fromVar) {
this.compileVariables(o);
}
if (!o.index) {
return this.compileArray(o);
}
known = (this.fromNum != null) && (this.toNum != null);
idx = del(o, 'index');
idxName = del(o, 'name');
namedIndex = idxName && idxName !== idx;
varPart = idx + " = " + this.fromC;
if (this.toC !== this.toVar) {
varPart += ", " + this.toC;
}
if (this.step !== this.stepVar) {
varPart += ", " + this.step;
}
ref3 = [idx + " <" + this.equals, idx + " >" + this.equals], lt = ref3[0], gt = ref3[1];
condPart = this.stepNum != null ? this.stepNum > 0 ? lt + " " + this.toVar : gt + " " + this.toVar : known ? ((ref4 = [this.fromNum, this.toNum], from = ref4[0], to = ref4[1], ref4), from <= to ? lt + " " + to : gt + " " + to) : (cond = this.stepVar ? this.stepVar + " > 0" : this.fromVar + " <= " + this.toVar, cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
stepPart = this.stepVar ? idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? idx + "++" : idx + "--" : namedIndex ? cond + " ? ++" + idx + " : --" + idx : cond + " ? " + idx + "++ : " + idx + "--";
if (namedIndex) {
varPart = idxName + " = " + varPart;
}
if (namedIndex) {
stepPart = idxName + " = " + stepPart;
}
return [this.makeCode(varPart + "; " + condPart + "; " + stepPart)];
};
Range.prototype.compileArray = function(o) {
var args, body, cond, hasArgs, i, idt, j, known, post, pre, range, ref3, ref4, result, results, vars;
known = (this.fromNum != null) && (this.toNum != null);
if (known && Math.abs(this.fromNum - this.toNum) <= 20) {
range = (function() {
results = [];
for (var j = ref3 = this.fromNum, ref4 = this.toNum; ref3 <= ref4 ? j <= ref4 : j >= ref4; ref3 <= ref4 ? j++ : j--){ results.push(j); }
return results;
}).apply(this);
if (this.exclusive) {
range.pop();
}
return [this.makeCode("[" + (range.join(', ')) + "]")];
}
idt = this.tab + TAB;
i = o.scope.freeVariable('i', {
single: true
});
result = o.scope.freeVariable('results');
pre = "\n" + idt + result + " = [];";
if (known) {
o.index = i;
body = fragmentsToText(this.compileNode(o));
} else {
vars = (i + " = " + this.fromC) + (this.toC !== this.toVar ? ", " + this.toC : '');
cond = this.fromVar + " <= " + this.toVar;
body = "var " + vars + "; " + cond + " ? " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + cond + " ? " + i + "++ : " + i + "--";
}
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
hasArgs = function(node) {
return node != null ? node.contains(isLiteralArguments) : void 0;
};
if (hasArgs(this.from) || hasArgs(this.to)) {
args = ', arguments';
}
return [this.makeCode("(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this" + (args != null ? args : '') + ")")];
};
return Range;
})(Base);
exports.Slice = Slice = (function(superClass1) {
extend1(Slice, superClass1);
Slice.prototype.children = ['range'];
function Slice(range1) {
this.range = range1;
Slice.__super__.constructor.call(this);
}
Slice.prototype.compileNode = function(o) {
var compiled, compiledText, from, fromCompiled, ref3, to, toStr;
ref3 = this.range, to = ref3.to, from = ref3.from;
fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
if (to) {
compiled = to.compileToFragments(o, LEVEL_PAREN);
compiledText = fragmentsToText(compiled);
if (!(!this.range.exclusive && +compiledText === -1)) {
toStr = ', ' + (this.range.exclusive ? compiledText : to.isNumber() ? "" + (+compiledText + 1) : (compiled = to.compileToFragments(o, LEVEL_ACCESS), "+" + (fragmentsToText(compiled)) + " + 1 || 9e9"));
}
}
return [this.makeCode(".slice(" + (fragmentsToText(fromCompiled)) + (toStr || '') + ")")];
};
return Slice;
})(Base);
exports.Obj = Obj = (function(superClass1) {
extend1(Obj, superClass1);
function Obj(props, generated) {
this.generated = generated != null ? generated : false;
this.objects = this.properties = props || [];
}
Obj.prototype.children = ['properties'];
Obj.prototype.compileNode = function(o) {
var answer, dynamicIndex, hasDynamic, i, idt, indent, j, join, k, key, l, lastNoncom, len1, len2, len3, node, oref, prop, props, ref3, value;
props = this.properties;
if (this.generated) {
for (j = 0, len1 = props.length; j < len1; j++) {
node = props[j];
if (node instanceof Value) {
node.error('cannot have an implicit value in an implicit object');
}
}
}
for (dynamicIndex = k = 0, len2 = props.length; k < len2; dynamicIndex = ++k) {
prop = props[dynamicIndex];
if ((prop.variable || prop).base instanceof Parens) {
break;
}
}
hasDynamic = dynamicIndex < props.length;
idt = o.indent += TAB;
lastNoncom = this.lastNonComment(this.properties);
answer = [];
if (hasDynamic) {
oref = o.scope.freeVariable('obj');
answer.push(this.makeCode("(\n" + idt + oref + " = "));
}
answer.push(this.makeCode("{" + (props.length === 0 || dynamicIndex === 0 ? '}' : '\n')));
for (i = l = 0, len3 = props.length; l < len3; i = ++l) {
prop = props[i];
if (i === dynamicIndex) {
if (i !== 0) {
answer.push(this.makeCode("\n" + idt + "}"));
}
answer.push(this.makeCode(',\n'));
}
join = i === props.length - 1 || i === dynamicIndex - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : idt;
if (hasDynamic && i < dynamicIndex) {
indent += TAB;
}
if (prop instanceof Assign) {
if (prop.context !== 'object') {
prop.operatorToken.error("unexpected " + prop.operatorToken.value);
}
if (prop.variable instanceof Value && prop.variable.hasProperties()) {
prop.variable.error('invalid object key');
}
}
if (prop instanceof Value && prop["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
}
if (!(prop instanceof Comment)) {
if (i < dynamicIndex) {
if (!(prop instanceof Assign)) {
prop = new Assign(prop, prop, 'object');
}
} else {
if (prop instanceof Assign) {
key = prop.variable;
value = prop.value;
} else {
ref3 = prop.base.cache(o), key = ref3[0], value = ref3[1];
if (key instanceof IdentifierLiteral) {
key = new PropertyName(key.value);
}
}
prop = new Assign(new Value(new IdentifierLiteral(oref), [new Access(key)]), value);
}
}
if (indent) {
answer.push(this.makeCode(indent));
}
answer.push.apply(answer, prop.compileToFragments(o, LEVEL_TOP));
if (join) {
answer.push(this.makeCode(join));
}
}
if (hasDynamic) {
answer.push(this.makeCode(",\n" + idt + oref + "\n" + this.tab + ")"));
} else {
if (props.length !== 0) {
answer.push(t