no-import-assign
禁止分配给导入的绑定
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
ES 模块对导入绑定的更新会导致运行时错误。
🌐 The updates of imported bindings by ES Modules cause runtime errors.
规则详情
🌐 Rule Details
此规则警告导入绑定的分配、增量和减量。
🌐 This rule warns the assignments, increments, and decrements of imported bindings.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*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.
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*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.
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
何时不使用
🌐 When Not To Use It
如果你不想收到有关修改导入绑定的通知,你可以禁用此规则。
🌐 If you don’t want to be notified about modifying imported bindings, you can disable this rule.
由 TypeScript 处理
使用 TypeScript 时禁用此规则是安全的,因为 TypeScript 的编译器强制执行此检查。
Note that the compiler will not catch the Object.assign() case. Thus, if you use Object.assign() in your codebase, this rule will still provide some value.
版本
此规则是在 ESLint v6.4.0 中引入。