1
- use crate :: wasm_lang:: WasmLang ;
1
+ use crate :: wasm_lang:: { WasmLang , WasmDoc } ;
2
2
use ast_grep_core:: {
3
3
meta_var:: { MetaVarEnv , MetaVariable } ,
4
- Node as SgNode , NodeMatch as SgNodeMatch , StrDoc ,
4
+ Node as SgNode , NodeMatch as SgNodeMatch ,
5
+ replacer:: Replacer ,
5
6
} ;
6
- use ast_grep_config:: RuleConfig ;
7
+ use ast_grep_config:: { RuleConfig , Fixer } ;
7
8
use serde:: { Deserialize , Serialize } ;
8
9
use std:: collections:: BTreeMap ;
9
10
@@ -18,8 +19,8 @@ pub fn set_panic_hook() {
18
19
console_error_panic_hook:: set_once ( ) ;
19
20
}
20
21
21
- type Node < ' a > = SgNode < ' a , StrDoc < WasmLang > > ;
22
- type NodeMatch < ' a > = SgNodeMatch < ' a , StrDoc < WasmLang > > ;
22
+ type Node < ' a > = SgNode < ' a , WasmDoc > ;
23
+ type NodeMatch < ' a > = SgNodeMatch < ' a , WasmDoc > ;
23
24
24
25
#[ derive( Serialize , Deserialize ) ]
25
26
pub struct WasmNode {
@@ -35,19 +36,27 @@ pub struct WasmMatch {
35
36
pub message : String ,
36
37
}
37
38
39
+
40
+ // TODO: move to ast-grep-core
41
+ fn get_message ( rule : & RuleConfig < WasmLang > , node : & NodeMatch ) -> String {
42
+ let parsed = Fixer :: from_str ( & rule. message , & rule. language ) . expect ( "should work" ) ;
43
+ let bytes = parsed. generate_replacement ( node) ;
44
+ bytes. into_iter ( ) . collect ( )
45
+ }
46
+
38
47
impl WasmMatch {
39
48
pub fn from_match ( nm : NodeMatch , rule : & RuleConfig < WasmLang > ) -> Self {
40
49
let node = nm. get_node ( ) . clone ( ) ;
41
50
let id = node. node_id ( ) ;
42
51
let node = WasmNode :: from ( node) ;
43
52
let env = nm. get_env ( ) . clone ( ) ;
44
53
let env = env_to_map ( env) ;
45
- let message = rule . get_message ( & nm) ;
54
+ let message = get_message ( rule , & nm) ;
46
55
Self { node, env, message, id }
47
56
}
48
57
}
49
58
50
- fn env_to_map ( env : MetaVarEnv < ' _ , StrDoc < WasmLang > > ) -> BTreeMap < String , WasmNode > {
59
+ fn env_to_map ( env : MetaVarEnv < ' _ , WasmDoc > ) -> BTreeMap < String , WasmNode > {
51
60
let mut map = BTreeMap :: new ( ) ;
52
61
for id in env. get_matched_variables ( ) {
53
62
match id {
@@ -56,7 +65,7 @@ fn env_to_map(env: MetaVarEnv<'_, StrDoc<WasmLang>>) -> BTreeMap<String, WasmNod
56
65
map. insert ( name, WasmNode :: from ( node. clone ( ) ) ) ;
57
66
} else if let Some ( bytes) = env. get_transformed ( & name) {
58
67
let node = WasmNode {
59
- text : String :: from_utf8_lossy ( bytes) . to_string ( ) ,
68
+ text : bytes. iter ( ) . collect ( ) ,
60
69
range : ( 0 , 0 , 0 , 0 ) ,
61
70
} ;
62
71
map. insert ( name, WasmNode :: from ( node) ) ;
0 commit comments