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

feat(rome_js_analyze): rule noConstAssign #3409

Merged
merged 3 commits into from
Oct 17, 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 @@ -60,6 +60,7 @@ define_dategories! {
"lint/nursery/useKeyWithClickEvents": "https://rome.tools/docs/lint/rules/useKeyWithClickEvents",
"lint/nursery/noRestrictedGlobals": "https://rome.tools/docs/lint/rules/noRestrictedGlobals",
"lint/nursery/noPositiveTabindex": "https://rome.tools/docs/lint/rules/noPositiveTabindex",
"lint/nursery/noConstAssign": "https://rome.tools/docs/lint/rules/noConstAssign",
"lint/nursery/noExplicitAny": "https://rome.tools/docs/lint/rules/noExplicitAny",
"lint/style/noNegationElse": "https://rome.tools/docs/lint/rules/noNegationElse",
"lint/style/noShoutyConstants": "https://rome.tools/docs/lint/rules/noShoutyConstants",
Expand Down
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,93 @@
use crate::semantic_services::Semantic;
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::{JsIdentifierAssignment, JsVariableDeclaration};
use rome_rowan::{AstNode, TextRange};

declare_rule! {
/// Prevents from having `const` variables being re-assigned.
///
/// Trying to assign a value to a `const` will cause an `TypeError` when the code is executed.
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// const a = 1;
/// a = 4;
/// ```
///
/// ```js,expect_diagnostic
/// const a = 2;
/// a += 1;
/// ```
///
/// ```js,expect_diagnostic
/// const a = 1;
/// ++a;
/// ```
///
/// ```js,expect_diagnostic
/// const a = 1, b = 2;
///
/// a = 2;
/// ```
///
/// ### Valid
///
/// ```js
/// const a = 10;
/// let b = 10;
/// b = 20;
/// ```
///
pub(crate) NoConstAssign {
version: "10.0.0",
name: "noConstAssign",
recommended: false,
}
}

impl Rule for NoConstAssign {
type Query = Semantic<JsIdentifierAssignment>;
type State = TextRange;
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let model = ctx.model();

let declared_binding = model.declaration(node)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried that this will have a significant performance cost because it requires querying the model for every single identifier assignment.

I'm not familiar with our semantic model but wonder if it would be cheaper to

  • Match on all variable declarations
  • test if it is a const declaration
  • test if there are any write references for that variable.

That could be cheaper assuming there are fewer declarations than assignments. But this is only a hypothesis and also heavily depends on how the semantic model is implemented internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should able to use the semantic model straight from the rule. Let me if I am up to the task

if let Some(variable_declaration) = declared_binding
.syntax()
.ancestors()
.find_map(|ancestor| JsVariableDeclaration::cast_ref(&ancestor))
{
if variable_declaration.is_const() {
return Some(declared_binding.syntax().text_trimmed_range());
}
}

None
}

fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
let name = node.name_token().ok()?;
let name = name.text_trimmed();
Some(
RuleDiagnostic::new(
rule_category!(),
node.syntax().text_trimmed_range(),
markup! {"Can't assign "<Emphasis>{name}</Emphasis>" because it's a constant"},
)
.secondary(
state,
markup! {"This is where the variable is defined as constant"},
),
)
}
}
11 changes: 11 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noConstAssign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const a = 1;
a = 2;

const b = 2, c = 43;
b = 4;
++b;
b += 45;
b--;
function f() {
b++;
}
160 changes: 160 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noConstAssign.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: noConstAssign.js
---
# Input
```js
const a = 1;
a = 2;

const b = 2, c = 43;
b = 4;
++b;
b += 45;
b--;
function f() {
b++;
}
```

# Diagnostics
```
noConstAssign.js:2:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign a because it's a constant

1 │ const a = 1;
> 2 │ a = 2;
│ ^
3 │
4 │ const b = 2, c = 43;

i This is where the variable is defined as constant

> 1 │ const a = 1;
│ ^
2 │ a = 2;
3 │


```

```
noConstAssign.js:5:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign b because it's a constant

4 │ const b = 2, c = 43;
> 5 │ b = 4;
│ ^
6 │ ++b;
7 │ b += 45;

i This is where the variable is defined as constant

2 │ a = 2;
3 │
> 4 │ const b = 2, c = 43;
│ ^
5 │ b = 4;
6 │ ++b;


```

```
noConstAssign.js:6:3 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign b because it's a constant

4 │ const b = 2, c = 43;
5 │ b = 4;
> 6 │ ++b;
│ ^
7 │ b += 45;
8 │ b--;

i This is where the variable is defined as constant

2 │ a = 2;
3 │
> 4 │ const b = 2, c = 43;
│ ^
5 │ b = 4;
6 │ ++b;


```

```
noConstAssign.js:7:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign b because it's a constant

5 │ b = 4;
6 │ ++b;
> 7 │ b += 45;
│ ^
8 │ b--;
9 │ function f() {

i This is where the variable is defined as constant

2 │ a = 2;
3 │
> 4 │ const b = 2, c = 43;
│ ^
5 │ b = 4;
6 │ ++b;


```

```
noConstAssign.js:8:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign b because it's a constant

6 │ ++b;
7 │ b += 45;
> 8 │ b--;
│ ^
9 │ function f() {
10 │ b++;

i This is where the variable is defined as constant

2 │ a = 2;
3 │
> 4 │ const b = 2, c = 43;
│ ^
5 │ b = 4;
6 │ ++b;


```

```
noConstAssign.js:10:5 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Can't assign b because it's a constant

8 │ b--;
9 │ function f() {
> 10 │ b++;
│ ^
11 │ }

i This is where the variable is defined as constant

2 │ a = 2;
3 │
> 4 │ const b = 2, c = 43;
│ ^
5 │ b = 4;
6 │ ++b;


```


4 changes: 3 additions & 1 deletion 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.

10 changes: 10 additions & 0 deletions editors/vscode/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@
}
]
},
"noConstAssign": {
"anyOf": [
{
"$ref": "#/definitions/RuleConfiguration"
},
{
"type": "null"
}
]
},
"noDangerouslySetInnerHtml": {
"anyOf": [
{
Expand Down
2 changes: 2 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.

1 change: 1 addition & 0 deletions rome.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"noNewSymbol": "error",
"noVoidElementsWithChildren": "error",
"noDangerouslySetInnerHtmlWithChildren": "error",
"noConstAssign": "error",
"noAutofocus": "error",
"useKeyWithClickEvents": "error"
}
Expand Down
1 change: 1 addition & 0 deletions website/playground/src/romeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ self.addEventListener("message", async (e) => {
noUselessFragments: "error",
noVoidElementsWithChildren: "error",
noDangerouslySetInnerHtmlWithChildren: "error",
noConstAssign: "error",
},
},
};
Expand Down
7 changes: 7 additions & 0 deletions website/src/docs/lint/rules/index.md

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

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