Merge pull request #1662 from geraldalewis/1195_trailing_semicolons

#1195 Ignore trailing semicolons
This commit is contained in:
Jeremy Ashkenas
2011-09-06 06:15:13 -07:00
3 changed files with 21 additions and 0 deletions

View File

@@ -275,6 +275,9 @@
}
}
if (dent) this.outdebt -= moveOut;
while (this.value() === ';') {
this.tokens.pop();
}
if (!(this.tag() === 'TERMINATOR' || noNewlines)) {
this.token('TERMINATOR', '\n');
}
@@ -294,6 +297,9 @@
}
};
Lexer.prototype.newlineToken = function() {
while (this.value() === ';') {
this.tokens.pop();
}
if (this.tag() !== 'TERMINATOR') this.token('TERMINATOR', '\n');
return this;
};

View File

@@ -279,6 +279,7 @@ exports.Lexer = class Lexer
@outdebt = 0
@token 'OUTDENT', dent
@outdebt -= moveOut if dent
@tokens.pop() while @value() is ';'
@token 'TERMINATOR', '\n' unless @tag() is 'TERMINATOR' or noNewlines
this
@@ -293,6 +294,7 @@ exports.Lexer = class Lexer
# Generate a newline token. Consecutive newlines get merged together.
newlineToken: ->
@tokens.pop() while @value() is ';'
@token 'TERMINATOR', '\n' unless @tag() is 'TERMINATOR'
this

View File

@@ -121,3 +121,16 @@ test "indented heredoc", ->
test "#1492: Nested blocks don't cause double semicolons", ->
js = CoffeeScript.compile '(0;0)'
eq -1, js.indexOf ';;'
test "#1195 Ignore trailing semicolons (before newlines or as the last char in a program)", ->
preNewline = (numSemicolons) ->
"""
nonce = {}; nonce2 = {}
f = -> nonce#{Array(numSemicolons+1).join(';')}
nonce2
unless f() is nonce then throw new Error('; before linebreak should = newline')
"""
CoffeeScript.run(preNewline(n), bare: true) for n in [1,2,3]
lastChar = '-> lastChar;'
doesNotThrow -> CoffeeScript.compile lastChar, bare: true