Skip to content

[Bug]: generate does not output innerComments on class expression #13819

@overlookmotel

Description

@overlookmotel

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const {parse} = require('@babel/parser'),
  generate = require('@babel/generator').default;

const ast = parse('(class /*foo*/ extends X {})');
console.log(generate(node).code);

Configuration file name

No response

Configuration

n/a

Current and expected behavior

Would expect generate() to output code identical to the input, preserving the /*foo*/ comment.

Actual output is '(class extends X {})'. The comment is omitted.

AST returned by parse() does include the comment. ast.program.body[0].expression.innerComments[0] equals {type: 'CommentBlock', value: 'foo'}. It's generate() which ignores it.

Environment

System:
    OS: macOS 10.15.7
Binaries:
  Node: 16.8.0 - ~/.nvm/versions/node/v16.8.0/bin/node
  npm: 7.21.0 - ~/.nvm/versions/node/v16.8.0/bin/npm
npmPackages:
  @babel/core: ^7.15.5 => 7.15.5
  @babel/generator: ^7.15.4 => 7.15.4
  @babel/helper-module-transforms: ^7.15.7 => 7.15.7
  @babel/helper-plugin-utils: ^7.14.5 => 7.14.5
  @babel/parser: ^7.15.7 => 7.15.7
  @babel/plugin-transform-arrow-functions: ^7.14.5 => 7.14.5
  @babel/plugin-transform-modules-commonjs: ^7.15.4 => 7.15.4
  @babel/plugin-transform-react-jsx: ^7.14.9 => 7.14.9
  @babel/plugin-transform-strict-mode: ^7.14.5 => 7.14.5
  @babel/register: ^7.15.3 => 7.15.3
  @babel/traverse: ^7.15.4 => 7.15.4
  @babel/types: ^7.15.6 => 7.15.6
  babel-jest: ^27.2.2 => 27.2.2
  babel-plugin-dynamic-import-node: ^2.3.3 => 2.3.3
  eslint: ^7.32.0 => 7.32.0
  jest: ^27.2.2 => 27.2.2

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions