Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Still the error "Error: Comment "Hello" was not printed. Please report this error!" even though I have added a comment in the Doc. #16217

Closed
nurulakbaral opened this issue Apr 11, 2024 · 1 comment

Comments

@nurulakbaral
Copy link

Prettier 3.2.5

# Options (if any):
{
  "semi": false,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 120,
  "tabWidth": 2,
  "jsxSingleQuote": true,
  "plugins": ["./plugin.js"]
}

Input:

import * as React from 'react'

export default function Component() {
  return (
    // Hello
    <div>
      <h1></h1>
      <h2></h2>
    </div>
  )
}

Expected output:

import * as React from 'react'

export default function Component() {
  return (
    // Hello
    <div>
      <h1></h1>

      <h2></h2>
    </div>
  )
}

Why?

I created a Prettier Plugin, but the error always appears like this "Error: Comment "Hello" was not printed. Please report this error!". I even attached the comments manual in my Docs. Like this:

      print(path, options, print) {
        let node = path.getNode()

        if (node.type === 'ReturnStatement') {
          if (Array.isArray(node.argument.children)) {
            if (node.argument.children.length > 2) {
              let children = node.argument.children.filter(child => child.type !== 'JSXText')
              let childrenWithHardLine = children.map((child, idx) => {
                return [
                  path.call(
                    print,
                    'argument',
                    'children',
                    node.argument.children.indexOf(child)
                  ),
                  idx === children.length - 1 ? [] : [hardline, hardline]
                ]
              })

              return group([
                'return', 
                ' ',
                group([
                  ifBreak('('),
                  indent([
                    softline,
                    "// Hello", <-- ATTACH MANUALLY, BUT STILL ERROR.
                    hardline,
                    group([
                      group([path.call(print, 'argument', 'openingElement')]),
                      indent([
                        hardline, 
                        group([...childrenWithHardLine.flat()])
                      ]),
                      hardline,
                      [path.call(print, 'argument', 'closingElement')]
                    ]),
                  ]),
                  softline,
                  ifBreak(')'),
                ]),
                ';'
              ])
            }
          }
        }
}
@fisker
Copy link
Member

fisker commented Apr 11, 2024

You need mark the comment is printed

comment.printed = true;

@fisker fisker closed this as completed Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants