diff --git a/.changeset/fifty-goats-beg.md b/.changeset/fifty-goats-beg.md new file mode 100644 index 000000000000..88e35be03887 --- /dev/null +++ b/.changeset/fifty-goats-beg.md @@ -0,0 +1,6 @@ +--- +swc_core: breaking +swc_ecma_parser: breaking +--- + +feat(es/parser): Ability to get script's potential module errors diff --git a/crates/swc_ecma_parser/src/lexer/state.rs b/crates/swc_ecma_parser/src/lexer/state.rs index 26e307285eba..c881a0bc8d07 100644 --- a/crates/swc_ecma_parser/src/lexer/state.rs +++ b/crates/swc_ecma_parser/src/lexer/state.rs @@ -192,6 +192,10 @@ impl Tokens for Lexer<'_> { take(&mut self.errors.borrow_mut()) } + fn take_script_module_errors(&mut self) -> Vec { + take(&mut self.module_errors.borrow_mut()) + } + fn end_pos(&self) -> BytePos { self.input.end_pos() } diff --git a/crates/swc_ecma_parser/src/parser/input.rs b/crates/swc_ecma_parser/src/parser/input.rs index fc7faa38434c..2ce9fbbfa279 100644 --- a/crates/swc_ecma_parser/src/parser/input.rs +++ b/crates/swc_ecma_parser/src/parser/input.rs @@ -49,6 +49,10 @@ pub trait Tokens: Clone + Iterator { fn end_pos(&self) -> BytePos; fn take_errors(&mut self) -> Vec; + + /// If the program was parsed as a script, this contains the module + /// errors should the program be identified as a module in the future. + fn take_script_module_errors(&mut self) -> Vec; } #[derive(Clone)] @@ -145,6 +149,10 @@ impl Tokens for TokensInput { take(&mut self.errors.borrow_mut()) } + fn take_script_module_errors(&mut self) -> Vec { + take(&mut self.module_errors.borrow_mut()) + } + fn end_pos(&self) -> BytePos { self.iter .as_slice() @@ -269,6 +277,10 @@ impl Tokens for Capturing { self.inner.take_errors() } + fn take_script_module_errors(&mut self) -> Vec { + self.inner.take_script_module_errors() + } + fn end_pos(&self) -> BytePos { self.inner.end_pos() } diff --git a/crates/swc_ecma_parser/src/parser/mod.rs b/crates/swc_ecma_parser/src/parser/mod.rs index e699b4c3a58d..5510a7a6db50 100644 --- a/crates/swc_ecma_parser/src/parser/mod.rs +++ b/crates/swc_ecma_parser/src/parser/mod.rs @@ -90,6 +90,10 @@ impl Parser { self.input().take_errors() } + pub fn take_script_module_errors(&mut self) -> Vec { + self.input().take_script_module_errors() + } + pub fn parse_script(&mut self) -> PResult