Mismatched pragmas cause unpredictable output
betaorbust opened this issue · comments
Issue
Currently, groundskeeper just matches on /^[<]([/]([^\s])[>]$/ for any pragma and then does an every-other pass to decide what to strip out without looking at the content of the directives. This means that a malformed directive can cause very unpredictable output. This initially came up when somebody removed the opening tag of a pragma, but forgot to remove the closing one.
Example
Source:
console.log('keep me!');
//<pragma>
console.log('remove me!');
// <oops>
console.log('remove me, too!');
// </pragma>
console.log('keep me!');
Output:
console.log('keep me!');
console.log('remove me, too!');
Proposal
Keep track of current open pragmas, and ignore mismatched directives.
Having now worked with it, my proposal has shifted to throwing an error. It's much better to know you have broken code before you get it up on your server ;)
Don't get me wrong but this project has been deprecated since UglifyJS (and now terser) support their own flavor of pragmas: https://github.com/mishoo/UglifyJS2#conditional-compilation