question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

throw the error when there is flow shorthand annotation comment with other comments

See original GitHub issue

Prettier 1.19.1 Playground link

--parser babel

Input:

f(
  // comment
  (0 /*: number */)
);

Output:

ensureAllCommentsPrinted/<@https://prettier.io/lib/standalone.js:15543:15
ensureAllCommentsPrinted@https://prettier.io/lib/standalone.js:15541:17
coreFormat@https://prettier.io/lib/standalone.js:15592:29
format@https://prettier.io/lib/standalone.js:15832:75
formatWithCursor@https://prettier.io/lib/standalone.js:15848:14
withPlugins/<@https://prettier.io/lib/standalone.js:31794:17
format@https://prettier.io/lib/standalone.js:31802:14
formatCode@https://prettier.io/worker.js:234:21
handleMessage@https://prettier.io/worker.js:185:18
self.onmessage@https://prettier.io/worker.js:155:14

Expected behavior: Prettier exits successfully. Prettier does not change the formatting of this code.

Observations: The following changes to the input make the crash disappear:

  • Remove the : in the comment on line 3
  • Remove the ( and ) on line 3
  • Remove the comment on line 2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thorn0commented, Oct 24, 2022

Not an issue anymore because support for Flow comments was removed in https://github.com/prettier/prettier/pull/13687

1reaction
stragercommented, Jan 28, 2020

If a Flow shorthand annotation comment is detected, Prettier disables the default comment handling and implements custom handling for Flow comments. (This custom handling was implemented in pull request #5280.) The problem is that the custom handling only prints one comment (the first trailing comment), and forgets about all other comments. Prettier’s sanity checks detect the fact that some comments were not printed, and reports an error.

This issue does not reproduce when using Flow’s parser (--parser flow).

I see three solutions:

  1. Extend the custom handling for Flow comments to print all other comments too.
  2. In Prettier, transform the AST given by Babel to make it look like the AST given by Flow. In particular, wrap each commented node in a TypeCastExpression node. (See the AST comparisons below.) Then, remove the custom handling for Flow comments.
  3. In Babel, add an option to parse Flow shorthand annotation comments into TypeCastExpression nodes, mimicking Flow’s parser. (See the AST comparisons below.) Then, enable this feature in Prettier’s use of Babel, and remove Prettier’s custom handling for Flow comments.

Which of the above solutions sounds best? Does anyone have any other ideas on how to fix this crash?

cc @swac, @j-f1, @lydell

AST comparison

Input program

f(
  // comment
  (0 /*: number */)
);

--parser babel AST (abridged)

Node {
  type: 'File',
  program:
   Node {
     type: 'Program',
     sourceType: 'module',
     interpreter: null,
     body:
      [ Node {
          type: 'ExpressionStatement',
          expression:
           Node {
             type: 'CallExpression',
             callee:
              Node {
                type: 'Identifier',
                name: 'f' },
             arguments:
              [ Node {
                  type: 'NumericLiteral',
                  extra:
                   { rawValue: 0, raw: '0', parenthesized: true, parenStart: 18 },
                  value: 0,
                  leadingComments:
                   [ { type: 'CommentLine',
                       value: ' comment' } ],
                  trailingComments:
                   [ { type: 'CommentBlock',
                       value: ': number ' } ] } ] } } ],
     directives: [] },
  comments:
   [ { type: 'CommentLine',
       value: ' comment' },
     { type: 'CommentBlock',
       value: ': number ' } ] }

--parser flow AST (abridged)

{ type: 'Program',
  body:
   [ { type: 'ExpressionStatement',
       expression:
        { type: 'CallExpression',
          callee:
           { type: 'Identifier',
             name: 'f',
             typeAnnotation: null,
             optional: false },
          typeArguments: null,
          arguments:
           [ { type: 'TypeCastExpression',
               expression:
                { type: 'Literal',
                  value: 0,
                  raw: '0' },
               typeAnnotation:
                { type: 'TypeAnnotation',
                  typeAnnotation:
                   { type: 'NumberTypeAnnotation' } } } ] },
       directive: null } ],
  comments:
   [ { type: 'Line',
       value: ' comment' } ] }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Comment Types - JavaScript. Flow
These comments allow Flow to work in plain JavaScript files without any additional work. Comment types syntax. There are two primary pieces of...
Read more >
Best practices for writing code comments - Stack Overflow Blog
Rule 4: Comments should dispel confusion, not cause it. Rule 5: Explain unidiomatic code in comments. Rule 6: Provide links to the original ......
Read more >
@babel/plugin-transform-flow-comments | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Why doesn't logical OR work with error throwing in JavaScript?
As other answers have stated, it is because throw is a statement, which can't be used in contexts which expect expressions, such as...
Read more >
Comment (computer programming) - Wikipedia
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found