|
| 1 | +/** |
| 2 | + * Flags direct value-level references to Web API globals that don't exist in React Native. |
| 3 | + * |
| 4 | + * In environments like Hermes/JSC, referencing `Event` (or similar) as a value throws |
| 5 | + * `ReferenceError: Property 'Event' doesn't exist`. Use `typeof Event !== 'undefined'` |
| 6 | + * for existence checks, or `isBuiltin(val, 'Event')` for type checks. |
| 7 | + * |
| 8 | + * Type-level references (annotations, generics, type guards) are allowed. |
| 9 | + */ |
| 10 | + |
| 11 | +const WEB_GLOBALS = new Set([ |
| 12 | + 'Event', |
| 13 | + 'ErrorEvent', |
| 14 | + 'PromiseRejectionEvent', |
| 15 | + 'CustomEvent', |
| 16 | + 'MouseEvent', |
| 17 | + 'KeyboardEvent', |
| 18 | + 'TouchEvent', |
| 19 | + 'PointerEvent', |
| 20 | + 'FocusEvent', |
| 21 | + 'InputEvent', |
| 22 | + 'ClipboardEvent', |
| 23 | + 'DragEvent', |
| 24 | + 'AnimationEvent', |
| 25 | + 'TransitionEvent', |
| 26 | + 'PopStateEvent', |
| 27 | + 'HashChangeEvent', |
| 28 | + 'PageTransitionEvent', |
| 29 | + 'MutationObserver', |
| 30 | + 'IntersectionObserver', |
| 31 | + 'ResizeObserver', |
| 32 | + 'PerformanceObserver', |
| 33 | + 'XMLHttpRequest', |
| 34 | + 'WebSocket', |
| 35 | + 'Worker', |
| 36 | + 'SharedWorker', |
| 37 | + 'BroadcastChannel', |
| 38 | + 'MessageChannel', |
| 39 | + 'MessagePort', |
| 40 | +]) |
| 41 | + |
| 42 | +module.exports = { |
| 43 | + meta: { |
| 44 | + type: 'problem', |
| 45 | + docs: { |
| 46 | + description: |
| 47 | + 'Disallow direct value-level references to Web API globals that may not exist in React Native', |
| 48 | + }, |
| 49 | + messages: { |
| 50 | + unsafeWebGlobal: |
| 51 | + "Direct value reference to '{{name}}' will throw a ReferenceError in React Native. " + |
| 52 | + "Use `typeof {{name}} !== 'undefined'` for existence checks, or `isBuiltin(val, '{{name}}')` for type checks.", |
| 53 | + }, |
| 54 | + }, |
| 55 | + create(context) { |
| 56 | + /** |
| 57 | + * Check if a node is guarded by a `typeof X !== 'undefined'` check |
| 58 | + * on the left side of a short-circuit `&&` expression. |
| 59 | + * e.g. `typeof Event !== 'undefined' && isInstanceOf(candidate, Event)` |
| 60 | + */ |
| 61 | + function isGuardedByTypeofCheck(node, globalName) { |
| 62 | + let current = node |
| 63 | + while (current.parent) { |
| 64 | + const parent = current.parent |
| 65 | + if (parent.type === 'LogicalExpression' && parent.operator === '&&' && parent.right === current) { |
| 66 | + if (containsTypeofGuard(parent.left, globalName)) { |
| 67 | + return true |
| 68 | + } |
| 69 | + } |
| 70 | + current = parent |
| 71 | + } |
| 72 | + return false |
| 73 | + } |
| 74 | + |
| 75 | + function containsTypeofGuard(node, globalName) { |
| 76 | + if (node.type === 'BinaryExpression' && (node.operator === '!==' || node.operator === '!=')) { |
| 77 | + return hasTypeofOperand(node, globalName) && hasUndefinedOperand(node) |
| 78 | + } |
| 79 | + if (node.type === 'LogicalExpression' && node.operator === '&&') { |
| 80 | + return containsTypeofGuard(node.left, globalName) || containsTypeofGuard(node.right, globalName) |
| 81 | + } |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + function hasTypeofOperand(binaryNode, globalName) { |
| 86 | + return ( |
| 87 | + (binaryNode.left.type === 'UnaryExpression' && |
| 88 | + binaryNode.left.operator === 'typeof' && |
| 89 | + binaryNode.left.argument.type === 'Identifier' && |
| 90 | + binaryNode.left.argument.name === globalName) || |
| 91 | + (binaryNode.right.type === 'UnaryExpression' && |
| 92 | + binaryNode.right.operator === 'typeof' && |
| 93 | + binaryNode.right.argument.type === 'Identifier' && |
| 94 | + binaryNode.right.argument.name === globalName) |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + function hasUndefinedOperand(binaryNode) { |
| 99 | + return ( |
| 100 | + (binaryNode.left.type === 'Literal' && binaryNode.left.value === 'undefined') || |
| 101 | + (binaryNode.right.type === 'Literal' && binaryNode.right.value === 'undefined') |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + Identifier(node) { |
| 107 | + if (!WEB_GLOBALS.has(node.name)) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + const parent = node.parent |
| 112 | + |
| 113 | + // Allow: typeof Event (safe existence check) |
| 114 | + if (parent.type === 'UnaryExpression' && parent.operator === 'typeof') { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + // Allow: value reference guarded by typeof check via short-circuit && |
| 119 | + // e.g. `typeof Event !== 'undefined' && isInstanceOf(candidate, Event)` |
| 120 | + if (isGuardedByTypeofCheck(node, node.name)) { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + // Allow: all TypeScript type-level usage (annotations, generics, predicates, implements/extends) |
| 125 | + if ( |
| 126 | + parent.type === 'TSTypeReference' || |
| 127 | + parent.type === 'TSTypeAnnotation' || |
| 128 | + parent.type === 'TSTypeQuery' || |
| 129 | + parent.type === 'TSQualifiedName' || |
| 130 | + parent.type === 'TSTypeParameterInstantiation' || |
| 131 | + parent.type === 'TSTypePredicate' || |
| 132 | + parent.type === 'TSClassImplements' || |
| 133 | + parent.type === 'TSInterfaceHeritage' |
| 134 | + ) { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + // Allow: property access on an object (e.g. err.Event, obj.CustomEvent) |
| 139 | + if (parent.type === 'MemberExpression' && parent.property === node && !parent.computed) { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + // Allow: string literals that happen to match (e.g. 'Event' in isBuiltin calls) |
| 144 | + if (node.type === 'Literal') { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + // Allow: import specifiers |
| 150 | + if ( |
| 151 | + parent.type === 'ImportSpecifier' || |
| 152 | + parent.type === 'ImportDefaultSpecifier' || |
| 153 | + parent.type === 'ImportNamespaceSpecifier' |
| 154 | + ) { |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + context.report({ |
| 159 | + node, |
| 160 | + messageId: 'unsafeWebGlobal', |
| 161 | + data: { name: node.name }, |
| 162 | + }) |
| 163 | + }, |
| 164 | + } |
| 165 | + }, |
| 166 | +} |
0 commit comments