{"record":{"id":"48ea2a5b6728c1fc","repo":"oxc-project/oxc","slug":"use-array-isarray-instead-of-instanceof-array","errorCode":null,"errorMessage":"Use `Array.isArray()` instead of `instanceof Array`.","messagePattern":"Use `Array\\.isArray\\(\\)` instead of `instanceof Array`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_instanceof_array.rs","lineNumber":10,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_instanceof_array_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Use `Array.isArray()` instead of `instanceof Array`.\")\n        .with_help(\"The instanceof Array check doesn't work across realms/contexts, for example, frames/windows in browsers or the vm module in Node.js.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoInstanceofArray;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Require `Array.isArray()` instead of `instanceof Array`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// The `instanceof Array` check doesn't work across realms/contexts.\n    /// For example, frames/windows in browsers or the `vm` module in Node.js.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_instanceof_array.rs#L1-L28","documentation":"Diagnostic from the oxlint rule `unicorn/no-instanceof-array`. `value instanceof Array` returns false for arrays created in another realm — an iframe's window, a Web Worker postMessage payload, a Node `vm` sandbox — because each realm has its own Array constructor. `Array.isArray()` checks the object's internal array-ness instead of a prototype chain, so it works across realms and the rule requires it.","triggerScenarios":"Any binary expression `x instanceof Array` where the right side resolves to the global Array constructor: `if (data instanceof Array)`, `[1,2,3] instanceof Array`, `obj.arr instanceof Array`. Typical with data arriving via postMessage, workers, iframes, or vm.runInNewContext.","commonSituations":"Browser extensions and embedded iframes exchanging structured data; Node sandboxed plugins; Jest/jsdom tests where arrays cross environment boundaries.","solutions":["Replace with `Array.isArray(data)` — unicorn/no-instanceof-builtins auto-fixes this exact shape with `oxlint --fix`","If you must also accept array-likes, check them explicitly (iterability, length) instead of loosening the array check","If single-realm is guaranteed and instanceof is a team convention, disable the rule for that scope"],"exampleFix":"// before\nif (data instanceof Array) {\n  process(data);\n}\n\n// after\nif (Array.isArray(data)) {\n  process(data);\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isUnknownArray(v: unknown): v is unknown[] {\n  return Array.isArray(v);\n}\n// works for arrays from any realm (iframe, worker, vm)","tryCatchPattern":null,"preventionTips":["Never use instanceof for built-in type checks; use Array.isArray, typeof, Object.prototype.toString","Sanitize postMessage/worker payloads with Array.isArray at the trust boundary","Auto-fix legacy `instanceof Array` occurrences via oxlint --fix"],"tags":["arrays","cross-realm","browser","nodejs","oxlint"],"backgroundTag":"cross-realm-instanceof","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}