@@ -8,33 +8,38 @@ use deno_ast::ParsedSource;
88use std:: path:: Path ;
99use std:: sync:: Arc ;
1010
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 ( ) ) {
1313 Ok ( result) => Ok ( result) ,
1414 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) ) ;
1616 let new_file_path = match lowercase_ext. as_deref ( ) {
1717 Some ( "ts" ) | Some ( "cts" ) | Some ( "mts" ) => file_path. with_extension ( "tsx" ) ,
1818 Some ( "js" ) | Some ( "cjs" ) | Some ( "mjs" ) => file_path. with_extension ( "jsx" ) ,
1919 _ => return Err ( err) ,
2020 } ;
2121 // try to parse as jsx
22- match parse_inner ( & new_file_path, file_text) {
22+ match parse_inner ( & new_file_path, None , file_text) {
2323 Ok ( result) => Ok ( result) ,
2424 Err ( _) => Err ( err) , // return the original error
2525 }
2626 }
2727 }
2828}
2929
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) ?;
3232 ensure_no_specific_syntax_errors ( & parsed_source) ?;
3333 Ok ( parsed_source)
3434}
3535
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+
3843 let mut syntax = deno_ast:: get_syntax ( media_type) ;
3944 if let Syntax :: Es ( es) = & mut syntax {
4045 // support decorators in js
@@ -260,7 +265,7 @@ mod tests {
260265
261266 fn run_fatal_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
262267 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) ;
264269 }
265270
266271 #[ test]
@@ -286,6 +291,12 @@ mod tests {
286291 ) ;
287292 }
288293
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+
289300 #[ test]
290301 fn it_should_error_for_exected_close_brace ( ) {
291302 // swc can parse this, but we explicitly fail formatting
@@ -342,11 +353,11 @@ Merge conflict marker encountered. at file:///test.ts:6:1
342353
343354 fn run_non_fatal_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
344355 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) ;
346357
347358 // this error should also be surfaced in `format_parsed_source` if someone provides
348359 // 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 ( ) ;
350361 let config = ConfigurationBuilder :: new ( ) . build ( ) ;
351362 assert_eq ! ( crate :: format_parsed_source( & parsed_source, & config) . err( ) . unwrap( ) . to_string( ) , expected) ;
352363 }
0 commit comments