This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
feat(rome_js_analyzer): add noWith
rule
#4025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30db79b
feat(linter): added no with statement
ktfth a637463
feat(linter): added formatted code
ktfth 3896380
feat(linter): added test case for with statement
ktfth 49567a8
feat(rome_js_analyze): added the structure of no with statements
ktfth 1e451e7
feat(rome_js_analyze): removed codegen for aria
ktfth c629cba
feat(rome_js_analyze): added resource to support rule
ktfth baa8b52
feat(rome_js_analyzer): updated suggestions based on review
ktfth fbb9a06
feat(rome_js_analayzer): removed dead code
ktfth c0e7957
feat(rome_js_analyzer): updated tests
ktfth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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.
This file contains hidden or 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,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
5
crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.cjs
This file contains hidden or 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,5 @@ | ||
function f() { | ||
with (point) { | ||
r = Math.sqrt(x * x + y * y); // is r a member of point? | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.cjs.snap
This file contains hidden or 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,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
12
crates/rome_js_analyze/tests/specs/nursery/noWith/invalid.js.snap
This file contains hidden or 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,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
10
crates/rome_js_analyze/tests/specs/nursery/noWith/valid.js.snap
This file contains hidden or 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,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); | ||
``` | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,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;">></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;">></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;">></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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.