no-restricted-globals doesn't allow self in service worker envs
See original GitHub issueUpgrading 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:
- Created 6 years ago
- Reactions:30
- Comments:10
Top GitHub Comments
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, }, };
@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!