1- use crate :: wasm_lang:: WasmLang ;
1+ use crate :: wasm_lang:: { WasmLang , WasmDoc } ;
22use ast_grep_core:: {
33 meta_var:: { MetaVarEnv , MetaVariable } ,
4- Node as SgNode , NodeMatch as SgNodeMatch , StrDoc ,
4+ Node as SgNode , NodeMatch as SgNodeMatch ,
5+ replacer:: Replacer ,
56} ;
6- use ast_grep_config:: RuleConfig ;
7+ use ast_grep_config:: { RuleConfig , Fixer } ;
78use serde:: { Deserialize , Serialize } ;
89use std:: collections:: BTreeMap ;
910
@@ -18,8 +19,8 @@ pub fn set_panic_hook() {
1819 console_error_panic_hook:: set_once ( ) ;
1920}
2021
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 > ;
2324
2425#[ derive( Serialize , Deserialize ) ]
2526pub struct WasmNode {
@@ -35,19 +36,27 @@ pub struct WasmMatch {
3536 pub message : String ,
3637}
3738
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+
3847impl WasmMatch {
3948 pub fn from_match ( nm : NodeMatch , rule : & RuleConfig < WasmLang > ) -> Self {
4049 let node = nm. get_node ( ) . clone ( ) ;
4150 let id = node. node_id ( ) ;
4251 let node = WasmNode :: from ( node) ;
4352 let env = nm. get_env ( ) . clone ( ) ;
4453 let env = env_to_map ( env) ;
45- let message = rule . get_message ( & nm) ;
54+ let message = get_message ( rule , & nm) ;
4655 Self { node, env, message, id }
4756 }
4857}
4958
50- fn env_to_map ( env : MetaVarEnv < ' _ , StrDoc < WasmLang > > ) -> BTreeMap < String , WasmNode > {
59+ fn env_to_map ( env : MetaVarEnv < ' _ , WasmDoc > ) -> BTreeMap < String , WasmNode > {
5160 let mut map = BTreeMap :: new ( ) ;
5261 for id in env. get_matched_variables ( ) {
5362 match id {
@@ -56,7 +65,7 @@ fn env_to_map(env: MetaVarEnv<'_, StrDoc<WasmLang>>) -> BTreeMap<String, WasmNod
5665 map. insert ( name, WasmNode :: from ( node. clone ( ) ) ) ;
5766 } else if let Some ( bytes) = env. get_transformed ( & name) {
5867 let node = WasmNode {
59- text : String :: from_utf8_lossy ( bytes) . to_string ( ) ,
68+ text : bytes. iter ( ) . collect ( ) ,
6069 range : ( 0 , 0 , 0 , 0 ) ,
6170 } ;
6271 map. insert ( name, WasmNode :: from ( node) ) ;
0 commit comments