1
1
use std:: path:: Path ;
2
2
3
+ use deno_ast:: ParsedSource ;
3
4
use dprint_core:: configuration:: resolve_new_line_kind;
4
5
use dprint_core:: formatting:: * ;
5
6
use dprint_core:: types:: ErrBox ;
6
- use swc_common :: comments :: SingleThreadedCommentsMapInner ;
7
- use swc_ecmascript :: parser :: token :: TokenAndSpan ;
7
+
8
+ use crate :: swc :: ensure_no_specific_syntax_errors ;
8
9
9
10
use super :: configuration:: Configuration ;
10
11
use super :: parsing:: parse;
@@ -39,75 +40,40 @@ use super::swc::parse_swc_ast;
39
40
/// ```
40
41
pub fn format_text ( file_path : & Path , file_text : & str , config : & Configuration ) -> Result < String , ErrBox > {
41
42
if super :: utils:: file_text_has_ignore_comment ( file_text, & config. ignore_file_comment_text ) {
42
- return Ok ( String :: from ( file_text) ) ;
43
+ Ok ( String :: from ( file_text) )
44
+ } else {
45
+ let parsed_source = parse_swc_ast ( file_path, file_text) ?;
46
+ inner_format ( & parsed_source, config)
43
47
}
44
-
45
- let parsed_source_file = parse_swc_ast ( file_path, file_text) ?;
46
- Ok ( dprint_core:: formatting:: format (
47
- || {
48
- let print_items = parse (
49
- & SourceFileInfo {
50
- is_jsx : parsed_source_file. is_jsx ,
51
- module : & parsed_source_file. module ,
52
- info : & parsed_source_file. info ,
53
- tokens : & parsed_source_file. tokens ,
54
- leading_comments : & parsed_source_file. leading_comments ,
55
- trailing_comments : & parsed_source_file. trailing_comments ,
56
- } ,
57
- config,
58
- ) ;
59
- // println!("{}", print_items.get_as_text());
60
- print_items
61
- } ,
62
- config_to_print_options ( file_text, config) ,
63
- ) )
64
48
}
65
49
66
- #[ derive( Clone ) ]
67
- pub struct SourceFileInfo < ' a > {
68
- pub is_jsx : bool ,
69
- pub module : & ' a swc_ecmascript:: ast:: Module ,
70
- pub info : & ' a dyn swc_ast_view:: SourceFile ,
71
- pub tokens : & ' a [ TokenAndSpan ] ,
72
- pub leading_comments : & ' a SingleThreadedCommentsMapInner ,
73
- pub trailing_comments : & ' a SingleThreadedCommentsMapInner ,
50
+ /// Formats an already parsed source. This is useful as a performance optimization.
51
+ pub fn format_parsed_source ( source : & ParsedSource , config : & Configuration ) -> Result < String , ErrBox > {
52
+ if super :: utils:: file_text_has_ignore_comment ( source. source ( ) . text_str ( ) , & config. ignore_file_comment_text ) {
53
+ Ok ( source. source ( ) . text_str ( ) . to_string ( ) )
54
+ } else {
55
+ inner_format ( source, config)
56
+ }
74
57
}
75
58
76
- /// Formats the already parsed file. This is useful as a performance optimization.
77
- pub fn format_parsed_file ( info : & SourceFileInfo < ' _ > , config : & Configuration ) -> String {
78
- if super :: utils:: file_text_has_ignore_comment ( info. info . text ( ) , & config. ignore_file_comment_text ) {
79
- return info. info . text ( ) . to_string ( ) ;
80
- }
59
+ fn inner_format ( parsed_source : & ParsedSource , config : & Configuration ) -> Result < String , ErrBox > {
60
+ ensure_no_specific_syntax_errors ( parsed_source) ?;
81
61
82
- dprint_core:: formatting:: format (
62
+ Ok ( dprint_core:: formatting:: format (
83
63
|| {
84
- let print_items = parse ( & info , config) ;
64
+ let print_items = parse ( & parsed_source , config) ;
85
65
// println!("{}", print_items.get_as_text());
86
66
print_items
87
67
} ,
88
- config_to_print_options ( info . info . text ( ) , config) ,
89
- )
68
+ config_to_print_options ( parsed_source . source ( ) . text_str ( ) , config) ,
69
+ ) )
90
70
}
91
71
92
72
#[ cfg( feature = "tracing" ) ]
93
73
pub fn trace_file ( file_path : & Path , file_text : & str , config : & Configuration ) -> dprint_core:: formatting:: TracingResult {
94
- let parsed_source_file = parse_swc_ast ( file_path, file_text) . expect ( "Expected to parse to SWC AST." ) ;
95
- dprint_core:: formatting:: trace_printing (
96
- || {
97
- parse (
98
- & SourceFileInfo {
99
- is_jsx : parsed_source_file. is_jsx ,
100
- info : & parsed_source_file. info ,
101
- module : & parsed_source_file. module ,
102
- tokens : & parsed_source_file. tokens ,
103
- leading_comments : & parsed_source_file. leading_comments ,
104
- trailing_comments : & parsed_source_file. trailing_comments ,
105
- } ,
106
- config,
107
- )
108
- } ,
109
- config_to_print_options ( file_text, config) ,
110
- )
74
+ let parsed_source = parse_swc_ast ( file_path, file_text) . unwrap ( ) ;
75
+ ensure_no_specific_syntax_errors ( parsed_source) . unwrap ( ) ;
76
+ dprint_core:: formatting:: trace_printing ( || parse ( & parsed_source, config) , config_to_print_options ( file_text, config) )
111
77
}
112
78
113
79
fn config_to_print_options ( file_text : & str , config : & Configuration ) -> PrintOptions {
@@ -118,3 +84,53 @@ fn config_to_print_options(file_text: &str, config: &Configuration) -> PrintOpti
118
84
new_line_text : resolve_new_line_kind ( file_text, config. new_line_kind ) ,
119
85
}
120
86
}
87
+
88
+ #[ cfg( test) ]
89
+ mod test {
90
+ use std:: path:: PathBuf ;
91
+
92
+ use crate :: configuration:: ConfigurationBuilder ;
93
+
94
+ use super :: * ;
95
+
96
+ #[ test]
97
+ fn it_should_error_for_no_equals_sign_in_var_decl ( ) {
98
+ run_diagnostic_test (
99
+ "./test.ts" ,
100
+ "const Methods {\n f: (x, y) => x + y,\n };" ,
101
+ concat ! ( "Line 1, column 15: Expected a semicolon\n " , "\n " , " const Methods {\n " , " ~" ) ,
102
+ ) ;
103
+ }
104
+
105
+ #[ test]
106
+ fn it_should_error_when_var_stmts_sep_by_comma ( ) {
107
+ run_diagnostic_test (
108
+ "./test.ts" ,
109
+ "let a = 0, let b = 1;" ,
110
+ concat ! (
111
+ "Line 1, column 16: Expected a semicolon\n " ,
112
+ "\n " ,
113
+ " let a = 0, let b = 1;\n " ,
114
+ " ~"
115
+ ) ,
116
+ ) ;
117
+ }
118
+
119
+ #[ test]
120
+ fn it_should_error_for_exected_expr_issue_121 ( ) {
121
+ run_diagnostic_test (
122
+ "./test.ts" ,
123
+ "type T =\n | unknown\n { } & unknown;" ,
124
+ concat ! ( "Line 3, column 7: Expression expected\n " , "\n " , " { } & unknown;\n " , " ~" ) ,
125
+ ) ;
126
+ }
127
+
128
+ fn run_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
129
+ let file_path = PathBuf :: from ( file_path) ;
130
+ let parsed_source = crate :: swc:: parse_swc_ast ( & file_path, text) . unwrap ( ) ;
131
+ let config = ConfigurationBuilder :: new ( ) . build ( ) ;
132
+ // ensure it happens for both of these methods
133
+ assert_eq ! ( format_text( & file_path, text, & config) . err( ) . unwrap( ) . to_string( ) , expected) ;
134
+ assert_eq ! ( format_parsed_source( & parsed_source, & config) . err( ) . unwrap( ) . to_string( ) , expected) ;
135
+ }
136
+ }
0 commit comments