Skip to content

Commit 861b442

Browse files
committed
closes #179; disallowed cascades without &
1 parent f51c51d commit 861b442

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

lib/ast.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ exports.Literal = Literal = (function(superclass){
520520
if (!(val = o.cascadee)) {
521521
this.carp('stray cascadee');
522522
}
523+
val.referred = true;
523524
}
524525
return val;
525526
};
@@ -3134,11 +3135,18 @@ exports.Cascade = Cascade = (function(superclass){
31343135
}
31353136
prototype.children = ['target', 'block'];
31363137
prototype.terminator = '';
3138+
prototype.makeReturn = function(it){
3139+
this.block.makeReturn(it);
3140+
return this;
3141+
};
31373142
prototype.compileNode = function(o){
31383143
var ref, t, b;
31393144
this.temps = [ref = o.scope.temporary('x')];
31403145
t = ref + ' = ' + this.target.compile(o, LEVEL_LIST);
3141-
b = this.block.compile((o.cascadee = ref, o));
3146+
o.level && (o.level = LEVEL_PAREN);
3147+
o.cascadee = new String(ref);
3148+
b = this.block.compile(o);
3149+
o.cascadee.referred || this.carp('unreferred cascadee');
31423150
if (o.level) {
31433151
return "(" + t + ", " + b + ")";
31443152
} else {

src/ast.co

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ class exports.Literal extends Atom
323323
case \debugger then if level
324324
return "(function(){\n#TAB#{o.indent}debugger;\n#{o.indent}}())"
325325
case \* then @carp 'stray star'
326-
case \& then @carp 'stray cascadee' unless val = o.cascadee
326+
case \&
327+
@carp 'stray cascadee' unless val = o.cascadee
328+
val.referred = true
327329
val
328330

329331
#### Var
@@ -1911,18 +1913,23 @@ class exports.Label extends Node
19111913
else it.compile o
19121914

19131915
#### Cascade
1914-
#
1916+
# A construction that allows `&`-references to target expression.
19151917
class exports.Cascade extends Node
19161918
(@target, @block) ->
19171919

19181920
children: <[ target block ]>
19191921

19201922
terminator: ''
19211923

1924+
makeReturn: -> @block.makeReturn it; this
1925+
19221926
compileNode: (o) ->
19231927
@temps = [ref = o.scope.temporary \x]
19241928
t = ref + ' = ' + @target.compile o, LEVEL_LIST
1925-
b = @block.compile o <<< cascadee: ref
1929+
o.level &&= LEVEL_PAREN
1930+
o.cascadee = new String ref
1931+
b = @block.compile o
1932+
o.cascadee.referred or @carp 'unreferred cascadee'
19261933
if o.level then "(#t, #b)" else "#t;\n#b"
19271934

19281935
#### JS

test/literal.co

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,5 @@ eq
444444
ok &valueOf!
445445

446446
compileThrows 'stray cascadee' 2 '\n&'
447+
448+
compileThrows 'unreferred cascadee' 1 'a\n b'

0 commit comments

Comments
 (0)