Skip to content

Commit 3d88eeb

Browse files
committed
fix(linter/no-console): false negative when console.* methods are used as args to functions (#11790)
we can't really fix this case as ``` function x(log: (...args: any[]) => void) { log() } // changing this arg to undefined would cause a type error x(console.log) ```
1 parent 7a05e71 commit 3d88eeb

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare_oxc_lint!(
8888
NoConsole,
8989
eslint,
9090
restriction,
91-
suggestion
91+
conditional_suggestion
9292
);
9393

9494
impl Rule for NoConsole {
@@ -106,26 +106,31 @@ impl Rule for NoConsole {
106106
}
107107

108108
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
109-
let AstKind::CallExpression(call_expr) = node.kind() else {
110-
return;
111-
};
112-
let Some(mem) = call_expr.callee.as_member_expression() else {
113-
return;
114-
};
115-
let Expression::Identifier(ident) = mem.object() else {
109+
let AstKind::MemberExpression(member_expr) = node.kind() else { return };
110+
let Expression::Identifier(ident) = member_expr.object() else {
116111
return;
117112
};
118113

119114
if ident.name == "console"
120115
&& ctx.scoping().root_unresolved_references().contains_key(ident.name.as_str())
121-
&& !self.allow.iter().any(|s| mem.static_property_name().is_some_and(|f| f == s))
116+
&& !self
117+
.allow
118+
.iter()
119+
.any(|s| member_expr.static_property_name().is_some_and(|f| f == s))
122120
{
123-
if let Some((mem_span, _)) = mem.static_property_info() {
121+
if let Some((mem_span, _)) = member_expr.static_property_info() {
124122
let diagnostic_span = ident.span().merge(mem_span);
125123

126124
ctx.diagnostic_with_suggestion(
127125
no_console_diagnostic(diagnostic_span, &self.allow),
128-
|fixer| remove_console(fixer, ctx, node),
126+
|fixer| {
127+
if let Some(parent) = ctx.nodes().parent_node(node.id()) {
128+
if let AstKind::CallExpression(_) = parent.kind() {
129+
return remove_console(fixer, ctx, parent);
130+
}
131+
}
132+
fixer.noop()
133+
},
129134
);
130135
}
131136
}
@@ -203,7 +208,7 @@ fn test() {
203208

204209
let fail = vec![
205210
("console.log()", None, None),
206-
("(console.log())", None, None),
211+
("foo(console.log)", None, None),
207212
("console.log(foo)", None, None),
208213
("console.error(foo)", None, None),
209214
("console.info(foo)", None, None),

crates/oxc_linter/src/snapshots/eslint_no_console.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ source: crates/oxc_linter/src/tester.rs
99
help: Delete this console statement.
1010

1111
eslint(no-console): Unexpected console statement.
12-
╭─[no_console.tsx:1:2]
13-
1 │ (console.log())
14-
· ───────────
12+
╭─[no_console.tsx:1:5]
13+
1foo(console.log)
14+
· ───────────
1515
╰────
1616
help: Delete this console statement.
1717

0 commit comments

Comments
 (0)