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

report: remove Util.UIStrings mutation, add I18n renderer class #10153

Merged
merged 13 commits into from
Jan 7, 2020

Conversation

connorjclark
Copy link
Collaborator

@connorjclark connorjclark commented Dec 27, 2019

@@ -249,9 +243,6 @@ class ReportRenderer {
}
}

/** @type {LH.I18NRendererStrings} */
ReportRenderer._UIStringsStash = {};
Copy link
Collaborator Author

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) {
Copy link
Collaborator Author

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

Copy link
Collaborator

@patrickhulce patrickhulce left a 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

@connorjclark
Copy link
Collaborator Author

perhaps use the formatting from

would that include getEmulationDescriptions?

@connorjclark
Copy link
Collaborator Author

connorjclark commented Dec 30, 2019

alright, with this second pass I moved all the stuff that is mutable (strings, locale, formatters) to an I18n class, and made it globally accessible via Util.i18n. This gets rid of the nasty mutations of Util.UIStrings without passing an extra object everywhere. Also, the audits that used some formatting now actually use the correct locale (before, it was just doing en).

Copy link
Collaborator

@patrickhulce patrickhulce left a 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 👍

lighthouse-core/lib/i18n/i18n.js Outdated Show resolved Hide resolved

/* globals self, URL */

// Not named `NBSP` because that creates a duplicate identifier (util.js).
Copy link
Collaborator

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 :)

lighthouse-core/report/html/renderer/i18n.js Outdated Show resolved Hide resolved
lighthouse-core/report/html/renderer/report-renderer.js Outdated Show resolved Hide resolved
lighthouse-core/report/html/renderer/report-ui-features.js Outdated Show resolved Hide resolved
lighthouse-core/report/html/renderer/i18n.js Outdated Show resolved Hide resolved
@@ -97,28 +96,30 @@ class DOMSize extends Audit {
{key: 'value', itemType: 'numeric', text: str_(UIStrings.columnValue)},
];

const i18n = new I18n(context.settings.locale);
Copy link
Collaborator

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?

Copy link
Collaborator Author

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 :)

Copy link
Collaborator

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

Copy link
Collaborator Author

@connorjclark connorjclark Dec 31, 2019

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

does that make sense?

Copy link
Collaborator

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...

Copy link
Collaborator Author

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..

@connorjclark connorjclark changed the title report: remove mutable Util.UIStrings report: remove Util.UIStrings mutation, add I18n class Dec 31, 2019
@connorjclark connorjclark changed the title report: remove Util.UIStrings mutation, add I18n class report: remove Util.UIStrings mutation, add I18n renderer class Dec 31, 2019
@@ -0,0 +1,142 @@
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🎉

Copy link
Collaborator

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?? 😢 😢

Copy link
Collaborator Author

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

Copy link
Member

@exterkamp exterkamp left a 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.
Copy link
Member

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.

Copy link
Collaborator Author

@connorjclark connorjclark Jan 7, 2020

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.

Copy link
Member

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.

Copy link
Collaborator Author

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.

lighthouse-core/report/html/renderer/i18n.js Outdated Show resolved Hide resolved
@@ -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));
Copy link
Member

Choose a reason for hiding this comment

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

🙏 🎉

@connorjclark
Copy link
Collaborator Author

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.

yea https://github.com/GoogleChrome/lighthouse/pull/9166/files#r361547882

Copy link
Collaborator

@patrickhulce patrickhulce left a 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;
Copy link
Collaborator

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.
Copy link
Collaborator

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;
Copy link
Collaborator

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?

Copy link
Collaborator Author

@connorjclark connorjclark Jan 7, 2020

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.

Copy link
Collaborator

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 :)

@connorjclark connorjclark merged commit 81c6e92 into master Jan 7, 2020
@connorjclark connorjclark deleted the refactor-util-ui-strings branch January 7, 2020 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants