Skip to content
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

Template does not escape ES template literals #4039

Closed
goncalo-matos opened this issue Nov 3, 2018 · 3 comments
Closed

Template does not escape ES template literals #4039

goncalo-matos opened this issue Nov 3, 2018 · 3 comments
Labels

Comments

@goncalo-matos
Copy link

About version 4.17.11

although the template function allows using ES template literals it does not allow it to be escaped. Case in point:

With lodash's template:

let lodashTemplate = _.template('\${foo}')({foo: 'bar'}); 
console.log(lodashTemplate); // prints 'bar'

With template literals:

let foo = 'bar';
let esTemplate = `\${foo}`;
console.log(esTemplate); // prints '${foo}'

Is this a bug or does template not allow to escape template literals (as it can be disabled with a custom interpolator function)?

@goncalo-matos
Copy link
Author

By reading the code, it seems to me that it only uses the es template regex if no interpolation is given (as per the docs as well), but it does not have any kind of logic for the escape regex (it always uses the one provided/the default).

lodash/lodash.js

Lines 14788 to 14793 in 39a7eae

var reDelimiters = RegExp(
(options.escape || reNoMatch).source + '|' +
interpolate.source + '|' +
(interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
(options.evaluate || reNoMatch).source + '|$'
, 'g');

Wouldn't it be the same to make the default regex one that includes both the es delimiter (${}) and the <% delimiters? By using the same logic the escape template can easily get both escape characters as well.

@jdalton jdalton added the invalid label Nov 3, 2018
@jdalton
Copy link
Member

jdalton commented Nov 3, 2018

Hi @Atekon!

Our template method is pretty simple you can customize the regexes it uses to match delimiters though as _.templateSettings or options to the _.template method.

@jdalton jdalton closed this as completed Nov 3, 2018
@lock
Copy link

lock bot commented Nov 3, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants