[parser] Don't crash on comment after trailing comma after eli… (#10490)

This commit is contained in:
Nicolò Ribaudo
2019-09-24 17:08:58 +02:00
committed by GitHub
parent 26be14b24d
commit 0e9502685a
3 changed files with 200 additions and 4 deletions

View File

@@ -40,7 +40,7 @@ export default class CommentsParser extends BaseParser {
adjustCommentsAfterTrailingComma(
node: Node,
elements: Node[],
elements: (Node | null)[],
// When the current node is followed by a token which hasn't a respective AST node, we
// need to take all the trailing comments to prevent them from being attached to an
// unrelated node. e.g. in
@@ -55,12 +55,15 @@ export default class CommentsParser extends BaseParser {
return;
}
if (elements.length === 0) {
let lastElement = null;
let i = elements.length;
while (lastElement === null && i > 0) {
lastElement = elements[--i];
}
if (lastElement === null) {
return;
}
const lastElement = last(elements);
for (let j = 0; j < this.state.leadingComments.length; j++) {
if (
this.state.leadingComments[j].end < this.state.commentPreviousNode.end