Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_js_analyzer): noConsoleLog rule (#4357)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 7, 2023
1 parent 38104b3 commit 6cb9585
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 132 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ npm/backend-jsonrpc/src/workspace.ts linguist-generated=true text=auto eol=lf
website/src/docs/lint/rules/**/*.md linguist-generated=true text=auto eol=lf
npm/rome/configuration_schema.json linguist-generated=true text=auto eol=lf
editors/vscode/configuration_schema.json linguist-generated=true text=auto eol=lf
crates/rome_service/src/configuration/parse/json/rules.rs linguist-generated=true text=auto eol=lf


crates/rome_js_formatter/tests/**/*.ts.prettier-snap linguist-language=TypeScript
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ the code action is not formatted.
- [`noRedundantRoles`](https://docs.rome.tools/lint/rules/noRedundantRoles/)
- [`noNoninteractiveTabindex`](https://docs.rome.tools/lint/rules/noNoninteractiveTabindex/)
- [`noAriaUnsupportedElements`](https://docs.rome.tools/lint/rules/noAriaUnsupportedElements/)
- [`noConsoleLog`](https://docs.rome.tools/lint/rules/noConsoleLog/)

### Parser

Expand Down
11 changes: 6 additions & 5 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ define_categories! {
"lint/nursery/noPrototypeBuiltins": "https://docs.rome.tools/lint/rules/noPrototypeBuiltins",
"lint/nursery/noSvgWithoutTitle": "https://docs.rome.tools/lint/rules/noSvgWithoutTitle",
"lint/nursery/noUselessCatch": "https://docs.rome.tools/lint/rules/noUselessCatch",
"lint/nursery/noParameterAssign": "https://docs.rome.tools/lint/rules/noParameterAssign",
"lint/nursery/noNamespace": "https://docs.rome.tools/lint/rules/noNamespace",
"lint/nursery/noConfusingArrow": "https://docs.rome.tools/lint/rules/noConfusingArrow",
"lint/nursery/noNoninteractiveTabindex": "https://docs.rome.tools/lint/rules/noNoninteractiveTabindex",
"lint/nursery/noAriaUnsupportedElements": "https://docs.rome.tools/lint/rules/noAriaUnsupportedElements",
"lint/nursery/noParameterAssign": "https://docs.rome.tools/lint/rules/noParameterAssign",
"lint/nursery/noNamespace": "https://docs.rome.tools/lint/rules/noNamespace",
"lint/nursery/noConfusingArrow": "https://docs.rome.tools/lint/rules/noConfusingArrow",
"lint/nursery/noNoninteractiveTabindex": "https://docs.rome.tools/lint/rules/noNoninteractiveTabindex",
"lint/nursery/noAriaUnsupportedElements": "https://docs.rome.tools/lint/rules/noAriaUnsupportedElements",
"lint/nursery/noConsoleLog": "https://docs.rome.tools/lint/rules/noConsoleLog",
// Insert new nursery rule here
"lint/nursery/noRedeclare": "https://docs.rome.tools/lint/rules/noRedeclare",
"lint/nursery/useNamespaceKeyword": "https://docs.rome.tools/lint/rules/useNamespaceKeyword",
Expand Down
26 changes: 2 additions & 24 deletions crates/rome_js_analyze/src/analyzers/nursery/no_confusing_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ declare_rule! {
///
/// ```js,expect_diagnostic
/// var x = a => 1 ? 2 : 3;
/// ```
/// ```js,expect_diagnostic
/// var x = (a) => 1 ? 2 : 3;
/// ```
///
Expand Down Expand Up @@ -69,28 +71,4 @@ impl Rule for NoConfusingArrow {
},
))
}

// Formatter is not happy with the added parenthesis at the time of commit.
// This should be fixed before enabling the action.
//fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
// let body_expr = ctx.query().body().ok()?.as_any_js_expression()?.clone();
//
// let mut mutation = ctx.root().begin();
//
// mutation.replace_node(
// body_expr.clone(),
// AnyJsExpression::from(make::js_parenthesized_expression(
// JsSyntaxToken::new_detached(JsSyntaxKind::L_PAREN, "(", [], []),
// body_expr,
// JsSyntaxToken::new_detached(JsSyntaxKind::R_PAREN, ")", [], []),
// )),
// );
//
// Some(JsRuleAction {
// category: ActionCategory::QuickFix,
// applicability: Applicability::Always,
// message: markup! { "Wrap the function body in parenthesis." }.to_owned(),
// mutation,
// })
//}
}
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/semantic_analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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."
}),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("something")
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.
```


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

```


Loading

0 comments on commit 6cb9585

Please sign in to comment.