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: Prevent/warn about the usage of window in favor of self.

See original GitHub issue

Please describe what the rule should do: Prevent/warn about the usage of window in favor of self, with self being a proxy to the current global context and should work across all environments in the browser.

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

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

  • Enforces portability across non DOM environments

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

// should warn/error on use of window and suggest usage of self instead
if (window.crypto && window.crypto.getRandomValues) {
    // Modern browsers
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      window.crypto.getRandomValues(arr);
      return arr;
    };
  } else if (window.msCrypto && window.msCrypto.getRandomValues) {
    // IE
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      window.msCrypto.getRandomValues(arr);
      return arr;
    };
  } else {
    // Old junk
    Rand.prototype._rand = function() {
      throw new Error('Not implemented yet');
    };
  }

===============

// should not warn/error on the usage of self
if (self.crypto && self.crypto.getRandomValues) {
    // Modern browsers
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      self.crypto.getRandomValues(arr);
      return arr;
    };
  } else if (self.msCrypto && self.msCrypto.getRandomValues) {
    // IE
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      self.msCrypto.getRandomValues(arr);
      return arr;
    };
  } else {
    // Old junk
    Rand.prototype._rand = function() {
      throw new Error('Not implemented yet');
    };
  }

Why should this rule be included in ESLint (instead of a plugin)? There is a large surface of non DOM related APIs being added to browsers that work across DOM and non DOM (WebWorker/ServiceWorker) environments. In many cases window is used as a reference to the global context to test for the presence of those features which makes it non-portable across non DOM environments. As we move towards browsers with more and more capabilities beyond the DOM, this is going to become more of a problem. An eslint rule should alleviate the transition and enforce a very important best practice, having it as a plugin would mean less penetration and adoption across the board.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
platinumazurecommented, Mar 13, 2017

@dryajov Haha, that’s my mistake then, I thought you had already edited the issue. Sorry about that!

1reaction
dryajovcommented, Mar 13, 2017

@platinumazure , I think there is no confusion, im going to enhance no-restricted-globals to take a custom message for globals, which is what has been suggested. I just haven’t gotten around changing this issue, will try to get to it today. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window.self - Web APIs - MDN Web Docs - Mozilla
The Window.self read-only property returns the window itself, as a WindowProxy. It can be used with dot notation on a window object (that...
Read more >
RHSA-2019:0212 - Security Advisory - Red Hat 客户门户网站
Red Hat Product Security has rated this update as having a security impact of Moderate. A Common Vulnerability Scoring System (CVSS) base score, ......
Read more >
HCEM Crisis Communications Plan 2022 - Hamilton County, IN
The Hamilton County Emergency Operations Plan serves as the strategic foundation for Hamilton. County's Emergency Management Plan.
Read more >
Options Manager - TechDocs - Broadcom Inc.
Specifies the URL of the Support Automation web application, which CA SDM uses to communicate with the Support Automation main server. The host ......
Read more >
AT&T Motivate Max User Guide
Thank you for purchasing your new U668AA Smartphone . The following topics explain how best to use this guide to get the most...
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