Skip to content

Commit

Permalink
Allow local data
Browse files Browse the repository at this point in the history
Details: posthtml#54
  • Loading branch information
andreyvolokitin committed Jan 29, 2018
1 parent 02c9ad8 commit 3558696
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,26 @@ function walk (opts, nodes) {
// parse scopes
if (node.tag === scopes[0]) {
// handle syntax error
if (!node.attrs || !node.attrs.with) {
throw new Error(`the "${scopes[0]}" tag must have a "with" attribute`)
if (!node.attrs) {
throw new Error(`the "${scopes[0]}" tag must have attributes with data or/and a "with" attribute`)
}

const target = vm.runInContext(node.attrs.with, ctx)
const targetScope = node.attrs.with ? vm.runInContext(node.attrs.with, ctx) : {}

// handle additional syntax errors
if (typeof target !== 'object' || Array.isArray(target)) {
if (typeof targetScope !== 'object' || Array.isArray(targetScope)) {
throw new Error('You must provide an object to make scope')
}

const keys = Object.keys(target)
Object.assign(targetScope, node.attrs)
delete targetScope.with

const keys = Object.keys(targetScope)

// creates a copy of the keys that will be changed within the loop
const localsBackup = makeLocalsBackup(keys, opts.locals)

m.push(executeScope(target, opts.locals, node))
m.push(executeScope(targetScope, opts.locals, node))

// returns the original keys values that was changed within the loop
opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup)
Expand Down

0 comments on commit 3558696

Please sign in to comment.