Skip to content

Commit cfdc518

Browse files
committed
perf(linter/no-inner-declarations): move work to cold path (#11746)
Small perf optimization to `eslint/no-inner-declarations` rule. Common case is that rules will not trigger an error, so move as much work as possible to after the checks which exit without error.
1 parent 80c87d4 commit cfdc518

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,35 +70,32 @@ impl Rule for NoInnerDeclarations {
7070
}
7171

7272
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
73-
let kind = node.kind();
74-
let span = match kind {
75-
AstKind::VariableDeclaration(decl)
76-
if decl.kind.is_var() && self.config == NoInnerDeclarationsConfig::Both =>
77-
{
78-
Span::sized(decl.span.start, 3) // 3 for "var".len()
73+
match node.kind() {
74+
AstKind::VariableDeclaration(decl) => {
75+
if self.config == NoInnerDeclarationsConfig::Functions || !decl.kind.is_var() {
76+
return;
77+
}
7978
}
80-
AstKind::Function(func) if func.is_function_declaration() => {
81-
Span::sized(func.span.start, 8) // 8 for "function".len()
79+
AstKind::Function(func) => {
80+
if !func.is_function_declaration() {
81+
return;
82+
}
8283
}
8384
_ => return,
84-
};
85+
}
8586

8687
let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
87-
match parent_node.kind() {
88+
if matches!(
89+
parent_node.kind(),
8890
AstKind::Program(_)
89-
| AstKind::FunctionBody(_)
90-
| AstKind::StaticBlock(_)
91-
| AstKind::ExportNamedDeclaration(_)
92-
| AstKind::ExportDefaultDeclaration(_) => return,
93-
_ => {}
91+
| AstKind::FunctionBody(_)
92+
| AstKind::StaticBlock(_)
93+
| AstKind::ExportNamedDeclaration(_)
94+
| AstKind::ExportDefaultDeclaration(_)
95+
) {
96+
return;
9497
}
9598

96-
let decl_type = match node.kind() {
97-
AstKind::VariableDeclaration(_) => "variable",
98-
AstKind::Function(_) => "function",
99-
_ => unreachable!(),
100-
};
101-
10299
let mut body = "program";
103100
let mut parent = ctx.nodes().parent_node(parent_node.id());
104101
while let Some(parent_node) = parent {
@@ -116,6 +113,18 @@ impl Rule for NoInnerDeclarations {
116113
}
117114
}
118115

116+
let (decl_type, span) = match node.kind() {
117+
AstKind::VariableDeclaration(decl) => {
118+
let span = Span::sized(decl.span.start, 3); // 3 for "var".len()
119+
("variable", span)
120+
}
121+
AstKind::Function(func) => {
122+
let span = Span::sized(func.span.start, 8); // 8 for "function".len()
123+
("function", span)
124+
}
125+
_ => unreachable!(),
126+
};
127+
119128
ctx.diagnostic(no_inner_declarations_diagnostic(decl_type, body, span));
120129
}
121130
}

0 commit comments

Comments
 (0)