-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
report: remove Util.UIStrings mutation, add I18n renderer class #10153
Conversation
@@ -249,9 +243,6 @@ class ReportRenderer { | |||
} | |||
} | |||
|
|||
/** @type {LH.I18NRendererStrings} */ | |||
ReportRenderer._UIStringsStash = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm
*/ | ||
constructor(dom) { | ||
/** @type {DOM} */ | ||
constructor(dom, report) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
easiest way to get the report strings everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the only thing I don't like about this is that different parts of the code access the renderer formatted strings differently now with some direct JSON, some indirectly through the detailsRenderer
(which feels a little muddying the purpose of detailsRenderer just for the strings :/)
WDYT about a separate object passed around called i18n
or something that they can all reference consistently and perhaps use the formatting from? a pain yes, but the current tradeoff of mutability for inconsistency doesn't really seem like a clear victory
would that include getEmulationDescriptions? |
alright, with this second pass I moved all the stuff that is mutable (strings, locale, formatters) to an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a far more incremental improvement, but I love that everything i18n is collected in a disposable class and split out from util now 👍
|
||
/* globals self, URL */ | ||
|
||
// Not named `NBSP` because that creates a duplicate identifier (util.js). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬, on of these days the grimaces will add up to a build step for renderer :)
@@ -97,28 +96,30 @@ class DOMSize extends Audit { | |||
{key: 'value', itemType: 'numeric', text: str_(UIStrings.columnValue)}, | |||
]; | |||
|
|||
const i18n = new I18n(context.settings.locale); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a bummer these won't get into the localeLog, maybe we file a separate issue to track that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no idea what this means :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry I meant icuMessagePaths
when we create i18n'd strings using str
we get to track it in the lhr so we can use swap-locale
later, it'd be great if these usages could take advantage of that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah right. an additional challenge - besides figuring out how to sink these paths to a central place - the swap locale script only works for UIStrings, not for value formatting.
Instead, I think we shouldn't format in the audit at all. These values should be raw numbers - and all the numeric
detail types should be formatted by the report renderer (odd that this isn't the case now ...). That way there's nothing to swap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh sorry! yes!
I think we shouldn't format in the audit at all. These values should be raw numbers - and all the numeric detail types should be formatted by the report renderer (odd that this isn't the case now ...). That way there's nothing to swap.
Agreed I think there was even a todo I put in one of my PRs during review on this topic that I've totally forgotten to merge...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool.. so I'll leave the changes in the audits as they are now. It's still an improvement. We can follow up on formatting in just the renderer later.
I'll fix those tests now..
@@ -0,0 +1,142 @@ | |||
/** | |||
* @license | |||
* Copyright 2020 Google Inc. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're avoiding the condensed version though?? 😢 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol didnt notice, i just here to copy and paste stuff man
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the removal of that mutation, it was always confusing. I don't have any huge feedback 👍
This is a good incremental improvement, but is it part of a larger change? I feel like it is and I'm missing the context from the PR desc.
/** | ||
* Format number. | ||
* @param {number} number | ||
* @param {number=} granularity Number of decimal places to include. Defaults to 0.1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove granularity in most of these functions.
We never send a granularity, and sending it as a decimal, instead of the number of digits you want to be significant seems like a bad API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't want to change functionality in this pr. this change would at least require changing many tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the unit-tests use granularity except 1. I'm fine if it's a follow up/not done, just seems like old YAGNI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, i assumed.
detailsRenderer
uses this a bit.
@@ -44,19 +44,13 @@ class ReportRenderer { | |||
* @return {Element} | |||
*/ | |||
renderReport(result, container) { | |||
// Mutate the UIStrings if necessary (while saving originals) | |||
const originalUIStrings = JSON.parse(JSON.stringify(Util.UIStrings)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏 🎉
yea https://github.com/GoogleChrome/lighthouse/pull/9166/files#r361547882 |
Co-Authored-By: Shane Exterkamp <[email protected]>
…ghthouse into refactor-util-ui-strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! (with license and minor future concern) :)
textEl.classList.add('tooltip-boundary'); | ||
const tooltip = this.dom.createChildOf(textEl, 'div', 'tooltip tooltip--error'); | ||
tooltip.textContent = audit.result.errorMessage || Util.UIStrings.errorMissingAuditInfo; | ||
tooltip.textContent = | ||
audit.result.errorMessage || strings.errorMissingAuditInfo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't this line get shorter? must be a leftover newline from earlier version :)
@@ -0,0 +1,142 @@ | |||
/** | |||
* @license | |||
* Copyright 2020 Google Inc. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're avoiding the condensed version though?? 😢 😢
...Util.UIStrings, | ||
...report.i18n.rendererFormattedStrings, | ||
}); | ||
Util.i18n = i18n; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little concerned that somewhere, someday we're going to do the const strings = Util.i18n.strings
shortcut in a place that doesn't happen on every render and we're going to have a subtle bug we'll probably never catch, but I don't have great suggestions around this, .strings
returns an object that proxies properties to the latest Util.i18n._strings prop?
any thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would require stashing that value in what is effectively a global (if it isn't being reset every render i assume it must be a global). I think good PRs / healthy dosages of "global is evil" will prevent that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it isn't being reset every render i assume it must be a global
This assumption during reviewing is another reason I worry about this :)
Cleanup for https://github.com/GoogleChrome/lighthouse/pull/9166/files#r361547882