@@ -8,33 +8,38 @@ use deno_ast::ParsedSource;
8
8
use std:: path:: Path ;
9
9
use std:: sync:: Arc ;
10
10
11
- pub fn parse_swc_ast ( file_path : & Path , file_text : Arc < str > ) -> Result < ParsedSource > {
12
- match parse_inner ( file_path, file_text. clone ( ) ) {
11
+ pub fn parse_swc_ast ( file_path : & Path , file_extension : Option < & str > , file_text : Arc < str > ) -> Result < ParsedSource > {
12
+ match parse_inner ( file_path, file_extension , file_text. clone ( ) ) {
13
13
Ok ( result) => Ok ( result) ,
14
14
Err ( err) => {
15
- let lowercase_ext = get_lowercase_extension ( file_path) ;
15
+ let lowercase_ext = file_extension . map ( |ext| ext . to_string ( ) ) . or_else ( || get_lowercase_extension ( file_path) ) ;
16
16
let new_file_path = match lowercase_ext. as_deref ( ) {
17
17
Some ( "ts" ) | Some ( "cts" ) | Some ( "mts" ) => file_path. with_extension ( "tsx" ) ,
18
18
Some ( "js" ) | Some ( "cjs" ) | Some ( "mjs" ) => file_path. with_extension ( "jsx" ) ,
19
19
_ => return Err ( err) ,
20
20
} ;
21
21
// try to parse as jsx
22
- match parse_inner ( & new_file_path, file_text) {
22
+ match parse_inner ( & new_file_path, None , file_text) {
23
23
Ok ( result) => Ok ( result) ,
24
24
Err ( _) => Err ( err) , // return the original error
25
25
}
26
26
}
27
27
}
28
28
}
29
29
30
- fn parse_inner ( file_path : & Path , text : Arc < str > ) -> Result < ParsedSource > {
31
- let parsed_source = parse_inner_no_diagnostic_check ( file_path, text) ?;
30
+ fn parse_inner ( file_path : & Path , file_extension : Option < & str > , text : Arc < str > ) -> Result < ParsedSource > {
31
+ let parsed_source = parse_inner_no_diagnostic_check ( file_path, file_extension , text) ?;
32
32
ensure_no_specific_syntax_errors ( & parsed_source) ?;
33
33
Ok ( parsed_source)
34
34
}
35
35
36
- fn parse_inner_no_diagnostic_check ( file_path : & Path , text : Arc < str > ) -> Result < ParsedSource > {
37
- let media_type = deno_ast:: MediaType :: from_path ( file_path) ;
36
+ fn parse_inner_no_diagnostic_check ( file_path : & Path , file_extension : Option < & str > , text : Arc < str > ) -> Result < ParsedSource > {
37
+ let media_type = if let Some ( file_extension) = file_extension {
38
+ deno_ast:: MediaType :: from_path ( & file_path. with_extension ( file_extension) )
39
+ } else {
40
+ deno_ast:: MediaType :: from_path ( file_path)
41
+ } ;
42
+
38
43
let mut syntax = deno_ast:: get_syntax ( media_type) ;
39
44
if let Syntax :: Es ( es) = & mut syntax {
40
45
// support decorators in js
@@ -260,7 +265,7 @@ mod tests {
260
265
261
266
fn run_fatal_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
262
267
let file_path = PathBuf :: from ( file_path) ;
263
- assert_eq ! ( parse_swc_ast( & file_path, text. into( ) ) . err( ) . unwrap( ) . to_string( ) , expected) ;
268
+ assert_eq ! ( parse_swc_ast( & file_path, None , text. into( ) ) . err( ) . unwrap( ) . to_string( ) , expected) ;
264
269
}
265
270
266
271
#[ test]
@@ -286,6 +291,12 @@ mod tests {
286
291
) ;
287
292
}
288
293
294
+ #[ test]
295
+ fn file_extension_overwrite ( ) {
296
+ let file_path = PathBuf :: from ( "./test.js" ) ;
297
+ assert ! ( parse_swc_ast( & file_path, Some ( "ts" ) , "const foo: string = 'bar';" . into( ) ) . is_ok( ) ) ;
298
+ }
299
+
289
300
#[ test]
290
301
fn it_should_error_for_exected_close_brace ( ) {
291
302
// swc can parse this, but we explicitly fail formatting
@@ -342,11 +353,11 @@ Merge conflict marker encountered. at file:///test.ts:6:1
342
353
343
354
fn run_non_fatal_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
344
355
let file_path = PathBuf :: from ( file_path) ;
345
- assert_eq ! ( format!( "{}" , parse_swc_ast( & file_path, text. into( ) ) . err( ) . unwrap( ) ) , expected) ;
356
+ assert_eq ! ( format!( "{}" , parse_swc_ast( & file_path, None , text. into( ) ) . err( ) . unwrap( ) ) , expected) ;
346
357
347
358
// this error should also be surfaced in `format_parsed_source` if someone provides
348
359
// a source file that had a non-fatal diagnostic
349
- let parsed_source = parse_inner_no_diagnostic_check ( & file_path, text. into ( ) ) . unwrap ( ) ;
360
+ let parsed_source = parse_inner_no_diagnostic_check ( & file_path, None , text. into ( ) ) . unwrap ( ) ;
350
361
let config = ConfigurationBuilder :: new ( ) . build ( ) ;
351
362
assert_eq ! ( crate :: format_parsed_source( & parsed_source, & config) . err( ) . unwrap( ) . to_string( ) , expected) ;
352
363
}
0 commit comments