no-import-assign

不允许分配给导入的绑定

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

ES 模块对导入绑定的更新会导致运行时错误。

规则详情

此规则警告导入绑定的分配、增量和减量。

此规则的错误代码示例:

/*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.

L9CfmI+BHDwYx59RS77HEHanEO40ClscOusGIf+RQNhowUc7Zz7f+Hb0VCfK93W9

/*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.

何时不使用

0cGnaIg3gQrdLEQBgBUV3uzKCbGPtnO0ebjO+E4PkRgcqjtWbvleHIX1oUsX/n3WAato/T5uDXymnwWvrlcqKLqUWep4j/SGX6D/REP+T/Os/6+4Y4O9H+l7gY0udS3K