Skip to content

Commit 1d7596a

Browse files
committed
fix: check if key exists in locals as text, close #102
1 parent baf0ddc commit 1d7596a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function walk (opts, nodes) {
182182

183183
// if we have a string, match and replace it
184184
if (typeof node === 'string') {
185-
node = placeholders(node, ctx, delimitersSettings)
185+
node = placeholders(node, ctx, delimitersSettings, opts)
186186
node = node
187187
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
188188
.replace(delimitersReplace, delimitersSettings[1].text[0])
@@ -196,14 +196,14 @@ function walk (opts, nodes) {
196196
if (node.attrs) {
197197
for (const key in node.attrs) {
198198
if (typeof node.attrs[key] === 'string') {
199-
node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings)
199+
node.attrs[key] = placeholders(node.attrs[key], ctx, delimitersSettings, opts)
200200
node.attrs[key] = node.attrs[key]
201201
.replace(unescapeDelimitersReplace, delimitersSettings[0].text[0])
202202
.replace(delimitersReplace, delimitersSettings[1].text[0])
203203
}
204204

205205
// if key is parametr
206-
const _key = placeholders(key, ctx, delimitersSettings)
206+
const _key = placeholders(key, ctx, delimitersSettings, opts)
207207
if (key !== _key) {
208208
node.attrs[_key] = node.attrs[key]
209209
delete node.attrs[key]

lib/placeholders.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function escapeHTML (unescaped) {
3535
*
3636
* @return {String} input Replaced Input
3737
*/
38-
function placeholders (input, ctx, settings) {
38+
function placeholders (input, ctx, settings, opts) {
3939
// Since we are matching multiple sets of delimiters, we need to run a loop
4040
// here to match each one.
4141
for (let i = 0; i < settings.length; i++) {
@@ -54,7 +54,13 @@ function placeholders (input, ctx, settings) {
5454
let value
5555

5656
if (/\W+/.test(expression)) {
57-
value = vm.runInContext(expression, ctx)
57+
try {
58+
value = vm.runInContext(expression, ctx)
59+
} catch (error) {
60+
if (opts.strictMode) {
61+
throw new SyntaxError(error)
62+
}
63+
}
5864
} else if (Object.prototype.hasOwnProperty.call(ctx, expression)) {
5965
value = ctx[expression]
6066
}

0 commit comments

Comments
 (0)