Replace `cheerio` with one dependency (`parse5`)
fregante opened this issue · 0 comments
fregante commented
addons-linter
uses cheerio
to validate the HTML, currently there are two rules1 and cheerio is unreasonably large just for that (2.80MB!)2.
Cheerio wraps parse5
to parse the AST and addons-linter
could use it directly, saving almost 2 megabytes.3 The issue is that parse5
only returns an object4, no querying ability.
💡 I'm not sure whether this is actually possible, but would you be open to a PR that passes current tests?
Given the number of rules (2!) would it make sense to try and walk the AST directly? Maybe a simple walker would be as simple as:
function walk(dom, filter) {
if (filter(dom)) {
return dom;
}
if (!dom.childNodes) {
return;
}
for (const node of dom.childNodes) {
const result = walk(node);
if (result) {
return result
}
}
}
And then the rules would look something like:
const invalid = walk(parse5(source), node => node.tagName === 'script' && node.attrs.some(attr => attr.type === 'etc etc'))
if (invalid) {
rules.push(invalid, ...details)
}