|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const vm = require('vm')
|
4 |
| -const merge = require('lodash.merge') |
5 | 4 | const cloneDeep = require('lodash.clonedeep')
|
6 | 5 | const placeholders = require('./placeholders')
|
7 | 6 |
|
@@ -215,7 +214,15 @@ function walk (opts, nodes) {
|
215 | 214 | throw new Error('You must provide an object to make scope')
|
216 | 215 | }
|
217 | 216 |
|
218 |
| - m.push(executeScoped(target, node)) |
| 217 | + const keys = Object.keys(target) |
| 218 | + |
| 219 | + // creates a copy of the keys that will be changed within the loop |
| 220 | + const localsBackup = makeLocalsBackup(keys, opts.locals) |
| 221 | + |
| 222 | + m.push(executeScoped(target, opts.locals, node)) |
| 223 | + |
| 224 | + // returns the original keys values that was changed within the loop |
| 225 | + opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup) |
219 | 226 |
|
220 | 227 | // return directly out of the loop, which will skip the "scope" tag
|
221 | 228 | return m
|
@@ -316,11 +323,7 @@ function executeLoop (loopParams, p1, p2, locals, treeString) {
|
316 | 323 | /**
|
317 | 324 | * Runs walk function with arbitrary set of local variables
|
318 | 325 | */
|
319 |
| -function executeScoped (scopedLocals, node) { |
320 |
| - // merge nondestructively into existing locals |
321 |
| - const scopedOptions = merge(cloneDeep(data), { locals: scopedLocals }) |
322 |
| - // walk through the contents and run replacements with modified options |
323 |
| - // we need to clone the node because the normal operation modifies |
324 |
| - // the node directly |
325 |
| - return walk(scopedOptions, cloneDeep(node.content)) |
| 326 | +function executeScoped (scopeLocals, locals, node) { |
| 327 | + const scopedLocals = Object.assign(locals, scopeLocals) |
| 328 | + return walk({ locals: scopedLocals }, node.content) |
326 | 329 | }
|
0 commit comments