From f67da27d2fba9623fe7734aa78a26a0cadb44b3b Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Mon, 14 Jan 2013 17:11:07 -0500 Subject: [PATCH] Add unit tests, fix last_column reporting. --- lib/coffee-script/lexer.js | 5 +++-- src/lexer.coffee | 6 +++++- test/location.coffee | 22 ++++++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 55fe04a7..4c36aade 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -731,14 +731,15 @@ }; Lexer.prototype.makeToken = function(tag, value, offsetInChunk, length) { - var locationData, token, _ref2, _ref3; + var lastCharacter, locationData, token, _ref2, _ref3; offsetInChunk = offsetInChunk || 0; if (length === void 0) { length = value.length; } locationData = {}; _ref2 = this.getLineAndColumnFromChunk(offsetInChunk), locationData.first_line = _ref2[0], locationData.first_column = _ref2[1]; - _ref3 = this.getLineAndColumnFromChunk(offsetInChunk + length), locationData.last_line = _ref3[0], locationData.last_column = _ref3[1]; + lastCharacter = length > 0 ? length - 1 : 0; + _ref3 = this.getLineAndColumnFromChunk(offsetInChunk + (length - 1)), 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 5b331a8e..8ba73da6 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -645,8 +645,12 @@ exports.Lexer = class Lexer locationData = {} [locationData.first_line, locationData.first_column] = @getLineAndColumnFromChunk offsetInChunk + + # 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 = if length > 0 then (length - 1) else 0 [locationData.last_line, locationData.last_column] = - @getLineAndColumnFromChunk offsetInChunk + length + @getLineAndColumnFromChunk offsetInChunk + (length - 1) token = [tag, value, locationData] diff --git a/test/location.coffee b/test/location.coffee index 31a858af..d15002ff 100644 --- a/test/location.coffee +++ b/test/location.coffee @@ -16,10 +16,28 @@ x = () -> ''' test "Verify location of generated tokens", -> - tokens = CoffeeScript.tokens "a = 7" + tokens = CoffeeScript.tokens "a = 79" eq tokens.length, 4 - a = tokens[0] + + aToken = tokens[0] + eq aToken[2].first_line, 0 + eq aToken[2].first_column, 0 + eq aToken[2].last_line, 0 + eq aToken[2].last_column, 0 + + equalsToken = tokens[1] + eq equalsToken[2].first_line, 0 + eq equalsToken[2].first_column, 2 + eq equalsToken[2].last_line, 0 + eq equalsToken[2].last_column, 2 + + numberToken = tokens[2] + eq numberToken[2].first_line, 0 + eq numberToken[2].first_column, 4 + eq numberToken[2].last_line, 0 + eq numberToken[2].last_column, 5 + test "Verify all tokens get a location", -> doesNotThrow ->