From 2c4dfbfd883e5f31bbe195ace03080e3142e5c87 Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Fri, 25 Aug 2017 15:13:59 -0700 Subject: [PATCH] Fix node6 support for Object.entries and URL object (#548) This patch introduces a polyfill for Object.entries which is missing in Node 6 --- lib/NetworkManager.js | 10 +++++----- lib/helper.js | 5 +++++ utils/ESTreeWalker.js | 2 +- utils/node6-transform/TransformAsyncFunctions.js | 2 +- utils/node6-transform/test/test.js | 6 ++++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/NetworkManager.js b/lib/NetworkManager.js index 0564b85e9c1..948fe8aaff2 100644 --- a/lib/NetworkManager.js +++ b/lib/NetworkManager.js @@ -16,7 +16,7 @@ const EventEmitter = require('events'); const helper = require('./helper'); const Multimap = require('./Multimap'); -const {URL} = require('url'); +const url = require('url'); class NetworkManager extends EventEmitter { /** @@ -356,13 +356,13 @@ function generateRequestHash(request) { } /** - * @param {string} url + * @param {string} urlString * @return {string} */ -function removeURLHash(url) { - const urlObject = new URL(url); +function removeURLHash(urlString) { + const urlObject = url.parse(urlString); urlObject.hash = ''; - return urlObject.toString(); + return url.format(urlObject); } NetworkManager.Events = { diff --git a/lib/helper.js b/lib/helper.js index eefa5b8f1c2..2c1957fdfbf 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -218,4 +218,9 @@ class Helper { } } +// Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries +if (!Object.entries) + Object.entries = obj => Object.keys(obj).map(key => [key, obj[key]]); + + module.exports = Helper; diff --git a/utils/ESTreeWalker.js b/utils/ESTreeWalker.js index 5ac8d48ed31..189186a2698 100644 --- a/utils/ESTreeWalker.js +++ b/utils/ESTreeWalker.js @@ -76,7 +76,7 @@ ESTreeWalker.SkipSubtree = {}; /** @enum {!Array.} */ ESTreeWalker._walkOrder = { - 'AwaitExpression': ['arguments'], + 'AwaitExpression': ['argument'], 'ArrayExpression': ['elements'], 'ArrowFunctionExpression': ['params', 'body'], 'AssignmentExpression': ['left', 'right'], diff --git a/utils/node6-transform/TransformAsyncFunctions.js b/utils/node6-transform/TransformAsyncFunctions.js index db306b6b338..696826374a5 100644 --- a/utils/node6-transform/TransformAsyncFunctions.js +++ b/utils/node6-transform/TransformAsyncFunctions.js @@ -53,7 +53,7 @@ const asyncToGenerator = fn => { function transformAsyncFunctions(text) { const edits = []; - const ast = esprima.parseScript(text, {range: true}); + const ast = esprima.parseScript(text, {range: true, tolerant: true}); const walker = new ESTreeWalker(node => { if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression') onFunction(node); diff --git a/utils/node6-transform/test/test.js b/utils/node6-transform/test/test.js index 0e8c326b5b7..233ecf0de89 100644 --- a/utils/node6-transform/test/test.js +++ b/utils/node6-transform/test/test.js @@ -64,4 +64,10 @@ describe('TransformAsyncFunctions', function() { expect(output instanceof Promise).toBe(true); output.then(result => expect(result).toBe(123)).then(done); }); + it('should work with double await', function(done) { + const input = `async function f(){ return 23 + await Promise.resolve(50 + await Promise.resolve(50)); } f();`; + const output = eval(transformAsyncFunctions(input)); + expect(output instanceof Promise).toBe(true); + output.then(result => expect(result).toBe(123)).then(done); + }); }); \ No newline at end of file