From bd0024a9c263fa3684e1e433dabb65644519583b Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Wed, 27 Jul 2016 23:16:30 -0700 Subject: [PATCH] Fix incorrect location data in OUTDENT nodes In f609036beef3aa1529c210ffad3ced818ee94cce, a line was changed from `if length > 0 then (length - 1) else 0` to `Math.max 0, length - 1`. However, in some cases, the `length` variable can be `undefined`. The previous code would correctly compute `lastCharacter` as 0, but the new code would compute it as `NaN`. This would cause trouble later on: the end location would just be the end of the current chunk, which would be incorrect. Here's a specific case where the parser was behaving incorrectly: ``` a { b: -> return c d, if e f } g ``` The OUTDENT tokens after the `f` had an undefined length, so the `NaN` made it so the end location was at the end of the file. That meant that various nodes in the AST, like the `return` node, would incorrectly have an end location at the end of the file. To fix, I just reverted the change to that particular line. --- lib/coffee-script/lexer.js | 2 +- src/lexer.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 21e5ac20..250f1037 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 2e6090df..83f9f907 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