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

Created on 3 Nov 2017  路  10Comments  路  Source: airbnb/javascript

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...

needs eslint rule changaddition

Most helpful comment

@DonkeyTronKilla thanks, that鈥檚 good to know. The env setting doesn鈥檛 affect the no-restricted-globals rule, though.

If there鈥檚 a way to make these mesh well, I鈥檓 on board!

All 10 comments

eslint probably should add a new env for service workers; for a workaround, you'd need to override no-restricted-globals to be https://github.com/airbnb/javascript/blob/7ff6303513b46e8b7dd0bc03327b138a6db7b3b4/packages/eslint-config-airbnb-base/rules/variables.js#L19 , but explicitly removing self - or, override this eslint rule in your file with /* eslint no-restricted-globals: 1 */.

One possible solution (if eslint adds a new service worker env), is to add an option to no-restricted-globals that allows specific globals to be allowed or restricted based on the env - but we'd have to file that on eslint itself.

I have the same problem

Use Number.isNaN/Number.isFinite instead of isNaN/isFinite.

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鈥檚 good to know. The env setting doesn鈥檛 affect the no-restricted-globals rule, though.

If there鈥檚 a way to make these mesh well, I鈥檓 on board!

FWIW, I'm using a filename-based override, would prefer if env: worker would just work instead.

````yaml
overrides:

  • files: ["*/.worker.js"]
    rules:
    no-restricted-globals: [0]
    ````

This happens because eslint-config-airbnb-base defines no-restricted-globals as

['error', 'isFinite', 'isNaN'].concat(confusingBrowserGlobals)

Where confusingBrowserGlobals imported from confusing-browser-globals is a long array with "confusing" names, including self.

It should be easy to override or use the default and remove self if eslint config is in JavaScript, but not so easy if it's a JSON file. In that case override as mentioned by "silverwind" is an easy and quick, but not as good fix.

i had the same thing, but fixed it by using environment in eslint configurations as follows:

env: {
    browser: true
}

This issue has been open since 2017, is there a plan to remove that word from the no-restricted-globals rule when the serviceworker environment is enabled? As amiramix pointed out, an override does work, but its far away from a fix.

@GitGangGuy since envs need to be configured at the app level, and there's no way inside this config to detect the env settings (since they're configured after our config is evaluated), i don't think there's any way to solve it here.

In other words, I think an eslint overrides block for each relevant env is the only, and best available, solution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

graingert picture graingert  路  3Comments

kozhevnikov picture kozhevnikov  路  3Comments

progre picture progre  路  3Comments

mismith picture mismith  路  3Comments