Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve no-console and no-window rule analysis #1235

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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
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