1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffeescript/scope.js
Geoffrey Booth b192e215a5 [CS2] Destructuring (#4478)
* Output simple array destructuring assignments to ES2015

* Output simple object destructured assignments to ES2015

* Compile shorthand object properties to ES2015 shorthand properties

This dramatically improves the appearance of destructured imports.

* Compile default values in destructured assignment to ES2015

* Rename `wrapInBraces` to `wrapInParentheses`, and `compilePatternMatch` to `compileDestructuring`, for clarity; style improvements (no `==` or `!=`, etc.)

* Don’t confuse the syntax highlighter

* Comment Assign::compilePatternMatch a bit

* Assignment expressions in conditionals are a bad practice

* Optional check for existence that only checks `!== undefined`, not `!= null`, to follow ES convention that default values only apply when a variable is undefined, not falsy

* Add comments; remove unnecessary array splats in function tests

* The fallback destructuring code should apply default values only if `undefined`, not falsy, to follow ES spec

* Support destructuring in function parameters (first pass); catch destructured reserved words

* Destructured variables in function parameter lists shouldn’t be added to the function body with `var` declarations; treat splat array function parameters the legacy way to avoid rethinking #4005

* Remove redundancy in undefined-only check for existence; fix passing option to check

* Fix undefined redundancy

* Simplify getting the variable name

* Reimplement “check for existence if not undefined” without creating a new operator

* `Obj::isAssignable` should not mutate; pass `lhs` property in from `Assign` or `Code` to child arrays and objects so that those child nodes are set as allowable for destructuring

* Revert changes to tests

* Restore revised test for empty destructuring assignment
2017-04-06 10:06:45 -07:00

151 lines
3.8 KiB
JavaScript

// Generated by CoffeeScript 2.0.0-alpha1
(function() {
var Scope,
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; };
exports.Scope = Scope = class Scope {
constructor(parent, expressions, method, referencedVars) {
var ref, ref1;
this.parent = parent;
this.expressions = expressions;
this.method = method;
this.referencedVars = referencedVars;
this.variables = [
{
name: 'arguments',
type: 'arguments'
}
];
this.positions = {};
if (!this.parent) {
this.utilities = {};
}
this.root = (ref = (ref1 = this.parent) != null ? ref1.root : void 0) != null ? ref : this;
}
add(name, type, immediate) {
if (this.shared && !immediate) {
return this.parent.add(name, type, immediate);
}
if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
return this.variables[this.positions[name]].type = type;
} else {
return this.positions[name] = this.variables.push({name, type}) - 1;
}
}
namedMethod() {
var ref;
if (((ref = this.method) != null ? ref.name : void 0) || !this.parent) {
return this.method;
}
return this.parent.namedMethod();
}
find(name, type = 'var') {
if (this.check(name)) {
return true;
}
this.add(name, type);
return false;
}
parameter(name) {
if (this.shared && this.parent.check(name, true)) {
return;
}
return this.add(name, 'param');
}
check(name) {
var ref;
return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
}
temporary(name, index, single = false) {
var diff, endCode, letter, newCode, num, startCode;
if (single) {
startCode = name.charCodeAt(0);
endCode = 'z'.charCodeAt(0);
diff = endCode - startCode;
newCode = startCode + index % (diff + 1);
letter = String.fromCharCode(newCode);
num = Math.floor(index / (diff + 1));
return `${letter}${num || ''}`;
} else {
return `${name}${index || ''}`;
}
}
type(name) {
var i, len, ref, v;
ref = this.variables;
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
if (v.name === name) {
return v.type;
}
}
return null;
}
freeVariable(name, options = {}) {
var index, ref, temp;
index = 0;
while (true) {
temp = this.temporary(name, index, options.single);
if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
break;
}
index++;
}
if ((ref = options.reserve) != null ? ref : true) {
this.add(temp, 'var', true);
}
return temp;
}
assign(name, value) {
this.add(name, {
value,
assigned: true
}, true);
return this.hasAssignments = true;
}
hasDeclarations() {
return !!this.declaredVariables().length;
}
declaredVariables() {
var v;
return ((function() {
var i, len, ref, results;
ref = this.variables;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
if (v.type === 'var') {
results.push(v.name);
}
}
return results;
}).call(this)).sort();
}
assignedVariables() {
var i, len, ref, results, v;
ref = this.variables;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
if (v.type.assigned) {
results.push(`${v.name} = ${v.type.value}`);
}
}
return results;
}
};
}).call(this);