mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
[red-knot] Remove Scope::name
(#12137)
This commit is contained in:
parent
955138b74a
commit
228b1c4235
3 changed files with 96 additions and 56 deletions
|
@ -307,8 +307,8 @@ mod tests {
|
|||
use ruff_db::vfs::{system_path_to_file, VfsFile};
|
||||
|
||||
use crate::db::tests::TestDb;
|
||||
use crate::semantic_index::symbol::{FileScopeId, ScopeKind, SymbolTable};
|
||||
use crate::semantic_index::{root_scope, semantic_index, symbol_table};
|
||||
use crate::semantic_index::symbol::{FileScopeId, FileSymbolId, Scope, ScopeKind, SymbolTable};
|
||||
use crate::semantic_index::{root_scope, semantic_index, symbol_table, SemanticIndex};
|
||||
|
||||
struct TestCase {
|
||||
db: TestDb,
|
||||
|
@ -440,12 +440,18 @@ y = 2
|
|||
|
||||
let index = semantic_index(&db, file);
|
||||
|
||||
let root = index.symbol_table(FileScopeId::root());
|
||||
let scopes: Vec<_> = index.child_scopes(FileScopeId::root()).collect();
|
||||
assert_eq!(scopes.len(), 1);
|
||||
|
||||
let (class_scope_id, class_scope) = scopes[0];
|
||||
assert_eq!(class_scope.kind(), ScopeKind::Class);
|
||||
assert_eq!(class_scope.name(), "C");
|
||||
assert_eq!(
|
||||
class_scope
|
||||
.defining_symbol()
|
||||
.map(super::symbol::FileSymbolId::scoped_symbol_id),
|
||||
root.symbol_id_by_name("C")
|
||||
);
|
||||
|
||||
let class_table = index.symbol_table(class_scope_id);
|
||||
assert_eq!(names(&class_table), vec!["x"]);
|
||||
|
@ -474,7 +480,12 @@ y = 2
|
|||
|
||||
let (function_scope_id, function_scope) = scopes[0];
|
||||
assert_eq!(function_scope.kind(), ScopeKind::Function);
|
||||
assert_eq!(function_scope.name(), "func");
|
||||
assert_eq!(
|
||||
function_scope
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("func")
|
||||
);
|
||||
|
||||
let function_table = index.symbol_table(function_scope_id);
|
||||
assert_eq!(names(&function_table), vec!["x"]);
|
||||
|
@ -509,9 +520,20 @@ def func():
|
|||
let (func_scope2_id, func_scope_2) = scopes[1];
|
||||
|
||||
assert_eq!(func_scope_1.kind(), ScopeKind::Function);
|
||||
assert_eq!(func_scope_1.name(), "func");
|
||||
|
||||
assert_eq!(
|
||||
func_scope_1
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("func")
|
||||
);
|
||||
assert_eq!(func_scope_2.kind(), ScopeKind::Function);
|
||||
assert_eq!(func_scope_2.name(), "func");
|
||||
assert_eq!(
|
||||
func_scope_2
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("func")
|
||||
);
|
||||
|
||||
let func1_table = index.symbol_table(func_scope1_id);
|
||||
let func2_table = index.symbol_table(func_scope2_id);
|
||||
|
@ -546,7 +568,12 @@ def func[T]():
|
|||
let (ann_scope_id, ann_scope) = scopes[0];
|
||||
|
||||
assert_eq!(ann_scope.kind(), ScopeKind::Annotation);
|
||||
assert_eq!(ann_scope.name(), "func");
|
||||
assert_eq!(
|
||||
ann_scope
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("func")
|
||||
);
|
||||
let ann_table = index.symbol_table(ann_scope_id);
|
||||
assert_eq!(names(&ann_table), vec!["T"]);
|
||||
|
||||
|
@ -554,7 +581,12 @@ def func[T]():
|
|||
assert_eq!(scopes.len(), 1);
|
||||
let (func_scope_id, func_scope) = scopes[0];
|
||||
assert_eq!(func_scope.kind(), ScopeKind::Function);
|
||||
assert_eq!(func_scope.name(), "func");
|
||||
assert_eq!(
|
||||
func_scope
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("func")
|
||||
);
|
||||
let func_table = index.symbol_table(func_scope_id);
|
||||
assert_eq!(names(&func_table), vec!["x"]);
|
||||
}
|
||||
|
@ -578,7 +610,12 @@ class C[T]:
|
|||
assert_eq!(scopes.len(), 1);
|
||||
let (ann_scope_id, ann_scope) = scopes[0];
|
||||
assert_eq!(ann_scope.kind(), ScopeKind::Annotation);
|
||||
assert_eq!(ann_scope.name(), "C");
|
||||
assert_eq!(
|
||||
ann_scope
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("C")
|
||||
);
|
||||
let ann_table = index.symbol_table(ann_scope_id);
|
||||
assert_eq!(names(&ann_table), vec!["T"]);
|
||||
assert!(
|
||||
|
@ -590,10 +627,15 @@ class C[T]:
|
|||
|
||||
let scopes: Vec<_> = index.child_scopes(ann_scope_id).collect();
|
||||
assert_eq!(scopes.len(), 1);
|
||||
let (func_scope_id, func_scope) = scopes[0];
|
||||
let (func_scope_id, class_scope) = scopes[0];
|
||||
|
||||
assert_eq!(func_scope.kind(), ScopeKind::Class);
|
||||
assert_eq!(func_scope.name(), "C");
|
||||
assert_eq!(class_scope.kind(), ScopeKind::Class);
|
||||
assert_eq!(
|
||||
class_scope
|
||||
.defining_symbol()
|
||||
.map(FileSymbolId::scoped_symbol_id),
|
||||
root_table.symbol_id_by_name("C")
|
||||
);
|
||||
assert_eq!(names(&index.symbol_table(func_scope_id)), vec!["x"]);
|
||||
}
|
||||
|
||||
|
@ -654,6 +696,27 @@ class C[T]:
|
|||
|
||||
#[test]
|
||||
fn scope_iterators() {
|
||||
fn scope_names<'a>(
|
||||
scopes: impl Iterator<Item = (FileScopeId, &'a Scope)>,
|
||||
index: &'a SemanticIndex,
|
||||
) -> Vec<&'a str> {
|
||||
let mut names = Vec::new();
|
||||
|
||||
for (_, scope) in scopes {
|
||||
if let Some(defining_symbol) = scope.defining_symbol {
|
||||
let symbol_table = &index.symbol_tables[defining_symbol.scope()];
|
||||
let symbol = symbol_table.symbol(defining_symbol.scoped_symbol_id());
|
||||
names.push(symbol.name().as_str());
|
||||
} else if scope.parent.is_none() {
|
||||
names.push("<module>");
|
||||
} else {
|
||||
panic!("Unsupported");
|
||||
}
|
||||
}
|
||||
|
||||
names
|
||||
}
|
||||
|
||||
let TestCase { db, file } = test_case(
|
||||
r#"
|
||||
class Test:
|
||||
|
@ -669,35 +732,29 @@ def x():
|
|||
|
||||
let index = semantic_index(&db, file);
|
||||
|
||||
let descendents: Vec<_> = index
|
||||
.descendent_scopes(FileScopeId::root())
|
||||
.map(|(_, scope)| scope.name().as_str())
|
||||
.collect();
|
||||
assert_eq!(descendents, vec!["Test", "foo", "bar", "baz", "x"]);
|
||||
let descendents = index.descendent_scopes(FileScopeId::root());
|
||||
assert_eq!(
|
||||
scope_names(descendents, index),
|
||||
vec!["Test", "foo", "bar", "baz", "x"]
|
||||
);
|
||||
|
||||
let children: Vec<_> = index
|
||||
.child_scopes(FileScopeId::root())
|
||||
.map(|(_, scope)| scope.name.as_str())
|
||||
.collect();
|
||||
assert_eq!(children, vec!["Test", "x"]);
|
||||
let children = index.child_scopes(FileScopeId::root());
|
||||
assert_eq!(scope_names(children, index), vec!["Test", "x"]);
|
||||
|
||||
let test_class = index.child_scopes(FileScopeId::root()).next().unwrap().0;
|
||||
let test_child_scopes: Vec<_> = index
|
||||
.child_scopes(test_class)
|
||||
.map(|(_, scope)| scope.name.as_str())
|
||||
.collect();
|
||||
assert_eq!(test_child_scopes, vec!["foo", "baz"]);
|
||||
let test_child_scopes = index.child_scopes(test_class);
|
||||
assert_eq!(scope_names(test_child_scopes, index), vec!["foo", "baz"]);
|
||||
|
||||
let bar_scope = index
|
||||
.descendent_scopes(FileScopeId::root())
|
||||
.nth(2)
|
||||
.unwrap()
|
||||
.0;
|
||||
let ancestors: Vec<_> = index
|
||||
.ancestor_scopes(bar_scope)
|
||||
.map(|(_, scope)| scope.name())
|
||||
.collect();
|
||||
let ancestors = index.ancestor_scopes(bar_scope);
|
||||
|
||||
assert_eq!(ancestors, vec!["bar", "foo", "Test", "<module>"]);
|
||||
assert_eq!(
|
||||
scope_names(ancestors, index),
|
||||
vec!["bar", "foo", "Test", "<module>"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue