diff --git a/src/language-js/utils.js b/src/language-js/utils.js index c4d288cce..1498a27a7 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -54,7 +54,7 @@ function hasFlowShorthandAnnotationComment(node) { return ( node.extra && node.extra.parenthesized && - node.trailingComments && + isNonEmptyArray(node.trailingComments) && isBlockComment(node.trailingComments[0]) && FLOW_SHORTHAND_ANNOTATION.test(node.trailingComments[0].value) ); diff --git a/tests_integration/__tests__/parser-api.js b/tests_integration/__tests__/parser-api.js index e790caa8e..f3ed30025 100644 --- a/tests_integration/__tests__/parser-api.js +++ b/tests_integration/__tests__/parser-api.js @@ -29,6 +29,23 @@ test("allows usage of prettier's supported parsers", () => { expect(output).toEqual("bar();\n"); }); +test("allows add empty `trailingComments` array", () => { + const output = prettier.format("(foo /* comment */)( )", { + parser(text, parsers) { + const ast = parsers.babel(text); + + // This may happens in this use case https://github.com/prettier/prettier/pull/5497#issuecomment-439841965 + // Simply simulate it + const callExpression = ast.program.body[0].expression; + callExpression.extra = { parenthesized: true }; + callExpression.trailingComments = []; + + return ast; + }, + }); + expect(output).toEqual("foo(/* comment */);\n"); +}); + describe("allows passing a string to resolve a parser", () => { runPrettier("./custom-parsers/", [ "--end-of-line",