Skip to content

[Bug]: Inner comments for anonymous functions/classes printed in leading position #15138

@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('(function() {})');
  const fnNode = ast.program.body[0].expression;
  fnNode.innerComments = [{type: 'CommentBlock', value: 'foo'}];
  console.log(generate(ast, {compact: true}).code);
  // Prints (/*foo*/function(){});
}

{
  const ast = parse('(class {})');
  const classNode = ast.program.body[0].expression;
  classNode.innerComments = [{type: 'CommentBlock', value: 'foo'}];
  console.log(generate(ast, {compact: true}).code);
  // Prints (/*foo*/class{});
}

Configuration file name

No response

Configuration

n/a

Current and expected behavior

Previous to 7.20.1, innerComments were printed after the function or class keyword. Now they are printed before.

Previously: function /*foo*/ () {}
Now: /*foo*/function() {}

So now there's no difference between innerComments and leadingComments in these cases. In my opinion the old behavior made more sense ("inner" implies that position of comment is somewhere inside the node).

I assume this change occurred in #15080.

Environment

System:
OS: macOS 12.4
Binaries:
Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
npmPackages:
@babel/core: ^7.20.2 => 7.20.2
@babel/generator: ^7.20.2 => 7.20.2
@babel/helper-module-transforms: ^7.20.2 => 7.20.2
@babel/parser: ^7.20.2 => 7.20.2
@babel/plugin-transform-arrow-functions: ^7.18.6 => 7.18.6
@babel/plugin-transform-modules-commonjs: ^7.19.6 => 7.19.6
@babel/plugin-transform-react-jsx: ^7.19.0 => 7.19.0
@babel/plugin-transform-strict-mode: ^7.18.6 => 7.18.6
@babel/traverse: ^7.20.1 => 7.20.1
@babel/types: ^7.20.2 => 7.20.2
babel-plugin-dynamic-import-node: ^2.3.3 => 2.3.3
eslint: ^8.26.0 => 8.26.0
jest: ^29.2.2 => 29.2.2

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions