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:
parent
57f5297714
commit
568a0c7b4e
|
@ -267,22 +267,22 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (indent) {
|
if (indent) {
|
||||||
indentRegex = RegExp("^" + indent, "gm");
|
indentRegex = RegExp("\\n" + indent, "g");
|
||||||
}
|
}
|
||||||
this.mergeInterpolationTokens(tokens, {
|
this.mergeInterpolationTokens(tokens, {
|
||||||
delimiter: delimiter
|
delimiter: delimiter
|
||||||
}, (function(_this) {
|
}, (function(_this) {
|
||||||
return function(value, i) {
|
return function(value, i) {
|
||||||
value = _this.formatString(value);
|
value = _this.formatString(value);
|
||||||
|
if (indentRegex) {
|
||||||
|
value = value.replace(indentRegex, '\n');
|
||||||
|
}
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
value = value.replace(LEADING_BLANK_LINE, '');
|
value = value.replace(LEADING_BLANK_LINE, '');
|
||||||
}
|
}
|
||||||
if (i === $) {
|
if (i === $) {
|
||||||
value = value.replace(TRAILING_BLANK_LINE, '');
|
value = value.replace(TRAILING_BLANK_LINE, '');
|
||||||
}
|
}
|
||||||
if (indentRegex) {
|
|
||||||
value = value.replace(indentRegex, '');
|
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
})(this));
|
})(this));
|
||||||
|
|
|
@ -248,12 +248,12 @@ exports.Lexer = class Lexer
|
||||||
while match = HEREDOC_INDENT.exec doc
|
while match = HEREDOC_INDENT.exec doc
|
||||||
attempt = match[1]
|
attempt = match[1]
|
||||||
indent = attempt if indent is null or 0 < attempt.length < indent.length
|
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) =>
|
@mergeInterpolationTokens tokens, {delimiter}, (value, i) =>
|
||||||
value = @formatString value
|
value = @formatString value
|
||||||
|
value = value.replace indentRegex, '\n' if indentRegex
|
||||||
value = value.replace LEADING_BLANK_LINE, '' if i is 0
|
value = value.replace LEADING_BLANK_LINE, '' if i is 0
|
||||||
value = value.replace TRAILING_BLANK_LINE, '' if i is $
|
value = value.replace TRAILING_BLANK_LINE, '' if i is $
|
||||||
value = value.replace indentRegex, '' if indentRegex
|
|
||||||
value
|
value
|
||||||
else
|
else
|
||||||
@mergeInterpolationTokens tokens, {delimiter}, (value, i) =>
|
@mergeInterpolationTokens tokens, {delimiter}, (value, i) =>
|
||||||
|
|
|
@ -390,3 +390,13 @@ test "#3795: Escape otherwise invalid characters", ->
|
||||||
eq """#{a}
""", 'a\u2029'
|
eq """#{a}
""", 'a\u2029'
|
||||||
eq """#{a}\0\
|
eq """#{a}\0\
|
||||||
1""", 'a\0' + '1'
|
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"""
|
||||||
|
|
Loading…
Reference in New Issue