From 2087923163f90b9d4898d606f8b67623fc7df67f Mon Sep 17 00:00:00 2001 From: James Ide Date: Wed, 20 May 2015 18:46:01 -0700 Subject: [PATCH] Replace "instanceof Array" in transformer with "[object Array]" comparison Testing with `'[object Array]' is Object::toString.call element` allows arrays from another JS context to be properly handled. The specific use case here is to support jest, which sets up JS contexts using Node/io.js's "vm" module. This approach works in ES3 environments in contrast with ES5's `Array.isArray`. --- lib/coffee-script/helpers.js | 2 +- src/helpers.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/helpers.js b/lib/coffee-script/helpers.js index 70bfa0b1d6..4fae1f6e3f 100644 --- a/lib/coffee-script/helpers.js +++ b/lib/coffee-script/helpers.js @@ -67,7 +67,7 @@ flattened = []; for (i = 0, len1 = array.length; i < len1; i++) { element = array[i]; - if (element instanceof Array) { + if ('[object Array]' === Object.prototype.toString.call(element)) { flattened = flattened.concat(flatten(element)); } else { flattened.push(element); diff --git a/src/helpers.coffee b/src/helpers.coffee index cfa5547d79..b7d73acfa6 100644 --- a/src/helpers.coffee +++ b/src/helpers.coffee @@ -49,7 +49,7 @@ extend = exports.extend = (object, properties) -> exports.flatten = flatten = (array) -> flattened = [] for element in array - if element instanceof Array + if '[object Array]' is Object::toString.call element flattened = flattened.concat flatten element else flattened.push element