diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 21e5ac2027..250f10371f 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -768,7 +768,7 @@ } locationData = {}; ref2 = this.getLineAndColumnFromChunk(offsetInChunk), locationData.first_line = ref2[0], locationData.first_column = ref2[1]; - lastCharacter = Math.max(0, length - 1); + lastCharacter = length > 0 ? length - 1 : 0; ref3 = this.getLineAndColumnFromChunk(offsetInChunk + lastCharacter), locationData.last_line = ref3[0], locationData.last_column = ref3[1]; token = [tag, value, locationData]; return token; diff --git a/src/lexer.coffee b/src/lexer.coffee index 2e6090df61..83f9f90787 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -658,7 +658,7 @@ exports.Lexer = class Lexer # Use length - 1 for the final offset - we're supplying the last_line and the last_column, # so if last_column == first_column, then we're looking at a character of length 1. - lastCharacter = Math.max 0, length - 1 + lastCharacter = if length > 0 then (length - 1) else 0 [locationData.last_line, locationData.last_column] = @getLineAndColumnFromChunk offsetInChunk + lastCharacter diff --git a/test/location.coffee b/test/location.coffee index 783b44afbd..35b9584c51 100644 --- a/test/location.coffee +++ b/test/location.coffee @@ -450,6 +450,25 @@ test "#3621: Multiline regex and manual `Regex` call with interpolation should eq tokenA.origin?[1], tokenB.origin?[1] eq tokenA.stringEnd, tokenB.stringEnd +test "Verify tokens have locations that are in order", -> + source = ''' + a { + b: -> + return c d, + if e + f + } + g + ''' + tokens = CoffeeScript.tokens source + lastToken = null + for token in tokens + if lastToken + ok token[2].first_line >= lastToken[2].last_line + if token[2].first_line == lastToken[2].last_line + ok token[2].first_column >= lastToken[2].last_column + lastToken = token + test "Verify all tokens get a location", -> doesNotThrow -> tokens = CoffeeScript.tokens testScript