Skip to content

Commit

Permalink
Split up Checker
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 2, 2023
1 parent 310f13c commit f666edb
Show file tree
Hide file tree
Showing 120 changed files with 1,357 additions and 1,122 deletions.
11 changes: 5 additions & 6 deletions crates/ruff/src/ast/function_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::Expr;

use crate::ast::helpers::{map_callable, to_call_path};
use crate::ast::types::{Scope, ScopeKind};
use crate::checkers::ast::Checker;
use crate::checkers::ctx::AstContext;

const CLASS_METHODS: [&str; 3] = ["__new__", "__init_subclass__", "__class_getitem__"];
const METACLASS_BASES: [(&str, &str); 2] = [("", "type"), ("abc", "ABCMeta")];
Expand All @@ -16,7 +16,7 @@ pub enum FunctionType {

/// Classify a function based on its scope, name, and decorators.
pub fn classify(
checker: &Checker,
ctx: &AstContext,
scope: &Scope,
name: &str,
decorator_list: &[Expr],
Expand All @@ -29,8 +29,7 @@ pub fn classify(
if decorator_list.iter().any(|expr| {
// The method is decorated with a static method decorator (like
// `@staticmethod`).
checker
.resolve_call_path(map_callable(expr))
ctx.resolve_call_path(map_callable(expr))
.map_or(false, |call_path| {
staticmethod_decorators
.iter()
Expand All @@ -42,15 +41,15 @@ pub fn classify(
// Special-case class method, like `__new__`.
|| scope.bases.iter().any(|expr| {
// The class itself extends a known metaclass, so all methods are class methods.
checker.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
ctx.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
METACLASS_BASES
.iter()
.any(|(module, member)| call_path.as_slice() == [*module, *member])
})
})
|| decorator_list.iter().any(|expr| {
// The method is decorated with a class method decorator (like `@classmethod`).
checker.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
ctx.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
classmethod_decorators
.iter()
.any(|decorator| call_path == to_call_path(decorator))
Expand Down
15 changes: 7 additions & 8 deletions crates/ruff/src/ast/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use smallvec::{smallvec, SmallVec};
use crate::ast::types::{Binding, BindingKind, CallPath, Range};
use crate::ast::visitor;
use crate::ast::visitor::Visitor;
use crate::checkers::ast::Checker;
use crate::checkers::ctx::AstContext;
use crate::source_code::{Generator, Indexer, Locator, Stylist};

/// Create an `Expr` with default location from an `ExprKind`.
Expand Down Expand Up @@ -98,17 +98,16 @@ pub fn format_call_path(call_path: &[&str]) -> String {
}

/// Return `true` if the `Expr` contains a reference to `${module}.${target}`.
pub fn contains_call_path(checker: &Checker, expr: &Expr, target: &[&str]) -> bool {
pub fn contains_call_path(ctx: &AstContext, expr: &Expr, target: &[&str]) -> bool {
any_over_expr(expr, &|expr| {
checker
.resolve_call_path(expr)
ctx.resolve_call_path(expr)
.map_or(false, |call_path| call_path.as_slice() == target)
})
}

/// Return `true` if the `Expr` contains an expression that appears to include a
/// side-effect (like a function call).
pub fn contains_effect(checker: &Checker, expr: &Expr) -> bool {
pub fn contains_effect(ctx: &AstContext, expr: &Expr) -> bool {
any_over_expr(expr, &|expr| {
// Accept empty initializers.
if let ExprKind::Call {
Expand All @@ -124,7 +123,7 @@ pub fn contains_effect(checker: &Checker, expr: &Expr) -> bool {
|| id == "tuple"
|| id == "dict"
|| id == "frozenset")
&& checker.is_builtin(id);
&& ctx.is_builtin(id);
return !is_empty_initializer;
}
}
Expand Down Expand Up @@ -669,10 +668,10 @@ pub fn has_comments_in(range: Range, locator: &Locator) -> bool {
}

/// Return `true` if the body uses `locals()`, `globals()`, `vars()`, `eval()`.
pub fn uses_magic_variable_access(checker: &Checker, body: &[Stmt]) -> bool {
pub fn uses_magic_variable_access(ctx: &AstContext, body: &[Stmt]) -> bool {
any_over_body(body, &|expr| {
if let ExprKind::Call { func, .. } = &expr.node {
checker.resolve_call_path(func).map_or(false, |call_path| {
ctx.resolve_call_path(func).map_or(false, |call_path| {
call_path.as_slice() == ["", "locals"]
|| call_path.as_slice() == ["", "globals"]
|| call_path.as_slice() == ["", "vars"]
Expand Down
16 changes: 8 additions & 8 deletions crates/ruff/src/ast/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ast::helpers::any_over_expr;
use crate::ast::types::{BindingKind, Scope};
use crate::ast::visitor;
use crate::ast::visitor::Visitor;
use crate::checkers::ast::Checker;
use crate::checkers::ctx::AstContext;

bitflags! {
#[derive(Default)]
Expand All @@ -19,7 +19,7 @@ bitflags! {

/// Extract the names bound to a given __all__ assignment.
pub fn extract_all_names(
checker: &Checker,
ctx: &AstContext,
stmt: &Stmt,
scope: &Scope,
) -> (Vec<String>, AllNamesFlags) {
Expand All @@ -38,7 +38,7 @@ pub fn extract_all_names(
}

fn extract_elts<'a>(
checker: &'a Checker,
ctx: &'a AstContext,
expr: &'a Expr,
) -> (Option<&'a Vec<Expr>>, AllNamesFlags) {
match &expr.node {
Expand All @@ -60,7 +60,7 @@ pub fn extract_all_names(
} => {
// Allow `tuple()` and `list()` calls.
if keywords.is_empty() && args.len() <= 1 {
if checker.resolve_call_path(func).map_or(false, |call_path| {
if ctx.resolve_call_path(func).map_or(false, |call_path| {
call_path.as_slice() == ["", "tuple"]
|| call_path.as_slice() == ["", "list"]
}) {
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn extract_all_names(
// Grab the existing bound __all__ values.
if let StmtKind::AugAssign { .. } = &stmt.node {
if let Some(index) = scope.bindings.get("__all__") {
if let BindingKind::Export(existing) = &checker.bindings[*index].kind {
if let BindingKind::Export(existing) = &ctx.bindings[*index].kind {
names.extend_from_slice(existing);
}
}
Expand All @@ -113,7 +113,7 @@ pub fn extract_all_names(
let mut current_right = right;
loop {
// Process the right side, which should be a "real" value.
let (elts, new_flags) = extract_elts(checker, current_right);
let (elts, new_flags) = extract_elts(ctx, current_right);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(&mut names, elts, &mut flags);
Expand All @@ -125,7 +125,7 @@ pub fn extract_all_names(
current_left = left;
current_right = right;
} else {
let (elts, new_flags) = extract_elts(checker, current_left);
let (elts, new_flags) = extract_elts(ctx, current_left);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(&mut names, elts, &mut flags);
Expand All @@ -134,7 +134,7 @@ pub fn extract_all_names(
}
}
} else {
let (elts, new_flags) = extract_elts(checker, value);
let (elts, new_flags) = extract_elts(ctx, value);
flags |= new_flags;
if let Some(elts) = elts {
add_to_names(&mut names, elts, &mut flags);
Expand Down
Loading

0 comments on commit f666edb

Please sign in to comment.