From 568a0c7b4ec2f58aa8ffd717046360648d6f0ed9 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Mon, 26 Sep 2016 17:10:38 +0200 Subject: [PATCH] Fix indentation-stripping in `"""` strings `"""` (and `"`) strings are lexed into an array of tokens, consisting of strings and interpolations. Previously, the minimum indententation inside `"""` strings was stripped from the beginning of _all_ of those string tokens. Usually, the indentation is longer than any other sequence of spaces in a `"""` string, so the problem didn't occur in most cases. This commit makes sure to only strip indentation after newlines. Fixes #4314. --- lib/coffee-script/lexer.js | 8 ++++---- src/lexer.coffee | 4 ++-- test/strings.coffee | 10 ++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index f022de68..ee2d4b6c 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -267,22 +267,22 @@ } } if (indent) { - indentRegex = RegExp("^" + indent, "gm"); + indentRegex = RegExp("\\n" + indent, "g"); } this.mergeInterpolationTokens(tokens, { delimiter: delimiter }, (function(_this) { return function(value, i) { value = _this.formatString(value); + if (indentRegex) { + value = value.replace(indentRegex, '\n'); + } if (i === 0) { value = value.replace(LEADING_BLANK_LINE, ''); } if (i === $) { value = value.replace(TRAILING_BLANK_LINE, ''); } - if (indentRegex) { - value = value.replace(indentRegex, ''); - } return value; }; })(this)); diff --git a/src/lexer.coffee b/src/lexer.coffee index 3e55780f..e776c91d 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -248,12 +248,12 @@ exports.Lexer = class Lexer while match = HEREDOC_INDENT.exec doc attempt = match[1] indent = attempt if indent is null or 0 < attempt.length < indent.length - indentRegex = /// ^#{indent} ///gm if indent + indentRegex = /// \n#{indent} ///g if indent @mergeInterpolationTokens tokens, {delimiter}, (value, i) => value = @formatString value + value = value.replace indentRegex, '\n' if indentRegex value = value.replace LEADING_BLANK_LINE, '' if i is 0 value = value.replace TRAILING_BLANK_LINE, '' if i is $ - value = value.replace indentRegex, '' if indentRegex value else @mergeInterpolationTokens tokens, {delimiter}, (value, i) => diff --git a/test/strings.coffee b/test/strings.coffee index 6eb314d5..0f1975e2 100644 --- a/test/strings.coffee +++ b/test/strings.coffee @@ -390,3 +390,13 @@ test "#3795: Escape otherwise invalid characters", -> eq """#{a}
""", 'a\u2029' eq """#{a}\0\ 1""", 'a\0' + '1' + +test "#4314: Whitespace less than or equal to stripped indentation", -> + # The odd indentation is intentional here, to test 1-space indentation. + eq ' ', """ + #{} #{} +""" + + eq '1 2 3 4 5 end\na 0 b', """ + #{1} #{2} #{3} #{4} #{5} end + a #{0} b"""