Skip to content

Transformer don't take source_type param #6248

@overlookmotel

Description

@overlookmotel

Currently Transformer::new takes a source_type param:

pub fn new(
allocator: &'a Allocator,
source_path: &Path,
source_type: SourceType,
source_text: &'a str,
trivias: Trivias,
options: TransformOptions,
) -> Self {
let ctx = TransformCtx::new(source_path, source_type, source_text, trivias, &options);
Self { ctx, options, allocator }
}

Can we get rid of it? source_type is present in Program and parser updates it from "unambiguous" to either "script" or "module".

It seems to make more sense to use the value of source_type present in Program, than allow user to over-ride it.

I can't see any circumstance in which that's a useful thing to do, and it's a potential footgun. It's confusing that in transformer it's possible that self.ctx.source_type.is_script() != !self.ctx.source_type.is_module(), because it can also be "unambiguous". That's not theoretical - I hit this problem while trying to pass some TypeScript tests.

If for some obscure reason, the user does want to override it, they can always alter the value of Program::source_type before passing Program to transformer.

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / TranspilerC-cleanupCategory - technical debt or refactoring. Solution not expected to change behavior

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions