Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 28, 2023
1 parent 9d243cd commit d87cf3d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
23 changes: 10 additions & 13 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,20 @@ where
&stmt.node,
StmtKind::Import { .. } | StmtKind::ImportFrom { .. }
) {
let scope_id = self.ctx.scope_id();
if scope_id.is_global() && self.ctx.current_stmt_parent().is_none() {
if self.ctx.scope_id.is_global() && self.ctx.current_stmt_parent().is_none() {
self.importer.visit_import(stmt);
}
}

// Pre-visit.
match &stmt.node {
StmtKind::Global { names } => {
let scope_id = self.ctx.scope_id();
let ranges: Vec<TextRange> = helpers::find_names(stmt, self.locator).collect();
if !scope_id.is_global() {
if !self.ctx.scope_id.is_global() {
// Add the binding to the current scope.
let context = self.ctx.execution_context();
let exceptions = self.ctx.exceptions();
let scope = &mut self.ctx.scopes[scope_id];
let scope = &mut self.ctx.scopes[self.ctx.scope_id];
let usage = Some((scope.id, stmt.range()));
for (name, range) in names.iter().zip(ranges.iter()) {
let id = self.ctx.bindings.push(Binding {
Expand All @@ -248,12 +246,11 @@ where
}
}
StmtKind::Nonlocal { names } => {
let scope_id = self.ctx.scope_id();
let ranges: Vec<TextRange> = helpers::find_names(stmt, self.locator).collect();
if !scope_id.is_global() {
if !self.ctx.scope_id.is_global() {
let context = self.ctx.execution_context();
let exceptions = self.ctx.exceptions();
let scope = &mut self.ctx.scopes[scope_id];
let scope = &mut self.ctx.scopes[self.ctx.scope_id];
let usage = Some((scope.id, stmt.range()));
for (name, range) in names.iter().zip(ranges.iter()) {
// Add a binding to the current scope.
Expand All @@ -276,7 +273,7 @@ where
let binding_id = self
.ctx
.scopes
.ancestors(scope_id)
.ancestors(self.ctx.scope_id)
.skip(1)
.take_while(|scope_id| !scope_id.is_global())
.find_map(|scope_id| {
Expand Down Expand Up @@ -907,7 +904,7 @@ where
kind: BindingKind::FutureImportation,
runtime_usage: None,
// Always mark `__future__` imports as used.
synthetic_usage: Some((self.ctx.scope_id(), alias.range())),
synthetic_usage: Some((self.ctx.scope_id, alias.range())),
typing_usage: None,
range: alias.range(),
source: Some(*self.ctx.current_stmt()),
Expand Down Expand Up @@ -962,7 +959,7 @@ where
kind: BindingKind::Importation(Importation { name, full_name }),
runtime_usage: None,
synthetic_usage: if is_explicit_reexport {
Some((self.ctx.scope_id(), alias.range()))
Some((self.ctx.scope_id, alias.range()))
} else {
None
},
Expand Down Expand Up @@ -1218,7 +1215,7 @@ where
kind: BindingKind::FutureImportation,
runtime_usage: None,
// Always mark `__future__` imports as used.
synthetic_usage: Some((self.ctx.scope_id(), alias.range())),
synthetic_usage: Some((self.ctx.scope_id, alias.range())),
typing_usage: None,
range: alias.range(),
source: Some(*self.ctx.current_stmt()),
Expand Down Expand Up @@ -1311,7 +1308,7 @@ where
}),
runtime_usage: None,
synthetic_usage: if is_explicit_reexport {
Some((self.ctx.scope_id(), alias.range()))
Some((self.ctx.scope_id, alias.range()))
} else {
None
},
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pylint/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn in_dunder_init(checker: &Checker) -> bool {
if name != "__init__" {
return false;
}
let Some(parent) = checker.ctx.parent_scope() else {
let Some(parent) = scope.parent.map(|scope_id| &checker.ctx.scopes[scope_id]) else {
return false;
};

Expand Down
15 changes: 3 additions & 12 deletions crates/ruff_python_semantic/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<'a> Context<'a> {

/// Push a [`Scope`] with the given [`ScopeKind`] onto the stack.
pub fn push_scope(&mut self, kind: ScopeKind<'a>) {
let id = self.scopes.push_scope(self.scope_id, kind);
let id = self.scopes.push_scope(kind, self.scope_id);
self.scope_id = id;
}

Expand Down Expand Up @@ -389,26 +389,17 @@ impl<'a> Context<'a> {
&self.scopes[self.scope_id]
}

/// Returns the id of the top-most scope
pub fn scope_id(&self) -> ScopeId {
self.scope_id
}

/// Returns a mutable reference to the current top most scope.
pub fn scope_mut(&mut self) -> &mut Scope<'a> {
&mut self.scopes[self.scope_id]
}

pub fn parent_scope(&self) -> Option<&Scope> {
self.scopes[self.scope_id]
.parent
.map(|index| &self.scopes[index])
}

/// Returns an iterator over all scopes, starting from the current scope.
pub fn scopes(&self) -> impl Iterator<Item = &Scope> {
self.scopes.ancestor_scopes(self.scope_id)
}

/// Returns `true` if the context is in an exception handler.
pub const fn in_exception_handler(&self) -> bool {
self.in_exception_handler
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a> Scopes<'a> {
}

/// Pushes a new scope and returns its unique id
pub fn push_scope(&mut self, parent: ScopeId, kind: ScopeKind<'a>) -> ScopeId {
pub fn push_scope(&mut self, kind: ScopeKind<'a>, parent: ScopeId) -> ScopeId {
let next_id = ScopeId::try_from(self.0.len()).unwrap();
self.0.push(Scope::local(next_id, parent, kind));
next_id
Expand Down

0 comments on commit d87cf3d

Please sign in to comment.