Merge branch 'master' of http://github.com/jashkenas/coffee-script into refactorTests

This commit is contained in:
Michael Ficarra
2010-12-09 22:49:58 -05:00
4 changed files with 47 additions and 16 deletions

View File

@@ -1034,10 +1034,11 @@
}();
exports.Assign = Assign = function() {
__extends(Assign, Base);
function Assign(variable, value, context) {
function Assign(variable, value, context, options) {
this.variable = variable;
this.value = value;
this.context = context;
this.param = options && options.param;
}
Assign.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/;
Assign.prototype.children = ['variable', 'value'];
@@ -1075,7 +1076,11 @@
throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned.");
}
if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) {
o.scope.find(name);
if (this.param) {
o.scope.add(name, 'var');
} else {
o.scope.find(name);
}
}
val = name + (" " + (this.context || '=') + " ") + val;
if (o.level <= LEVEL_LIST) {
@@ -1152,7 +1157,9 @@
}
val = new Value(new Literal(vvar), [new (acc ? Access : Index)(idx)]);
}
assigns.push(new Assign(obj, val).compile(o, LEVEL_TOP));
assigns.push(new Assign(obj, val, null, {
param: this.param
}).compile(o, LEVEL_TOP));
}
if (!top) {
assigns.push(vvar);
@@ -1246,7 +1253,9 @@
if (param.value) {
val = new Op('?', ref, param.value);
}
exprs.push(new Assign(new Value(param.name), val, '='));
exprs.push(new Assign(new Value(param.name), val, '=', {
param: true
}));
} else {
ref = param;
if (param.value) {
@@ -1800,7 +1809,7 @@
step: this.step
}));
} else {
svar = this.source.compile(o, LEVEL_TOP);
svar = this.source.compile(o, LEVEL_LIST);
if ((name || this.own) && !IDENTIFIER.test(svar)) {
defPart = "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
svar = ref;
@@ -1821,7 +1830,7 @@
body = Expressions.wrap([new If(this.guard, body)]);
}
if (hasCode) {
body = Closure.wrap(body, true);
body = Closure.wrap(body, true, !this.returns);
}
if (namePart) {
varPart = "\n" + idt1 + namePart + ";";
@@ -2052,8 +2061,8 @@
args.push(new Literal('arguments'));
}
func = new Value(func, [new Access(meth)]);
func.noReturn = noReturn;
}
func.noReturn = noReturn;
call = new Call(func, args);
if (statement) {
return Expressions.wrap([call]);

View File

@@ -832,7 +832,8 @@ exports.Class = class Class extends Base
# The **Assign** is used to assign a local variable to value, or to set the
# property of an object -- including within object literals.
exports.Assign = class Assign extends Base
constructor: (@variable, @value, @context) ->
constructor: (@variable, @value, @context, options) ->
@param = options and options.param
# Matchers for detecting class/method names
METHOD_DEF: /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/
@@ -862,8 +863,11 @@ exports.Assign = class Assign extends Base
return "#{name}: #{val}" if @context is 'object'
unless @variable.isAssignable()
throw SyntaxError "\"#{ @variable.compile o }\" cannot be assigned."
o.scope.find name unless @context or
isValue and (@variable.namespaced or @variable.hasProperties())
unless @context or isValue and (@variable.namespaced or @variable.hasProperties())
if @param
o.scope.add name, 'var'
else
o.scope.find name
val = name + " #{ @context or '=' } " + val
if o.level <= LEVEL_LIST then val else "(#{val})"
@@ -932,7 +936,7 @@ exports.Assign = class Assign extends Base
else
acc = isObject and IDENTIFIER.test idx.unwrap().value or 0
val = new Value new Literal(vvar), [new (if acc then Access else Index) idx]
assigns.push new Assign(obj, val).compile o, LEVEL_TOP
assigns.push new Assign(obj, val, null, param: @param).compile o, LEVEL_TOP
assigns.push vvar unless top
code = assigns.join ', '
if o.level < LEVEL_LIST then code else "(#{code})"
@@ -1000,7 +1004,7 @@ exports.Code = class Code extends Base
if param.isComplex()
val = ref = param.asReference o
val = new Op '?', ref, param.value if param.value
exprs.push new Assign new Value(param.name), val, '='
exprs.push new Assign new Value(param.name), val, '=', param: yes
else
ref = param
if param.value
@@ -1438,7 +1442,7 @@ exports.For = class For extends Base
if @range
forPart = source.compile merge(o, {index: ivar, @step})
else
svar = @source.compile o, LEVEL_TOP
svar = @source.compile o, LEVEL_LIST
if (name or @own) and not IDENTIFIER.test svar
defPart = "#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n"
svar = ref
@@ -1457,7 +1461,7 @@ exports.For = class For extends Base
if @guard
body = Expressions.wrap [new If @guard, body]
if hasCode
body = Closure.wrap(body, yes)
body = Closure.wrap(body, yes, not @returns)
varPart = "\n#{idt1}#{namePart};" if namePart
if @object
forPart = "#{ivar} in #{svar}"
@@ -1632,7 +1636,7 @@ Closure =
args = [new Literal 'this']
args.push new Literal 'arguments' if mentionsArgs
func = new Value func, [new Access meth]
func.noReturn = noReturn
func.noReturn = noReturn
call = new Call func, args
if statement then Expressions.wrap [call] else call

View File

@@ -241,3 +241,11 @@ list = ['one', 'two']
eq typeof entity, 'undefined'
eq facets['two'](), 'two'
# Issue #905. Soaks as the for loop subject.
a = {b: {c: [1, 2, 3]}}
for d in a.b?.c
e = d
eq e, 3

View File

@@ -366,4 +366,14 @@ class Foo
new Foo().bar([101]...)
eq x, 101
eq x, 101
# Issue #904: Destructuring function arguments with same-named variables in scope.
key for key, value of a: 1
f = ([key, value]) ->
key + value
eq f([10, 11]), 21
eq key, 'a'
eq value, 1