修复: 变量定义判定误伤函数调用值
子树包含判定改直取 value 节点 kind const x = arr.map(...) 不再误判为符号定义 subtree_contains_kind 无其他使用方一并删除
This commit is contained in:
@@ -131,8 +131,9 @@ const FUNCTION_VALUE_KINDS: &[&str] = &["arrow_function", "function", "generator
|
||||
///
|
||||
/// variable_declarator 单看 kind 过宽:`let msg = "fn main() {}"` 这类普通声明也命中,
|
||||
/// contains_definition 会对几乎任何 JS/TS 文件误报提示。收窄为 `const fn = () => {}`
|
||||
/// 形态(初始化值是函数/类表达式)才算定义,contains_definition / read_symbol /
|
||||
/// outline 三处共用同一判定。
|
||||
/// 形态(初始化值本身是函数/类表达式)才算定义,contains_definition / read_symbol /
|
||||
/// outline 三处共用同一判定。判定看 value 节点自身 kind 而非子树包含——
|
||||
/// `const x = arr.map(n => ...)` 子树含 arrow_function,但 x 不是函数定义。
|
||||
fn is_definition_node(node: &Node<'_>) -> bool {
|
||||
if !DEFINITION_KINDS.contains(&node.kind()) {
|
||||
return false;
|
||||
@@ -141,25 +142,11 @@ fn is_definition_node(node: &Node<'_>) -> bool {
|
||||
return true;
|
||||
}
|
||||
match node.child_by_field_name("value") {
|
||||
Some(v) => subtree_contains_kind(v, FUNCTION_VALUE_KINDS),
|
||||
Some(v) => FUNCTION_VALUE_KINDS.contains(&v.kind()),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// 子树内是否含任一目标 kind 节点
|
||||
fn subtree_contains_kind(node: Node<'_>, kinds: &[&str]) -> bool {
|
||||
if kinds.contains(&node.kind()) {
|
||||
return true;
|
||||
}
|
||||
let mut cursor = node.walk();
|
||||
for child in node.children(&mut cursor) {
|
||||
if subtree_contains_kind(child, kinds) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// 递归遍历语法树,判断是否含任一定义节点(contains_definition 的遍历内核)。
|
||||
fn tree_has_definition(node: &Node<'_>) -> bool {
|
||||
if is_definition_node(node) {
|
||||
|
||||
Reference in New Issue
Block a user