Skip to content

Commit

Permalink
refactor(linter): dereference IDs as soon as possible (#6821)
Browse files Browse the repository at this point in the history
Style nit. Dereference `&ScopeId` to `ScopeId` (and other IDs) as early as possible. `&ScopeId` is 8 bytes, whereas `ScopeId` is 4 bytes.

In simple cases like these, compiler will optimize it anyway, but still I think it's a better pattern to dererence early. In more complicated cases, it will be better for performance, and in my opinion, it makes things clearer if vars called `scope_id` are always a `ScopeId`, not sometimes a `&ScopeId`.
  • Loading branch information
overlookmotel committed Oct 23, 2024
1 parent 1107770 commit a148023
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ impl Symbol<'_, '_> {
fn is_in_declare_global(&self) -> bool {
self.scopes()
.ancestors(self.scope_id())
.filter(|scope_id| {
let flags = self.scopes().get_flags(*scope_id);
.filter(|&scope_id| {
let flags = self.scopes().get_flags(scope_id);
flags.contains(ScopeFlags::TsModuleBlock)
})
.any(|ambient_module_scope_id| {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_mocks_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Rule for NoMocksImport {
return;
};

for reference_id in require_reference_ids {
let reference = ctx.symbols().get_reference(*reference_id);
for &reference_id in require_reference_ids {
let reference = ctx.symbols().get_reference(reference_id);
let Some(parent) = ctx.nodes().parent_node(reference.node_id()) else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ impl Rule for NoUnsafeDeclarationMerging {
match node.kind() {
AstKind::Class(decl) => {
if let Some(ident) = decl.id.as_ref() {
for (_, symbol_id) in ctx.semantic().scopes().get_bindings(node.scope_id()) {
for (_, &symbol_id) in ctx.semantic().scopes().get_bindings(node.scope_id()) {
if let AstKind::TSInterfaceDeclaration(scope_interface) =
get_symbol_kind(*symbol_id, ctx)
get_symbol_kind(symbol_id, ctx)
{
check_and_diagnostic(ident, &scope_interface.id, ctx);
}
}
}
}
AstKind::TSInterfaceDeclaration(decl) => {
for (_, symbol_id) in ctx.semantic().scopes().get_bindings(node.scope_id()) {
if let AstKind::Class(scope_class) = get_symbol_kind(*symbol_id, ctx) {
for (_, &symbol_id) in ctx.semantic().scopes().get_bindings(node.scope_id()) {
if let AstKind::Class(scope_class) = get_symbol_kind(symbol_id, ctx) {
if let Some(scope_class_ident) = scope_class.id.as_ref() {
check_and_diagnostic(&decl.id, scope_class_ident, ctx);
}
Expand Down
9 changes: 4 additions & 5 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ fn collect_ids_referenced_to_import<'a, 'c>(

if matches!(import_decl.source.value.as_str(), "@jest/globals" | "vitest") {
let original = find_original_name(import_decl, name);
let mut ret = vec![];
for reference_id in reference_ids {
ret.push((*reference_id, original));
}

let ret = reference_ids
.iter()
.map(|&reference_id| (reference_id, original))
.collect::<Vec<_>>();
return Some(ret);
}
}
Expand Down

0 comments on commit a148023

Please sign in to comment.