Skip to content

Commit

Permalink
refactor(parser): use ModuleRecord::has_module_syntax for setting s…
Browse files Browse the repository at this point in the history
…ourceType (#7646)
  • Loading branch information
Boshen committed Dec 4, 2024
1 parent d503a84 commit 36d1493
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
1 change: 0 additions & 1 deletion crates/oxc_parser/src/js/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
26 changes: 11 additions & 15 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<T>(&self, value: T) -> ArenaBox<'a, T> {
self.ast.alloc(value)
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_parser/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 36d1493

Please sign in to comment.