question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule: no-implicit-module

See original GitHub issue

Please describe what the rule should do:

Disallow module scope variable declaration (variable that are not declared in function or Object).

What category of rule is this? (place an “X” next to just one item)

[ ] Enforces code style [X] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

This should warn:

abc.js

var a = 0;

function test () {
  return a + 1;
}

While this should be OK:

efg.js

function test(a) {
  return a + 1;
}

Why should this rule be included in ESLint (instead of a plugin)?

Like no-implicit-globals disallow global variable and function declaration, it could be a good practice to disallow global variable inside a module scope (global variable are often a bad practice, not only in JS)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Oct 16, 2017

I see – so if I’m understanding correctly, the goal would be to avoid global mutable state in modules.

I have a few thoughts on the proposal:

  • The rule would report code like this, where mutable state is used as an implementation detail (e.g. for caching) without being exposed as part of the module’s API.

    let foo;
    
    export function getFoo() {
        if (!foo) {
            // lazy load
            foo = expensiveComputation();
        }
        return foo;
    }
    

    I agree that it’s almost always a bad idea to expose global mutable state. I don’t think it’s necessarily a problem to have global mutable state, provided that the state is an implementation detail and the module’s API acts as if it were immutable. The proposed rule would report the latter, even if the goal is to prevent the former.

  • It’s worth noting that module-scoped variables are not the only way to accidentally expose mutable state. For example, a user could modify a property of a module-scoped variable, and it wouldn’t be caught by this rule.

  • Bikeshed: I’m not a fan of the name no-implicit-module. I assume no-implicit-module was a modification of the existing no-implicit-global rule, but I don’t think that wording makes sense (there isn’t anything implicit going on, and the module itself isn’t the problem.) Maybe something like no-mutable-module-bindings would be better.

0reactions
thubacommented, Nov 30, 2017

Ok, I understand, thank you for explanation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-implicit-globals - ESLint - Pluggable JavaScript Linter
This rule is mostly useful for browser scripts. Top-level declarations in ES modules and CommonJS modules create module-scoped variables. ES modules also have ......
Read more >
Eslint Typescript "No Implicit Any" rule
I have the problem with the "noImplicitAny" rule, which I use in tsconfig.json , but I am unable check for this in eslint....
Read more >
no-implicit-dependencies - Rule
Rule : no-implicit-dependencies​​ Disallows importing transient dependencies and modules installed above your package's root directory.
Read more >
Support wildcard whitelisting for no-implicit-dependencies ...
Would be nice to be able to whitelist the dependencies that shouldn't be checked by the rule using wildcards: @module/* .
Read more >
no-implicit-any-catch - TypeScript ESLint
no -implicit-any-catch ... Disallow usage of the implicit any type in catch clauses. ... Some problems reported by this rule are automatically fixable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found