Skip to content

Commit

Permalink
fix: copy options.expressions into new object to not pollute locals
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Clausmeyer committed Dec 6, 2022
1 parent de0be4a commit b5a6dfe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Expand Up @@ -50,8 +50,10 @@ function handleExtendsNodes(tree, options, messages) {
} catch {}
}

options.expressions.locals = merge(options.expressions.locals, locals);
const plugins = [...options.plugins, expressions(options.expressions)];
const plugins = [...options.plugins, expressions({
...options.expressions,
locals: merge(options.expressions.locals, locals)
})];

const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
Expand Down
12 changes: 6 additions & 6 deletions test/extend.js
Expand Up @@ -428,11 +428,11 @@ describe('Extend', () => {
});

/*
This plugin merges the locals from options.expressions.locals and the
This plugin used to merge the locals from options.expressions.locals and the
"locals" attribute into the options.expressions.locals property. Arrays in
the attribute locals will be copied into options.expressions.locals and when
the plugin is called again they will be merged again, doubling all array
entries every time the plugin is called.
the attribute locals would be copied into options.expressions.locals and
when the plugin was called again they would be merged again, doubling all
array entries every time the plugin was called.
*/
it('should not pollute options.expression.locals ', async () => {
mfs.writeFileSync('./base.html', `<div class="base">{{ list.join(", ") }}</div>`);
Expand All @@ -452,8 +452,8 @@ describe('Extend', () => {
expect(await init(preHtml, options)).toBe(postHtml);
expect(options.expressions.locals).toStrictEqual({});
/*
Since the entries in "list" have been merged with themselves, the content
would be "One, Two, Three, One, Two, Three"
Since the entries in "list" would have been merged with themselves, the
content would have been "One, Two, Three, One, Two, Three"
*/
expect(await init(preHtml, options)).toBe(postHtml);
});
Expand Down

0 comments on commit b5a6dfe

Please sign in to comment.