-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patch lodash template #64985
Closed
Closed
Patch lodash template #64985
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a35ccc
Patching lodash's template
kobelb c7ed319
And now some tests!
kobelb 5b44865
Removing errant console.log
kobelb 14c49e6
Merge remote-tracking branch 'upstream/master' into patch-lodash-temp…
kobelb a0bc325
chore(NA): extend patches to lodash/fp and test for using template as…
mistic 5dde890
Merge pull request #13 from mistic/patch-lodash-template
kobelb 5b3ba3e
Separate patch for 'lodash/fp'
kobelb 179198f
Merge branch 'master' into patch-lodash-template
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
module.exports = function (lodash) { | ||
lodash.template = new Proxy(lodash.template, { | ||
apply: function (target, thisArg, args) { | ||
var options; | ||
if (args.length === 1) { | ||
options = { | ||
sourceURL: '', | ||
}; | ||
} else { | ||
options = Object.assign({}, args[1]); | ||
options.sourceURL = (options.sourceURL + '').replace(/\s/g, ' '); | ||
} | ||
|
||
args[1] = options; | ||
return target.apply(thisArg, args); | ||
}, | ||
}); | ||
|
||
return lodash; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
module.exports = function (fp, _) { | ||
// per https://github.com/lodash/lodash/wiki/FP-Guide | ||
// > Iteratee arguments are capped to avoid gotchas with variadic iteratees. | ||
// this means that we can't specify thhe options in the second argument... in the proxy | ||
// and just have everything work. Instead, we're going to use the non-FP _.template | ||
// with just the first argument that is specified, and a hardcoded options with sourceURL of '' | ||
fp.template = new Proxy(fp.template, { | ||
apply: function (target, thisArg, args) { | ||
return _.template(args[0], { sourceURL: '' }); | ||
}, | ||
}); | ||
|
||
return fp; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
require('../../src/setup_node_env'); | ||
const _ = require('lodash'); | ||
const test = require('tape'); | ||
|
||
Object.prototype.sourceURL = '\u2028\u2029\n;global.whoops=true'; // eslint-disable-line no-extend-native | ||
|
||
test.onFinish(() => { | ||
delete Object.prototype.sourceURL; | ||
}); | ||
|
||
test('test setup ok', (t) => { | ||
t.equal({}.sourceURL, '\u2028\u2029\n;global.whoops=true'); | ||
t.end(); | ||
}); | ||
|
||
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) => { | ||
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) => { | ||
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) => { | ||
// 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); | ||
try { | ||
template(); | ||
} catch (err) { | ||
const path = parsePathFromStack(err.stack); | ||
t.equal(path, '/foo/bar'); | ||
t.equal(global.whoops, undefined); | ||
} | ||
}); | ||
|
||
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() %>', | ||
Object.freeze({ sourceURL: '\u2028\u2029\nglobal.whoops=true' }) | ||
); | ||
t.plan(2); | ||
try { | ||
template(); | ||
} catch (err) { | ||
const path = parsePathFromStack(err.stack); | ||
t.equal(path, 'global.whoops=true'); | ||
t.equal(global.whoops, undefined); | ||
} | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
function parsePathFromStack(stack) { | ||
const lines = stack.split('\n'); | ||
// the frame starts at the second line | ||
const frame = lines[1]; | ||
|
||
// the path is in parathensis, and ends with a colon before the line/column numbers | ||
const [, path] = /\(([^:]+)/.exec(frame); | ||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
require('../../src/setup_node_env'); | ||
const fp = require('lodash/fp'); | ||
const test = require('tape'); | ||
|
||
Object.prototype.sourceURL = '\u2028\u2029\n;global.whoops=true'; // eslint-disable-line no-extend-native | ||
|
||
test.onFinish(() => { | ||
delete Object.prototype.sourceURL; | ||
}); | ||
|
||
test('test setup ok', (t) => { | ||
t.equal({}.sourceURL, '\u2028\u2029\n;global.whoops=true'); | ||
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(); | ||
}); | ||
|
||
test(`fp.template('<%= foo %>', {})`, (t) => { | ||
// fp.template ignores the second argument, this is negligible in this situation since options is an empty object | ||
const output = fp.template('<%= foo %>', Object.freeze({}))({ foo: 'bar' }); | ||
t.equal(output, 'bar'); | ||
t.equal(global.whoops, undefined); | ||
t.end(); | ||
}); | ||
|
||
test(`fp.template('<%= data.foo %>', { variable: 'data' })`, (t) => { | ||
// fp.template ignores the second argument, this causes an error to be thrown | ||
t.plan(2); | ||
try { | ||
fp.template('<%= data.foo %>', Object.freeze({ variable: 'data' }))({ foo: 'bar' }); | ||
} catch (err) { | ||
t.equal(err.message, 'data is not defined'); | ||
t.equal(global.whoops, undefined); | ||
} | ||
}); | ||
|
||
test(`fp.template('<%= foo %>', { sourceURL: '/foo/bar' })`, (t) => { | ||
// fp.template ignores the second argument, the sourceURL is ignored | ||
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do | ||
// our patching to hard-code the sourceURL and use non-FP _.template does slightly alter the stack-traces but it's negligible | ||
const template = fp.template('<% throw new Error() %>', Object.freeze({ sourceURL: '/foo/bar' })); | ||
t.plan(3); | ||
try { | ||
template(); | ||
} catch (err) { | ||
const path = parsePathFromStack(err.stack); | ||
t.match(path, /^eval at <anonymous> /); | ||
t.doesNotMatch(path, /\/foo\/bar/); | ||
t.equal(global.whoops, undefined); | ||
} | ||
}); | ||
|
||
test(`fp.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, (t) => { | ||
// fp.template ignores the second argument, the sourceURL is ignored | ||
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do | ||
// our patching to hard-code the sourceURL and use non-FP _.template does slightly alter the stack-traces but it's negligible | ||
const template = fp.template( | ||
'<% throw new Error() %>', | ||
Object.freeze({ sourceURL: '\u2028\u2029\nglobal.whoops=true' }) | ||
); | ||
t.plan(3); | ||
try { | ||
template(); | ||
} catch (err) { | ||
const path = parsePathFromStack(err.stack); | ||
t.match(path, /^eval at <anonymous> /); | ||
t.doesNotMatch(path, /\/foo\/bar/); | ||
t.equal(global.whoops, undefined); | ||
} | ||
}); | ||
|
||
test(`fp.template used as an iteratee call(`, (t) => { | ||
const templateStrArr = ['<%= data.foo %>', 'example <%= data.foo %>']; | ||
const output = fp.map(fp.template)(templateStrArr); | ||
|
||
t.equal(output[0]({ data: { foo: 'bar' } }), 'bar'); | ||
t.equal(output[1]({ data: { foo: 'bar' } }), 'example bar'); | ||
t.equal(global.whoops, undefined); | ||
t.end(); | ||
}); | ||
|
||
function parsePathFromStack(stack) { | ||
const lines = stack.split('\n'); | ||
// the frame starts at the second line | ||
const frame = lines[1]; | ||
|
||
// the path is in parathensis, and ends with a colon before the line/column numbers | ||
const [, path] = /\(([^:]+)/.exec(frame); | ||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/harden", | ||
"version": "1.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"lodash": "4.17.15" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worthwhile to test lodash with the fourth
guard
parameter provided? In other words, testing that calling_.template
as an iteree of_.map
still works as expected? I only ask because there is special handling today to clear out the provided options:https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L14776-L14778
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do... I need to give the
guard
parameter more attention. I came across it on Friday, grew frustrated, and moved on...