posthtml / posthtml-extend

Template extending (Jade-like)
MIT License
46 stars 9 forks source link

fix: copy options.expressions into new object to not pollute locals #54

Closed shwao closed 1 year ago

shwao commented 1 year ago

This plugin merges the locals from options.expressions.locals and the locals attribute into the options.expressions.locals property. Arrays in the locals attribute 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.

https://github.com/posthtml/posthtml-extend/blob/ccf454a4b0ead941626bd7c09aae79c0e8c2388c/src/index.js#L53-L54

I fixed that by copying the options.expressions object into a new object.

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

I added a test which checks if options.expressions.locals stays the same after rendering with the plugin. Run the GitHub Action for the commit test: add test for options.expressions.locals pollution to see the failing test. Run Actions for latest commit for succeeding tests.