perf(lint/no_var_requires): quicker way to check if the IdentifierReference point to a global variable (#2376)

The old code use this function to do the checking, which is too much for
this simple checking


ef336cb66b/crates/oxc_linter/src/ast_util.rs (L267-L276)
This commit is contained in:
Yunfei He 2024-02-10 22:46:02 +08:00 committed by GitHub
parent 92afbb102e
commit 7d7a3fcef8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 8 deletions

View file

@ -381,3 +381,26 @@ pub fn get_new_expr_ident_name<'a>(new_expr: &'a NewExpression<'a>) -> Option<&'
Some(ident.name.as_str())
}
/// Check if the given [IdentifierReference] is a global reference.
/// Such as `window`, `document`, `globalThis`, etc.
pub fn is_global_reference(ident: &IdentifierReference, ctx: &LintContext) -> bool {
let symbol_table = ctx.semantic().symbols();
let Some(reference_id) = ident.reference_id.get() else {
return false;
};
let reference = symbol_table.get_reference(reference_id);
reference.symbol_id().is_none()
}
pub fn is_global_require_call(call_expr: &CallExpression, ctx: &LintContext) -> bool {
if call_expr.arguments.len() != 1 {
return false;
}
if let Expression::Identifier(id_ref) = &call_expr.callee {
id_ref.name == "require" && is_global_reference(id_ref, ctx)
} else {
false
}
}

View file

@ -1,4 +1,4 @@
use oxc_ast::{ast::Expression, AstKind};
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
@ -6,7 +6,7 @@ use oxc_diagnostics::{
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{ast_util::get_declaration_of_variable, context::LintContext, rule::Rule, AstNode};
use crate::{ast_util::is_global_require_call, context::LintContext, rule::Rule, AstNode};
#[derive(Debug, Error, Diagnostic)]
#[error("typescript-eslint(no-var-requires): Require statement not part of import statement.")]
@ -41,7 +41,7 @@ impl Rule for NoVarRequires {
}
let AstKind::CallExpression(expr) = node.kind() else { return };
if expr.is_require_call() && no_local_require_declaration(&expr.callee, ctx) {
if is_global_require_call(expr, ctx) {
// If the parent is an expression statement => this is a top level require()
// Or, if the parent is a chain expression (require?.()) and
// the grandparent is an expression statement => this is a top level require()
@ -70,11 +70,6 @@ impl Rule for NoVarRequires {
}
}
fn no_local_require_declaration(expr: &Expression, ctx: &LintContext) -> bool {
let Expression::Identifier(ident) = expr else { return true };
get_declaration_of_variable(ident, ctx).is_none()
}
#[test]
fn test() {
use crate::tester::Tester;