Move walk handlers out of lint (#2822)
I want to make it more useful and generally applicable. I think in the future we'll need a &mut variant, or an in-place tree replacer.
This commit is contained in:
		@ -159,7 +159,7 @@ impl Program {
 | 
			
		||||
        RuleT: crate::lint::rule::Rule<'a>,
 | 
			
		||||
    {
 | 
			
		||||
        let v = Arc::new(Mutex::new(vec![]));
 | 
			
		||||
        crate::lint::walk(self, &|node: crate::lint::Node<'a>| {
 | 
			
		||||
        crate::walk::walk(self, &|node: crate::walk::Node<'a>| {
 | 
			
		||||
            let mut findings = v.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
            findings.append(&mut rule.check(node)?);
 | 
			
		||||
            Ok(true)
 | 
			
		||||
@ -171,13 +171,13 @@ impl Program {
 | 
			
		||||
    /// Walk the ast and get all the variables and tags as completion items.
 | 
			
		||||
    pub fn completion_items<'a>(&'a self) -> Result<Vec<CompletionItem>> {
 | 
			
		||||
        let completions = Arc::new(Mutex::new(vec![]));
 | 
			
		||||
        crate::lint::walk(self, &|node: crate::lint::Node<'a>| {
 | 
			
		||||
        crate::walk::walk(self, &|node: crate::walk::Node<'a>| {
 | 
			
		||||
            let mut findings = completions.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
            match node {
 | 
			
		||||
                crate::lint::Node::TagDeclarator(tag) => {
 | 
			
		||||
                crate::walk::Node::TagDeclarator(tag) => {
 | 
			
		||||
                    findings.push(tag.into());
 | 
			
		||||
                }
 | 
			
		||||
                crate::lint::Node::VariableDeclaration(variable) => {
 | 
			
		||||
                crate::walk::Node::VariableDeclaration(variable) => {
 | 
			
		||||
                    findings.extend::<Vec<CompletionItem>>(variable.into());
 | 
			
		||||
                }
 | 
			
		||||
                _ => {}
 | 
			
		||||
@ -255,13 +255,13 @@ impl Program {
 | 
			
		||||
    /// Returns all the lsp symbols in the program.
 | 
			
		||||
    pub fn get_lsp_symbols<'a>(&'a self, code: &str) -> Result<Vec<DocumentSymbol>> {
 | 
			
		||||
        let symbols = Arc::new(Mutex::new(vec![]));
 | 
			
		||||
        crate::lint::walk(self, &|node: crate::lint::Node<'a>| {
 | 
			
		||||
        crate::walk::walk(self, &|node: crate::walk::Node<'a>| {
 | 
			
		||||
            let mut findings = symbols.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
            match node {
 | 
			
		||||
                crate::lint::Node::TagDeclarator(tag) => {
 | 
			
		||||
                crate::walk::Node::TagDeclarator(tag) => {
 | 
			
		||||
                    findings.extend::<Vec<DocumentSymbol>>(tag.get_lsp_symbols(code));
 | 
			
		||||
                }
 | 
			
		||||
                crate::lint::Node::VariableDeclaration(variable) => {
 | 
			
		||||
                crate::walk::Node::VariableDeclaration(variable) => {
 | 
			
		||||
                    findings.extend::<Vec<DocumentSymbol>>(variable.get_lsp_symbols(code));
 | 
			
		||||
                }
 | 
			
		||||
                _ => {}
 | 
			
		||||
 | 
			
		||||
@ -28,5 +28,6 @@ pub mod std;
 | 
			
		||||
pub mod test_server;
 | 
			
		||||
pub mod thread;
 | 
			
		||||
pub mod token;
 | 
			
		||||
pub mod walk;
 | 
			
		||||
#[cfg(target_arch = "wasm32")]
 | 
			
		||||
pub mod wasm;
 | 
			
		||||
 | 
			
		||||
@ -3,10 +3,8 @@ use anyhow::Result;
 | 
			
		||||
use crate::{
 | 
			
		||||
    ast::types::VariableDeclarator,
 | 
			
		||||
    executor::SourceRange,
 | 
			
		||||
    lint::{
 | 
			
		||||
        rule::{def_finding, Discovered, Finding},
 | 
			
		||||
        Node,
 | 
			
		||||
    },
 | 
			
		||||
    lint::rule::{def_finding, Discovered, Finding},
 | 
			
		||||
    walk::Node,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
def_finding!(
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,4 @@
 | 
			
		||||
mod ast_node;
 | 
			
		||||
mod ast_walk;
 | 
			
		||||
pub mod checks;
 | 
			
		||||
pub mod rule;
 | 
			
		||||
 | 
			
		||||
pub use ast_node::Node;
 | 
			
		||||
pub use ast_walk::walk;
 | 
			
		||||
// pub(crate) use rule::{def_finding, finding};
 | 
			
		||||
pub use rule::{Discovered, Finding};
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,7 @@ use schemars::JsonSchema;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity};
 | 
			
		||||
 | 
			
		||||
use crate::{executor::SourceRange, lint::Node, lsp::IntoDiagnostic};
 | 
			
		||||
use crate::{executor::SourceRange, lsp::IntoDiagnostic, walk::Node};
 | 
			
		||||
 | 
			
		||||
/// Check the provided AST for any found rule violations.
 | 
			
		||||
///
 | 
			
		||||
 | 
			
		||||
@ -374,7 +374,7 @@ impl Backend {
 | 
			
		||||
            let token_modifiers_bitset: u32 = if let Some(ast) = self.ast_map.get(¶ms.uri.to_string()).await {
 | 
			
		||||
                let token_index = Arc::new(Mutex::new(token_type_index));
 | 
			
		||||
                let modifier_index: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
 | 
			
		||||
                crate::lint::walk(&ast, &|node: crate::lint::Node| {
 | 
			
		||||
                crate::walk::walk(&ast, &|node: crate::walk::Node| {
 | 
			
		||||
                    let node_range: SourceRange = (&node).into();
 | 
			
		||||
                    if !node_range.contains(source_range.start()) {
 | 
			
		||||
                        return Ok(true);
 | 
			
		||||
@ -394,10 +394,10 @@ impl Backend {
 | 
			
		||||
                    };
 | 
			
		||||
 | 
			
		||||
                    match node {
 | 
			
		||||
                        crate::lint::Node::TagDeclarator(_) => {
 | 
			
		||||
                        crate::walk::Node::TagDeclarator(_) => {
 | 
			
		||||
                            return get_modifier(SemanticTokenModifier::DEFINITION);
 | 
			
		||||
                        }
 | 
			
		||||
                        crate::lint::Node::VariableDeclarator(variable) => {
 | 
			
		||||
                        crate::walk::Node::VariableDeclarator(variable) => {
 | 
			
		||||
                            let sr: SourceRange = variable.id.clone().into();
 | 
			
		||||
                            if sr.contains(source_range.start()) {
 | 
			
		||||
                                if let Value::FunctionExpression(_) = &variable.init {
 | 
			
		||||
@ -411,7 +411,7 @@ impl Backend {
 | 
			
		||||
                                return get_modifier(SemanticTokenModifier::DECLARATION);
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        crate::lint::Node::Parameter(_) => {
 | 
			
		||||
                        crate::walk::Node::Parameter(_) => {
 | 
			
		||||
                            let mut ti = token_index.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
                            *ti = match self.get_semantic_token_type_index(SemanticTokenType::PARAMETER) {
 | 
			
		||||
                                Some(index) => index,
 | 
			
		||||
@ -419,7 +419,7 @@ impl Backend {
 | 
			
		||||
                            };
 | 
			
		||||
                            return Ok(false);
 | 
			
		||||
                        }
 | 
			
		||||
                        crate::lint::Node::MemberExpression(member_expression) => {
 | 
			
		||||
                        crate::walk::Node::MemberExpression(member_expression) => {
 | 
			
		||||
                            let sr: SourceRange = member_expression.property.clone().into();
 | 
			
		||||
                            if sr.contains(source_range.start()) {
 | 
			
		||||
                                let mut ti = token_index.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
@ -430,7 +430,7 @@ impl Backend {
 | 
			
		||||
                                return Ok(false);
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        crate::lint::Node::ObjectProperty(object_property) => {
 | 
			
		||||
                        crate::walk::Node::ObjectProperty(object_property) => {
 | 
			
		||||
                            let sr: SourceRange = object_property.key.clone().into();
 | 
			
		||||
                            if sr.contains(source_range.start()) {
 | 
			
		||||
                                let mut ti = token_index.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
@ -441,7 +441,7 @@ impl Backend {
 | 
			
		||||
                            }
 | 
			
		||||
                            return get_modifier(SemanticTokenModifier::DECLARATION);
 | 
			
		||||
                        }
 | 
			
		||||
                        crate::lint::Node::CallExpression(call_expr) => {
 | 
			
		||||
                        crate::walk::Node::CallExpression(call_expr) => {
 | 
			
		||||
                            let sr: SourceRange = call_expr.callee.clone().into();
 | 
			
		||||
                            if sr.contains(source_range.start()) {
 | 
			
		||||
                                let mut ti = token_index.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@ use crate::{
 | 
			
		||||
        BinaryPart, BodyItem, LiteralIdentifier, MemberExpression, MemberObject, ObjectExpression, ObjectProperty,
 | 
			
		||||
        Parameter, Program, UnaryExpression, Value, VariableDeclarator,
 | 
			
		||||
    },
 | 
			
		||||
    lint::Node,
 | 
			
		||||
    walk::Node,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Walker is implemented by things that are able to walk an AST tree to
 | 
			
		||||
							
								
								
									
										5
									
								
								src/wasm-lib/kcl/src/walk/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/wasm-lib/kcl/src/walk/mod.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
mod ast_node;
 | 
			
		||||
mod ast_walk;
 | 
			
		||||
 | 
			
		||||
pub use ast_node::Node;
 | 
			
		||||
pub use ast_walk::walk;
 | 
			
		||||
		Reference in New Issue
	
	Block a user