From a0bc325b813493495c7436b90d477063753689c2 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 1 Jul 2020 18:06:23 +0100 Subject: [PATCH] chore(NA): extend patches to lodash/fp and test for using template as an iteratee --- src/setup_node_env/harden.js | 4 ++-- src/setup_node_env/patches/lodash.js | 6 +++--- test/harden/lodash.js | 30 ++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/setup_node_env/harden.js b/src/setup_node_env/harden.js index c74cde5d5d80a..90bae0722eb31 100644 --- a/src/setup_node_env/harden.js +++ b/src/setup_node_env/harden.js @@ -23,6 +23,6 @@ hook(['child_process'], function (exports, name) { return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require }); -hook(['lodash'], function(exports, name) { - return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require +hook(['lodash', 'lodash/fp'], function (exports) { + return require(`./patches/lodash`)(exports); // eslint-disable-line import/no-dynamic-require }); diff --git a/src/setup_node_env/patches/lodash.js b/src/setup_node_env/patches/lodash.js index 9390f12cc320f..206ebc033553b 100644 --- a/src/setup_node_env/patches/lodash.js +++ b/src/setup_node_env/patches/lodash.js @@ -17,16 +17,16 @@ * under the License. */ -module.exports = function(lodash) { +module.exports = function (lodash) { lodash.template = new Proxy(lodash.template, { - apply: function(target, thisArg, args) { + apply: function (target, thisArg, args) { var options; if (args.length === 1) { options = { sourceURL: '', }; } else { - options = { ...args[1] }; + options = Object.assign({}, args[1]); options.sourceURL = (options.sourceURL + '').replace(/\s/g, ' '); } diff --git a/test/harden/lodash.js b/test/harden/lodash.js index c0d2988e0839f..331bacb876b9f 100644 --- a/test/harden/lodash.js +++ b/test/harden/lodash.js @@ -19,6 +19,7 @@ require('../../src/setup_node_env'); const _ = require('elasticsearch/node_modules/lodash'); // our fork has been patched already, using a non-patched version +const _fp = require('elasticsearch/node_modules/lodash'); // our fork has been patched already, using a non-patched version const test = require('tape'); Object.prototype.sourceURL = '\u2028\u2029\n;global.whoops=true'; // eslint-disable-line no-extend-native @@ -27,33 +28,33 @@ test.onFinish(() => { delete Object.prototype.sourceURL; }); -test('test setup ok', t => { +test('test setup ok', (t) => { t.equal({}.sourceURL, '\u2028\u2029\n;global.whoops=true'); t.end(); }); -test(`_.template('<%= foo %>')`, t => { +test(`_.template('<%= foo %>')`, (t) => { const output = _.template('<%= foo %>')({ foo: 'bar' }); t.equal(output, 'bar'); t.equal(global.whoops, undefined); t.end(); }); -test(`_.template('<%= foo %>', {})`, t => { +test(`_.template('<%= foo %>', {})`, (t) => { const output = _.template('<%= foo %>', Object.freeze({}))({ foo: 'bar' }); t.equal(output, 'bar'); t.equal(global.whoops, undefined); t.end(); }); -test(`_.template('<%= data.foo %>', { variable: 'data' })`, t => { +test(`_.template('<%= data.foo %>', { variable: 'data' })`, (t) => { const output = _.template('<%= data.foo %>', Object.freeze({ variable: 'data' }))({ foo: 'bar' }); t.equal(output, 'bar'); t.equal(global.whoops, undefined); t.end(); }); -test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, t => { +test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, (t) => { // throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do const template = _.template('<% throw new Error() %>', Object.freeze({ sourceURL: '/foo/bar' })); t.plan(2); @@ -66,7 +67,7 @@ test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, t => { } }); -test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, t => { +test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, (t) => { // throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do const template = _.template( '<% throw new Error() %>', @@ -82,6 +83,23 @@ test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true } }); +test(`_.template used as an iteratee call(`, (t) => { + const templateStrArr = ['<%= data.foo %>', 'example <%= data.foo %>']; + const output = _.map(templateStrArr, _.template); + + t.equal(output[0]({ data: { foo: 'bar' } }), 'bar'); + t.equal(output[1]({ data: { foo: 'bar' } }), 'example bar'); + t.equal(global.whoops, undefined); + t.end(); +}); + +test(`_fp.template('<%= foo %>')`, (t) => { + const output = _fp.template('<%= foo %>')({ foo: 'bar' }); + t.equal(output, 'bar'); + t.equal(global.whoops, undefined); + t.end(); +}); + function parsePathFromStack(stack) { const lines = stack.split('\n'); // the frame starts at the second line