noImportAssign (since v0.9.0)
This rule is recommended by Rome.
Disallow assigning to imported bindings
Examples
Invalid
import x from "y";
x = 1; error[js/noImportAssign]: The imported variable x is read-only
┌─ js/noImportAssign.js:2:1
│
1 │ import x from "y";
│ - The variable is imported here
2 │ x = 1;
│ ^
= note: Use a local variable instead of reassigning an import.
import y from "y";
[y] = 1; error[js/noImportAssign]: The imported variable y is read-only
┌─ js/noImportAssign.js:2:2
│
1 │ import y from "y";
│ - The variable is imported here
2 │ [y] = 1;
│ ^
= note: Use a local variable instead of reassigning an import.
import z from "y";
({ z } = 1); /// ```
```js,expect_diagnostic
import a from "y";
[...a] = 1; error: unterminated template literal
┌─ js/noImportAssign.js:3:4
│
3 │ ```js,expect_diagnostic
│ ┌────^
4 │ │ import a from "y";
5 │ │ [...a] = 1;
6 │ │
│ └^
import b from "y";
({ ...b } = 1); error[js/noImportAssign]: The imported variable b is read-only
┌─ js/noImportAssign.js:2:7
│
1 │ import b from "y";
│ - The variable is imported here
2 │ ({ ...b } = 1);
│ ^
= note: Use a local variable instead of reassigning an import.
import c from "y";
for (c in y) {}; error[js/noImportAssign]: The imported variable c is read-only
┌─ js/noImportAssign.js:2:6
│
1 │ import c from "y";
│ - The variable is imported here
2 │ for (c in y) {};
│ ^
= note: Use a local variable instead of reassigning an import.
import d from "y";
d += 1; error[js/noImportAssign]: The imported variable d is read-only
┌─ js/noImportAssign.js:2:1
│
1 │ import d from "y";
│ - The variable is imported here
2 │ d += 1;
│ ^
= note: Use a local variable instead of reassigning an import.
import * as e from "y";
e = 1; error[js/noImportAssign]: The imported variable e is read-only
┌─ js/noImportAssign.js:2:1
│
1 │ import * as e from "y";
│ - The variable is imported here
2 │ e = 1;
│ ^
= note: Use a local variable instead of reassigning an import.