Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Latest commit

 

History

History
78 lines (66 loc) · 1.3 KB

no-top-level-side-effect.md

File metadata and controls

78 lines (66 loc) · 1.3 KB

No top level side effect (no-top-level-side-effect)

Based on eslint-plugin-toplevel.

Rule Details

Lets you disallow top level side effects.

Examples of incorrect code for this rule:

console.log('hello world');
for (let i = 0; i < 10; i++) {
  s += i;
}
console.log(s);
fetch('/api')
  .then((res) => res.text())
  .then(console.log);

Examples of correct code for this rule:

export default function () {
  console.log('hello world');
  for (let i = 0; i < 10; i++) {
    s += i;
  }
  console.log(s);
  fetch('/api')
    .then((res) => res.text())
    .then(console.log);
}
(function () {
  console.log('hello world');
  for (let i = 0; i < 10; i++) {
    s += i;
  }
  console.log(s);
  fetch('/api')
    .then((res) => res.text())
    .then(console.log);
})();
module.exports = () => {
  console.log('hello world');
  for (let i = 0; i < 10; i++) {
    s += i;
  }
  console.log(s);
  fetch('/api')
    .then((res) => res.text())
    .then(console.log);
};
(() => {
  console.log('hello world');
  for (let i = 0; i < 10; i++) {
    s += i;
  }
  console.log(s);
  fetch('/api')
    .then((res) => res.text())
    .then(console.log);
})();

When Not To Use It

If you want to allow top level side effects