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.

no-restricted-globals doesn't allow self in service worker envs

See original GitHub issue

Upgrading to eslint-config-airbnb broke my service worker linting and the fix is not clear.

Inside a service worker, self is a global variable referring to the ServiceWorkerGlobalScope and is the standard method for setting event listeners. However, the new no-restricted-globals rule disallows the use of self, even if the top of the file includes /* global self */.

One possible solution to the problem would be to set const self = this explicitly on top of the file, but this poses an issue with es modules, where you cannot have this on the global context, and bundlers like rollup actually rewrite this as undefined.

Maybe removing self as a restricted global would be the way to go, or maybe the restricted globals should depend on the environment itself…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:30
  • Comments:10

github_iconTop GitHub Comments

4reactions
ghostcommented, Jan 9, 2018

It seems like eslint has had an environment for service workers for a while now: https://github.com/eslint/eslint/issues/2557. However simply adding the environment to my eslintconfig doesn’t seem to be ignoring the self reference in my service worker.

My eslintconfig:

module.exports = { extends: ['airbnb'], env: { browser: true, serviceworker: true }, rules: { indent: ['error', 4, { SwitchCase: 1, VariableDeclarator: 1, outerIIFEBody: 1 }], 'brace-style': ['error', 'stroustrup', { allowSingleLine: true }], 'consistent-return': 0, 'function-paren-newline': 0, 'react/jsx-filename-extension': ['error', { extensions: ['.js', '.jsx'] }], 'react/jsx-indent': ['error', 4], 'react/jsx-indent-props': ['error', 4], 'no-use-before-define': 0, 'import/prefer-default-export': 0, 'react/prop-types': 0, 'react/sort-comp': 0, }, };

3reactions
ljharbcommented, Aug 27, 2020

@DonkeyTronKilla thanks, that’s good to know. The env setting doesn’t affect the no-restricted-globals rule, though.

If there’s a way to make these mesh well, I’m on board!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected use of 'self' no-restricted-globals React
I don't understand why this answer is so highly rated. The question is about accessing the global object in a ServiceWorker. Both ServiceWorkers ......
Read more >
no-restricted-globals - ESLint - Pluggable JavaScript Linter
This rule allows you to specify global variable names that you don't want to use in your application. Options. This rule takes a...
Read more >
Environments and deployments - GitLab Docs
Provides a full history of deployments to each environment. Tracks your deployments, so you always know what is deployed on your servers. If...
Read more >
Paulo Freire's Pedagogy of the Oppressed
The justification for a pedagogy of the oppressed; the contradiction between the oppressors and the oppressed, and how it is overcome; oppression and...
Read more >
FOA-ETA-16-01 - U.S. Department of Labor
On a limited basis, this grant program will also enable applicants to work with companies on increasing the skills of existing workers in...
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