This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyzer):
noConsoleLog
rule (#4357)
- Loading branch information
Showing
18 changed files
with
356 additions
and
132 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_console_log.rs
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,81 @@ | ||
use crate::semantic_services::Semantic; | ||
use rome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::JsCallExpression; | ||
use rome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Disallow the use of `console.log` | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// console.log() | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// console.info("info"); | ||
/// console.warn("warn"); | ||
/// console.error("error"); | ||
/// console.assert(true); | ||
/// console.table(["foo", "bar"]); | ||
/// const console = { log() {} }; | ||
/// console.log(); | ||
/// ``` | ||
/// | ||
pub(crate) NoConsoleLog { | ||
version: "next", | ||
name: "noConsoleLog", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoConsoleLog { | ||
type Query = Semantic<JsCallExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let call_expression = ctx.query(); | ||
let model = ctx.model(); | ||
let callee = call_expression.callee().ok()?; | ||
let callee = callee.as_js_static_member_expression()?; | ||
|
||
let member = callee.member().ok()?; | ||
let object = callee.object().ok()?; | ||
let object = object.as_js_identifier_expression()?; | ||
|
||
if member.as_js_name()?.value_token().ok()?.text_trimmed() == "log" | ||
&& object.name().ok()?.value_token().ok()?.text_trimmed() == "console" | ||
{ | ||
let binding = object.name().ok()?; | ||
let reference_binding = model.binding(&binding); | ||
if reference_binding.is_none() { | ||
return Some(()); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.syntax().text_trimmed_range(), | ||
markup! { | ||
"Don't use "<Emphasis>"console.log"</Emphasis> | ||
}, | ||
) | ||
.note(markup! { | ||
<Emphasis>"console.log"</Emphasis>" is usually a tool for debugging and you don't want to have that in production." | ||
}), | ||
) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
crates/rome_js_analyze/tests/specs/nursery/noConsoleLog/invalid.js
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 @@ | ||
console.log("something") |
26 changes: 26 additions & 0 deletions
26
crates/rome_js_analyze/tests/specs/nursery/noConsoleLog/invalid.js.snap
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,26 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```js | ||
console.log("something") | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:1 lint/nursery/noConsoleLog ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Don't use console.log | ||
> 1 │ console.log("something") | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
i console.log is usually a tool for debugging and you don't want to have that in production. | ||
``` | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
crates/rome_js_analyze/tests/specs/nursery/noConsoleLog/valid.js
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,8 @@ | ||
const console = { | ||
log() {} | ||
}; | ||
console.log(); | ||
console.info("info"); | ||
console.warn("warn"); | ||
console.error("error"); | ||
console.assert(true); |
18 changes: 18 additions & 0 deletions
18
crates/rome_js_analyze/tests/specs/nursery/noConsoleLog/valid.js.snap
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,18 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```js | ||
const console = { | ||
log() {} | ||
}; | ||
console.log(); | ||
console.info("info"); | ||
console.warn("warn"); | ||
console.error("error"); | ||
console.assert(true); | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.