Skip to content

Commit 2cd786b

Browse files
committed
perf(linter/no-inner-declarations): remove unnecessary code and reduce branches (#11633)
1. Remove unnecessary check that grandparent of node is a `Function` or `ArrowFunctionExpression` when its parent is a `FunctionBody`. `FunctionBody` is always the child of `Function` or `ArrowFunctionExpression`, so this check is redundant. 2. If node is a `VariableStatement` or `Function`, it always has a parent node. So `unwrap` the parent instead of branching `if let Some(parent_node) = parent`. It's always `Some`. The 2nd also allows skipping 1 turn of the loop further down because: * If direct parent is a `StaticBlock`, we already exited. * It's impossible for a variable or function declaration's parent to be a `Function` - there'd have to be a `FunctionBody` in between.
1 parent 8776301 commit 2cd786b

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,25 @@ impl Rule for NoInnerDeclarations {
8383
_ => return,
8484
};
8585

86-
let mut parent = ctx.nodes().parent_node(node.id());
87-
if let Some(parent_node) = parent {
88-
match parent_node.kind() {
89-
AstKind::FunctionBody(_) => {
90-
if let Some(grandparent) = ctx.nodes().parent_node(parent_node.id()) {
91-
if grandparent.kind().is_function_like() {
92-
return;
93-
}
94-
}
95-
}
96-
AstKind::Program(_)
97-
| AstKind::StaticBlock(_)
98-
| AstKind::ExportNamedDeclaration(_)
99-
| AstKind::ExportDefaultDeclaration(_) => return,
100-
AstKind::ForStatement(for_stmt) => {
101-
if for_stmt.init.as_ref().is_some_and(|init| init.span() == kind.span()) {
102-
return;
103-
}
104-
}
105-
AstKind::ForInStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
106-
return;
107-
}
108-
AstKind::ForOfStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
86+
let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
87+
match parent_node.kind() {
88+
AstKind::Program(_)
89+
| AstKind::FunctionBody(_)
90+
| AstKind::StaticBlock(_)
91+
| AstKind::ExportNamedDeclaration(_)
92+
| AstKind::ExportDefaultDeclaration(_) => return,
93+
AstKind::ForStatement(for_stmt) => {
94+
if for_stmt.init.as_ref().is_some_and(|init| init.span() == kind.span()) {
10995
return;
11096
}
111-
_ => {}
11297
}
98+
AstKind::ForInStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
99+
return;
100+
}
101+
AstKind::ForOfStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
102+
return;
103+
}
104+
_ => {}
113105
}
114106

115107
let decl_type = match node.kind() {
@@ -119,6 +111,7 @@ impl Rule for NoInnerDeclarations {
119111
};
120112

121113
let mut body = "program";
114+
let mut parent = ctx.nodes().parent_node(parent_node.id());
122115
while let Some(parent_node) = parent {
123116
let parent_kind = parent_node.kind();
124117
match parent_kind {

0 commit comments

Comments
 (0)