Skip to content

Support onInsertedSemicolon callback when parsing #11053

@ahabhgk

Description

@ahabhgk

Describe the feature

Support pass a callback or a Option<HashSet> for swc_ecma_parser::Parser to collect the BytePos position of automatically inserted semicolons

Babel plugin or link to the feature description

https://github.com/acornjs/acorn/blob/ca53db28c88ce5a8ee8c7db7027be71ed29b6aaf/acorn/src/options.js#L19-L24

Additional context

The background is when I trying to solve web-infra-dev/rspack#11551, I notice that the swc_ecma_parser::Lexer will output different tokens when using it in parse() and .collect_vec()

For example:

import {assert} from './a'

const labelType = `${node}Label`

const node = []
assert(labelType)

In parse() this will have correct tokens, but using with .collect_vec() (or Iterator) it will have a lexing error: Error { error: (60..97, UnterminatedTpl) } (and the lexing error causes Rspack collected the wrong semicolons, which is used for Rspack to add ; at proper position to generate the correct code)

So it will be great for correctness and performance if the ASI position can be collected by SWC when parsing (currently Rspack lexes twice, once for parse and once for collecting ASI positions)

Or provide an API that I can get the correct tokens without parsing, which is a workable alternative solution.

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