2015-09-03 12:37:36 -04:00
|
|
|
// Generated by CoffeeScript 1.10.0
|
2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2015-02-07 15:50:41 -05:00
|
|
|
var Scope,
|
2015-01-30 14:33:03 -05:00
|
|
|
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; };
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
exports.Scope = Scope = (function() {
|
2015-01-30 14:33:03 -05:00
|
|
|
function Scope(parent, expressions, method, referencedVars) {
|
2015-02-07 15:50:41 -05:00
|
|
|
var ref, ref1;
|
2015-01-30 14:33:03 -05:00
|
|
|
this.parent = parent;
|
|
|
|
this.expressions = expressions;
|
|
|
|
this.method = method;
|
|
|
|
this.referencedVars = referencedVars;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.variables = [
|
|
|
|
{
|
|
|
|
name: 'arguments',
|
|
|
|
type: 'arguments'
|
2010-10-04 23:21:16 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
];
|
|
|
|
this.positions = {};
|
2012-04-10 14:57:45 -04:00
|
|
|
if (!this.parent) {
|
2015-01-11 06:12:40 -05:00
|
|
|
this.utilities = {};
|
2012-04-10 14:57:45 -04:00
|
|
|
}
|
2015-02-07 15:50:41 -05:00
|
|
|
this.root = (ref = (ref1 = this.parent) != null ? ref1.root : void 0) != null ? ref : this;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-13 21:24:32 -05:00
|
|
|
Scope.prototype.add = function(name, type, immediate) {
|
2012-04-10 14:57:45 -04:00
|
|
|
if (this.shared && !immediate) {
|
|
|
|
return this.parent.add(name, type, immediate);
|
|
|
|
}
|
2011-11-14 11:18:45 -05:00
|
|
|
if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
|
2011-11-10 03:08:38 -05:00
|
|
|
return this.variables[this.positions[name]].type = type;
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
|
|
|
return this.positions[name] = this.variables.push({
|
|
|
|
name: name,
|
|
|
|
type: type
|
|
|
|
}) - 1;
|
|
|
|
}
|
2010-10-19 19:51:34 -04:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2012-04-24 17:23:37 -04:00
|
|
|
Scope.prototype.namedMethod = function() {
|
2015-02-07 15:50:41 -05:00
|
|
|
var ref;
|
|
|
|
if (((ref = this.method) != null ? ref.name : void 0) || !this.parent) {
|
2012-04-11 19:32:40 -04:00
|
|
|
return this.method;
|
2012-04-11 18:05:33 -04:00
|
|
|
}
|
2012-04-24 17:23:37 -04:00
|
|
|
return this.parent.namedMethod();
|
2012-04-11 18:05:33 -04:00
|
|
|
};
|
|
|
|
|
2012-04-24 15:37:26 -04:00
|
|
|
Scope.prototype.find = function(name) {
|
|
|
|
if (this.check(name)) {
|
2012-04-10 14:57:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-20 23:09:06 -04:00
|
|
|
this.add(name, 'var');
|
2010-03-30 09:02:51 -04:00
|
|
|
return false;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2012-04-10 14:57:45 -04:00
|
|
|
if (this.shared && this.parent.check(name, true)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-20 23:09:06 -04:00
|
|
|
return this.add(name, 'param');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2012-04-24 15:37:26 -04:00
|
|
|
Scope.prototype.check = function(name) {
|
2015-02-07 15:50:41 -05:00
|
|
|
var ref;
|
|
|
|
return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2015-01-30 14:33:03 -05:00
|
|
|
Scope.prototype.temporary = function(name, index, single) {
|
|
|
|
if (single == null) {
|
|
|
|
single = false;
|
|
|
|
}
|
|
|
|
if (single) {
|
|
|
|
return (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
2015-01-29 16:24:30 -05:00
|
|
|
} else {
|
2015-01-30 14:33:03 -05:00
|
|
|
return name + (index || '');
|
2010-11-28 12:28:46 -05:00
|
|
|
}
|
2010-09-19 09:18:01 -04:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-10-19 19:36:50 -04:00
|
|
|
Scope.prototype.type = function(name) {
|
2015-02-07 15:50:41 -05:00
|
|
|
var i, len, ref, v;
|
|
|
|
ref = this.variables;
|
|
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
|
|
v = ref[i];
|
2012-04-10 14:57:45 -04:00
|
|
|
if (v.name === name) {
|
|
|
|
return v.type;
|
|
|
|
}
|
2010-10-19 19:36:50 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2015-01-30 14:33:03 -05:00
|
|
|
Scope.prototype.freeVariable = function(name, options) {
|
2015-02-07 15:50:41 -05:00
|
|
|
var index, ref, temp;
|
2015-01-30 14:33:03 -05:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
2012-04-10 14:57:45 -04:00
|
|
|
}
|
2010-09-19 10:03:45 -04:00
|
|
|
index = 0;
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
while (true) {
|
2015-01-30 14:33:03 -05:00
|
|
|
temp = this.temporary(name, index, options.single);
|
|
|
|
if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-09-19 10:03:45 -04:00
|
|
|
index++;
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2015-02-07 15:50:41 -05:00
|
|
|
if ((ref = options.reserve) != null ? ref : true) {
|
2012-04-10 14:57:45 -04:00
|
|
|
this.add(temp, 'var', true);
|
|
|
|
}
|
2010-09-19 08:50:17 -04:00
|
|
|
return temp;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-12-06 23:32:32 -05:00
|
|
|
this.add(name, {
|
2010-10-19 19:51:34 -04:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2011-11-10 03:08:38 -05:00
|
|
|
}, true);
|
2010-12-06 23:32:32 -05:00
|
|
|
return this.hasAssignments = true;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-12-12 12:16:27 -05:00
|
|
|
Scope.prototype.hasDeclarations = function() {
|
|
|
|
return !!this.declaredVariables().length;
|
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2015-01-30 14:33:03 -05:00
|
|
|
var v;
|
|
|
|
return ((function() {
|
2015-02-07 15:50:41 -05:00
|
|
|
var i, len, ref, results;
|
|
|
|
ref = this.variables;
|
2015-01-30 14:33:03 -05:00
|
|
|
results = [];
|
2015-02-07 15:50:41 -05:00
|
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
|
|
v = ref[i];
|
2015-01-30 14:33:03 -05:00
|
|
|
if (v.type === 'var') {
|
|
|
|
results.push(v.name);
|
|
|
|
}
|
2011-08-08 12:55:22 -04:00
|
|
|
}
|
2015-01-30 14:33:03 -05:00
|
|
|
return results;
|
|
|
|
}).call(this)).sort();
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2015-02-07 15:50:41 -05:00
|
|
|
var i, len, ref, results, v;
|
|
|
|
ref = this.variables;
|
2015-01-30 14:33:03 -05:00
|
|
|
results = [];
|
2015-02-07 15:50:41 -05:00
|
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
|
|
v = ref[i];
|
2012-04-10 14:57:45 -04:00
|
|
|
if (v.type.assigned) {
|
2015-01-30 14:33:03 -05:00
|
|
|
results.push(v.name + " = " + v.type.value);
|
2012-04-10 14:57:45 -04:00
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2015-01-30 14:33:03 -05:00
|
|
|
return results;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2010-02-27 19:46:45 -05:00
|
|
|
return Scope;
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-12-23 13:50:52 -05:00
|
|
|
})();
|
2011-12-14 10:39:20 -05:00
|
|
|
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|