escodegen
escodegen copied to clipboard
Badly performing regexp when aligning trailing comments after long line
Hi! I've run into a case where escodegen never seems to complete the formatting of a program. It happens with a combination of a very long line (a minified JavaScript bundle) that has two trailing comments.
This reproduces it:
console.log(require('./escodegen').generate({
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'a'.repeat(1000000)
},
trailingComments: [
{
type: 'Line',
value: '# sourceMappingURL=foo.map'
},
{
value: '# sourceMappingURL=bar.map',
type: 'Line'
}
]
}
]
}, {
format: { newline: '' },
comment: true
}));
It seems like escodegen tries to align the two comments by putting the second one on a new line with 1000004 leading spaces, and then fragment.replace(/\s+$/, '')
in this line takes a very long time due to exponential performance characteristics.
I discovered it because of a bug in assetgraph that caused those two sourceMappingURL
comments to be there under very specific circumstances. I've fixed the bug now, but still thought I'd let you know :)
An easy fix might be to disable the indentation of the subsequent trailing line comments when the first line is outrageously long?