Skip to content

Commit 2b31521

Browse files
fix: fix wasm doc issue
1 parent cb1611b commit 2b31521

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use dump_tree::{dump_one_node, DumpNode, dump_pattern as dump_pattern_impl};
77
use utils::WasmMatch;
88

99
use ast_grep_config::{RuleConfig, SerializableRuleConfig, CombinedScan};
10-
use ast_grep_core::language::Language;
1110
use ast_grep_core::{AstGrep, Node as SgNode};
1211
use serde_wasm_bindgen::from_value as from_js_val;
1312
use std::collections::HashMap;
@@ -39,7 +38,8 @@ pub fn find_nodes(src: String, configs: Vec<JsValue>) -> Result<JsValue, JsError
3938
rules.push(finder);
4039
}
4140
let combined = CombinedScan::new(rules.iter().collect());
42-
let root = lang.ast_grep(src);
41+
let doc = WasmDoc::new(src.clone(), lang);
42+
let root = AstGrep::doc(doc);
4343
let sets = combined.find(&root);
4444
let ret: HashMap<_, _> = combined.scan(&root, sets, false).matches.into_iter().map(|(id, matches)| {
4545
let rule = combined.get_rule(id);

src/utils.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::wasm_lang::WasmLang;
1+
use crate::wasm_lang::{WasmLang, WasmDoc};
22
use 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};
78
use serde::{Deserialize, Serialize};
89
use 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)]
2526
pub 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+
3847
impl 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

Comments
 (0)