Skip to content

Commit

Permalink
feat: change no-console-log to no-console (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 31, 2023
1 parent b98b352 commit cbc93b7
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 121 deletions.
38 changes: 38 additions & 0 deletions docs/rules/no_console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# no_console

Disallows the use of the `console` global.

Oftentimes, developers accidentally commit `console.log`/`console.error`
statements, left in particularly after debugging. Moreover, using these in code
may leak sensitive information to the output or clutter the console with
unnecessary information. This rule helps maintain clean and secure code by
disallowing the use of `console`.

This rule is especially useful in libraries where you almost never want to
output to the console.

### Invalid

```typescript
console.log("Debug message");
console.error("Debug message");
console.debug(obj);

if (debug) console.log("Debugging");

function log() {
console.log("Log");
}
```

### Valid

It is recommended to explicitly enable the console via a `deno-lint-ignore`
comment for any calls where you actually want to use it.

```typescript
function logWarning(message: string) {
// deno-lint-ignore no-console
console.warn(message);
}
```
31 changes: 0 additions & 31 deletions docs/rules/no_console_log.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod no_case_declarations;
pub mod no_class_assign;
pub mod no_compare_neg_zero;
pub mod no_cond_assign;
pub mod no_console_log;
pub mod no_console;
pub mod no_const_assign;
pub mod no_constant_condition;
pub mod no_control_regex;
Expand Down
92 changes: 92 additions & 0 deletions src/rules/no_console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::Ident;
use deno_ast::SourceRanged;

#[derive(Debug)]
pub struct NoConsole;

const MESSAGE: &str = "`console` usage is not allowed.";
const CODE: &str = "no-console";

impl LintRule for NoConsole {
fn tags(&self) -> &'static [&'static str] {
&[]
}

fn code(&self) -> &'static str {
CODE
}

fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program,
) {
NoConsoleHandler.traverse(program, context);
}

#[cfg(feature = "docs")]
fn docs(&self) -> &'static str {
include_str!("../../docs/rules/no_console.md")
}
}

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);
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn console_allowed() {
assert_lint_ok!(
NoConsole,
// ignored
r#"// deno-lint-ignore no-console\nconsole.error('Error message');"#,
// not global
r#"const console = { log() {} } console.log('Error message');"#,
);
}

#[test]
fn no_console_invalid() {
// Test cases where console is present
assert_lint_err!(
NoConsole,
r#"console.log('Debug message');"#: [{
col: 0,
message: MESSAGE,
}],
r#"if (debug) { console.log('Debugging'); }"#: [{
col: 13,
message: MESSAGE,
}],
r#"function log() { console.log('Log'); }"#: [{
col: 17,
message: MESSAGE,
}],
r#"function log() { console.debug('Log'); }"#: [{
col: 17,
message: MESSAGE,
}],
r#"console;"#: [{
col: 0,
message: MESSAGE,
}],
r#"console.warn("test");"#: [{
col: 0,
message: MESSAGE,
}],
);
}
}
89 changes: 0 additions & 89 deletions src/rules/no_console_log.rs

This file was deleted.

0 comments on commit cbc93b7

Please sign in to comment.