Skip to content

Commit

Permalink
refactor(linter): simplify NoObjCalls resolution logic (#4765)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucab committed Aug 9, 2024
1 parent f5eeebd commit 63f274c
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,49 +80,45 @@ fn resolve_global_binding<'a, 'b: 'a>(
scope_id: ScopeId,
ctx: &LintContext<'a>,
) -> Option<&'a str> {
let scope = ctx.scopes();
let nodes = ctx.nodes();
let symbols = ctx.symbols();

if ctx.semantic().is_reference_to_global_variable(ident) {
Some(ident.name.as_str())
} else {
let scope = ctx.scopes();
let nodes = ctx.nodes();
let symbols = ctx.symbols();
scope.ancestors(scope_id).find_map(|id| scope.get_binding(id, &ident.name)).map_or_else(
// panic in debug builds, but fail gracefully in release builds
|| {
debug_assert!(
false,
"No binding id found for {}, but this IdentifierReference
return Some(ident.name.as_str());
}

let Some(binding_id) = scope.find_binding(scope_id, &ident.name) else {
// Panic in debug builds, but fail gracefully in release builds.
debug_assert!(
false,
"No binding id found for {}, but this IdentifierReference
is not a global",
&ident.name
);
None
},
|binding_id| {
let decl = nodes.get_node(symbols.get_declaration(binding_id));
let decl_scope = decl.scope_id();
match decl.kind() {
AstKind::VariableDeclarator(parent_decl) => {
if !parent_decl.id.kind.is_binding_identifier() {
return Some(ident.name.as_str());
}
match &parent_decl.init {
// handles "let a = JSON; let b = a; a();"
Some(Expression::Identifier(parent_ident))
if parent_ident.name != ident.name =>
{
return resolve_global_binding(parent_ident, decl_scope, ctx)
}
// handles "let a = globalThis.JSON; let b = a; a();"
Some(parent_expr) if parent_expr.is_member_expression() => {
return global_this_member(parent_expr.to_member_expression())
}
_ => None,
}
}
_ => None,
&ident.name
);
return None;
};

let decl = nodes.get_node(symbols.get_declaration(binding_id));
match decl.kind() {
AstKind::VariableDeclarator(parent_decl) => {
if !parent_decl.id.kind.is_binding_identifier() {
return Some(ident.name.as_str());
}
match &parent_decl.init {
// handles "let a = JSON; let b = a; a();"
Some(Expression::Identifier(parent_ident)) if parent_ident.name != ident.name => {
let decl_scope = decl.scope_id();
return resolve_global_binding(parent_ident, decl_scope, ctx);
}
},
)
// handles "let a = globalThis.JSON; let b = a; a();"
Some(parent_expr) if parent_expr.is_member_expression() => {
return global_this_member(parent_expr.to_member_expression())
}
_ => None,
}
}
_ => None,
}
}

Expand Down

0 comments on commit 63f274c

Please sign in to comment.