Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,32 @@ impl Rule for NoInnerDeclarations {
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let kind = node.kind();
let span = match kind {
AstKind::VariableDeclaration(decl)
if decl.kind.is_var() && self.config == NoInnerDeclarationsConfig::Both =>
{
Span::sized(decl.span.start, 3) // 3 for "var".len()
match node.kind() {
AstKind::VariableDeclaration(decl) => {
if self.config == NoInnerDeclarationsConfig::Functions || !decl.kind.is_var() {
return;
}
}
AstKind::Function(func) if func.is_function_declaration() => {
Span::sized(func.span.start, 8) // 8 for "function".len()
AstKind::Function(func) => {
if !func.is_function_declaration() {
return;
}
}
_ => return,
};
}

let parent_node = ctx.nodes().parent_node(node.id()).unwrap();
match parent_node.kind() {
if matches!(
parent_node.kind(),
AstKind::Program(_)
| AstKind::FunctionBody(_)
| AstKind::StaticBlock(_)
| AstKind::ExportNamedDeclaration(_)
| AstKind::ExportDefaultDeclaration(_) => return,
_ => {}
| AstKind::FunctionBody(_)
| AstKind::StaticBlock(_)
| AstKind::ExportNamedDeclaration(_)
| AstKind::ExportDefaultDeclaration(_)
) {
return;
}

let decl_type = match node.kind() {
AstKind::VariableDeclaration(_) => "variable",
AstKind::Function(_) => "function",
_ => unreachable!(),
};

let mut body = "program";
let mut parent = ctx.nodes().parent_node(parent_node.id());
while let Some(parent_node) = parent {
Expand All @@ -116,6 +113,18 @@ impl Rule for NoInnerDeclarations {
}
}

let (decl_type, span) = match node.kind() {
AstKind::VariableDeclaration(decl) => {
let span = Span::sized(decl.span.start, 3); // 3 for "var".len()
("variable", span)
}
AstKind::Function(func) => {
let span = Span::sized(func.span.start, 8); // 8 for "function".len()
("function", span)
}
_ => unreachable!(),
};

ctx.diagnostic(no_inner_declarations_diagnostic(decl_type, body, span));
}
}
Expand Down
Loading