@@ -2,7 +2,6 @@ use anyhow::anyhow;
2
2
use anyhow:: bail;
3
3
use anyhow:: Result ;
4
4
use deno_ast:: swc:: parser:: error:: SyntaxError ;
5
- use deno_ast:: Diagnostic ;
6
5
use deno_ast:: ParsedSource ;
7
6
use deno_ast:: SourceTextInfo ;
8
7
use std:: path:: Path ;
@@ -40,9 +39,9 @@ fn parse_inner_no_diagnostic_check(file_path: &Path, text_info: SourceTextInfo)
40
39
maybe_syntax : None ,
41
40
media_type : file_path. into ( ) ,
42
41
scope_analysis : false ,
43
- text_info : text_info . clone ( ) ,
42
+ text_info,
44
43
} )
45
- . map_err ( |diagnostic| anyhow ! ( "{}" , format_diagnostic ( & diagnostic, & text_info ) ) )
44
+ . map_err ( |diagnostic| anyhow ! ( "{:# }" , & diagnostic) )
46
45
}
47
46
48
47
pub fn ensure_no_specific_syntax_errors ( parsed_source : & ParsedSource ) -> Result < ( ) > {
@@ -78,11 +77,11 @@ pub fn ensure_no_specific_syntax_errors(parsed_source: &ParsedSource) -> Result<
78
77
Ok ( ( ) )
79
78
} else {
80
79
let mut final_message = String :: new ( ) ;
81
- for error in diagnostics {
80
+ for diagnostic in diagnostics {
82
81
if !final_message. is_empty ( ) {
83
82
final_message. push_str ( "\n \n " ) ;
84
83
}
85
- final_message. push_str ( & format_diagnostic ( error , parsed_source . text_info ( ) ) ) ;
84
+ final_message. push_str ( & format ! ( "{}" , diagnostic ) ) ;
86
85
}
87
86
bail ! ( "{}" , final_message)
88
87
}
@@ -92,15 +91,10 @@ fn get_lowercase_extension(file_path: &Path) -> Option<String> {
92
91
file_path. extension ( ) . and_then ( |e| e. to_str ( ) ) . map ( |f| f. to_lowercase ( ) )
93
92
}
94
93
95
- fn format_diagnostic ( error : & Diagnostic , text_info : & SourceTextInfo ) -> String {
96
- let file_text = text_info. text_str ( ) ;
97
- let range = error. range . as_byte_range ( text_info. range ( ) . start ) ;
98
- dprint_core:: formatting:: utils:: string_utils:: format_diagnostic ( Some ( ( range. start , range. end ) ) , & error. message ( ) , file_text)
99
- }
100
-
101
94
#[ cfg( test) ]
102
95
mod tests {
103
96
use crate :: configuration:: ConfigurationBuilder ;
97
+ use pretty_assertions:: assert_eq;
104
98
105
99
use super :: * ;
106
100
use std:: path:: PathBuf ;
@@ -110,7 +104,7 @@ mod tests {
110
104
run_fatal_diagnostic_test (
111
105
"./test.ts" ,
112
106
"test;\n as#;" ,
113
- concat ! ( "Line 2, column 3: Expected ';', '}' or <eof>\n " , "\n " , " as#;\n " , " ~" ) ,
107
+ concat ! ( "Expected ';', '}' or <eof> at ./test.ts:2:3 \n " , "\n " , " as#;\n " , " ~" ) ,
114
108
) ;
115
109
}
116
110
@@ -121,7 +115,7 @@ mod tests {
121
115
"./test.ts" ,
122
116
"+value." ,
123
117
concat ! (
124
- "Line 1, column 8: Unexpected eof \n \n " ,
118
+ "Unexpected eof at ./test.ts:1:8 \n \n " ,
125
119
" +value.\n " ,
126
120
// this excess whitespace is a bug, but not a big deal
127
121
" "
@@ -135,7 +129,7 @@ mod tests {
135
129
run_fatal_diagnostic_test (
136
130
"./test.ts" ,
137
131
"+value.;" ,
138
- concat ! ( "Line 1, column 8: Expected ident \n \n " , " +value.;\n " , " ~" ) ,
132
+ concat ! ( "Expected ident at ./test.ts:1:8 \n \n " , " +value.;\n " , " ~" ) ,
139
133
) ;
140
134
}
141
135
@@ -145,13 +139,13 @@ mod tests {
145
139
"./test.ts" ,
146
140
concat ! (
147
141
"test;\n " ,
148
- r#"console.log("x" , `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;"# ,
142
+ r#"console.log('x' , `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;"# ,
149
143
) ,
150
144
concat ! (
151
- "Line 2, column 81: Expression expected \n " ,
145
+ "Expression expected at ./test.ts:2:81 \n " ,
152
146
"\n " ,
153
- " && ${max} ≥ ${d}`),;\n " ,
154
- " ~"
147
+ " console.log('x', `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;\n " ,
148
+ " ~" ,
155
149
) ,
156
150
) ;
157
151
}
@@ -166,7 +160,7 @@ mod tests {
166
160
run_non_fatal_diagnostic_test (
167
161
"./test.ts" ,
168
162
"const Methods {\n f: (x, y) => x + y,\n };" ,
169
- concat ! ( "Line 1, column 15: Expected a semicolon\n " , "\n " , " const Methods {\n " , " ~" ) ,
163
+ concat ! ( "Expected a semicolon at ./test.ts:1:15 \n " , "\n " , " const Methods {\n " , " ~" ) ,
170
164
) ;
171
165
}
172
166
@@ -176,7 +170,7 @@ mod tests {
176
170
"./test.ts" ,
177
171
"let a = 0, let b = 1;" ,
178
172
concat ! (
179
- "Line 1, column 16: Expected a semicolon\n " ,
173
+ "Expected a semicolon at ./test.ts:1:16 \n " ,
180
174
"\n " ,
181
175
" let a = 0, let b = 1;\n " ,
182
176
" ~"
@@ -189,7 +183,7 @@ mod tests {
189
183
run_non_fatal_diagnostic_test (
190
184
"./test.ts" ,
191
185
"type T =\n | unknown\n { } & unknown;" ,
192
- concat ! ( "Line 3, column 7: Expression expected \n \n " , " { } & unknown;\n " , " ~" ) ,
186
+ concat ! ( "Expression expected at ./test.ts:3:7 \n \n " , " { } & unknown;\n " , " ~" ) ,
193
187
) ;
194
188
}
195
189
@@ -201,13 +195,13 @@ mod tests {
201
195
run_non_fatal_diagnostic_test (
202
196
"./test.ts" ,
203
197
"class Test {" ,
204
- concat ! ( "Line 1, column 12: Expected '}', got '<eof>'\n \n " , " class Test {\n " , " ~" ) ,
198
+ concat ! ( "Expected '}', got '<eof>' at ./test.ts:1:12 \n \n " , " class Test {\n " , " ~" ) ,
205
199
) ;
206
200
}
207
201
208
202
fn run_non_fatal_diagnostic_test ( file_path : & str , text : & str , expected : & str ) {
209
203
let file_path = PathBuf :: from ( file_path) ;
210
- assert_eq ! ( parse_swc_ast( & file_path, text) . err( ) . unwrap( ) . to_string ( ) , expected) ;
204
+ assert_eq ! ( format! ( "{}" , parse_swc_ast( & file_path, text) . err( ) . unwrap( ) ) , expected) ;
211
205
212
206
// this error should also be surfaced in `format_parsed_source` if someone provides
213
207
// a source file that had a non-fatal diagnostic
0 commit comments