Skip to content

Commit

Permalink
Merge pull request #13 from mistic/patch-lodash-template
Browse files Browse the repository at this point in the history
chore(NA): extend patches to lodash/fp and test for using template as an iteratee
  • Loading branch information
kobelb authored Jul 2, 2020
2 parents 14c49e6 + a0bc325 commit 5dde890
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/setup_node_env/harden.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
6 changes: 3 additions & 3 deletions src/setup_node_env/patches/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, ' ');
}

Expand Down
30 changes: 24 additions & 6 deletions test/harden/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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() %>',
Expand All @@ -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
Expand Down

0 comments on commit 5dde890

Please sign in to comment.