Implicit Globals vs Declaration Enforcement: no‑implicit‑globals vs no‑var
0 reputation · 03 Jan 2020, 11:01 UTC
0 reputation · 03 Jan 2020, 11:01 UTC
In projects that exclusively use ES modules, the no-implicit-globals rule still flags top‑level function declarations as implicit globals, even though module scope prevents global leakage.
The rule’s documentation states it is intended for scripts, but its implementation does not automatically detect sourceType. This leads to false positives when the configuration uses sourceType: "module".
Should top‑level function declarations in modules be exempt from the warning, or should the rule enforce a stricter declaration style regardless of module boundaries?
no-implicit-globals regarding top‑level function declarations in ES modules?sourceType automatically?34775 reputation · 03 Jan 2020, 21:55 UTC
no-implicit-globals is intended for scripts, where a top-level function declaration becomes a property of the global object. In ES modules, top-level declarations are module-scoped and do not leak to the global object, so the rule’s premise does not apply. Nevertheless, some ESLint versions still report these declarations because the implementation inspects Program-level nodes without consulting languageOptions.sourceType. For a module-only project, the practical resolution is to disable no-implicit-globals. Do not treat no-var as a replacement; it enforces let/const and does not address global leakage.
Confirmed: The rule documentation describes a script-oriented goal, and no-var is a separate stylistic rule with no bearing on global scope.
Version-dependent: Whether your installed ESLint flags module top-level functions depends on implementation details. Historically, the rule has not checked sourceType automatically, but exact behavior can vary by major version and parser configuration. Any change to auto-detect modules should be treated as version-specific until verified.
eslint --print-config src/example.js
Confirm that languageOptions.sourceType is "module" and that no-implicit-globals is active.// repro.js
function helper() {}
export default helper;
Run ESLint against the file to see if the rule reports the declaration.no-var enabled only if you want let/const enforcement; it solves a different problem.There is no confirmed knowledge that ESLint has modified no-implicit-globals to detect sourceType automatically in a specific release. Treat any such change as version-specific and verify against your installed version’s rule source or changelog. I cannot confirm the existence of a specific open issue tracking this ambiguity; avoid citing one without direct verification.
Which ESLint major version are you running? The answer determines whether the rule actually fires in your environment and whether the configuration override is necessary.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.