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

fix: less noisy CSS unused rules #1466

Merged
merged 3 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all 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: 9 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@
font-size: 20px;
}
</style>

<style>
/* PASS - Empty on purpose */
</style>

<style>
/* PASS - Just an import shouldn't show up */
@import url('https://fonts.googleapis.com/css?family=Open+Sans');
</style>
</template>

</head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ module.exports = [
}
},
'unused-css-rules': {
score: false
score: false,
extendedInfo: {
value: {
length: 4
}
}
},
'uses-passive-event-listeners': {
score: false,
Expand Down
13 changes: 11 additions & 2 deletions lighthouse-core/audits/unused-css-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class UnusedCSSRules extends Audit {
rules.forEach(rule => {
const stylesheetInfo = indexedStylesheets[rule.styleSheetId];

if (stylesheetInfo.isDuplicate) {
return;
}

if (rule.used) {
stylesheetInfo.used.push(rule);
} else {
Expand All @@ -81,7 +85,12 @@ class UnusedCSSRules extends Audit {
static mapSheetToResult(stylesheetInfo) {
const numUsed = stylesheetInfo.used.length;
const numUnused = stylesheetInfo.unused.length;
const percentUsed = Math.round(100 * numUsed / (numUsed + numUnused)) || 0;

if ((numUsed === 0 && numUnused === 0) || stylesheetInfo.isDuplicate) {
return null;
}

const percentUsed = Math.round(100 * numUsed / (numUsed + numUnused));

let contentPreview = stylesheetInfo.content;
if (contentPreview.length > PREVIEW_LENGTH) {
Expand Down Expand Up @@ -127,7 +136,7 @@ class UnusedCSSRules extends Audit {
const unusedRatio = (unused / usage.length) || 0;
const results = Object.keys(indexedSheets).map(sheetId => {
return UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId]);
});
}).filter(Boolean);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't feel strongly about it, but .filter(n => n) would also filter out the falsey values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess this is a pattern I'm used to, but is there a reason we don't use Boolean anywhere? !! seems to be preferred for casting throughout.



let displayValue = '';
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/scripts/run-mocha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ elif [ "$flag" == '--cli' ]; then
_runmocha 'lighthouse-cli'
elif [ "$flag" == '--viewer' ]; then
_runmocha 'lighthouse-viewer'
elif [ "$flag" == '--core' ]; then
_runmocha 'lighthouse-core'
else
_runmocha 'lighthouse-cli' && _runmocha 'lighthouse-core' && _runmocha 'lighthouse-viewer'
fi
66 changes: 63 additions & 3 deletions lighthouse-core/test/audits/unused-css-rules-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ describe('Best Practices: unused css rules audit', () => {
beforeEach(() => {
baseSheet = {
header: {sourceURL: ''},
content: '',
used: [],
content: 'dummy',
used: [{dummy: 1}],
unused: [],
};
});

it('correctly computes percentUsed', () => {
assert.ok(/0%/.test(map({used: [], unused: []}).label));
assert.ok(/0%/.test(map({used: [], unused: [1, 2]}).label));
assert.ok(/50%/.test(map({used: [1, 2], unused: [1, 2]}).label));
assert.ok(/100%/.test(map({used: [1, 2], unused: []}).label));
Expand Down Expand Up @@ -177,5 +176,66 @@ describe('Best Practices: unused css rules audit', () => {
assert.equal(result.rawValue, false);
assert.equal(result.extendedInfo.value.length, 3);
});

it('does not include duplicate sheets', () => {
const result = UnusedCSSAudit.audit({
CSSUsage: [
{styleSheetId: 'a', used: true},
{styleSheetId: 'a', used: true},
{styleSheetId: 'b', used: false},
],
Styles: [
{
header: {styleSheetId: 'a', sourceURL: 'a.css'},
content: '.my.selector {color: #ccc;}\n a {color: #fff}'
},
{
isDuplicate: true,
header: {styleSheetId: 'b', sourceURL: 'b.css'},
content: 'a.other {color: #fff}'
},
]
});

assert.ok(!result.displayValue);
assert.equal(result.rawValue, true);
assert.equal(result.extendedInfo.value.length, 1);
});

it('does not include empty sheets', () => {
const result = UnusedCSSAudit.audit({
CSSUsage: [
{styleSheetId: 'a', used: true},
{styleSheetId: 'a', used: true},
{styleSheetId: 'b', used: true},
],
Styles: [
{
header: {styleSheetId: 'a', sourceURL: 'a.css'},
content: '.my.selector {color: #ccc;}\n a {color: #fff}'
},
{
header: {styleSheetId: 'b', sourceURL: 'b.css'},
content: '.my.favorite.selector { rule: content; }'
},
{
header: {styleSheetId: 'c', sourceURL: 'c.css'},
content: '@import url(http://googlefonts.com?myfont)'
},
{
header: {styleSheetId: 'd', sourceURL: 'd.css'},
content: '/* nothing to see here */'
},
{
header: {styleSheetId: 'e', sourceURL: 'e.css'},
content: ' '
},
]
});

assert.ok(!result.displayValue);
assert.equal(result.rawValue, true);
assert.equal(result.extendedInfo.value.length, 2);
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls",
"start": "node ./lighthouse-cli/index.js",
"test": "npm run lint --silent && npm run unit",
"core-unit": "lighthouse-core/scripts/run-mocha.sh --core",
"cli-unit": "lighthouse-core/scripts/run-mocha.sh --cli",
"viewer-unit": "lighthouse-core/scripts/run-mocha.sh --viewer",
"unit": "lighthouse-core/scripts/run-mocha.sh --default",
Expand Down