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.
This commit is contained in:
Simon Lydell 2016-09-26 17:10:38 +02:00
parent 57f5297714
commit 568a0c7b4e
3 changed files with 16 additions and 6 deletions

View File

@ -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));

View File

@ -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) =>

View File

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