Skip to content

Commit 61c9043

Browse files
committed
Fixed heredoc blank line bugs
1 parent 1f158b2 commit 61c9043

File tree

4 files changed

+632
-125
lines changed

4 files changed

+632
-125
lines changed

src/lexer/strings.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ module.exports = {
135135
// consumeLeadingSpaces is false happen DOC prematch END HEREDOC stage.
136136

137137
// Ensure current state is really after a new line break, not after a such as ${variables}
138-
const prev_ch = this._input.substring(offset - 2, offset - 1);
138+
const prev_ch = this._input[offset - 2];
139139
if (prev_ch !== "\n" && prev_ch !== "\r") {
140140
return false;
141141
}
@@ -145,26 +145,34 @@ module.exports = {
145145
let indentation_uses_tabs = false;
146146
// reset heredoc_label structure
147147
let indentation = 0;
148-
let leading_ch = this._input.substring(offset - 1, offset);
148+
let leading_ch = this._input[offset - 1];
149149
let valid_endings = ["\n", "\r", ";"];
150150

151151
if (this.version >= 703) {
152152
valid_endings = valid_endings.concat(["\t", " ", ",", ")", "]"]);
153-
}
154153

155-
while (leading_ch === "\t" || leading_ch === " ") {
156-
if (leading_ch === " ") {
157-
indentation_uses_spaces = true;
158-
} else if (leading_ch === "\t") {
159-
indentation_uses_tabs = true;
154+
while (leading_ch === "\t" || leading_ch === " ") {
155+
if (leading_ch === " ") {
156+
indentation_uses_spaces = true;
157+
} else if (leading_ch === "\t") {
158+
indentation_uses_tabs = true;
159+
}
160+
161+
leading_ch = this._input[offset + indentation];
162+
indentation++;
160163
}
161164

162-
leading_ch = this._input[offset + indentation];
163-
indentation++;
164-
}
165+
// Move offset to skip leading whitespace
166+
offset = offset + indentation;
165167

166-
// Move offset to skip leading whitespace
167-
offset = offset + indentation;
168+
// return out if there was only whitespace on this line
169+
if (
170+
this._input[offset - 1] === "\n" ||
171+
this._input[offset - 1] === "\r"
172+
) {
173+
return false;
174+
}
175+
}
168176

169177
if (
170178
this._input.substring(
@@ -216,9 +224,11 @@ module.exports = {
216224
return;
217225
}
218226

219-
// skip one line
220-
while (this._input[offset++] !== "\n" && offset < this._input.length) {
221-
// skip
227+
if (this._input[offset - 1] !== "\n") {
228+
// skip one line
229+
while (this._input[offset++] !== "\n" && offset < this._input.length) {
230+
// skip
231+
}
222232
}
223233

224234
offset++;

src/parser/scalar.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ module.exports = {
128128
inCoutingState = false;
129129
}
130130

131-
if (inCheckState && leadingWhitespaceCharCount < indentation) {
131+
if (
132+
text[offset] !== "\n" &&
133+
inCheckState &&
134+
leadingWhitespaceCharCount < indentation
135+
) {
132136
this.raiseError(
133137
`Invalid body indentation level (expecting an indentation at least ${indentation})`
134138
);
@@ -335,12 +339,14 @@ module.exports = {
335339
result = result(
336340
"string",
337341
false,
338-
this.remove_heredoc_leading_whitespace_chars(
339-
this.resolve_special_chars(text, isDoubleQuote),
340-
this.lexer.heredoc_label.indentation,
341-
this.lexer.heredoc_label.indentation_uses_spaces,
342-
this.lexer.heredoc_label.first_encaps_node
343-
),
342+
this.version >= 703
343+
? this.remove_heredoc_leading_whitespace_chars(
344+
this.resolve_special_chars(text, isDoubleQuote),
345+
this.lexer.heredoc_label.indentation,
346+
this.lexer.heredoc_label.indentation_uses_spaces,
347+
this.lexer.heredoc_label.first_encaps_node
348+
)
349+
: text,
344350
false,
345351
text
346352
);

0 commit comments

Comments
 (0)