Skip to content

Latest commit

 

History

History
55 lines (36 loc) · 1.25 KB

no-import-assign.md

File metadata and controls

55 lines (36 loc) · 1.25 KB
规则名 规则类型
no-import-assign
problem

The updates of imported bindings by ES Modules cause runtime errors.

规则详解

This rule warns the assignments, increments, and decrements of imported bindings.

此规则的 错误 代码实例:

/*eslint no-import-assign: "error"*/

import mod, { named } from "./mod.mjs"
import * as mod_ns from "./mod.mjs"

mod = 1          // ERROR: 'mod' is readonly.
named = 2        // ERROR: 'named' is readonly.
mod_ns.named = 3 // ERROR: The members of 'mod_ns' are readonly.
mod_ns = {}      // ERROR: 'mod_ns' is readonly.
// Can't extend 'mod_ns'
Object.assign(mod_ns, { foo: "foo" }) // ERROR: The members of 'mod_ns' are readonly.

此规则的 正确 代码实例:

::: correct

/*eslint no-import-assign: "error"*/

import mod, { named } from "./mod.mjs"
import * as mod_ns from "./mod.mjs"

mod.prop = 1
named.prop = 2
mod_ns.named.prop = 3

// Known Limitation
function test(obj) {
    obj.named = 4 // Not errored because 'obj' is not namespace objects.
}
test(mod_ns) // Not errored because it doesn't know that 'test' updates the member of the argument.

禁用建议

If you don't want to be notified about modifying imported bindings, you can disable this rule.