oxc_ecmascript 0.142.0

A collection of JavaScript tools written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Known global identifiers, properties, and methods for side-effect analysis.
//!
//! This module contains lookup tables that classify JavaScript globals by their
//! side-effect behavior: whether accessing them, reading their properties, or
//! calling their methods is side-effect-free.

use oxc_ast::ast::*;
use oxc_compat::ESFeature;
use oxc_regular_expression::{
    LiteralParser, Options, RegexUnsupportedFlags, RegexUnsupportedPatterns,
    has_unsupported_regular_expression_flags, has_unsupported_regular_expression_pattern,
};

use super::context::MayHaveSideEffectsContext;

/// Validate that a RegExp constructor call cannot throw on the target engines.
///
/// Returns `true` if the pattern and flags are valid and supported by the target engines
/// (pure/side-effect free), and `false` if invalid, unsupported, or not statically known.
///
/// A modern parser accepting a pattern is not enough: constructor calls are commonly used for
/// feature detection, and valid newer syntax can still throw a `SyntaxError` on an older engine.
///
/// See <https://github.com/oxc-project/oxc/issues/18050>
pub fn is_valid_regexp<'a>(
    args: &[Argument<'a>],
    ctx: &impl MayHaveSideEffectsContext<'a>,
) -> bool {
    // Extract pattern from first argument
    let (pattern, is_regexp_literal) = match args.first() {
        // No arguments: `RegExp()` is valid, returns /(?:)/
        None => ("", false),
        Some(arg) => match arg.as_expression() {
            // A RegExp literal is already known to contain a valid pattern. Replacement flags are
            // validated below because support for that constructor form varies by target.
            Some(Expression::RegExpLiteral(_)) => ("", true),
            // String literal: extract the pattern to validate
            Some(Expression::StringLiteral(s)) if !s.lone_surrogates => (s.value.as_str(), false),
            // Non-literal argument: can't statically determine, assume side effects
            _ => return false,
        },
    };

    // Extract flags from second argument
    let flags = match args.get(1) {
        None => None,
        Some(arg) => match arg.as_expression() {
            Some(Expression::StringLiteral(s)) => Some(s.value.as_str()),
            // Non-literal flags: can't statically determine, assume side effects
            _ => return false,
        },
    };

    if has_unsupported_regular_expression_flags(
        flags.unwrap_or_default(),
        &unsupported_regexp_flags(ctx),
    ) {
        return false;
    }

    if is_regexp_literal {
        // ES5 throws whenever a RegExp object and flags are both supplied.
        return flags.is_none()
            || supports_es_feature(ctx, ESFeature::ES2015RegExpConstructorCanAlterFlags);
    }

    let unsupported_patterns = unsupported_regexp_patterns(ctx);

    // The parser performs complete syntax validation beyond the compatibility checks above.
    let allocator = oxc_allocator::Allocator::default();
    LiteralParser::new(&allocator, pattern, flags, Options::default()).parse().is_ok_and(
        |pattern| !has_unsupported_regular_expression_pattern(&pattern, &unsupported_patterns),
    )
}

fn unsupported_regexp_flags<'a>(ctx: &impl MayHaveSideEffectsContext<'a>) -> RegexUnsupportedFlags {
    RegexUnsupportedFlags {
        sticky: !supports_es_feature(ctx, ESFeature::ES2015StickyRegex),
        unicode: !supports_es_feature(ctx, ESFeature::ES2015UnicodeRegex),
        dot_all: !supports_es_feature(ctx, ESFeature::ES2018DotallRegex),
        match_indices: !supports_es_feature(ctx, ESFeature::ES2022MatchIndicesRegex),
        unicode_sets: !supports_es_feature(ctx, ESFeature::ES2024UnicodeSetsRegex),
    }
}

fn unsupported_regexp_patterns<'a>(
    ctx: &impl MayHaveSideEffectsContext<'a>,
) -> RegexUnsupportedPatterns {
    RegexUnsupportedPatterns {
        named_capture_groups: !supports_es_feature(ctx, ESFeature::ES2018NamedCapturingGroupsRegex),
        duplicate_named_capture_groups: !supports_es_feature(
            ctx,
            ESFeature::ES2025DuplicateNamedCapturingGroupsRegex,
        ),
        unicode_property_escapes: !supports_es_feature(ctx, ESFeature::ES2018UnicodePropertyRegex),
        look_behind_assertions: !supports_es_feature(ctx, ESFeature::ES2018LookbehindRegex),
        pattern_modifiers: !supports_es_feature(ctx, ESFeature::ES2025RegexpModifiers),
    }
}

fn supports_es_feature<'a>(ctx: &impl MayHaveSideEffectsContext<'a>, feature: ESFeature) -> bool {
    ctx.engine_targets().is_some_and(|target| target.supports_es_feature(feature))
}

#[rustfmt::skip]
pub(super) fn is_pure_global_function(name: &str) -> bool {
    matches!(name, "decodeURI" | "decodeURIComponent" | "encodeURI" | "encodeURIComponent"
            | "escape" | "isFinite" | "isNaN" | "parseFloat" | "parseInt")
}

/// Constructors that are side-effect-free when called as functions (not `new`),
/// provided all arguments are side-effect-free.
///
/// Note: `Number`, `Symbol`, `BigInt`, and Error types are NOT included here because
/// they require special-case argument validation in `CallExpression`:
/// - `Number(Symbol())` throws TypeError (`ToNumeric` on Symbol)
/// - `Symbol(Symbol())` throws TypeError (`ToString` on Symbol)
/// - `Error(Symbol())` throws TypeError (`ToString` on Symbol)
/// - `BigInt(1.5)`, `BigInt(undefined)`, etc. throw for invalid values
///
/// `String` IS included because `String()` has special Symbol handling
/// (`String(Symbol())` returns `"Symbol()"` without throwing), and per the
/// "Coercion Methods Are Pure" assumption, `ToPrimitive` on objects is safe.
///
/// `Date` IS included because `Date()` as a function ignores all arguments
/// and just returns the current date as a string.
#[rustfmt::skip]
pub(super) fn is_pure_callable_constructor(name: &str) -> bool {
    matches!(name, "Date" | "Boolean" | "Object" | "String")
}

/// Constructors that are unconditionally side-effect-free with any arguments.
///
/// - `Object`: wraps/returns any argument, no coercion
/// - `Boolean`: `ToBoolean` is a purely internal operation, no user code
///
/// Note: Error types call `ToString(msg)` which throws on Symbol. They need argument
/// validation (Symbol check). `String`, `Number`, `Date`, `ArrayBuffer` also need checks.
#[rustfmt::skip]
pub(super) fn is_unconditionally_pure_constructor(name: &str) -> bool {
    matches!(name, "Object" | "Boolean")
}

/// Whether the name is an Error constructor.
#[rustfmt::skip]
pub(super) fn is_error_constructor(name: &str) -> bool {
    matches!(name, "Error" | "EvalError" | "RangeError" | "ReferenceError"
            | "SyntaxError" | "TypeError" | "URIError")
}

/// Whether the name matches any TypedArray constructor name.
///
/// See <https://tc39.es/ecma262/multipage/indexed-collections.html#sec-typedarray-objects> for the list of TypedArrays.
#[rustfmt::skip]
pub fn is_typed_array_constructor(name: &str) -> bool {
    matches!(name, "Int8Array" | "Uint8Array" | "Uint8ClampedArray"
            | "Int16Array" | "Uint16Array" | "Int32Array" | "Uint32Array"
            | "Float32Array" | "Float64Array" | "BigInt64Array" | "BigUint64Array")
}

/// Whether a collection constructor (`Map`, `Set`, `WeakMap`, `WeakSet`) call is pure.
///
/// These constructors iterate their argument via `Symbol.iterator`, which can have side effects
/// when the argument is a variable reference (custom iterators, proxies, etc.).
/// Only provably safe arguments are considered pure:
/// - No arguments: `new Set()`, `new Map()`
/// - `null` or `undefined`: `new Set(null)`, `new Map(undefined)`
/// - Array literals: `new Set([1,2,3])`, `new Map([[k,v]])`
///
/// Following esbuild and Rollup behavior.
pub(super) fn is_pure_collection_constructor<'a>(
    name: &str,
    args: &[Argument<'a>],
    ctx: &impl MayHaveSideEffectsContext<'a>,
) -> bool {
    if !matches!(name, "Set" | "Map" | "WeakSet" | "WeakMap") {
        return false;
    }
    match args.first() {
        // No arguments: always pure
        None => true,
        Some(arg) => match arg.as_expression() {
            Some(Expression::NullLiteral(_)) => true,
            Some(Expression::Identifier(id))
                if id.name == "undefined" && ctx.is_global_reference(id) =>
            {
                true
            }
            Some(Expression::ArrayExpression(arr)) => {
                // For Map/WeakMap, each element must also be an array literal (key-value pair)
                if matches!(name, "Map" | "WeakMap") {
                    arr.elements
                        .iter()
                        .all(|el| matches!(el, ArrayExpressionElement::ArrayExpression(_)))
                } else {
                    true
                }
            }
            _ => false,
        },
    }
}

/// Whether the name matches any known global constructors.
///
/// <https://tc39.es/ecma262/multipage/global-object.html#sec-constructor-properties-of-the-global-object>
pub(super) fn is_known_global_constructor(name: &str) -> bool {
    // technically, we need to exclude the constructors that are not supported by the target
    matches!(
        name,
        "AggregateError"
            | "Array"
            | "ArrayBuffer"
            | "BigInt"
            | "BigInt64Array"
            | "BigUint64Array"
            | "Boolean"
            | "DataView"
            | "Date"
            | "Error"
            | "EvalError"
            | "FinalizationRegistry"
            | "Float32Array"
            | "Float64Array"
            | "Function"
            | "Int8Array"
            | "Int16Array"
            | "Int32Array"
            | "Iterator"
            | "Map"
            | "Number"
            | "Object"
            | "Promise"
            | "Proxy"
            | "RangeError"
            | "ReferenceError"
            | "RegExp"
            | "Set"
            | "SharedArrayBuffer"
            | "String"
            | "Symbol"
            | "SyntaxError"
            | "TypeError"
            | "Uint8Array"
            | "Uint8ClampedArray"
            | "Uint16Array"
            | "Uint32Array"
            | "URIError"
            | "WeakMap"
            | "WeakSet"
    )
}

/// Whether the name matches any known global identifier that is side-effect-free to access.
///
/// This list is ported from Rolldown's `GLOBAL_IDENT` set, which mirrors Rollup's `knownGlobals`.
/// It includes browser/host-specific APIs (e.g. `document`, `window`, DOM classes) intentionally,
/// matching Rollup's behavior of assuming these globals exist in the target environment.
/// `NaN`, `Infinity`, `undefined` are excluded since they are already handled as special cases.
#[rustfmt::skip]
pub(super) fn is_known_global_identifier(name: &str) -> bool {
    matches!(name,
        // Core JS globals
        "Array" | "Boolean" | "Function" | "Math" | "Number" | "Object" | "RegExp" | "String"
        // Other globals present in both the browser and node
        | "AbortController" | "AbortSignal" | "AggregateError" | "ArrayBuffer" | "BigInt"
        | "DataView" | "Date" | "Error" | "EvalError" | "Event" | "EventTarget"
        | "Float32Array" | "Float64Array" | "Int16Array" | "Int32Array" | "Int8Array" | "Intl"
        | "JSON" | "Map" | "MessageChannel" | "MessageEvent" | "MessagePort" | "Promise"
        | "Proxy" | "RangeError" | "ReferenceError" | "Reflect" | "Set" | "Symbol"
        | "SyntaxError" | "TextDecoder" | "TextEncoder" | "TypeError" | "URIError" | "URL"
        | "URLSearchParams" | "Uint16Array" | "Uint32Array" | "Uint8Array"
        | "Uint8ClampedArray" | "WeakMap" | "WeakSet" | "WebAssembly"
        | "clearInterval" | "clearTimeout" | "console" | "decodeURI" | "decodeURIComponent"
        | "encodeURI" | "encodeURIComponent" | "escape" | "globalThis" | "isFinite" | "isNaN"
        | "parseFloat" | "parseInt" | "queueMicrotask" | "setInterval" | "setTimeout"
        | "unescape"
        // CSSOM APIs
        | "CSSAnimation" | "CSSFontFaceRule" | "CSSImportRule" | "CSSKeyframeRule"
        | "CSSKeyframesRule" | "CSSMediaRule" | "CSSNamespaceRule" | "CSSPageRule" | "CSSRule"
        | "CSSRuleList" | "CSSStyleDeclaration" | "CSSStyleRule" | "CSSStyleSheet"
        | "CSSSupportsRule" | "CSSTransition"
        // SVG DOM
        | "SVGAElement" | "SVGAngle" | "SVGAnimateElement" | "SVGAnimateMotionElement"
        | "SVGAnimateTransformElement" | "SVGAnimatedAngle" | "SVGAnimatedBoolean"
        | "SVGAnimatedEnumeration" | "SVGAnimatedInteger" | "SVGAnimatedLength"
        | "SVGAnimatedLengthList" | "SVGAnimatedNumber" | "SVGAnimatedNumberList"
        | "SVGAnimatedPreserveAspectRatio" | "SVGAnimatedRect" | "SVGAnimatedString"
        | "SVGAnimatedTransformList" | "SVGAnimationElement" | "SVGCircleElement"
        | "SVGClipPathElement" | "SVGComponentTransferFunctionElement" | "SVGDefsElement"
        | "SVGDescElement" | "SVGElement" | "SVGEllipseElement" | "SVGFEBlendElement"
        | "SVGFEColorMatrixElement" | "SVGFEComponentTransferElement"
        | "SVGFECompositeElement" | "SVGFEConvolveMatrixElement"
        | "SVGFEDiffuseLightingElement" | "SVGFEDisplacementMapElement"
        | "SVGFEDistantLightElement" | "SVGFEDropShadowElement" | "SVGFEFloodElement"
        | "SVGFEFuncAElement" | "SVGFEFuncBElement" | "SVGFEFuncGElement"
        | "SVGFEFuncRElement" | "SVGFEGaussianBlurElement" | "SVGFEImageElement"
        | "SVGFEMergeElement" | "SVGFEMergeNodeElement" | "SVGFEMorphologyElement"
        | "SVGFEOffsetElement" | "SVGFEPointLightElement" | "SVGFESpecularLightingElement"
        | "SVGFESpotLightElement" | "SVGFETileElement" | "SVGFETurbulenceElement"
        | "SVGFilterElement" | "SVGForeignObjectElement" | "SVGGElement"
        | "SVGGeometryElement" | "SVGGradientElement" | "SVGGraphicsElement"
        | "SVGImageElement" | "SVGLength" | "SVGLengthList" | "SVGLineElement"
        | "SVGLinearGradientElement" | "SVGMPathElement" | "SVGMarkerElement"
        | "SVGMaskElement" | "SVGMatrix" | "SVGMetadataElement" | "SVGNumber"
        | "SVGNumberList" | "SVGPathElement" | "SVGPatternElement" | "SVGPoint"
        | "SVGPointList" | "SVGPolygonElement" | "SVGPolylineElement"
        | "SVGPreserveAspectRatio" | "SVGRadialGradientElement" | "SVGRect"
        | "SVGRectElement" | "SVGSVGElement" | "SVGScriptElement" | "SVGSetElement"
        | "SVGStopElement" | "SVGStringList" | "SVGStyleElement" | "SVGSwitchElement"
        | "SVGSymbolElement" | "SVGTSpanElement" | "SVGTextContentElement"
        | "SVGTextElement" | "SVGTextPathElement" | "SVGTextPositioningElement"
        | "SVGTitleElement" | "SVGTransform" | "SVGTransformList" | "SVGUnitTypes"
        | "SVGUseElement" | "SVGViewElement"
        // Other browser APIs
        | "AnalyserNode" | "Animation" | "AnimationEffect" | "AnimationEvent"
        | "AnimationPlaybackEvent" | "AnimationTimeline" | "Attr" | "Audio" | "AudioBuffer"
        | "AudioBufferSourceNode" | "AudioDestinationNode" | "AudioListener" | "AudioNode"
        | "AudioParam" | "AudioProcessingEvent" | "AudioScheduledSourceNode" | "BarProp"
        | "BeforeUnloadEvent" | "BiquadFilterNode" | "Blob" | "BlobEvent"
        | "ByteLengthQueuingStrategy" | "CDATASection" | "CSS" | "CanvasGradient"
        | "CanvasPattern" | "CanvasRenderingContext2D" | "ChannelMergerNode"
        | "ChannelSplitterNode" | "CharacterData" | "ClipboardEvent" | "CloseEvent"
        | "Comment" | "CompositionEvent" | "ConvolverNode" | "CountQueuingStrategy"
        | "Crypto" | "CustomElementRegistry" | "CustomEvent" | "DOMException"
        | "DOMImplementation" | "DOMMatrix" | "DOMMatrixReadOnly" | "DOMParser" | "DOMPoint"
        | "DOMPointReadOnly" | "DOMQuad" | "DOMRect" | "DOMRectList" | "DOMRectReadOnly"
        | "DOMStringList" | "DOMStringMap" | "DOMTokenList" | "DataTransfer"
        | "DataTransferItem" | "DataTransferItemList" | "DelayNode" | "Document"
        | "DocumentFragment" | "DocumentTimeline" | "DocumentType" | "DragEvent"
        | "DynamicsCompressorNode" | "Element" | "ErrorEvent" | "EventSource" | "File"
        | "FileList" | "FileReader" | "FocusEvent" | "FontFace" | "FormData" | "GainNode"
        | "Gamepad" | "GamepadButton" | "GamepadEvent" | "Geolocation"
        | "GeolocationPositionError" | "HTMLAllCollection" | "HTMLAnchorElement"
        | "HTMLAreaElement" | "HTMLAudioElement" | "HTMLBRElement" | "HTMLBaseElement"
        | "HTMLBodyElement" | "HTMLButtonElement" | "HTMLCanvasElement" | "HTMLCollection"
        | "HTMLDListElement" | "HTMLDataElement" | "HTMLDataListElement"
        | "HTMLDetailsElement" | "HTMLDirectoryElement" | "HTMLDivElement" | "HTMLDocument"
        | "HTMLElement" | "HTMLEmbedElement" | "HTMLFieldSetElement" | "HTMLFontElement"
        | "HTMLFormControlsCollection" | "HTMLFormElement" | "HTMLFrameElement"
        | "HTMLFrameSetElement" | "HTMLHRElement" | "HTMLHeadElement"
        | "HTMLHeadingElement" | "HTMLHtmlElement" | "HTMLIFrameElement"
        | "HTMLImageElement" | "HTMLInputElement" | "HTMLLIElement" | "HTMLLabelElement"
        | "HTMLLegendElement" | "HTMLLinkElement" | "HTMLMapElement" | "HTMLMarqueeElement"
        | "HTMLMediaElement" | "HTMLMenuElement" | "HTMLMetaElement" | "HTMLMeterElement"
        | "HTMLModElement" | "HTMLOListElement" | "HTMLObjectElement"
        | "HTMLOptGroupElement" | "HTMLOptionElement" | "HTMLOptionsCollection"
        | "HTMLOutputElement" | "HTMLParagraphElement" | "HTMLParamElement"
        | "HTMLPictureElement" | "HTMLPreElement" | "HTMLProgressElement"
        | "HTMLQuoteElement" | "HTMLScriptElement" | "HTMLSelectElement"
        | "HTMLSlotElement" | "HTMLSourceElement" | "HTMLSpanElement" | "HTMLStyleElement"
        | "HTMLTableCaptionElement" | "HTMLTableCellElement" | "HTMLTableColElement"
        | "HTMLTableElement" | "HTMLTableRowElement" | "HTMLTableSectionElement"
        | "HTMLTemplateElement" | "HTMLTextAreaElement" | "HTMLTimeElement"
        | "HTMLTitleElement" | "HTMLTrackElement" | "HTMLUListElement"
        | "HTMLUnknownElement" | "HTMLVideoElement" | "HashChangeEvent" | "Headers"
        | "History" | "IDBCursor" | "IDBCursorWithValue" | "IDBDatabase" | "IDBFactory"
        | "IDBIndex" | "IDBKeyRange" | "IDBObjectStore" | "IDBOpenDBRequest" | "IDBRequest"
        | "IDBTransaction" | "IDBVersionChangeEvent" | "Image" | "ImageData" | "InputEvent"
        | "IntersectionObserver" | "IntersectionObserverEntry" | "KeyboardEvent"
        | "KeyframeEffect" | "Location" | "MediaCapabilities"
        | "MediaElementAudioSourceNode" | "MediaEncryptedEvent" | "MediaError"
        | "MediaList" | "MediaQueryList" | "MediaQueryListEvent" | "MediaRecorder"
        | "MediaSource" | "MediaStream" | "MediaStreamAudioDestinationNode"
        | "MediaStreamAudioSourceNode" | "MediaStreamTrack" | "MediaStreamTrackEvent"
        | "MimeType" | "MimeTypeArray" | "MouseEvent" | "MutationEvent"
        | "MutationObserver" | "MutationRecord" | "NamedNodeMap" | "Navigator" | "Node"
        | "NodeFilter" | "NodeIterator" | "NodeList" | "Notification"
        | "OfflineAudioCompletionEvent" | "Option" | "OscillatorNode"
        | "PageTransitionEvent" | "Path2D" | "Performance" | "PerformanceEntry"
        | "PerformanceMark" | "PerformanceMeasure" | "PerformanceNavigation"
        | "PerformanceObserver" | "PerformanceObserverEntryList"
        | "PerformanceResourceTiming" | "PerformanceTiming" | "PeriodicWave" | "Plugin"
        | "PluginArray" | "PointerEvent" | "PopStateEvent" | "ProcessingInstruction"
        | "ProgressEvent" | "PromiseRejectionEvent" | "RTCCertificate" | "RTCDTMFSender"
        | "RTCDTMFToneChangeEvent" | "RTCDataChannel" | "RTCDataChannelEvent"
        | "RTCIceCandidate" | "RTCPeerConnection" | "RTCPeerConnectionIceEvent"
        | "RTCRtpReceiver" | "RTCRtpSender" | "RTCRtpTransceiver"
        | "RTCSessionDescription" | "RTCStatsReport" | "RTCTrackEvent" | "RadioNodeList"
        | "Range" | "ReadableStream" | "Request" | "ResizeObserver"
        | "ResizeObserverEntry" | "Response" | "Screen" | "ScriptProcessorNode"
        | "SecurityPolicyViolationEvent" | "Selection" | "ShadowRoot" | "SourceBuffer"
        | "SourceBufferList" | "SpeechSynthesisEvent" | "SpeechSynthesisUtterance"
        | "StaticRange" | "Storage" | "StorageEvent" | "StyleSheet" | "StyleSheetList"
        | "Text" | "TextMetrics" | "TextTrack" | "TextTrackCue" | "TextTrackCueList"
        | "TextTrackList" | "TimeRanges" | "TrackEvent" | "TransitionEvent" | "TreeWalker"
        | "UIEvent" | "VTTCue" | "ValidityState" | "VisualViewport" | "WaveShaperNode"
        | "WebGLActiveInfo" | "WebGLBuffer" | "WebGLContextEvent" | "WebGLFramebuffer"
        | "WebGLProgram" | "WebGLQuery" | "WebGLRenderbuffer" | "WebGLRenderingContext"
        | "WebGLSampler" | "WebGLShader" | "WebGLShaderPrecisionFormat" | "WebGLSync"
        | "WebGLTexture" | "WebGLUniformLocation" | "WebKitCSSMatrix" | "WebSocket"
        | "WheelEvent" | "Window" | "Worker" | "XMLDocument" | "XMLHttpRequest"
        | "XMLHttpRequestEventTarget" | "XMLHttpRequestUpload" | "XMLSerializer"
        | "XPathEvaluator" | "XPathExpression" | "XPathResult" | "XSLTProcessor"
        | "alert" | "atob" | "blur" | "btoa" | "cancelAnimationFrame" | "captureEvents"
        | "close" | "closed" | "confirm" | "customElements" | "devicePixelRatio"
        | "document" | "event" | "fetch" | "find" | "focus" | "frameElement" | "frames"
        | "getComputedStyle" | "getSelection" | "history" | "indexedDB" | "isSecureContext"
        | "length" | "location" | "locationbar" | "matchMedia" | "menubar" | "moveBy"
        | "moveTo" | "name" | "navigator"
        | "onabort" | "onafterprint" | "onanimationend" | "onanimationiteration"
        | "onanimationstart" | "onbeforeprint" | "onbeforeunload" | "onblur" | "oncanplay"
        | "oncanplaythrough" | "onchange" | "onclick" | "oncontextmenu" | "oncuechange"
        | "ondblclick" | "ondrag" | "ondragend" | "ondragenter" | "ondragleave"
        | "ondragover" | "ondragstart" | "ondrop" | "ondurationchange" | "onemptied"
        | "onended" | "onerror" | "onfocus" | "ongotpointercapture" | "onhashchange"
        | "oninput" | "oninvalid" | "onkeydown" | "onkeypress" | "onkeyup"
        | "onlanguagechange" | "onload" | "onloadeddata" | "onloadedmetadata"
        | "onloadstart" | "onlostpointercapture" | "onmessage" | "onmousedown"
        | "onmouseenter" | "onmouseleave" | "onmousemove" | "onmouseout" | "onmouseover"
        | "onmouseup" | "onoffline" | "ononline" | "onpagehide" | "onpageshow" | "onpause"
        | "onplay" | "onplaying" | "onpointercancel" | "onpointerdown" | "onpointerenter"
        | "onpointerleave" | "onpointermove" | "onpointerout" | "onpointerover"
        | "onpointerup" | "onpopstate" | "onprogress" | "onratechange"
        | "onrejectionhandled" | "onreset" | "onresize" | "onscroll" | "onseeked"
        | "onseeking" | "onselect" | "onstalled" | "onstorage" | "onsubmit" | "onsuspend"
        | "ontimeupdate" | "ontoggle" | "ontransitioncancel" | "ontransitionend"
        | "ontransitionrun" | "ontransitionstart" | "onunhandledrejection" | "onunload"
        | "onvolumechange" | "onwaiting" | "onwebkitanimationend"
        | "onwebkitanimationiteration" | "onwebkitanimationstart"
        | "onwebkittransitionend" | "onwheel"
        | "open" | "opener" | "origin" | "outerHeight" | "outerWidth" | "parent"
        | "performance" | "personalbar" | "postMessage" | "print" | "prompt"
        | "releaseEvents" | "requestAnimationFrame" | "resizeBy" | "resizeTo" | "screen"
        | "screenLeft" | "screenTop" | "screenX" | "screenY" | "scroll" | "scrollBy"
        | "scrollTo" | "scrollbars" | "self" | "speechSynthesis" | "status" | "statusbar"
        | "stop" | "toolbar" | "top" | "webkitURL" | "window"
    )
}

#[rustfmt::skip]
fn is_pure_math_method(method: &str) -> bool {
    matches!(method,
        "abs" | "acos" | "acosh" | "asin" | "asinh" | "atan" | "atan2" | "atanh"
        | "cbrt" | "ceil" | "clz32" | "cos" | "cosh" | "exp" | "expm1" | "floor"
        | "fround" | "hypot" | "imul" | "log" | "log10" | "log1p" | "log2" | "max"
        | "min" | "pow" | "random" | "round" | "sign" | "sin" | "sinh" | "sqrt"
        | "tan" | "tanh" | "trunc"
    )
}

/// Whether calling `Global.method()` is side-effect-free (given pure arguments).
///
/// This is distinct from `is_known_global_property` which checks property READ safety.
/// For example, `Object.freeze` is safe to read but NOT safe to call (it mutates).
#[rustfmt::skip]
pub(super) fn is_pure_global_method_call(object: &str, method: &str) -> bool {
    match object {
        "Array" => matches!(method, "isArray" | "of"),
        "ArrayBuffer" => method == "isView",
        "Date" => matches!(method, "now" | "parse" | "UTC"),
        "Math" => is_pure_math_method(method),
        "Number" => matches!(method, "isFinite" | "isInteger" | "isNaN" | "isSafeInteger" | "parseFloat" | "parseInt"),
        // Only `Object.is` is unconditionally pure. The introspection methods
        // (`keys`, `getOwnPropertyDescriptor`, ...) can trigger Proxy traps on their
        // target, and `Object.create` reads its `properties` argument; both are
        // handled in `CallExpression::may_have_side_effects`.
        "Object" => method == "is",
        // `String.raw(template)` reads `template.raw` and throws a TypeError on a
        // missing/non-template argument, which is never provably safe — kept as not-pure.
        // `fromCharCode`/`fromCodePoint` coerce via `ToNumber` (throw-checked in
        // `CallExpression::may_have_side_effects`).
        "String" => matches!(method, "fromCharCode" | "fromCodePoint"),
        // `Symbol.keyFor(sym)` requires a Symbol argument and throws otherwise, which is
        // never provable — kept as not-pure.
        "Symbol" => method == "for",
        // `URL.canParse(url)` has a required first argument (throws with none); the
        // arg-count check lives in `CallExpression::may_have_side_effects`.
        "URL" => method == "canParse",
        _ if is_typed_array_constructor(object) => method == "of",
        _ => false,
    }
}

/// Whether the property read on a known global is side-effect-free.
///
/// For example, `Math.PI`, `console.log`, `Object.keys` are all side-effect-free to read.
/// Lists ported from Rolldown's global_reference.rs.
#[rustfmt::skip]
pub(super) fn is_known_global_property(global: &str, property: &str) -> bool {
    match global {
        "Math" => matches!(property, "E" | "LN10" | "LN2" | "LOG10E" | "LOG2E" | "PI" | "SQRT1_2" | "SQRT2")
            || is_pure_math_method(property),
        "console" => matches!(property,
            "assert" | "clear" | "count" | "countReset" | "debug" | "dir" | "dirxml"
            | "error" | "group" | "groupCollapsed" | "groupEnd" | "info" | "log"
            | "table" | "time" | "timeEnd" | "timeLog" | "trace" | "warn"
        ),
        "Object" => matches!(property,
            "assign" | "create" | "defineProperties" | "defineProperty" | "entries"
            | "freeze" | "fromEntries" | "getOwnPropertyDescriptor"
            | "getOwnPropertyDescriptors" | "getOwnPropertyNames"
            | "getOwnPropertySymbols" | "getPrototypeOf" | "is" | "isExtensible"
            | "isFrozen" | "isSealed" | "keys" | "preventExtensions" | "prototype"
            | "seal" | "setPrototypeOf" | "values"
        ),
        "Reflect" => matches!(property,
            "apply" | "construct" | "defineProperty" | "deleteProperty" | "get"
            | "getOwnPropertyDescriptor" | "getPrototypeOf" | "has" | "isExtensible"
            | "ownKeys" | "preventExtensions" | "set" | "setPrototypeOf"
        ),
        "Symbol" => matches!(property,
            "asyncDispose" | "asyncIterator" | "dispose" | "hasInstance"
            | "isConcatSpreadable" | "iterator" | "match" | "matchAll" | "replace"
            | "search" | "species" | "split" | "toPrimitive" | "toStringTag"
            | "unscopables"
        ),
        "JSON" => matches!(property, "parse" | "stringify"),
        _ => false,
    }
}

/// Whether a 3-level property chain on a known global is side-effect-free.
///
/// For example, `Object.prototype.hasOwnProperty` is side-effect-free to read.
/// List ported from Rolldown's `OBJECT_PROTOTYPE_THIRD_PROP`.
#[rustfmt::skip]
pub(super) fn is_known_global_property_deep(global: &str, middle: &str, property: &str) -> bool {
    global == "Object" && middle == "prototype" && matches!(property,
        "__defineGetter__" | "__defineSetter__" | "__lookupGetter__" | "__lookupSetter__"
        | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable" | "toLocaleString"
        | "toString" | "unwatch" | "valueOf" | "watch"
    )
}