Skip to content

Commit

Permalink
Add table renderer to deprecations audit (GoogleChrome#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel authored and paulirish committed May 5, 2017
1 parent 18f660d commit 9d71fcf
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
23 changes: 22 additions & 1 deletion lighthouse-core/audits/deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

const Audit = require('./audit');
const Formatter = require('../report/formatter');
const URL = require('../lib/url-shim');

class Deprecations extends Audit {
/**
Expand Down Expand Up @@ -59,6 +60,25 @@ class Deprecations extends Audit {
}, log.entry);
});

const deprecationsV2 = entries.filter(log => log.entry.source === 'deprecation').map(log => {
// CSS deprecations can have missing URLs and lineNumbers. See https://crbug.com/680832.
const url = URL.isValid(log.entry.url) ? URL.getDisplayName(log.entry.url) : '';
return {
type: 'code',
text: log.entry.text,
url,
source: log.entry.source,
lineNumber: log.entry.lineNumber
};
});

const headings = [
{key: 'text', itemType: 'code', text: 'Deprecation / Warning'},
{key: 'url', itemType: 'url', text: 'URL'},
{key: 'lineNumber', itemType: 'text', text: 'Line'},
];
const details = Audit.makeV2TableDetails(headings, deprecationsV2);

let displayValue = '';
if (deprecations.length > 1) {
displayValue = `${deprecations.length} warnings found`;
Expand All @@ -72,7 +92,8 @@ class Deprecations extends Audit {
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.URL_LIST,
value: deprecations
}
},
details
};
}
}
Expand Down
24 changes: 18 additions & 6 deletions lighthouse-core/report/v2/renderer/details-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class DetailsRenderer {
return this._renderCards(/** @type {!DetailsRenderer.CardsDetailsJSON} */ (details));
case 'table':
return this._renderTable(/** @type {!DetailsRenderer.TableDetailsJSON} */ (details));
case 'code':
return this._renderCode(details);
case 'list':
return this._renderList(/** @type {!DetailsRenderer.ListDetailsJSON} */ (details));
default:
Expand All @@ -53,19 +55,19 @@ class DetailsRenderer {
* @param {!DetailsRenderer.DetailsJSON} text
* @return {!Element}
*/
_renderText(text) {
const element = this._dom.createElement('div', 'lh-text');
element.textContent = text.text;
_renderURL(text) {
const element = this._renderText(text);
element.classList.add('lh-text__url');
return element;
}

/**
* @param {!DetailsRenderer.DetailsJSON} text
* @return {!Element}
*/
_renderURL(text) {
const element = this._renderText(text);
element.classList.add('lh-text__url');
_renderText(text) {
const element = this._dom.createElement('div', 'lh-text');
element.textContent = text.text;
return element;
}

Expand Down Expand Up @@ -167,6 +169,16 @@ class DetailsRenderer {
element.appendChild(cardsParent);
return element;
}

/**
* @param {!DetailsRenderer.DetailsJSON} details
* @return {!Element}
*/
_renderCode(details) {
const pre = this._dom.createElement('pre', 'lh-code');
pre.textContent = details.text;
return pre;
}
}

if (typeof module !== 'undefined' && module.exports) {
Expand Down
30 changes: 29 additions & 1 deletion lighthouse-core/report/v2/report-styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lighthouse-core/test/report/v2/renderer/details-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,19 @@ describe('DetailsRenderer', () => {
assert.equal(cards[1].getAttribute('title'), 'snippet', 'adds title attribute for snippet');
assert.ok(!cards[1].querySelector('.lh-scorecard__target'), 'handles missing target');
});

it('renders code', () => {
const el = renderer.render({
type: 'code',
text: 'code snippet',
lineNumber: 123,
source: 'deprecation',
url: 'https://example.com/feature'
});

assert.ok(el.localName === 'pre');
assert.ok(el.classList.contains('lh-code'));
assert.equal(el.textContent, 'code snippet');
});
});
});

0 comments on commit 9d71fcf

Please sign in to comment.