Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub fn get_declaration_of_variable<'a, 'b>(
) -> Option<&'b AstNode<'a>> {
let symbol_id = get_symbol_id_of_variable(ident, semantic)?;
let symbol_table = semantic.scoping();
Some(semantic.nodes().get_node(symbol_table.get_symbol_declaration(symbol_id)))
Some(semantic.nodes().get_node(symbol_table.symbol_declaration(symbol_id)))
}

pub fn get_declaration_from_reference_id<'a, 'b>(
Expand All @@ -293,7 +293,7 @@ pub fn get_declaration_from_reference_id<'a, 'b>(
) -> Option<&'b AstNode<'a>> {
let symbol_table = semantic.scoping();
let symbol_id = symbol_table.get_reference(reference_id).symbol_id()?;
Some(semantic.nodes().get_node(symbol_table.get_symbol_declaration(symbol_id)))
Some(semantic.nodes().get_node(symbol_table.symbol_declaration(symbol_id)))
}

pub fn get_symbol_id_of_variable(
Expand Down Expand Up @@ -569,7 +569,7 @@ pub fn could_be_error(ctx: &LintContext, expr: &Expression) -> bool {
let Some(symbol_id) = reference.symbol_id() else {
return true;
};
let decl = ctx.nodes().get_node(ctx.scoping().get_symbol_declaration(symbol_id));
let decl = ctx.nodes().get_node(ctx.scoping().symbol_declaration(symbol_id));
match decl.kind() {
AstKind::VariableDeclarator(decl) => {
if let Some(init) = &decl.init {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_func_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare_oxc_lint!(
impl Rule for NoFuncAssign {
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
let symbol_table = ctx.scoping();
let decl = symbol_table.get_symbol_declaration(symbol_id);
let decl = symbol_table.symbol_declaration(symbol_id);
if let AstKind::Function(_) = ctx.nodes().kind(decl) {
for reference in symbol_table.get_resolved_references(symbol_id) {
if reference.is_write() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_import_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Rule for NoImportAssign {
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
let symbol_table = ctx.scoping();
if symbol_table.symbol_flags(symbol_id).is_import() {
let kind = ctx.nodes().kind(symbol_table.get_symbol_declaration(symbol_id));
let kind = ctx.nodes().kind(symbol_table.symbol_declaration(symbol_id));
let is_namespace_specifier = matches!(kind, AstKind::ImportNamespaceSpecifier(_));
for reference in symbol_table.get_resolved_references(symbol_id) {
if is_namespace_specifier {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn resolve_global_binding<'a, 'b: 'a>(
return None;
};

let decl = nodes.get_node(symbols.get_symbol_declaration(binding_id));
let decl = nodes.get_node(symbols.symbol_declaration(binding_id));
match decl.kind() {
AstKind::VariableDeclarator(parent_decl) => {
if !parent_decl.id.kind.is_binding_identifier() {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_redeclare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ impl Rule for NoRedeclare {

fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext) {
let symbol_table = ctx.scoping();
let decl_node_id = symbol_table.get_symbol_declaration(symbol_id);
let decl_node_id = symbol_table.symbol_declaration(symbol_id);
match ctx.nodes().kind(decl_node_id) {
AstKind::VariableDeclarator(var) => {
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind {
let symbol_name = symbol_table.symbol_name(symbol_id);
if symbol_name == ident.name.as_str() {
for span in ctx.scoping().get_symbol_redeclarations(symbol_id) {
for span in ctx.scoping().symbol_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
Expand All @@ -78,7 +78,7 @@ impl Rule for NoRedeclare {
if let BindingPatternKind::BindingIdentifier(ident) = &param.pattern.kind {
let symbol_name = symbol_table.symbol_name(symbol_id);
if symbol_name == ident.name.as_str() {
for span in ctx.scoping().get_symbol_redeclarations(symbol_id) {
for span in ctx.scoping().symbol_redeclarations(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Rule for NoShadowRestrictedNames {

if name == "undefined" {
// Allow to declare `undefined` variable but not allow to assign value to it.
let node_id = ctx.scoping().get_symbol_declaration(symbol_id);
let node_id = ctx.scoping().symbol_declaration(symbol_id);
if let AstKind::VariableDeclarator(declarator) = ctx.nodes().kind(node_id) {
if declarator.init.is_none()
&& ctx
Expand All @@ -101,7 +101,7 @@ impl Rule for NoShadowRestrictedNames {
let span = ctx.scoping().symbol_span(symbol_id);
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));

for &span in ctx.scoping().get_symbol_redeclarations(symbol_id) {
for &span in ctx.scoping().symbol_redeclarations(symbol_id) {
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'s, 'a> Symbol<'s, 'a> {

#[inline]
pub fn scope_id(&self) -> ScopeId {
self.scoping().get_symbol_scope_id(self.id)
self.scoping().symbol_scope_id(self.id)
}

#[inline]
Expand All @@ -79,12 +79,12 @@ impl<'s, 'a> Symbol<'s, 'a> {

/// Is this [`Symbol`] declared in the root scope?
pub fn is_root(&self) -> bool {
self.scoping().get_symbol_scope_id(self.id) == self.scoping().root_scope_id()
self.scoping().symbol_scope_id(self.id) == self.scoping().root_scope_id()
}

#[inline]
fn declaration_id(&self) -> NodeId {
self.scoping().get_symbol_declaration(self.id)
self.scoping().symbol_declaration(self.id)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/nextjs/no_duplicate_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Rule for NoDuplicateHead {
return;
}

let scope_id = symbols.get_symbol_scope_id(symbol_id);
let scope_id = symbols.symbol_scope_id(symbol_id);
if scope_id != ctx.scoping().root_scope_id() {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Rule for NoAccumulatingSpread {
let Some(referenced_symbol_id) = reference.symbol_id() else {
return;
};
let declaration_id = symbols.get_symbol_declaration(referenced_symbol_id);
let declaration_id = symbols.symbol_declaration(referenced_symbol_id);
let Some(declaration) = ctx.nodes().parent_node(declaration_id) else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl NoAsyncEndpointHandlers {
return;
}

let decl_id = ctx.scoping().get_symbol_declaration(symbol_id);
let decl_id = ctx.scoping().symbol_declaration(symbol_id);
let decl_node = ctx.nodes().get_node(decl_id);
let registered_at = registered_at.or(Some(handler.span));
match decl_node.kind() {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/oxc/no_map_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl NoMapSpread {
}

if self.ignore_args {
let declaration = ctx.nodes().get_node(ctx.scoping().get_symbol_declaration(symbol_id));
let declaration = ctx.nodes().get_node(ctx.scoping().symbol_declaration(symbol_id));
if matches!(declaration.kind(), AstKind::FormalParameter(_)) {
return true;
}
Expand Down Expand Up @@ -677,7 +677,7 @@ where
else {
return;
};
let declaration_scope = self.ctx.scoping().get_symbol_scope_id(symbol_id);
let declaration_scope = self.ctx.scoping().symbol_scope_id(symbol_id);

// symbol is not declared within the mapper callback
if !self
Expand All @@ -691,7 +691,7 @@ where

// walk the declaration
let declaration_node =
self.ctx.nodes().get_node(self.ctx.scoping().get_symbol_declaration(symbol_id));
self.ctx.nodes().get_node(self.ctx.scoping().symbol_declaration(symbol_id));
self.visit_kind(declaration_node.kind());
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/exhaustive_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl Rule for ExhaustiveDeps {

for dependency in &declared_dependencies {
if let Some(symbol_id) = dependency.symbol_id {
let dependency_scope_id = ctx.scoping().get_symbol_scope_id(symbol_id);
let dependency_scope_id = ctx.scoping().symbol_scope_id(symbol_id);
if !(ctx
.semantic()
.scoping()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn has_assignment_before_node(
}
}

let declaration_id = symbol_table.get_symbol_declaration(symbol_id);
let declaration_id = symbol_table.symbol_declaration(symbol_id);
let AstKind::VariableDeclarator(decl) = ctx.nodes().kind(declaration_id) else {
return false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn check_and_diagnostic(
}

fn get_symbol_kind<'a>(symbol_id: SymbolId, ctx: &LintContext<'a>) -> AstKind<'a> {
ctx.nodes().get_node(ctx.scoping().get_symbol_declaration(symbol_id)).kind()
ctx.nodes().get_node(ctx.scoping().symbol_declaration(symbol_id)).kind()
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Rule for ConsistentExistenceIndexCheck {
return;
};

let declaration_node_id = ctx.scoping().get_symbol_declaration(symbol_id);
let declaration_node_id = ctx.scoping().symbol_declaration(symbol_id);
let node = ctx.nodes().get_node(declaration_node_id);

if let AstKind::VariableDeclarator(variables_declarator) = node.kind() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,56 +145,55 @@ impl Rule for ConsistentFunctionScoping {
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let (function_declaration_symbol_id, function_body, reporter_span) = match node.kind() {
AstKind::Function(function) => {
if function.is_typescript_syntax() {
return;
}

let func_scope_id = function.scope_id();
if let Some(parent_scope_id) = ctx.scoping().get_scope_parent_id(func_scope_id) {
// Example: const foo = function bar() {};
// The bar function scope id is 1. In order to ignore this rule,
// its parent's scope id (in this case `foo`'s scope id is 0 and is equal to root scope id)
// should be considered.
if parent_scope_id == ctx.scoping().root_scope_id() {
let (function_declaration_symbol_id, function_body, reporter_span) =
match node.kind() {
AstKind::Function(function) => {
if function.is_typescript_syntax() {
return;
}
}

// NOTE: function.body will always be some here because of
// checks in `is_typescript_syntax`
let Some(function_body) = &function.body else { return };

if let Some(binding_ident) = get_function_like_declaration(node, ctx) {
(
binding_ident.symbol_id(),
function_body,
function
.id
.as_ref()
.map_or(Span::sized(function.span.start, 8), |func_binding_ident| {
func_binding_ident.span
}),
)
} else if let Some(function_id) = &function.id {
(function_id.symbol_id(), function_body, function_id.span())
} else {
return;
let func_scope_id = function.scope_id();
if let Some(parent_scope_id) = ctx.scoping().scope_parent_id(func_scope_id) {
// Example: const foo = function bar() {};
// The bar function scope id is 1. In order to ignore this rule,
// its parent's scope id (in this case `foo`'s scope id is 0 and is equal to root scope id)
// should be considered.
if parent_scope_id == ctx.scoping().root_scope_id() {
return;
}
}

// NOTE: function.body will always be some here because of
// checks in `is_typescript_syntax`
let Some(function_body) = &function.body else { return };

if let Some(binding_ident) = get_function_like_declaration(node, ctx) {
(
binding_ident.symbol_id(),
function_body,
function.id.as_ref().map_or(
Span::sized(function.span.start, 8),
|func_binding_ident| func_binding_ident.span,
),
)
} else if let Some(function_id) = &function.id {
(function_id.symbol_id(), function_body, function_id.span())
} else {
return;
}
}
}
AstKind::ArrowFunctionExpression(arrow_function) if self.check_arrow_functions => {
let Some(binding_ident) = get_function_like_declaration(node, ctx) else {
return;
};
AstKind::ArrowFunctionExpression(arrow_function) if self.check_arrow_functions => {
let Some(binding_ident) = get_function_like_declaration(node, ctx) else {
return;
};

(binding_ident.symbol_id(), &arrow_function.body, binding_ident.span())
}
_ => return,
};
(binding_ident.symbol_id(), &arrow_function.body, binding_ident.span())
}
_ => return,
};

// if the function is declared at the root scope, we don't need to check anything
if ctx.scoping().get_symbol_scope_id(function_declaration_symbol_id)
if ctx.scoping().symbol_scope_id(function_declaration_symbol_id)
== ctx.scoping().root_scope_id()
{
return;
Expand Down Expand Up @@ -224,10 +223,10 @@ impl Rule for ConsistentFunctionScoping {

let parent_scope_ids = {
let mut current_scope_id =
ctx.scoping().get_symbol_scope_id(function_declaration_symbol_id);
ctx.scoping().symbol_scope_id(function_declaration_symbol_id);
let mut parent_scope_ids = FxHashSet::default();
parent_scope_ids.insert(current_scope_id);
while let Some(parent_scope_id) = ctx.scoping().get_scope_parent_id(current_scope_id) {
while let Some(parent_scope_id) = ctx.scoping().scope_parent_id(current_scope_id) {
parent_scope_ids.insert(parent_scope_id);
current_scope_id = parent_scope_id;
}
Expand All @@ -237,7 +236,7 @@ impl Rule for ConsistentFunctionScoping {
for reference_id in function_body_var_references {
let reference = ctx.scoping().get_reference(reference_id);
let Some(symbol_id) = reference.symbol_id() else { continue };
let scope_id = ctx.scoping().get_symbol_scope_id(symbol_id);
let scope_id = ctx.scoping().symbol_scope_id(symbol_id);
if parent_scope_ids.contains(&scope_id) && symbol_id != function_declaration_symbol_id {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn is_invalid_fetch_options<'a>(
continue;
};

let decl = ctx.nodes().get_node(symbols.get_symbol_declaration(symbol_id));
let decl = ctx.nodes().get_node(symbols.symbol_declaration(symbol_id));

match decl.kind() {
AstKind::VariableDeclarator(declarator) => match &declarator.init {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/no_thenable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn check_expression(expr: &Expression, ctx: &LintContext<'_>) -> Option<oxc_span
let symbols = ctx.scoping();
let reference_id = ident.reference_id();
symbols.get_reference(reference_id).symbol_id().and_then(|symbol_id| {
let decl = ctx.nodes().get_node(symbols.get_symbol_declaration(symbol_id));
let decl = ctx.nodes().get_node(symbols.symbol_declaration(symbol_id));
let var_decl = decl.kind().as_variable_declarator()?;

match &var_decl.init {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn is_not_array(expr: &Expression, ctx: &LintContext) -> bool {
Expression::Identifier(ident) => {
if let Some(symbol_id) = ast_util::get_symbol_id_of_variable(ident, ctx) {
let symbol_table = ctx.scoping();
let node = ctx.nodes().get_node(symbol_table.get_symbol_declaration(symbol_id));
let node = ctx.nodes().get_node(symbol_table.symbol_declaration(symbol_id));

if let AstKind::VariableDeclarator(variable_declarator) = node.kind() {
if let Some(ref_expr) = &variable_declarator.init {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn collect_ids_referenced_to_import<'a, 'c>(
.filter_map(|(symbol_id, reference_ids)| {
let symbol_id = SymbolId::from_usize(symbol_id);
if semantic.scoping().symbol_flags(symbol_id).is_import() {
let id = semantic.scoping().get_symbol_declaration(symbol_id);
let id = semantic.scoping().symbol_declaration(symbol_id);
let Some(AstKind::ImportDeclaration(import_decl)) =
semantic.nodes().parent_kind(id)
else {
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_linter/src/utils/react_perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ where
// Symbols declared at the root scope won't (or, at least, shouldn't) be
// re-assigned inside component render functions, so we can safely
// ignore them.
if ctx.scoping().get_symbol_scope_id(symbol_id) == ctx.scoping().root_scope_id() {
if ctx.scoping().symbol_scope_id(symbol_id) == ctx.scoping().root_scope_id() {
return;
}

let declaration_node =
ctx.nodes().get_node(ctx.scoping().get_symbol_declaration(symbol_id));
let declaration_node = ctx.nodes().get_node(ctx.scoping().symbol_declaration(symbol_id));
if let Some((decl_span, init_span)) =
self.check_for_violation_on_ast_kind(&declaration_node.kind(), symbol_id)
{
Expand Down
Loading
Loading