Skip to content

Commit d13fefb

Browse files
authored
Merge pull request #54 from shwao/dev
fix: copy options.expressions into new object to not pollute locals
2 parents ccf454a + b5a6dfe commit d13fefb

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ function handleExtendsNodes(tree, options, messages) {
5050
} catch {}
5151
}
5252

53-
options.expressions.locals = merge(options.expressions.locals, locals);
54-
const plugins = [...options.plugins, expressions(options.expressions)];
53+
const plugins = [...options.plugins, expressions({
54+
...options.expressions,
55+
locals: merge(options.expressions.locals, locals)
56+
})];
5557

5658
const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
5759
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);

test/extend.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,37 @@ describe('Extend', () => {
426426
return init(page3, {strict: false})
427427
.then(html => expect(html).toBe(want));
428428
});
429+
430+
/*
431+
This plugin used to merge the locals from options.expressions.locals and the
432+
"locals" attribute into the options.expressions.locals property. Arrays in
433+
the attribute locals would be copied into options.expressions.locals and
434+
when the plugin was called again they would be merged again, doubling all
435+
array entries every time the plugin was called.
436+
*/
437+
it('should not pollute options.expression.locals ', async () => {
438+
mfs.writeFileSync('./base.html', `<div class="base">{{ list.join(", ") }}</div>`);
439+
440+
const options = {
441+
expressions: {
442+
locals: {}
443+
}
444+
};
445+
446+
const preHtml = `
447+
<extends src="base.html" locals='{"list": ["One", "Two", "Three"]}'>
448+
</extends>
449+
`;
450+
const postHtml = `<div class="base">One, Two, Three</div>`;
451+
452+
expect(await init(preHtml, options)).toBe(postHtml);
453+
expect(options.expressions.locals).toStrictEqual({});
454+
/*
455+
Since the entries in "list" would have been merged with themselves, the
456+
content would have been "One, Two, Three, One, Two, Three"
457+
*/
458+
expect(await init(preHtml, options)).toBe(postHtml);
459+
});
429460
});
430461

431462
describe('Messages', () => {

0 commit comments

Comments
 (0)