From 36d14932ff2e051d34610d70f6e68ca389e51ea6 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:52:21 +0000 Subject: [PATCH] refactor(parser): use `ModuleRecord::has_module_syntax` for setting sourceType (#7646) --- crates/oxc_parser/src/js/expression.rs | 2 +- crates/oxc_parser/src/js/module.rs | 1 - crates/oxc_parser/src/js/statement.rs | 1 - crates/oxc_parser/src/lib.rs | 26 +++++++++++--------------- crates/oxc_parser/src/module_record.rs | 4 ++++ 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 271b8ac870115..7263eb140c8fb 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -566,7 +566,7 @@ impl<'a> ParserImpl<'a> { self.bump_any(); // bump `.` let property = match self.cur_kind() { Kind::Meta => { - self.set_source_type_to_module_if_unambiguous(); + self.module_record_builder.visit_import_meta(); self.parse_keyword_identifier(Kind::Meta) } Kind::Target => self.parse_keyword_identifier(Kind::Target), diff --git a/crates/oxc_parser/src/js/module.rs b/crates/oxc_parser/src/js/module.rs index 82ada7b37b441..5e584677432b9 100644 --- a/crates/oxc_parser/src/js/module.rs +++ b/crates/oxc_parser/src/js/module.rs @@ -187,7 +187,6 @@ impl<'a> ParserImpl<'a> { self.expect(Kind::Eq)?; let expression = self.parse_assignment_expression_or_higher()?; self.asi()?; - self.set_source_type_to_module_if_unambiguous(); Ok(self.ast.alloc_ts_export_assignment(self.end_span(start_span), expression)) } diff --git a/crates/oxc_parser/src/js/statement.rs b/crates/oxc_parser/src/js/statement.rs index be87e6eee9fce..680916159d61e 100644 --- a/crates/oxc_parser/src/js/statement.rs +++ b/crates/oxc_parser/src/js/statement.rs @@ -44,7 +44,6 @@ impl<'a> ParserImpl<'a> { if is_top_level { if let Some(module_decl) = stmt.as_module_declaration() { - self.set_source_type_to_module_if_unambiguous(); self.module_record_builder.visit_module_declaration(module_decl); } } diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index b3658dc723dde..e5349ac6bbc8f 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -411,7 +411,7 @@ impl<'a> ParserImpl<'a> { /// Recoverable errors are stored inside `errors`. #[inline] pub fn parse(mut self) -> ParserReturn<'a> { - let (program, panicked) = match self.parse_program() { + let (mut program, panicked) = match self.parse_program() { Ok(program) => (program, false), Err(error) => { self.error(self.overlong_error().unwrap_or(error)); @@ -450,6 +450,16 @@ impl<'a> ParserImpl<'a> { } let irregular_whitespaces = self.lexer.trivia_builder.irregular_whitespaces.into_boxed_slice(); + + let source_type = program.source_type; + if source_type.is_unambiguous() { + program.source_type = if module_record.has_module_syntax { + source_type.with_module(true) + } else { + source_type.with_script(true) + }; + } + ParserReturn { program, module_record, @@ -481,8 +491,6 @@ impl<'a> ParserImpl<'a> { let (directives, statements) = self.parse_directives_and_statements(/* is_top_level */ true)?; - self.set_source_type_to_script_if_unambiguous(); - let span = Span::new(0, self.source_text.len() as u32); let comments = self.ast.vec_from_iter(self.lexer.trivia_builder.comments.iter().copied()); Ok(self.ast.program( @@ -564,18 +572,6 @@ impl<'a> ParserImpl<'a> { self.errors.len() + self.lexer.errors.len() } - fn set_source_type_to_module_if_unambiguous(&mut self) { - if self.source_type.is_unambiguous() { - self.source_type = self.source_type.with_module(true); - } - } - - fn set_source_type_to_script_if_unambiguous(&mut self) { - if self.source_type.is_unambiguous() { - self.source_type = self.source_type.with_script(true); - } - } - #[inline] fn alloc(&self, value: T) -> ArenaBox<'a, T> { self.ast.alloc(value) diff --git a/crates/oxc_parser/src/module_record.rs b/crates/oxc_parser/src/module_record.rs index bfb0eb4dafb59..5b295244a321a 100644 --- a/crates/oxc_parser/src/module_record.rs +++ b/crates/oxc_parser/src/module_record.rs @@ -173,6 +173,10 @@ impl<'a> ModuleRecordBuilder<'a> { } } + pub fn visit_import_meta(&mut self) { + self.module_record.has_module_syntax = true; + } + pub fn visit_module_declaration(&mut self, module_decl: &ModuleDeclaration<'a>) { self.module_record.has_module_syntax = true; match module_decl {