feat(transformer): sync Program::source_type after transform (#5887)

closes #5552
This commit is contained in:
Boshen 2024-09-19 14:41:06 +00:00
parent 127c88134e
commit a07f03aab3
4 changed files with 25 additions and 0 deletions

View file

@ -117,6 +117,11 @@ pub fn check_semantic_after_transform(
) -> Option<Vec<OxcDiagnostic>> {
let mut errors = Errors::default();
let source_type = program.source_type;
if !source_type.is_typescript_definition() && !source_type.is_javascript() {
errors.push(format!("SourceType is not javascript: {:?}", program.source_type));
}
// Collect `ScopeId`s, `SymbolId`s and `ReferenceId`s from AST after transformer
let scoping_after_transform =
Scoping { symbols: symbols_after_transform, scopes: scopes_after_transform };

View file

@ -294,6 +294,14 @@ impl SourceType {
self
}
#[must_use]
pub const fn with_javascript(mut self, yes: bool) -> Self {
if yes {
self.language = Language::JavaScript;
}
self
}
#[must_use]
pub const fn with_typescript(mut self, yes: bool) -> Self {
if yes {
@ -318,6 +326,14 @@ impl SourceType {
self
}
#[must_use]
pub const fn with_standard(mut self, yes: bool) -> Self {
if yes {
self.variant = LanguageVariant::Standard;
}
self
}
/// Converts a file [`Path`] to [`SourceType`].
///
/// ## Examples

View file

@ -70,6 +70,9 @@ impl<'a> React<'a> {
impl<'a> Traverse<'a> for React<'a> {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.jsx_plugin {
program.source_type = program.source_type.with_standard(true);
}
if self.refresh_plugin {
self.refresh.enter_program(program, ctx);
}

View file

@ -77,6 +77,7 @@ impl<'a> Traverse<'a> for TypeScript<'a> {
program.hashbang = None;
program.body.clear();
} else {
program.source_type = program.source_type.with_javascript(true);
self.namespace.enter_program(program, ctx);
}
}