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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: regression for module block parse #15051

Closed
1 task
fisker opened this issue Oct 17, 2022 · 9 comments 路 Fixed by #15052
Closed
1 task

[Bug]: regression for module block parse #15051

fisker opened this issue Oct 17, 2022 · 9 comments 路 Fixed by #15052
Assignees
Labels
i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@fisker
Copy link
Contributor

fisker commented Oct 17, 2022

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

> require('@babel/parser').parse('module {/* comment */}', {plugins: ['moduleBlocks']}).program.body[0].expression.body
Node {
  type: 'Program',
  start: 21,   // <---------------------
  end: 8,  // <---------------------
  loc: SourceLocation {
    start: Position { line: 1, column: 21, index: 21 },
    end: Position { line: 1, column: 8, index: 8 },
    filename: undefined,
    identifierName: undefined
  },
  sourceType: 'module',
  interpreter: null,
  body: [],
  directives: [],
  trailingComments: [
    {
      type: 'CommentBlock',
      value: ' comment ',
      start: 8,
      end: 21,
      loc: [SourceLocation]
    }
  ]
}

Configuration file name

No response

Configuration

N/A

Current and expected behavior

Range should be 8 ~ 21

Environment

N/A

Possible solution

No response

Additional context

Possible introduced in #14980 @JLHwung

@babel-bot
Copy link
Collaborator

Hey @fisker! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

If anyone has trouble understanding the problem: start and end are swapped.

@liuxingbaoyu
Copy link
Member

It's weird, escaping our testing and even end-to-end testing.

@JLHwung JLHwung self-assigned this Oct 17, 2022
@JLHwung
Copy link
Contributor

JLHwung commented Oct 17, 2022

In Babel-parser 7.18.0, the program node starts at 21 and ends with 22:

https://astexplorer.net/#/gist/1b193f48d92b558bcd78e1516bef9c71/5a9bf187ab80c21af33bbbff591361d5f33eb533

After #14980 it starts at 21 but ends at 8, which is worse. The previous situation is not ideal, the Program node should start at comment's start (8) and end at 21, like how we parse a comment-only plain program /* comment */.

@fisker
Copy link
Contributor Author

fisker commented Oct 17, 2022

like how we parse a comment-only plain program /* comment */

But we start at 0 for <SPACE>/* comment */, should this module block Program start from index after {?

@JLHwung
Copy link
Contributor

JLHwung commented Oct 17, 2022

But we start at 0 for <SPACE>/* comment */, should this module block Program start from index after {?

Yes, the module block Program should start immediately after { and end immediately before the matching }. I may have to rewind the tokenizer to the position immediately before }.

@fisker
Copy link
Contributor Author

fisker commented Oct 17, 2022

Is it possible this

module {
  #!/usr/bin/env node
}

became a thing? then shebang is not on first line. 馃槃

@JLHwung
Copy link
Contributor

JLHwung commented Oct 17, 2022

@fisker Good question! Per spec:

The InputElementHashbangOrRegExp goal is used at the start of a Script or Module.

Because a module block can only contains optional ModuleBody, the hashbang comment is not allowed in the module block. Currently Babel already disallows it but I will check if we have such a test case.

@fisker
Copy link
Contributor Author

fisker commented Oct 17, 2022

Maybe too earily to consider about the estree plugin, but Eslint(Espree)'s Program starts from first token.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants