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

fix(semantic_analyzers): fix the false positive for noConstAssign #3747

Merged
merged 13 commits into from
Nov 18, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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_js_syntax::{JsFormalParameter, JsIdentifierAssignment, JsVariableDeclaration};
use rome_rowan::{AstNode, TextRange};

declare_rule! {
Expand Down Expand Up @@ -61,13 +61,14 @@ impl Rule for NoConstAssign {
let model = ctx.model();

let declared_binding = model.declaration(node)?;
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());
for node in declared_binding.syntax().ancestors() {
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
if let Some(_) = JsFormalParameter::cast_ref(&node) {
mzbac marked this conversation as resolved.
Show resolved Hide resolved
return None;
}
if let Some(variable_declaration) = JsVariableDeclaration::cast_ref(&node) {
if variable_declaration.is_const() {
return Some(declared_binding.syntax().text_trimmed_range());
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions crates/rome_js_analyze/tests/specs/nursery/noConstAssign.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const a = 1;
a = 2;

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

const b = 2, c = 43;
const b = 2,
c = 43;
b = 4;
++b;
b += 45;
b--;
function f() {
b++;
b++;
}
function f(d) {
b++;
}
const fn = (val) => {
val = 0;
};

```

# Diagnostics
Expand All @@ -27,7 +36,7 @@ noConstAssign.js:2:1 lint/nursery/noConstAssign ━━━━━━━━━━
> 2 │ a = 2;
│ ^
3 │
4 │ const b = 2, c = 43;
4 │ const b = 2,

i This is where the variable is defined as constant

Expand All @@ -40,119 +49,145 @@ noConstAssign.js:2:1 lint/nursery/noConstAssign ━━━━━━━━━━
```

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

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

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

i This is where the variable is defined as constant

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


```

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

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

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

i This is where the variable is defined as constant

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


```

```
noConstAssign.js:7:1 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
noConstAssign.js:8: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() {
6 │ b = 4;
7 │ ++b;
> 8 │ b += 45;
│ ^
9 │ b--;
10 │ function f() {

i This is where the variable is defined as constant

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


```

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

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

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

i This is where the variable is defined as constant

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


```

```
noConstAssign.js:11:2 lint/nursery/noConstAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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

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

i This is where the variable is defined as constant

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


```

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

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

8 │ b--;
9 │ function f() {
> 10 │ b++;
│ ^
11 │ }
12 │ }
13 │ function f(d) {
> 14 │ b++;
│ ^
15 │ }
16 │ const fn = (val) => {

i This is where the variable is defined as constant

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


```
Expand Down