Skip to content

Commit

Permalink
feat(transformer): sync Program::source_type after transform
Browse files Browse the repository at this point in the history
closes #5552
  • Loading branch information
Boshen committed Sep 19, 2024
1 parent ae89145 commit bb28c97
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub fn check_semantic_after_transform(
) -> Option<Vec<OxcDiagnostic>> {
let mut errors = Errors::default();

if !program.source_type.is_javascript() || program.source_type.is_jsx() {
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 };
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_span/src/source_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/react/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl<'a> React<'a> {

impl<'a> Traverse<'a> for React<'a> {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
program.source_type = program.source_type.with_standard(true);
if self.refresh_plugin {
self.refresh.enter_program(program, ctx);
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(false);
self.namespace.enter_program(program, ctx);
}
}
Expand Down

0 comments on commit bb28c97

Please sign in to comment.