Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/HKalbasi/eslint-plugin-toplevel
An eslint plugin for disallow side effect at module toplevel
https://github.com/HKalbasi/eslint-plugin-toplevel
Last synced: 3 months ago
JSON representation
An eslint plugin for disallow side effect at module toplevel
- Host: GitHub
- URL: https://github.com/HKalbasi/eslint-plugin-toplevel
- Owner: HKalbasi
- License: gpl-3.0
- Created: 2019-09-09T10:59:19.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-12T21:24:00.000Z (8 months ago)
- Last Synced: 2024-04-14T11:15:03.025Z (7 months ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 17
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-eslint - toplevel - An eslint plugin for disallow side effect at module toplevel. (Plugins / Practices and Specific ES Features)
README
# eslint-plugin-toplevel
An eslint plugin for disallow side effect at module toplevel## Why?
Side effect in toplevel of a module are hard to reason, and [behave diffrently](https://developpaper.com/explain-the-difference-between-commonjs-and-es6-modules-in-cyclic-loading-processing/) in CommonJS and ES6 modules. And there is no way to import a module without it's side effects. Solution is simple: **Don't make side effect at top level**.## Example
### Bad
```JS
console.log('hello world');
let s=0;
for (let i=0;i<10;i++) {
s += i;
}
console.log(s);
fetch('/api').then(res=>res.text()).then(console.log);
```### Good
```JS
export async function main() {
console.log('hello world');
let s=0;
for (let i=0;i<10;i++) {
s += i;
}
console.log(s);
const res = await fetch('/api');
console.log(await res.text());
}
```And then in html:
```HTML
import { main } from "/js/main.mjs";
main();```
Or in nodejs make a runner.mjs file:
```JS
import { main } from "./main.mjs";
main();
```## Rules
There are three rules in this plugin:
### no-toplevel-var
Disallow var usage at toplevel module.
Goal of this rule is to prevent modules to have internal state.
### no-toplevel-let
similiar to `no-toplevel-var`
### no-toplevel-side-effect
Disallow any side effect at top level## Contributing
I'd love to accept your patches and contributions to this project.
There are many ways you can contribute. For example:
* Add tests
* Fix bugs
* Add option to ignore `module.exports` assigns in `no-toplevel-side-effect` rule
* Add good documention