]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-import-assign.md
c19cfd200c1de780f31814fc2e7c78210a5132d4
[pve-eslint.git] / eslint / docs / src / rules / no-import-assign.md
1 ---
2 title: no-import-assign
3 layout: doc
4 rule_type: problem
5 ---
6
7
8
9 The updates of imported bindings by ES Modules cause runtime errors.
10
11 ## Rule Details
12
13 This rule warns the assignments, increments, and decrements of imported bindings.
14
15 Examples of **incorrect** code for this rule:
16
17 ::: incorrect
18
19 ```js
20 /*eslint no-import-assign: "error"*/
21
22 import mod, { named } from "./mod.mjs"
23 import * as mod_ns from "./mod.mjs"
24
25 mod = 1 // ERROR: 'mod' is readonly.
26 named = 2 // ERROR: 'named' is readonly.
27 mod_ns.named = 3 // ERROR: The members of 'mod_ns' are readonly.
28 mod_ns = {} // ERROR: 'mod_ns' is readonly.
29 // Can't extend 'mod_ns'
30 Object.assign(mod_ns, { foo: "foo" }) // ERROR: The members of 'mod_ns' are readonly.
31 ```
32
33 :::
34
35 Examples of **correct** code for this rule:
36
37 ::: correct
38
39 ```js
40 /*eslint no-import-assign: "error"*/
41
42 import mod, { named } from "./mod.mjs"
43 import * as mod_ns from "./mod.mjs"
44
45 mod.prop = 1
46 named.prop = 2
47 mod_ns.named.prop = 3
48
49 // Known Limitation
50 function test(obj) {
51 obj.named = 4 // Not errored because 'obj' is not namespace objects.
52 }
53 test(mod_ns) // Not errored because it doesn't know that 'test' updates the member of the argument.
54 ```
55
56 :::
57
58 ## When Not To Use It
59
60 If you don't want to be notified about modifying imported bindings, you can disable this rule.