Skip to content

Commit 2e91dc4

Browse files
committed
refactor(semantic)!: rename SymbolTable and ScopeTree methods
part of #9607
1 parent 7d501be commit 2e91dc4

File tree

71 files changed

+372
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+372
-343
lines changed

crates/oxc_codegen/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<'a> Codegen<'a> {
517517
fn get_binding_identifier_name(&self, ident: &BindingIdentifier<'a>) -> &'a str {
518518
if let Some(symbol_table) = &self.symbol_table {
519519
if let Some(symbol_id) = ident.symbol_id.get() {
520-
let name = symbol_table.get_name(symbol_id);
520+
let name = symbol_table.symbol_name(symbol_id);
521521
// SAFETY: Hack the lifetime to be part of the allocator.
522522
return unsafe { std::mem::transmute_copy(&name) };
523523
}

crates/oxc_linter/src/ast_util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub fn get_declaration_of_variable<'a, 'b>(
284284
) -> Option<&'b AstNode<'a>> {
285285
let symbol_id = get_symbol_id_of_variable(ident, semantic)?;
286286
let symbol_table = semantic.symbols();
287-
Some(semantic.nodes().get_node(symbol_table.get_declaration(symbol_id)))
287+
Some(semantic.nodes().get_node(symbol_table.get_symbol_declaration(symbol_id)))
288288
}
289289

290290
pub fn get_declaration_from_reference_id<'a, 'b>(
@@ -293,7 +293,7 @@ pub fn get_declaration_from_reference_id<'a, 'b>(
293293
) -> Option<&'b AstNode<'a>> {
294294
let symbol_table = semantic.symbols();
295295
let symbol_id = symbol_table.get_reference(reference_id).symbol_id()?;
296-
Some(semantic.nodes().get_node(symbol_table.get_declaration(symbol_id)))
296+
Some(semantic.nodes().get_node(symbol_table.get_symbol_declaration(symbol_id)))
297297
}
298298

299299
pub fn get_symbol_id_of_variable(
@@ -569,7 +569,7 @@ pub fn could_be_error(ctx: &LintContext, expr: &Expression) -> bool {
569569
let Some(symbol_id) = reference.symbol_id() else {
570570
return true;
571571
};
572-
let decl = ctx.nodes().get_node(ctx.symbols().get_declaration(symbol_id));
572+
let decl = ctx.nodes().get_node(ctx.symbols().get_symbol_declaration(symbol_id));
573573
match decl.kind() {
574574
AstKind::VariableDeclarator(decl) => {
575575
if let Some(init) = &decl.init {

crates/oxc_linter/src/rules/eslint/no_alert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn is_global_this_ref_or_global_window<'a>(
6666
expr: &Expression<'a>,
6767
) -> bool {
6868
if let Expression::ThisExpression(_) = expr {
69-
if ctx.scopes().get_flags(scope_id).is_top() {
69+
if ctx.scopes().scope_flags(scope_id).is_top() {
7070
return true;
7171
}
7272
}

crates/oxc_linter/src/rules/eslint/no_class_assign.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ declare_oxc_lint!(
3737
impl Rule for NoClassAssign {
3838
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
3939
let symbol_table = ctx.semantic().symbols();
40-
if symbol_table.get_flags(symbol_id).is_class() {
40+
if symbol_table.symbol_flags(symbol_id).is_class() {
4141
for reference in symbol_table.get_resolved_references(symbol_id) {
4242
if reference.is_write() {
4343
ctx.diagnostic(no_class_assign_diagnostic(
44-
symbol_table.get_name(symbol_id),
45-
symbol_table.get_span(symbol_id),
44+
symbol_table.symbol_name(symbol_id),
45+
symbol_table.symbol_span(symbol_id),
4646
ctx.semantic().reference_span(reference),
4747
));
4848
}

crates/oxc_linter/src/rules/eslint/no_const_assign.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ declare_oxc_lint!(
5353
impl Rule for NoConstAssign {
5454
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
5555
let symbol_table = ctx.semantic().symbols();
56-
if symbol_table.get_flags(symbol_id).is_const_variable() {
56+
if symbol_table.symbol_flags(symbol_id).is_const_variable() {
5757
for reference in symbol_table.get_resolved_references(symbol_id) {
5858
if reference.is_write() {
5959
ctx.diagnostic(no_const_assign_diagnostic(
60-
symbol_table.get_name(symbol_id),
61-
symbol_table.get_span(symbol_id),
60+
symbol_table.symbol_name(symbol_id),
61+
symbol_table.symbol_span(symbol_id),
6262
ctx.semantic().reference_span(reference),
6363
));
6464
}

crates/oxc_linter/src/rules/eslint/no_eval.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,14 @@ impl Rule for NoEval {
165165
let Some((span, name)) = mem_expr.static_property_info() else { return };
166166

167167
if name == "eval" {
168-
let scope_id = ctx.scopes().ancestors(parent.scope_id()).find(|scope_id| {
169-
let scope_flags = ctx.scopes().get_flags(*scope_id);
170-
scope_flags.is_var() && !scope_flags.is_arrow()
171-
});
168+
let scope_id =
169+
ctx.scopes().scope_ancestors(parent.scope_id()).find(|scope_id| {
170+
let scope_flags = ctx.scopes().scope_flags(*scope_id);
171+
scope_flags.is_var() && !scope_flags.is_arrow()
172+
});
172173

173174
let scope_id = scope_id.unwrap();
174-
let scope_flags = ctx.scopes().get_flags(scope_id);
175+
let scope_flags = ctx.scopes().scope_flags(scope_id);
175176

176177
// The `TsModuleBlock` shouldn't be considered
177178
if scope_flags.is_ts_module_block() {

crates/oxc_linter/src/rules/eslint/no_ex_assign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare_oxc_lint!(
3939
impl Rule for NoExAssign {
4040
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
4141
let symbol_table = ctx.semantic().symbols();
42-
if symbol_table.get_flags(symbol_id).is_catch_variable() {
42+
if symbol_table.symbol_flags(symbol_id).is_catch_variable() {
4343
for reference in symbol_table.get_resolved_references(symbol_id) {
4444
if reference.is_write() {
4545
ctx.diagnostic(no_ex_assign_diagnostic(

crates/oxc_linter/src/rules/eslint/no_func_assign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ declare_oxc_lint!(
6868
impl Rule for NoFuncAssign {
6969
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
7070
let symbol_table = ctx.semantic().symbols();
71-
let decl = symbol_table.get_declaration(symbol_id);
71+
let decl = symbol_table.get_symbol_declaration(symbol_id);
7272
if let AstKind::Function(_) = ctx.nodes().kind(decl) {
7373
for reference in symbol_table.get_resolved_references(symbol_id) {
7474
if reference.is_write() {
7575
ctx.diagnostic(no_func_assign_diagnostic(
76-
symbol_table.get_name(symbol_id),
76+
symbol_table.symbol_name(symbol_id),
7777
ctx.semantic().reference_span(reference),
7878
));
7979
}

crates/oxc_linter/src/rules/eslint/no_import_assign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const REFLECT_MUTATION_METHODS: phf::Set<&'static str> =
5353
impl Rule for NoImportAssign {
5454
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
5555
let symbol_table = ctx.semantic().symbols();
56-
if symbol_table.get_flags(symbol_id).is_import() {
57-
let kind = ctx.nodes().kind(symbol_table.get_declaration(symbol_id));
56+
if symbol_table.symbol_flags(symbol_id).is_import() {
57+
let kind = ctx.nodes().kind(symbol_table.get_symbol_declaration(symbol_id));
5858
let is_namespace_specifier = matches!(kind, AstKind::ImportNamespaceSpecifier(_));
5959
for reference in symbol_table.get_resolved_references(symbol_id) {
6060
if is_namespace_specifier {

crates/oxc_linter/src/rules/eslint/no_label_var.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Rule for NoLabelVar {
6666
if let Some(symbol_id) =
6767
ctx.scopes().find_binding(node.scope_id(), &labeled_stmt.label.name)
6868
{
69-
let decl_span = ctx.symbols().get_span(symbol_id);
69+
let decl_span = ctx.symbols().symbol_span(symbol_id);
7070
let label_decl = labeled_stmt.span.start;
7171
ctx.diagnostic(no_label_var_diagnostic(
7272
&labeled_stmt.label.name,

crates/oxc_linter/src/rules/eslint/no_obj_calls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn resolve_global_binding<'a, 'b: 'a>(
9999
return None;
100100
};
101101

102-
let decl = nodes.get_node(symbols.get_declaration(binding_id));
102+
let decl = nodes.get_node(symbols.get_symbol_declaration(binding_id));
103103
match decl.kind() {
104104
AstKind::VariableDeclarator(parent_decl) => {
105105
if !parent_decl.id.kind.is_binding_identifier() {

crates/oxc_linter/src/rules/eslint/no_redeclare.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ impl Rule for NoRedeclare {
6262

6363
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext) {
6464
let symbol_table = ctx.semantic().symbols();
65-
let decl_node_id = symbol_table.get_declaration(symbol_id);
65+
let decl_node_id = symbol_table.get_symbol_declaration(symbol_id);
6666
match ctx.nodes().kind(decl_node_id) {
6767
AstKind::VariableDeclarator(var) => {
6868
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind {
69-
let symbol_name = symbol_table.get_name(symbol_id);
69+
let symbol_name = symbol_table.symbol_name(symbol_id);
7070
if symbol_name == ident.name.as_str() {
71-
for span in ctx.symbols().get_redeclarations(symbol_id) {
71+
for span in ctx.symbols().get_symbol_redeclarations(symbol_id) {
7272
self.report_diagnostic(ctx, *span, ident);
7373
}
7474
}
7575
}
7676
}
7777
AstKind::FormalParameter(param) => {
7878
if let BindingPatternKind::BindingIdentifier(ident) = &param.pattern.kind {
79-
let symbol_name = symbol_table.get_name(symbol_id);
79+
let symbol_name = symbol_table.symbol_name(symbol_id);
8080
if symbol_name == ident.name.as_str() {
81-
for span in ctx.symbols().get_redeclarations(symbol_id) {
81+
for span in ctx.symbols().get_symbol_redeclarations(symbol_id) {
8282
self.report_diagnostic(ctx, *span, ident);
8383
}
8484
}

crates/oxc_linter/src/rules/eslint/no_setter_return.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Rule for NoSetterReturn {
4646
return;
4747
}
4848

49-
for scope_id in ctx.scopes().ancestors(node.scope_id()) {
50-
let flags = ctx.scopes().get_flags(scope_id);
49+
for scope_id in ctx.scopes().scope_ancestors(node.scope_id()) {
50+
let flags = ctx.scopes().scope_flags(scope_id);
5151
if flags.is_set_accessor() {
5252
ctx.diagnostic(no_setter_return_diagnostic(stmt.span));
5353
} else if flags.is_function() {

crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ declare_oxc_lint!(
7777

7878
impl Rule for NoShadowRestrictedNames {
7979
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
80-
let name = ctx.symbols().get_name(symbol_id);
80+
let name = ctx.symbols().symbol_name(symbol_id);
8181

8282
if !PRE_DEFINE_VAR.contains_key(name) {
8383
return;
8484
}
8585

8686
if name == "undefined" {
8787
// Allow to declare `undefined` variable but not allow to assign value to it.
88-
let node_id = ctx.semantic().symbols().get_declaration(symbol_id);
88+
let node_id = ctx.semantic().symbols().get_symbol_declaration(symbol_id);
8989
if let AstKind::VariableDeclarator(declarator) = ctx.nodes().kind(node_id) {
9090
if declarator.init.is_none()
9191
&& ctx
@@ -98,10 +98,10 @@ impl Rule for NoShadowRestrictedNames {
9898
}
9999
}
100100

101-
let span = ctx.symbols().get_span(symbol_id);
101+
let span = ctx.symbols().symbol_span(symbol_id);
102102
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
103103

104-
for &span in ctx.symbols().get_redeclarations(symbol_id) {
104+
for &span in ctx.symbols().get_symbol_redeclarations(symbol_id) {
105105
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
106106
}
107107
}

crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Symbol<'_, '_> {
6565
pub fn is_in_declared_module(&self) -> bool {
6666
let scopes = self.scopes();
6767
let nodes = self.nodes();
68-
scopes.ancestors(self.scope_id())
68+
scopes.scope_ancestors(self.scope_id())
6969
.map(|scope_id| scopes.get_node_id(scope_id))
7070
.map(|node_id| nodes.get_node(node_id))
7171
.any(|node| matches!(node.kind(), AstKind::TSModuleDeclaration(namespace) if is_ambient_namespace(namespace)))

crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ impl Symbol<'_, '_> {
375375

376376
fn is_in_declare_global(&self) -> bool {
377377
self.scopes()
378-
.ancestors(self.scope_id())
378+
.scope_ancestors(self.scope_id())
379379
.filter(|&scope_id| {
380-
let flags = self.scopes().get_flags(scope_id);
380+
let flags = self.scopes().scope_flags(scope_id);
381381
flags.contains(ScopeFlags::TsModuleBlock)
382382
})
383383
.any(|ambient_module_scope_id| {

crates/oxc_linter/src/rules/eslint/no_unused_vars/symbol.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
3737
module_record: &'s ModuleRecord,
3838
symbol_id: SymbolId,
3939
) -> Self {
40-
let flags = semantic.symbols().get_flags(symbol_id);
40+
let flags = semantic.symbols().symbol_flags(symbol_id);
4141
Self { semantic, module_record, id: symbol_id, flags, span: OnceCell::new() }
4242
}
4343

@@ -48,7 +48,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
4848

4949
#[inline]
5050
pub fn name(&self) -> &str {
51-
self.symbols().get_name(self.id)
51+
self.symbols().symbol_name(self.id)
5252
}
5353

5454
#[inline]
@@ -58,7 +58,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
5858

5959
#[inline]
6060
pub fn scope_id(&self) -> ScopeId {
61-
self.symbols().get_scope_id(self.id)
61+
self.symbols().get_symbol_scope_id(self.id)
6262
}
6363

6464
#[inline]
@@ -80,12 +80,12 @@ impl<'s, 'a> Symbol<'s, 'a> {
8080

8181
/// Is this [`Symbol`] declared in the root scope?
8282
pub fn is_root(&self) -> bool {
83-
self.symbols().get_scope_id(self.id) == self.scopes().root_scope_id()
83+
self.symbols().get_symbol_scope_id(self.id) == self.scopes().root_scope_id()
8484
}
8585

8686
#[inline]
8787
fn declaration_id(&self) -> NodeId {
88-
self.symbols().get_declaration(self.id)
88+
self.symbols().get_symbol_declaration(self.id)
8989
}
9090

9191
#[inline]
@@ -159,13 +159,13 @@ impl<'s, 'a> Symbol<'s, 'a> {
159159
_ => break,
160160
}
161161
}
162-
self.symbols().get_span(self.id)
162+
self.symbols().symbol_span(self.id)
163163
}
164164

165165
/// <https://github.com/oxc-project/oxc/issues/4739>
166166
fn clean_binding_id(&self, binding: &BindingPattern) -> Span {
167167
if binding.kind.is_destructuring_pattern() {
168-
return self.symbols().get_span(self.id);
168+
return self.symbols().symbol_span(self.id);
169169
}
170170
let own = binding.kind.span();
171171
binding.type_annotation.as_ref().map_or(own, |ann| Span::new(own.start, ann.span.start))
@@ -185,7 +185,7 @@ impl<'a> Symbol<'_, 'a> {
185185

186186
#[inline]
187187
fn is_in_ts_namespace(&self) -> bool {
188-
self.scopes().get_flags(self.scope_id()).is_ts_module_block()
188+
self.scopes().scope_flags(self.scope_id()).is_ts_module_block()
189189
}
190190

191191
/// We need to do this due to limitations of [`Semantic`].

crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'a> Symbol<'_, 'a> {
692692
return false;
693693
}
694694

695-
for scope_id in self.scopes().ancestors(call_scope_id) {
695+
for scope_id in self.scopes().scope_ancestors(call_scope_id) {
696696
if scope_id == container_id {
697697
return true;
698698
} else if scope_id == decl_scope_id {

crates/oxc_linter/src/rules/jest/no_confusing_set_timeout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn handle_jest_set_time_out<'a>(
185185
};
186186

187187
if expr.property.name == "setTimeout" {
188-
if !scopes.get_flags(parent_node.scope_id()).is_top() {
188+
if !scopes.scope_flags(parent_node.scope_id()).is_top() {
189189
ctx.diagnostic(no_global_set_timeout_diagnostic(member_expr.span()));
190190
}
191191

crates/oxc_linter/src/rules/jest/prefer_lowercase_title/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Rule for PreferLowercaseTitle {
219219
}
220220

221221
if matches!(jest_fn_call.kind, JestFnKind::General(JestGeneralFnKind::Describe)) {
222-
if self.ignore_top_level_describe && scopes.get_flags(node.scope_id()).is_top() {
222+
if self.ignore_top_level_describe && scopes.scope_flags(node.scope_id()).is_top() {
223223
return;
224224
}
225225
} else if !matches!(

crates/oxc_linter/src/rules/jest/require_top_level_describe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl RequireTopLevelDescribe {
142142
) {
143143
let node = possible_jest_node.node;
144144
let scopes = ctx.scopes();
145-
let is_top = scopes.get_flags(node.scope_id()).is_top();
145+
let is_top = scopes.scope_flags(node.scope_id()).is_top();
146146

147147
let AstKind::CallExpression(call_expr) = node.kind() else {
148148
return;

crates/oxc_linter/src/rules/nextjs/no_duplicate_head.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ declare_oxc_lint!(
4848
impl Rule for NoDuplicateHead {
4949
fn run_on_symbol(&self, symbol_id: oxc_semantic::SymbolId, ctx: &LintContext<'_>) {
5050
let symbols = ctx.symbols();
51-
let name = symbols.get_name(symbol_id);
51+
let name = symbols.symbol_name(symbol_id);
5252
if name != "Head" {
5353
return;
5454
}
5555

56-
let flags = symbols.get_flags(symbol_id);
56+
let flags = symbols.symbol_flags(symbol_id);
5757
if !flags.is_import() {
5858
return;
5959
}
6060

61-
let scope_id = symbols.get_scope_id(symbol_id);
61+
let scope_id = symbols.get_symbol_scope_id(symbol_id);
6262
if scope_id != ctx.scopes().root_scope_id() {
6363
return;
6464
}

crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Rule for NoAccumulatingSpread {
140140
let Some(referenced_symbol_id) = reference.symbol_id() else {
141141
return;
142142
};
143-
let declaration_id = symbols.get_declaration(referenced_symbol_id);
143+
let declaration_id = symbols.get_symbol_declaration(referenced_symbol_id);
144144
let Some(declaration) = ctx.semantic().nodes().parent_node(declaration_id) else {
145145
return;
146146
};

crates/oxc_linter/src/rules/oxc/no_async_endpoint_handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ impl NoAsyncEndpointHandlers {
219219
};
220220

221221
// Cannot check imported handlers without cross-file analysis.
222-
let flags = ctx.symbols().get_flags(symbol_id);
222+
let flags = ctx.symbols().symbol_flags(symbol_id);
223223
if flags.is_import() {
224224
return;
225225
}
226226

227-
let decl_id = ctx.symbols().get_declaration(symbol_id);
227+
let decl_id = ctx.symbols().get_symbol_declaration(symbol_id);
228228
let decl_node = ctx.nodes().get_node(decl_id);
229229
let registered_at = registered_at.or(Some(handler.span));
230230
match decl_node.kind() {

0 commit comments

Comments
 (0)