Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(minification): properly handle regex character classes #6745

Merged
merged 4 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lighthouse-core/lib/minification-estimator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function computeTokenLength(content, features) {
let isInLicenseComment = false;
let isInString = false;
let isInRegex = false;
let isInRegexCharacterClass = false;
let stringOpenChar = null;

for (let i = 0; i < content.length; i++) {
Expand Down Expand Up @@ -94,7 +95,13 @@ function computeTokenLength(content, features) {
// Skip over any escaped characters
totalTokenLength++;
i++;
} else if (char === '/') {
} else if (char === '[') {
// Register that we're entering a character class so we don't leave the regex prematurely
isInRegexCharacterClass = true;
} else if (char === ']' && isInRegexCharacterClass) {
// Register that we're exiting the character class
isInRegexCharacterClass = false;
} else if (char === '/' && !isInRegexCharacterClass) {
// End the string when we hit the regex close character
isInRegex = false;
// console.log(i, 'leaving regex', char)
Expand Down
21 changes: 19 additions & 2 deletions lighthouse-core/test/lib/minification-estimator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ describe('minification estimator', () => {
assert.equal(computeJSTokenLength(js), 9);
});

it('should handle regular expression character classes', () => {
const js = `
/regex [^/]\\//.test('this should be in string not comment 123456789')
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
`;

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

it('should handle escaped regular expression characters', () => {
const js = `
/regex \\[/ // this should be in comment not string 123456789
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
`;

assert.equal(computeJSTokenLength(js), 10);
});

it('should distinguish regex from divide', () => {
const js = `
return 1 / 2 // hello
Expand All @@ -190,8 +207,8 @@ describe('minification estimator', () => {

it('should handle large, real javscript files', () => {
assert.equal(angularFullScript.length, 1364217);
// 1 - 405199 / 1364217 = estimated 70% smaller minified
assert.equal(computeJSTokenLength(angularFullScript), 405199);
// 1 - 334968 / 1364217 = estimated 75% smaller minified
assert.equal(computeJSTokenLength(angularFullScript), 334968);
});
});
});