@@ -9,8 +9,12 @@ use rome_console::{
9
9
markup, Markup ,
10
10
} ;
11
11
use rome_diagnostics:: { file:: SimpleFile , termcolor:: NoColor , Diagnostic } ;
12
- use rome_js_parser:: parse;
13
- use rome_rowan:: Language ;
12
+ use rome_js_parser:: {
13
+ parse,
14
+ test_utils:: { assert_errors_are_absent, has_unknown_nodes_or_empty_slots} ,
15
+ } ;
16
+ use rome_js_syntax:: { JsLanguage , SourceType } ;
17
+ use rome_rowan:: AstNode ;
14
18
use rome_text_edit:: apply_indels;
15
19
16
20
tests_macros:: gen_tests! { "tests/specs/**/*.{cjs,js,jsx,tsx,ts}" , crate :: run_test, "module" }
@@ -40,6 +44,7 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
40
44
rome_js_analyze:: analyze ( 0 , & root, filter, |event| {
41
45
if let Some ( mut diag) = event. diagnostic ( ) {
42
46
if let Some ( action) = event. action ( ) {
47
+ check_code_action ( input_file, & input_code, source_type, & action) ;
43
48
diag. suggestions . push ( action. into ( ) ) ;
44
49
}
45
50
@@ -48,6 +53,7 @@ fn run_test(input: &'static str, _: &str, _: &str, _: &str) {
48
53
}
49
54
50
55
if let Some ( action) = event. action ( ) {
56
+ check_code_action ( input_file, & input_code, source_type, & action) ;
51
57
code_fixes. push ( code_fix_to_string ( & input_code, action) ) ;
52
58
}
53
59
@@ -130,10 +136,38 @@ fn diagnostic_to_string(name: &str, source: &str, diag: Diagnostic) -> String {
130
136
text
131
137
}
132
138
133
- fn code_fix_to_string < L > ( source : & str , action : AnalyzerAction < L > ) -> String
134
- where
135
- L : Language ,
136
- {
139
+ fn check_code_action (
140
+ path : & Path ,
141
+ source : & str ,
142
+ source_type : SourceType ,
143
+ action : & AnalyzerAction < JsLanguage > ,
144
+ ) {
145
+ let mut output = source. to_string ( ) ;
146
+ let ( _, indels) = action. mutation . as_text_edits ( ) . unwrap_or_default ( ) ;
147
+
148
+ apply_indels ( & indels, & mut output) ;
149
+
150
+ let new_tree = action. mutation . clone ( ) . commit ( ) ;
151
+
152
+ // Checks that applying the text edits returned by the BatchMutation
153
+ // returns the same code as printing the modified syntax tree
154
+ assert_eq ! ( new_tree. to_string( ) , output) ;
155
+
156
+ if has_unknown_nodes_or_empty_slots ( new_tree. syntax ( ) ) {
157
+ panic ! ( "modified tree has unknown nodes or empty slots:\n {new_tree:#?}" )
158
+ }
159
+
160
+ // Checks the returned tree contains no missing children node
161
+ if format ! ( "{new_tree:?}" ) . contains ( "missing (required)" ) {
162
+ panic ! ( "modified tree has missing children:\n {new_tree:#?}" )
163
+ }
164
+
165
+ // Re-parse the modified code and panic if the resulting tree has syntax errors
166
+ let re_parse = parse ( & output, 0 , source_type) ;
167
+ assert_errors_are_absent ( & re_parse, path) ;
168
+ }
169
+
170
+ fn code_fix_to_string ( source : & str , action : AnalyzerAction < JsLanguage > ) -> String {
137
171
let mut output = source. to_string ( ) ;
138
172
let ( _, indels) = action. mutation . as_text_edits ( ) . unwrap_or_default ( ) ;
139
173
0 commit comments