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"""