Fixing literals that should be statements, and adding failed compilation tests.

This commit is contained in:
Jeremy Ashkenas 2010-12-23 09:33:12 -08:00
parent b56b08387d
commit 8fd78d3819
3 changed files with 31 additions and 15 deletions

View File

@ -320,22 +320,21 @@
this.value = value;
}
Literal.prototype.makeReturn = function() {
if (this.jumps()) {
return this;
} else {
return new Return(this);
}
return new Return(this);
};
Literal.prototype.isAssignable = function() {
return IDENTIFIER.test(this.value);
};
Literal.prototype.isStatement = function() {
var _ref;
return (_ref = this.value) === 'break' || _ref === 'continue' || _ref === 'debugger';
};
Literal.prototype.isComplex = NO;
Literal.prototype.assigns = function(name) {
return name === this.value;
};
Literal.prototype.jumps = function(o) {
var _ref;
if ((_ref = this.value) !== 'break' && _ref !== 'continue' && _ref !== 'debugger') {
if (!this.isStatement()) {
return false;
}
if (!(o && (o.loop || o.block && (this.value !== 'continue')))) {
@ -344,11 +343,13 @@
return false;
}
};
Literal.prototype.compile = function() {
if (this.value.reserved) {
return "\"" + this.value + "\"";
Literal.prototype.compileNode = function(o) {
var code;
code = this.value.reserved ? "\"" + this.value + "\"" : this.value;
if (this.isStatement()) {
return "" + this.tab + code + ";";
} else {
return this.value;
return code;
}
};
Literal.prototype.toString = function() {

View File

@ -271,22 +271,26 @@ exports.Literal = class Literal extends Base
constructor: (@value) ->
makeReturn: ->
if @jumps() then this else new Return this
new Return this
isAssignable: ->
IDENTIFIER.test @value
isStatement: ->
@value in ['break', 'continue', 'debugger']
isComplex: NO
assigns: (name) ->
name is @value
jumps: (o) ->
return no unless @value in ['break', 'continue', 'debugger']
return no unless @isStatement()
if not (o and (o.loop or o.block and (@value isnt 'continue'))) then this else no
compile: ->
if @value.reserved then "\"#{@value}\"" else @value
compileNode: (o) ->
code = if @value.reserved then "\"#{@value}\"" else @value
if @isStatement() then "#{@tab}#{code};" else code
toString: ->
' "' + @value + '"'

View File

@ -13,3 +13,14 @@ catch e then eq e.message, 'unclosed CALL_START on line 1'
eq CoffeeScript.compile('for k of o then', bare: on, globals: on),
'for (k in o) {}'
# Compilations that should fail.
cantCompile = (code) ->
throws -> CoffeeScript.compile code
cantCompile 'a = (break)'
cantCompile 'a = (return 5 for item in list)'
cantCompile 'a = (return 5 while condition)'
cantCompile 'a = for x in y\n return 5'