File tree 5 files changed +49
-0
lines changed
crates/swc_ecma_parser/src
5 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ swc_core : breaking
3
+ swc_ecma_parser : breaking
4
+ ---
5
+
6
+ feat(es/parser): Ability to get script's potential module errors
Original file line number Diff line number Diff line change @@ -192,6 +192,10 @@ impl Tokens for Lexer<'_> {
192
192
take ( & mut self . errors . borrow_mut ( ) )
193
193
}
194
194
195
+ fn take_script_module_errors ( & mut self ) -> Vec < Error > {
196
+ take ( & mut self . module_errors . borrow_mut ( ) )
197
+ }
198
+
195
199
fn end_pos ( & self ) -> BytePos {
196
200
self . input . end_pos ( )
197
201
}
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
49
49
fn end_pos ( & self ) -> BytePos ;
50
50
51
51
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 > ;
52
56
}
53
57
54
58
#[ derive( Clone ) ]
@@ -145,6 +149,10 @@ impl Tokens for TokensInput {
145
149
take ( & mut self . errors . borrow_mut ( ) )
146
150
}
147
151
152
+ fn take_script_module_errors ( & mut self ) -> Vec < Error > {
153
+ take ( & mut self . module_errors . borrow_mut ( ) )
154
+ }
155
+
148
156
fn end_pos ( & self ) -> BytePos {
149
157
self . iter
150
158
. as_slice ( )
@@ -269,6 +277,10 @@ impl<I: Tokens> Tokens for Capturing<I> {
269
277
self . inner . take_errors ( )
270
278
}
271
279
280
+ fn take_script_module_errors ( & mut self ) -> Vec < Error > {
281
+ self . inner . take_script_module_errors ( )
282
+ }
283
+
272
284
fn end_pos ( & self ) -> BytePos {
273
285
self . inner . end_pos ( )
274
286
}
Original file line number Diff line number Diff line change @@ -90,6 +90,10 @@ impl<I: Tokens> Parser<I> {
90
90
self . input ( ) . take_errors ( )
91
91
}
92
92
93
+ pub fn take_script_module_errors ( & mut self ) -> Vec < Error > {
94
+ self . input ( ) . take_script_module_errors ( )
95
+ }
96
+
93
97
pub fn parse_script ( & mut self ) -> PResult < Script > {
94
98
trace_cur ! ( self , parse_script) ;
95
99
Original file line number Diff line number Diff line change @@ -317,6 +317,7 @@ fn illegal_language_mode_directive1() {
317
317
} ,
318
318
) ;
319
319
}
320
+
320
321
#[ test]
321
322
fn illegal_language_mode_directive2 ( ) {
322
323
test_parser (
@@ -346,3 +347,25 @@ fn illegal_language_mode_directive2() {
346
347
fn parse_non_strict_for_loop ( ) {
347
348
script ( "for (var v1 = 1 in v3) {}" ) ;
348
349
}
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
+ }
You can’t perform that action at this time.
0 commit comments