diff --git a/src/check_cst.rs b/src/check_cst.rs index 54832ee292..72adfff0fc 100644 --- a/src/check_cst.rs +++ b/src/check_cst.rs @@ -1,22 +1,11 @@ use std::borrow::Borrow; use std::collections::BTreeMap; +use std::sync::Arc; use bat::PrettyPrinter; use bumpalo::Bump; use libcst_native::{ - AnnAssign, Annotation, Arg, AsName, Assert, Assign, AssignEqual, AssignTarget, - AssignTargetExpression, Asynchronous, Attribute, AugAssign, Await, BinaryOp, BinaryOperation, - BooleanOp, BooleanOperation, Break, Call, ClassDef, Codegen, CompFor, CompIf, CompOp, - Comparison, ComparisonTarget, CompoundStatement, ConcatenatedString, Continue, Decorator, Del, - DelTargetExpression, Dict, DictComp, DictElement, Element, Ellipsis, Else, ExceptHandler, - ExceptStarHandler, Expr, Expression, Finally, Float, For, FormattedString, - FormattedStringContent, FormattedStringExpression, FormattedStringText, FunctionDef, - GeneratorExp, Global, If, IfExp, Imaginary, Import, ImportAlias, ImportFrom, ImportStar, - IndentedBlock, Index, Integer, Lambda, List, ListComp, Match, Module, Name, NameItem, - NamedExpr, Nonlocal, OrElse, Param, ParamStar, Parameters, Pass, Raise, Return, Set, SetComp, - SimpleStatementLine, SimpleStatementSuite, SimpleString, Slice, SmallStatement, - StarredDictElement, StarredElement, Statement, Subscript, SubscriptElement, Try, TryStar, - Tuple, UnaryOp, UnaryOperation, While, With, WithItem, Yield, YieldValue, + Arg, ClassDef, Codegen, Expression, FormattedStringContent, If, Module, SimpleString, }; use rustpython_parser::ast::Location; @@ -56,44 +45,32 @@ struct Binding { } struct Checker<'a> { - bump: Bump, settings: &'a Settings, checks: Vec, + arena: Vec, } impl Checker<'_> { pub fn new(settings: &Settings) -> Checker { Checker { - bump: Bump::new(), settings, checks: vec![], + arena: vec![], } } } -impl CSTVisitor for Checker<'_> { - fn visit_If(&mut self, node: &If) { - if let Expression::Tuple { .. } = node.test { - self.checks.push(Check { - kind: CheckKind::IfTuple, - location: Default::default(), - }); - } - cst_visitor::walk_If(self, node); - } +const QUOTE: &str = "\""; - fn visit_Expression<'a, 'b>(&'b mut self, node: &'a Expression<'a>) -> Expression<'a> - where - 'b: 'a, - { +impl<'b> CSTVisitor for Checker<'_> { + fn visit_Expression<'a>(&mut self, node: &'a Expression<'a>) -> Expression<'a> { match node { Expression::FormattedString(node) => match &node.parts[..] { [node] => match node { FormattedStringContent::Text(node) => { - let x = node.value.to_string(); - println!("Found: {:?}", node); + self.arena.push(format!("\"{}\"", node.value)); return Expression::SimpleString(Box::new(SimpleString { - value: self.bump.alloc(format!("\"{}\"", x)), + value: node.value, lpar: vec![], rpar: vec![], })); @@ -133,6 +110,16 @@ impl CSTVisitor for Checker<'_> { transformed.bases = bases; transformed } + + fn visit_If(&mut self, node: &If) { + if let Expression::Tuple { .. } = node.test { + self.checks.push(Check { + kind: CheckKind::IfTuple, + location: Default::default(), + }); + } + cst_visitor::walk_If(self, node); + } } pub fn check_cst<'a>(python_cst: &'a Module<'a>, settings: &Settings) -> Vec { diff --git a/src/cst_visitor.rs b/src/cst_visitor.rs index e1221d080e..9de07c2276 100644 --- a/src/cst_visitor.rs +++ b/src/cst_visitor.rs @@ -1,5 +1,6 @@ #![allow(non_snake_case)] +use bumpalo::Bump; use libcst_native::{ AnnAssign, Annotation, Arg, AsName, Assert, Assign, AssignEqual, AssignTarget, AssignTargetExpression, Asynchronous, Attribute, AugAssign, Await, BaseSlice, BinaryOp, @@ -18,55 +19,31 @@ use libcst_native::{ }; pub trait CSTVisitor { - fn visit_Module<'a, 'b>(&'b mut self, node: &'a Module<'a>) -> Module<'a> - where - 'b: 'a, - { + fn visit_Module<'a>(&mut self, node: &'a Module<'a>) -> Module<'a> { walk_Module(self, node) } - fn visit_Statement<'a, 'b>(&'b mut self, node: &'a Statement<'a>) -> Option> - where - 'b: 'a, - { + fn visit_Statement<'a>(&mut self, node: &'a Statement<'a>) -> Option> { walk_Statement(self, node) } - fn visit_SimpleStatementLine<'a, 'b>( - &'b mut self, + fn visit_SimpleStatementLine<'a>( + &mut self, node: &'a SimpleStatementLine<'a>, - ) -> Option> - where - 'b: 'a, - { + ) -> Option> { walk_SimpleStatementLine(self, node) } - fn visit_CompoundStatement<'a, 'b>( - &'b mut self, + fn visit_CompoundStatement<'a>( + &mut self, node: &'a CompoundStatement<'a>, - ) -> Option> - where - 'b: 'a, - { + ) -> Option> { walk_CompoundStatement(self, node) } - fn visit_SmallStatement<'a, 'b>( - &'b mut self, - node: &'a SmallStatement<'a>, - ) -> SmallStatement<'a> - where - 'b: 'a, - { + fn visit_SmallStatement<'a>(&mut self, node: &'a SmallStatement<'a>) -> SmallStatement<'a> { walk_SmallStatement(self, node) } - fn visit_Expression<'a, 'b>(&'b mut self, node: &'a Expression<'a>) -> Expression<'a> - where - 'b: 'a, - { + fn visit_Expression<'a>(&mut self, node: &'a Expression<'a>) -> Expression<'a> { walk_Expression(self, node) } - fn visit_AnnAssign<'a, 'b>(&'b mut self, node: &'a AnnAssign<'a>) -> AnnAssign<'a> - where - 'b: 'a, - { + fn visit_AnnAssign<'a>(&mut self, node: &'a AnnAssign<'a>) -> AnnAssign<'a> { walk_AnnAssign(self, node) } fn visit_Annotation(&mut self, node: &Annotation) { @@ -105,20 +82,11 @@ pub trait CSTVisitor { fn visit_Await(&mut self, node: &Await) { walk_Await(self, node); } - fn visit_BinaryOperation<'a, 'b>( - &'b mut self, - node: &'a BinaryOperation<'a>, - ) -> BinaryOperation<'a> - where - 'b: 'a, - { + fn visit_BinaryOperation<'a>(&mut self, node: &'a BinaryOperation<'a>) -> BinaryOperation<'a> { walk_BinaryOperation(self, node) } - fn visit_BinaryOp<'a, 'b>(&'b mut self, node: &'a BinaryOp<'a>) -> BinaryOp<'a> - where - 'b: 'a, - { - walk_BinaryOp(self, node) + fn visit_BinaryOp(&mut self, node: &BinaryOp) { + walk_BinaryOp(self, node); } fn visit_BooleanOperation(&mut self, node: &BooleanOperation) { walk_BooleanOperation(self, node); @@ -374,14 +342,11 @@ pub trait CSTVisitor { } } -pub fn walk_Module<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, +pub fn walk_Module<'a, V: CSTVisitor + ?Sized>( + visitor: &mut V, node: &'a Module<'a>, -) -> Module<'a> -where - 'b: 'a, -{ - let mut body: Vec> = vec![]; +) -> Module<'a> { + let mut body: Vec = vec![]; for node in &node.body { if let Some(node) = visitor.visit_Statement(node) { body.push(node) @@ -392,13 +357,10 @@ where transformed.body = body; transformed } -pub fn walk_Statement<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, +pub fn walk_Statement<'a, V: CSTVisitor + ?Sized>( + visitor: &mut V, node: &'a Statement<'a>, -) -> Option> -where - 'b: 'a, -{ +) -> Option> { match node { Statement::Simple(node) => visitor .visit_SimpleStatementLine(node) @@ -408,14 +370,11 @@ where .map(Statement::Compound), } } -pub fn walk_SimpleStatementLine<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, +pub fn walk_SimpleStatementLine<'a, V: CSTVisitor + ?Sized>( + visitor: &mut V, node: &'a SimpleStatementLine<'a>, -) -> Option> -where - 'b: 'a, -{ - println!("{:?}", node); +) -> Option> { + // println!("{:?}", node); let mut body: Vec = vec![]; for node in &node.body { body.push(visitor.visit_SmallStatement(node).clone()); @@ -424,13 +383,10 @@ where transformed.body = body; Some(transformed) } -pub fn walk_CompoundStatement<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, +pub fn walk_CompoundStatement<'a, V: CSTVisitor + ?Sized>( + visitor: &mut V, node: &'a CompoundStatement<'a>, -) -> Option> -where - 'b: 'a, -{ +) -> Option> { match node { CompoundStatement::If(node) => { visitor.visit_If(node); @@ -450,13 +406,7 @@ where Some(node.clone()) } -pub fn walk_SmallStatement<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, - node: &'a SmallStatement<'a>, -) -> SmallStatement<'a> -where - 'b: 'a, -{ +pub fn walk_SmallStatement<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a SmallStatement<'a>) -> SmallStatement<'a> { match node { SmallStatement::Pass(node) => visitor.visit_Pass(node), SmallStatement::Break(node) => visitor.visit_Break(node), @@ -467,9 +417,7 @@ where SmallStatement::Import(node) => visitor.visit_Import(node), SmallStatement::ImportFrom(node) => visitor.visit_ImportFrom(node), SmallStatement::Assign(node) => visitor.visit_Assign(node), - SmallStatement::AnnAssign(node) => { - return SmallStatement::AnnAssign(visitor.visit_AnnAssign(node)); - } + SmallStatement::AnnAssign(node) => { return SmallStatement::AnnAssign(visitor.visit_AnnAssign(node)); }, SmallStatement::Raise(node) => visitor.visit_Raise(node), SmallStatement::Global(node) => visitor.visit_Global(node), SmallStatement::Nonlocal(node) => visitor.visit_Nonlocal(node), @@ -479,8 +427,8 @@ where node.clone() } -pub fn walk_Expression<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, +pub fn walk_Expression<'a, V: CSTVisitor + ?Sized>( + visitor: &mut V, node: &'a Expression<'a>, ) -> Expression<'a> { match node { @@ -491,9 +439,7 @@ pub fn walk_Expression<'a, 'b, V: CSTVisitor + ?Sized>( Expression::Imaginary(node) => visitor.visit_Imaginary(node), Expression::Comparison(node) => visitor.visit_Comparison(node), Expression::UnaryOperation(node) => visitor.visit_UnaryOperation(node), - Expression::BinaryOperation(node) => { - return Expression::BinaryOperation(Box::new(visitor.visit_BinaryOperation(node))) - } + Expression::BinaryOperation(node) => return Expression::BinaryOperation(Box::new(visitor.visit_BinaryOperation(node))), Expression::BooleanOperation(node) => visitor.visit_BooleanOperation(node), Expression::Attribute(node) => visitor.visit_Attribute(node), Expression::Tuple(node) => visitor.visit_Tuple(node), @@ -538,20 +484,14 @@ pub fn walk_AssignTargetExpression( AssignTargetExpression::Subscript(node) => visitor.visit_Subscript(node), } } -pub fn walk_AnnAssign<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, - node: &'a AnnAssign<'a>, -) -> AnnAssign<'a> -where - 'b: 'a, -{ - println!("walk_AnnAssign"); +pub fn walk_AnnAssign<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a AnnAssign<'a>) -> AnnAssign<'a> { + // println!("walk_AnnAssign"); let mut transformed: AnnAssign<'a> = node.clone(); visitor.visit_AssignTargetExpression(&node.target); if let Some(node) = &node.value { - println!("Before: {:?}", node); + // println!("Before: {:?}", node); transformed.value = Some(visitor.visit_Expression(node)); - println!("After: {:?}", transformed.value); + // println!("After: {:?}", transformed.value); } transformed } @@ -595,13 +535,7 @@ pub fn walk_AugAssign(visitor: &mut V, node: &AugAssign) pub fn walk_Await(visitor: &mut V, node: &Await) { visitor.visit_Expression(&node.expression); } -pub fn walk_BinaryOperation<'a, 'b, V: CSTVisitor + ?Sized>( - visitor: &'b mut V, - node: &'a BinaryOperation<'a>, -) -> BinaryOperation<'a> -where - 'b: 'a, -{ +pub fn walk_BinaryOperation<'a, V: CSTVisitor + ?Sized>(visitor: &mut V, node: &'a BinaryOperation<'a>) -> BinaryOperation<'a> { let mut transformed: BinaryOperation = node.clone(); transformed.left = Box::new(visitor.visit_Expression(&node.left)); @@ -610,9 +544,8 @@ where transformed } -pub fn walk_BinaryOp<'a, 'b, V: CSTVisitor + ?Sized>(visitor: &'b mut V, node: &'a BinaryOp<'a>) -> BinaryOp<'a> { +pub fn walk_BinaryOp(visitor: &mut V, node: &BinaryOp) { // Nothing to do. - node.clone() } pub fn walk_BooleanOperation(visitor: &mut V, node: &BooleanOperation) { visitor.visit_Expression(&node.left); @@ -726,12 +659,8 @@ pub fn walk_DictElement(visitor: &mut V, node: &DictElem } pub fn walk_Element(visitor: &mut V, node: &Element) { match node { - Element::Simple { value: node, .. } => { - visitor.visit_Expression(node); - } - Element::Starred(node) => { - visitor.visit_StarredElement(node); - } + Element::Simple { value: node, .. } => { visitor.visit_Expression(node); }, + Element::Starred(node) => { visitor.visit_StarredElement(node); }, }; } pub fn walk_Ellipsis(visitor: &mut V, node: &Ellipsis) { @@ -1104,11 +1033,7 @@ pub fn walk_Yield(visitor: &mut V, node: &Yield) { } pub fn walk_YieldValue(visitor: &mut V, node: &YieldValue) { match node { - YieldValue::Expression(node) => { - visitor.visit_Expression(node); - } - YieldValue::From(node) => { - visitor.visit_From(node); - } + YieldValue::Expression(node) => { visitor.visit_Expression(node); } + YieldValue::From(node) => { visitor.visit_From(node); } }; } diff --git a/src/linter.rs b/src/linter.rs index 242b0deae9..41646e416f 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -10,11 +10,11 @@ use crate::settings::Settings; use crate::{cache, fs}; pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Result> { - // Check the cache. - if let Some(messages) = cache::get(path, settings, mode) { - debug!("Cache hit for: {}", path.to_string_lossy()); - return Ok(messages); - } + // // Check the cache. + // if let Some(messages) = cache::get(path, settings, mode) { + // debug!("Cache hit for: {}", path.to_string_lossy()); + // return Ok(messages); + // } // Read the file from disk. let contents = fs::read_file(path)?; @@ -23,11 +23,15 @@ pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Resul let mut checks: Vec = vec![]; // Run the CST-based checks. - let python_cst = match libcst_native::parse_module(&contents, None) { + let _ = match libcst_native::parse_module(&contents, None) { Ok(m) => m, - Err(e) => panic!("Failed to parse CST."), + Err(e) => { + return Err(anyhow::anyhow!("Failed to parse")); + } }; - checks.extend(check_cst(&python_cst, settings)); + + Ok(vec![]) + // checks.extend(check_cst(&python_cst, settings)); // // Run the AST-based checks. // if settings @@ -42,20 +46,20 @@ pub fn check_path(path: &Path, settings: &Settings, mode: &cache::Mode) -> Resul // // // Run the lines-based checks. // check_lines(&mut checks, &contents, settings); - - // Convert to messages. - let messages: Vec = checks - .into_iter() - .map(|check| Message { - kind: check.kind, - location: check.location, - filename: path.to_string_lossy().to_string(), - }) - .collect(); - - cache::set(path, settings, &messages, mode); - - Ok(messages) + // + // // Convert to messages. + // let messages: Vec = checks + // .into_iter() + // .map(|check| Message { + // kind: check.kind, + // location: check.location, + // filename: path.to_string_lossy().to_string(), + // }) + // .collect(); + // + // cache::set(path, settings, &messages, mode); + // + // Ok(messages) } #[cfg(test)]