Skip to content

Commit

Permalink
core: fix js edge case in minification estimator (#12067)
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Feb 9, 2021
1 parent b439bf2 commit aaf1365
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lighthouse-core/lib/minification-estimator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-punctuators
// eslint-disable-next-line max-len
const PUNCTUATOR_REGEX = /(return|{|\(|\[|\.\.\.|;|,|<|>|<=|>=|==|!=|===|!==|\+|-|\*|%|\*\*|\+\+|--|<<|>>|>>>|&|\||\^|!|~|&&|\|\||\?|:|=|\+=|-=|\*=|%=|\*\*=|<<=|>>=|>>>=|&=|\|=|\^=|=>|\/|\/=|\})$/;
const PUNCTUATOR_REGEX = /(return|case|{|\(|\[|\.\.\.|;|,|<|>|<=|>=|==|!=|===|!==|\+|-|\*|%|\*\*|\+\+|--|<<|>>|>>>|&|\||\^|!|~|&&|\|\||\?|:|=|\+=|-=|\*=|%=|\*\*=|<<=|>>=|>>>=|&=|\|=|\^=|=>|\/|\/=|\})$/;
const WHITESPACE_REGEX = /( |\n|\t)+$/;

/**
Expand Down
9 changes: 9 additions & 0 deletions lighthouse-core/test/lib/minification-estimator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ describe('minification estimator', () => {
assert.equal(computeJSTokenLength(js), 9);
});

it('should handle regex as switch case clause edge cases', () => {
const js = `
switch(true){case/^hello!/.test("hello!"):"///123456789"}
`;

assert.equal(computeJSTokenLength(js), 57);
assert.equal(computeJSTokenLength(js), js.trim().length);
});

it('should handle large, real, unminified javscript files', () => {
assert.equal(angularJs.length, 1374505);
const minificationPct = 1 - computeJSTokenLength(angularJs) / angularJs.length;
Expand Down

0 comments on commit aaf1365

Please sign in to comment.