fix(semantic_analyzers): fix the false positive for `noConstAssign`
MODIFY
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_const_assign.rs
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_const_assign.rs
@@ -2,7 +2,12 @@ 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::{
+ JsAnyArrayBindingPatternElement, JsAnyObjectBindingPatternMember,
+ JsArrayBindingPatternElementList, JsForVariableDeclaration, JsIdentifierAssignment,
+ JsIdentifierBinding, JsObjectBindingPatternPropertyList, JsVariableDeclaration,
+ JsVariableDeclarator, JsVariableDeclaratorList,
+};
use rome_rowan::{AstNode, TextRange};
declare_rule! {
@@ -61,15 +66,33 @@ 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 let Some(possible_declarator) = declared_binding.syntax().ancestors().find(|node| {
+ !JsAnyObjectBindingPatternMember::can_cast(node.kind())
+ && !JsObjectBindingPatternPropertyList::can_cast(node.kind())
+ && !JsAnyArrayBindingPatternElement::can_cast(node.kind())
+ && !JsArrayBindingPatternElementList::can_cast(node.kind())
+ && !JsIdentifierBinding::can_cast(node.kind())
+ }) {
+ if JsVariableDeclarator::can_cast(possible_declarator.kind()) {
+ let possible_declaration = possible_declarator.parent()?;
+ if let Some(js_for_variable_declaration) =
+ JsForVariableDeclaration::cast_ref(&possible_declaration)
+ {
+ if js_for_variable_declaration.is_const() {
+ return Some(declared_binding.syntax().text_trimmed_range());
+ }
+ } else if let Some(js_variable_declaration) =
+ JsVariableDeclaratorList::cast_ref(&possible_declaration)
+ .and_then(|declaration| declaration.syntax().parent())
+ .and_then(JsVariableDeclaration::cast)
{
- if variable_declaration.is_const() {
+ if js_variable_declaration.is_const() {
return Some(declared_binding.syntax().text_trimmed_range());
}
}
+ }
+ }
None
}

MODIFY
crates/rome_js_analyze/tests/specs/nursery/noConstAssign.js
crates/rome_js_analyze/tests/specs/nursery/noConstAssign.js
const a = 1;
a = 2;
-const b = 2, c = 43;
+const b = 2,
+ c = 43;
b = 4;
++b;
b += 45;
@@ -9,3 +10,46 @@ b--;
function f() {
b++;
}
+function f(d) {
+ b++;
+}
+const fn = (val) => {
+ val = 0;
+};
+
+const e = () => {
+ try {
+ foo();
+ } catch (err) {
+ err = 4;
+ }
+};
+
+const f = (...rest) => {
+ rest = 4;
+};
+
+const g = class bar {};
+bar = 1;
+
+const h = function foo() {
+ foo = 1;
+};
+
+const {
+ i,
+ j: { l },
+} = { i: 1, j: { l: 2 } };
+i = 4;
+l = 4;
+
+for (const k in [1, 2]) {
+ k = 4;
+}
+
+const [p, { q }] = [1, { q: 2 }];
+p = 3;
+q = 4;
+
+const { r, ...rest } = s;
+r = 4;

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

Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Anchen Li <anchen.li@pslm-qxy9gygyvj.lan>