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

feat(rome_js_analyzer): add noWith rule #4025

Merged
merged 9 commits into from
Dec 12, 2022
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
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ define_dategories! {
// nursery
"lint/nursery/noAccessKey": "https://docs.rome.tools/lint/rules/noAccessKey",
"lint/nursery/noAssignInExpressions": "https://docs.rome.tools/lint/rules/noAssignInExpressions",
"lint/nursery/noWith": "https://docs.rome.tools/lint/rules/noWith",
"lint/nursery/noBannedTypes":"https://docs.rome.tools/lint/rules/noBannedTypes",
"lint/nursery/noConstEnum": "https://docs.rome.tools/lint/rules/noConstEnum",
"lint/nursery/noConstructorReturn": "https://docs.rome.tools/lint/rules/noConstructorReturn",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

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

54 changes: 54 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::JsWithStatement;

use rome_rowan::AstNode;

declare_rule! {
/// Disallow `with` statements in non-strict contexts.
///
/// The `with` statement is potentially problematic because it adds members of an object to the current
/// scope, making it impossible to tell what a variable inside the block actually refers to.
///
/// ## Examples
///
/// ### Invalid
///
/// ```cjs,expect_diagnostic
/// function f() {
/// with (point) {
/// r = Math.sqrt(x * x + y * y); // is r a member of point?
/// }
/// }
/// ```
pub(crate) NoWith {
version: "12.0.0",
name: "noWith",
recommended: true,
}
}

impl Rule for NoWith {
type Query = Ast<JsWithStatement>;
type State = ();
type Signals = Option<Self::State>;
type Options = ();

fn run(_ctx: &RuleContext<Self>) -> Option<Self::State> {
Some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
Some(RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Unexpected use of "<Emphasis>"with"</Emphasis>" statement."
},
).note(
"The with statement is potentially problematic because it adds members of an object to the current\nscope, making it impossible to tell what a variable inside the block actually refers to."
))
}
}
5 changes: 5 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function f() {
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
}
34 changes: 34 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.cjs.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.cjs
---
# Input
```js
function f() {
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
}
```

# Diagnostics
```
invalid.cjs:2:3 lint/nursery/noWith ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Unexpected use of with statement.

1 │ function f() {
> 2 │ with (point) {
│ ^^^^^^^^^^^^^^
> 3 │ r = Math.sqrt(x * x + y * y); // is r a member of point?
> 4 │ }
│ ^
5 │ }

i The with statement is potentially problematic because it adds members of an object to the current
scope, making it impossible to tell what a variable inside the block actually refers to.


```


12 changes: 12 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```js
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
```


10 changes: 10 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noWith/valid.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```js
const r = ({x, y}) => Math.sqrt(x * x + y * y);
```


19 changes: 12 additions & 7 deletions crates/rome_service/src/configuration/linter/rules.rs

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

11 changes: 11 additions & 0 deletions editors/vscode/configuration_schema.json

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

1 change: 0 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ codegen:
cargo codegen-configuration
cargo codegen-schema
cargo codegen-bindings
cargo codegen-aria

documentation:
cargo lintdoc
Expand Down
5 changes: 5 additions & 0 deletions npm/backend-jsonrpc/src/workspace.ts

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

11 changes: 11 additions & 0 deletions npm/rome/configuration_schema.json

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

6 changes: 6 additions & 0 deletions website/src/pages/lint/rules/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ Disallow the use of <code>var</code>
Disallow returning a value from a function with the return type 'void'
</section>
<section class="rule">
<h3 data-toc-exclude id="noWith">
<a href="/lint/rules/noWith">noWith</a>
</h3>
Disallow <code>with</code> statements in non-strict contexts.
</section>
<section class="rule">
<h3 data-toc-exclude id="useAriaPropTypes">
<a href="/lint/rules/useAriaPropTypes">useAriaPropTypes</a>
</h3>
Expand Down
42 changes: 42 additions & 0 deletions website/src/pages/lint/rules/noWith.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Lint Rule noWith
parent: lint/rules/index
---

# noWith (since v12.0.0)

Disallow `with` statements in non-strict contexts.

The `with` statement is potentially problematic because it adds members of an object to the current
scope, making it impossible to tell what a variable inside the block actually refers to.

## Examples

### Invalid

```js
function f() {
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
}
```

<pre class="language-text"><code class="language-text">nursery/noWith.js:2:3 <a href="https://docs.rome.tools/lint/rules/noWith">lint/nursery/noWith</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Unexpected use of </span><span style="color: Tomato;"><strong>with</strong></span><span style="color: Tomato;"> statement.</span>

<strong>1 │ </strong>function f() {
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> with (point) {
<strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong>
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> r = Math.sqrt(x * x + y * y); // is r a member of point?
<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> }
<strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong>
<strong>5 │ </strong>}
<strong>6 │ </strong>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">The with statement is potentially problematic because it adds members of an object to the current
</span><span style="color: rgb(38, 148, 255);"> </span><span style="color: rgb(38, 148, 255);"> </span><span style="color: rgb(38, 148, 255);">scope, making it impossible to tell what a variable inside the block actually refers to.</span>

</code></pre>

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载