Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow assigning to imported bindings (no-import-assign) #1412

Closed
feross opened this issue Sep 14, 2019 · 2 comments
Closed

Disallow assigning to imported bindings (no-import-assign) #1412

feross opened this issue Sep 14, 2019 · 2 comments

Comments

@feross
Copy link
Member

feross commented Sep 14, 2019

https://eslint.org/docs/rules/no-import-assign

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' is readonly.
mod_ns = {}      // ERROR: 'mod_ns' is 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.
@mightyiam
Copy link
Member

I'm pro this rule. Disallows code that should probably never be written.

Regarding the limitation: it seems like with TypeScript that would be possible to prevent as well.

@feross feross modified the milestones: standard 15, standard 16 Oct 22, 2020
@feross
Copy link
Member Author

feross commented Oct 29, 2020

Only 1 failing ecosystem repo. This is the code:

import '../../../polyfill'

import fetch, * as ponyfill from '../../..'
import addModuleSuite from '../../module.spec'

ponyfill.defaultExport = fetch

addModuleSuite('Browser: import on Webpack bundle', ponyfill)

Seems easy enough for them to add an // eslint-ignore comment when doing something hacky like this. Otherwise, this is a good rule.

This will ship in standard 16!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

2 participants