|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const MESSAGE_ID_ERROR = 'prefer-global-this/error'; |
| 4 | +const messages = { |
| 5 | + [MESSAGE_ID_ERROR]: 'Prefer `globalThis` over `{{value}}`.', |
| 6 | +}; |
| 7 | + |
| 8 | +const globalIdentifier = new Set(['window', 'self', 'global']); |
| 9 | + |
| 10 | +const windowSpecificEvents = new Set([ |
| 11 | + 'resize', |
| 12 | + 'blur', |
| 13 | + 'focus', |
| 14 | + 'load', |
| 15 | + 'scroll', |
| 16 | + 'scrollend', |
| 17 | + 'wheel', |
| 18 | + 'beforeunload', // Browsers might have specific behaviors on exactly `window.onbeforeunload =` |
| 19 | + 'message', |
| 20 | + 'messageerror', |
| 21 | + 'pagehide', |
| 22 | + 'pagereveal', |
| 23 | + 'pageshow', |
| 24 | + 'pageswap', |
| 25 | + 'unload', |
| 26 | +]); |
| 27 | + |
| 28 | +/** |
| 29 | +Note: What kind of API should be a windows-specific interface? |
| 30 | +
|
| 31 | +1. It's directly related to window (✅ window.close()) |
| 32 | +2. It does NOT work well as globalThis.x or x (✅ window.frames, window.top) |
| 33 | +
|
| 34 | +Some constructors are occasionally related to window (like Element !== iframe.contentWindow.Element), but they don't need to mention window anyway. |
| 35 | +
|
| 36 | +Please use these criteria to decide whether an API should be added here. Context: https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2410#discussion_r1695312427 |
| 37 | +*/ |
| 38 | +const windowSpecificAPIs = new Set([ |
| 39 | + // Properties and methods |
| 40 | + // https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-window-object |
| 41 | + 'name', |
| 42 | + 'locationbar', |
| 43 | + 'menubar', |
| 44 | + 'personalbar', |
| 45 | + 'scrollbars', |
| 46 | + 'statusbar', |
| 47 | + 'toolbar', |
| 48 | + 'status', |
| 49 | + 'close', |
| 50 | + 'closed', |
| 51 | + 'stop', |
| 52 | + 'focus', |
| 53 | + 'blur', |
| 54 | + 'frames', |
| 55 | + 'length', |
| 56 | + 'top', |
| 57 | + 'opener', |
| 58 | + 'parent', |
| 59 | + 'frameElement', |
| 60 | + 'open', |
| 61 | + 'originAgentCluster', |
| 62 | + 'postMessage', |
| 63 | + |
| 64 | + // Events commonly associated with "window" |
| 65 | + ...[...windowSpecificEvents].map(event => `on${event}`), |
| 66 | + |
| 67 | + // To add/remove/dispatch events that are commonly associated with "window" |
| 68 | + // https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow |
| 69 | + 'addEventListener', |
| 70 | + 'removeEventListener', |
| 71 | + 'dispatchEvent', |
| 72 | + |
| 73 | + // https://dom.spec.whatwg.org/#idl-index |
| 74 | + 'event', // Deprecated and quirky, best left untouched |
| 75 | + |
| 76 | + // https://drafts.csswg.org/cssom-view/#idl-index |
| 77 | + 'screen', |
| 78 | + 'visualViewport', |
| 79 | + 'moveTo', |
| 80 | + 'moveBy', |
| 81 | + 'resizeTo', |
| 82 | + 'resizeBy', |
| 83 | + 'innerWidth', |
| 84 | + 'innerHeight', |
| 85 | + 'scrollX', |
| 86 | + 'pageXOffset', |
| 87 | + 'scrollY', |
| 88 | + 'pageYOffset', |
| 89 | + 'scroll', |
| 90 | + 'scrollTo', |
| 91 | + 'scrollBy', |
| 92 | + 'screenX', |
| 93 | + 'screenLeft', |
| 94 | + 'screenY', |
| 95 | + 'screenTop', |
| 96 | + 'screenWidth', |
| 97 | + 'screenHeight', |
| 98 | + 'devicePixelRatio', |
| 99 | +]); |
| 100 | + |
| 101 | +const webWorkerSpecificAPIs = new Set([ |
| 102 | + // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface |
| 103 | + 'addEventListener', |
| 104 | + 'removeEventListener', |
| 105 | + 'dispatchEvent', |
| 106 | + |
| 107 | + 'self', |
| 108 | + 'location', |
| 109 | + 'navigator', |
| 110 | + 'onerror', |
| 111 | + 'onlanguagechange', |
| 112 | + 'onoffline', |
| 113 | + 'ononline', |
| 114 | + 'onrejectionhandled', |
| 115 | + 'onunhandledrejection', |
| 116 | + |
| 117 | + // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-dedicatedworkerglobalscope-interface |
| 118 | + 'name', |
| 119 | + 'postMessage', |
| 120 | + 'onconnect', |
| 121 | +]); |
| 122 | + |
| 123 | +/** |
| 124 | +Check if the node is a window-specific API. |
| 125 | +
|
| 126 | +@param {import('estree').MemberExpression} node |
| 127 | +@returns {boolean} |
| 128 | +*/ |
| 129 | +const isWindowSpecificAPI = node => { |
| 130 | + if (node.type !== 'MemberExpression') { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + if (node.object.name !== 'window' || node.property.type !== 'Identifier') { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + if (windowSpecificAPIs.has(node.property.name)) { |
| 139 | + if (['addEventListener', 'removeEventListener', 'dispatchEvent'].includes(node.property.name) && node.parent.type === 'CallExpression' && node.parent.callee === node) { |
| 140 | + const argument = node.parent.arguments[0]; |
| 141 | + return argument && argument.type === 'Literal' && windowSpecificEvents.has(argument.value); |
| 142 | + } |
| 143 | + |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | +}; |
| 149 | + |
| 150 | +/** |
| 151 | +@param {import('estree').Identifier} identifier |
| 152 | +@returns {boolean} |
| 153 | +*/ |
| 154 | +function isComputedMemberExpressionObject(identifier) { |
| 155 | + return identifier.parent.type === 'MemberExpression' && identifier.parent.computed && identifier.parent.object === identifier; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | +Check if the node is a web worker specific API. |
| 160 | +
|
| 161 | +@param {import('estree').MemberExpression} node |
| 162 | +@returns {boolean} |
| 163 | +*/ |
| 164 | +const isWebWorkerSpecificAPI = node => node.type === 'MemberExpression' && node.object.name === 'self' && node.property.type === 'Identifier' && webWorkerSpecificAPIs.has(node.property.name); |
| 165 | + |
| 166 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 167 | +const create = context => ({ |
| 168 | + * Program(program) { |
| 169 | + const scope = context.sourceCode.getScope(program); |
| 170 | + |
| 171 | + const references = [ |
| 172 | + // Variables declared at globals options |
| 173 | + ...scope.variables.flatMap(variable => globalIdentifier.has(variable.name) ? variable.references : []), |
| 174 | + // Variables not declared at globals options |
| 175 | + ...scope.through.filter(reference => globalIdentifier.has(reference.identifier.name)), |
| 176 | + ]; |
| 177 | + |
| 178 | + for (const {identifier} of references) { |
| 179 | + if ( |
| 180 | + isComputedMemberExpressionObject(identifier) |
| 181 | + || isWindowSpecificAPI(identifier.parent) |
| 182 | + || isWebWorkerSpecificAPI(identifier.parent) |
| 183 | + ) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + yield { |
| 188 | + node: identifier, |
| 189 | + messageId: MESSAGE_ID_ERROR, |
| 190 | + data: {value: identifier.name}, |
| 191 | + fix: fixer => fixer.replaceText(identifier, 'globalThis'), |
| 192 | + }; |
| 193 | + } |
| 194 | + }, |
| 195 | +}); |
| 196 | + |
| 197 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 198 | +module.exports = { |
| 199 | + create, |
| 200 | + meta: { |
| 201 | + type: 'suggestion', |
| 202 | + docs: { |
| 203 | + description: 'Prefer `globalThis` over `window`, `self`, and `global`.', |
| 204 | + recommended: true, |
| 205 | + }, |
| 206 | + fixable: 'code', |
| 207 | + hasSuggestions: false, |
| 208 | + messages, |
| 209 | + }, |
| 210 | +}; |
0 commit comments