oxc icon indicating copy to clipboard operation
oxc copied to clipboard

2 different `SourceType`s needed for parser options, and for in AST

Open overlookmotel opened this issue 1 year ago • 1 comments

ModuleKind has 3 different options:

https://github.com/oxc-project/oxc/blob/d085c2cd2525fb60b45fb84cbb03cbab8b40e032/crates/oxc_span/src/source_type/mod.rs#L49-L62

The "unambiguous" option only makes sense as an input argument to the parser. The parser decides whether it's a script or module during parsing, so a Program is always one or the other, never "unambiguous".

Can we rule out the erroneous "unambiguous" option for Program::source_type by making the existing ModuleKind only "script" or "module", and introducing a separate SourceType to oxc_parser which is only used in ParseOptions?

While we're at it, we could also simplify SourceType by making it just a bitflags byte, or a 1-byte enum.

overlookmotel avatar Oct 02 '24 14:10 overlookmotel

This is complicated ...

Boshen avatar Oct 02 '24 15:10 Boshen

While we're at it, we could also simplify SourceType by making it just a bitflags byte, or a 1-byte enum.

This is a huge breaking change because we export Language / ModuleKind / LanguageVariant, serialize them in a certain way.

Program::source_type vs ParserOptions::source_type

This introduces complexity to the project, where we need to distinguish the input and the output. We also need to implement conversions between them so they can be compared.


The amount of effort for making these changes is a bit of burden. I'll update the documentation instead around unambiguous instead.

Boshen avatar Oct 10 '24 14:10 Boshen

Supporting unambiguous as a Program.sourceType makes the types incompatible with an estree Program.

Walking an AST with something like estree-walker and passing ParseResult.program results in type error:

Types of property 'sourceType' are incompatible.
      Type 'ModuleKind' is not assignable to type '"script" | "module"'.
        Type '"unambiguous"' is not assignable to type '"script" | "module"'

You have to use a type assertion.

knightedcodemonkey avatar Mar 29 '25 14:03 knightedcodemonkey