Skip to content

Commit 2bbd1e8

Browse files
authoredOct 29, 2024··
feat(es/parser): Ability to get script's potential module errors (#9682)
**Description:** In Deno I'm now parsing stuff as a program more often because I sometimes now need to discover if something is a script or es module by looking at the contents. A problem is that the module errors are discarded when parsing as a script, but I still might need them later if I discover the program the program should be treated as an ES module. **BREAKING CHANGE:** Adds a new method to the `Tokens` trait.
1 parent 97c90a1 commit 2bbd1e8

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed
 

‎.changeset/fifty-goats-beg.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: breaking
3+
swc_ecma_parser: breaking
4+
---
5+
6+
feat(es/parser): Ability to get script's potential module errors

‎crates/swc_ecma_parser/src/lexer/state.rs

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ impl Tokens for Lexer<'_> {
192192
take(&mut self.errors.borrow_mut())
193193
}
194194

195+
fn take_script_module_errors(&mut self) -> Vec<Error> {
196+
take(&mut self.module_errors.borrow_mut())
197+
}
198+
195199
fn end_pos(&self) -> BytePos {
196200
self.input.end_pos()
197201
}

‎crates/swc_ecma_parser/src/parser/input.rs

+12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
4949
fn end_pos(&self) -> BytePos;
5050

5151
fn take_errors(&mut self) -> Vec<Error>;
52+
53+
/// If the program was parsed as a script, this contains the module
54+
/// errors should the program be identified as a module in the future.
55+
fn take_script_module_errors(&mut self) -> Vec<Error>;
5256
}
5357

5458
#[derive(Clone)]
@@ -145,6 +149,10 @@ impl Tokens for TokensInput {
145149
take(&mut self.errors.borrow_mut())
146150
}
147151

152+
fn take_script_module_errors(&mut self) -> Vec<Error> {
153+
take(&mut self.module_errors.borrow_mut())
154+
}
155+
148156
fn end_pos(&self) -> BytePos {
149157
self.iter
150158
.as_slice()
@@ -269,6 +277,10 @@ impl<I: Tokens> Tokens for Capturing<I> {
269277
self.inner.take_errors()
270278
}
271279

280+
fn take_script_module_errors(&mut self) -> Vec<Error> {
281+
self.inner.take_script_module_errors()
282+
}
283+
272284
fn end_pos(&self) -> BytePos {
273285
self.inner.end_pos()
274286
}

‎crates/swc_ecma_parser/src/parser/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl<I: Tokens> Parser<I> {
9090
self.input().take_errors()
9191
}
9292

93+
pub fn take_script_module_errors(&mut self) -> Vec<Error> {
94+
self.input().take_script_module_errors()
95+
}
96+
9397
pub fn parse_script(&mut self) -> PResult<Script> {
9498
trace_cur!(self, parse_script);
9599

‎crates/swc_ecma_parser/src/parser/tests.rs

+23
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ fn illegal_language_mode_directive1() {
317317
},
318318
);
319319
}
320+
320321
#[test]
321322
fn illegal_language_mode_directive2() {
322323
test_parser(
@@ -346,3 +347,25 @@ fn illegal_language_mode_directive2() {
346347
fn parse_non_strict_for_loop() {
347348
script("for (var v1 = 1 in v3) {}");
348349
}
350+
351+
#[test]
352+
fn parse_program_take_script_module_errors() {
353+
test_parser(r#"077;"#, Default::default(), |p| {
354+
let program = p.parse_program()?;
355+
356+
assert_eq!(p.take_errors(), vec![]);
357+
// will contain the script's potential module errors
358+
assert_eq!(
359+
p.take_script_module_errors(),
360+
vec![Error::new(
361+
Span {
362+
lo: BytePos(1),
363+
hi: BytePos(4),
364+
},
365+
crate::parser::SyntaxError::LegacyOctal
366+
)]
367+
);
368+
369+
Ok(program)
370+
});
371+
}

0 commit comments

Comments
 (0)
Please sign in to comment.