-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change no-console-log to no-console (#1194)
- Loading branch information
Showing
5 changed files
with
131 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}], | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.