Skip to content

Commit

Permalink
fix: improve no-console and no-window rule analysis (#1235)
Browse files Browse the repository at this point in the history
Fixes #1232
  • Loading branch information
littledivy authored Jan 24, 2024
1 parent 00c9025 commit 6e12763
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
42 changes: 38 additions & 4 deletions src/rules/no_console.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::Ident;

use deno_ast::view as ast_view;
use deno_ast::SourceRanged;
use if_chain::if_chain;

#[derive(Debug)]
pub struct NoConsole;
Expand Down Expand Up @@ -36,9 +38,39 @@ impl LintRule for NoConsole {
struct NoConsoleHandler;

impl Handler for NoConsoleHandler {
fn ident(&mut self, id: &Ident, ctx: &mut Context) {
if id.sym().as_ref() == "console" && ctx.scope().is_global(&id.to_id()) {
ctx.add_diagnostic(id.range(), CODE, MESSAGE);
fn member_expr(&mut self, expr: &ast_view::MemberExpr, ctx: &mut Context) {
if expr.parent().is::<ast_view::MemberExpr>() {
return;
}

use deno_ast::view::Expr;
if_chain! {
if let Expr::Ident(ident) = &expr.obj;
if ident.sym() == "console";
if ctx.scope().is_global(&ident.inner.to_id());
then {
ctx.add_diagnostic(
ident.range(),
CODE,
MESSAGE,
);
}
}
}

fn expr_stmt(&mut self, expr: &ast_view::ExprStmt, ctx: &mut Context) {
use deno_ast::view::Expr;
if_chain! {
if let Expr::Ident(ident) = &expr.expr;
if ident.sym() == "console";
if ctx.scope().is_global(&ident.inner.to_id());
then {
ctx.add_diagnostic(
ident.range(),
CODE,
MESSAGE,
);
}
}
}
}
Expand All @@ -55,6 +87,8 @@ mod tests {
r"// deno-lint-ignore no-console\nconsole.error('Error message');",
// not global
r"const console = { log() {} } console.log('Error message');",
// https://github.com/denoland/deno_lint/issues/1232
"const x: { console: any } = { console: 21 }; x.console",
);
}

Expand Down
31 changes: 29 additions & 2 deletions src/rules/no_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,33 @@ impl LintRule for NoWindow {
struct NoWindowGlobalHandler;

impl Handler for NoWindowGlobalHandler {
fn ident(&mut self, ident: &ast_view::Ident, ctx: &mut Context) {
fn member_expr(&mut self, expr: &ast_view::MemberExpr, ctx: &mut Context) {
if expr.parent().is::<ast_view::MemberExpr>() {
return;
}

use deno_ast::view::Expr;
if_chain! {
if let Expr::Ident(ident) = &expr.obj;
if ident.sym() == "window";
if ctx.scope().is_global(&ident.inner.to_id());
then {
ctx.add_diagnostic_with_hint(
ident.range(),
CODE,
MESSAGE,
HINT,
);
}
}
}

fn expr_stmt(&mut self, expr: &ast_view::ExprStmt, ctx: &mut Context) {
use deno_ast::view::Expr;
if_chain! {
if let Expr::Ident(ident) = &expr.expr;
if ident.sym() == "window";
if ctx.scope().is_global(&ident.to_id());
if ctx.scope().is_global(&ident.inner.to_id());
then {
ctx.add_diagnostic_with_hint(
ident.range(),
Expand Down Expand Up @@ -78,6 +101,10 @@ mod tests {
"const window = 42; window.alert();",
r#"const window = 42; window["alert"]();"#,
r#"const window = 42; window[`alert`]();"#,

// https://github.com/denoland/deno_lint/issues/1232
"const params: { window: number } = { window: 23 };",
"x.window"
};
}

Expand Down

0 comments on commit 6e12763

Please sign in to comment.