ESLint rule to require `var` instead of `let`/`const` for top-level declarations (opt-in, experimental).
npm install eslint-plugin-top-level-varExperimental rule that requires var for top-level variable declarations.
> Why? Some bundlers (via opt-in flags) or niche perf experiments might prefer var at top-level. This rule lets you
> enforce that only at the program root. Not recommended for general codebases.
``bash`
npm i -D eslint @typescript-eslint/parser @typescript-eslint/utils @typescript-eslint/rule-testerin this repo
npm i
`js`
// .eslintrc.cjs
module.exports = {
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
plugins: ['top-level-var'],
rules: {
'no-var': 'off',
'prefer-const': 'off',
'top-level-var/top-level-var': ['warn', {
allowAutoFix: false,
allowPragmaIgnore: true
}]
}
};
`js
// eslint.config.mjs
import topLevelVar from 'eslint-plugin-top-level-var/flat';
export default [
...topLevelVar,
];
`
- allowAutoFix (boolean, default false): enable real autofix via --fix.allowPragmaIgnore
- (boolean, default true): allow / top-level-var:ignore /` above a declaration to skip.