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

Polish a11y package. #21148

Merged
merged 1 commit into from
Mar 31, 2020
Merged

Polish a11y package. #21148

merged 1 commit into from
Mar 31, 2020

Conversation

ZebulanStanphill
Copy link
Member

Description

This PR is a collection of small code quality improvements to the a11y package to make the code easier to comprehend, more efficient, more consistent, and more compliant with the WordPress Coding Standards.

@ZebulanStanphill ZebulanStanphill added [Type] Code Quality Issues or PRs that relate to code quality [Package] A11y /packages/a11y labels Mar 25, 2020
@github-actions
Copy link

github-actions bot commented Mar 25, 2020

Size Change: +19 B (0%)

Total Size: 857 kB

Filename Size Change
build/a11y/index.js 1.02 kB +19 B (1%)
ℹ️ View Unchanged
Filename Size Change
build/annotations/index.js 3.45 kB 0 B
build/api-fetch/index.js 3.39 kB 0 B
build/autop/index.js 2.58 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.02 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 760 B 0 B
build/block-editor/index.js 102 kB 0 B
build/block-editor/style-rtl.css 11 kB 0 B
build/block-editor/style.css 11 kB 0 B
build/block-library/editor-rtl.css 7.22 kB 0 B
build/block-library/editor.css 7.23 kB 0 B
build/block-library/index.js 110 kB 0 B
build/block-library/style-rtl.css 7.49 kB 0 B
build/block-library/style.css 7.5 kB 0 B
build/block-library/theme-rtl.css 669 B 0 B
build/block-library/theme.css 671 B 0 B
build/block-serialization-default-parser/index.js 1.65 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 57.5 kB 0 B
build/components/index.js 190 kB 0 B
build/components/style-rtl.css 15.8 kB 0 B
build/components/style.css 15.7 kB 0 B
build/compose/index.js 6.21 kB 0 B
build/core-data/index.js 10.6 kB 0 B
build/data-controls/index.js 1.04 kB 0 B
build/data/index.js 8.25 kB 0 B
build/date/index.js 5.37 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.06 kB 0 B
build/edit-post/index.js 91.2 kB 0 B
build/edit-post/style-rtl.css 8.43 kB 0 B
build/edit-post/style.css 8.43 kB 0 B
build/edit-site/index.js 6.73 kB 0 B
build/edit-site/style-rtl.css 2.91 kB 0 B
build/edit-site/style.css 2.9 kB 0 B
build/edit-widgets/index.js 4.43 kB 0 B
build/edit-widgets/style-rtl.css 2.57 kB 0 B
build/edit-widgets/style.css 2.57 kB 0 B
build/editor/editor-styles-rtl.css 423 B 0 B
build/editor/editor-styles.css 426 B 0 B
build/editor/index.js 42.8 kB 0 B
build/editor/style-rtl.css 3.38 kB 0 B
build/editor/style.css 3.38 kB 0 B
build/element/index.js 4.44 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 6.95 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 1.93 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.49 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.3 kB 0 B
build/keycodes/index.js 1.7 kB 0 B
build/list-reusable-blocks/index.js 2.99 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 4.84 kB 0 B
build/notices/index.js 1.57 kB 0 B
build/nux/index.js 3.01 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.54 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 781 B 0 B
build/redux-routine/index.js 2.84 kB 0 B
build/rich-text/index.js 14.5 kB 0 B
build/server-side-render/index.js 2.55 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.01 kB 0 B
build/viewport/index.js 1.61 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@ZebulanStanphill ZebulanStanphill force-pushed the update/polish-a11y-package branch 2 times, most recently from 378506e to 1f39e28 Compare March 25, 2020 17:41
@ZebulanStanphill
Copy link
Member Author

I decided to remove the stricter type checking from this PR in order to get the rest of the improvements in sooner and deal with the tricky stuff in another PR.

Unfortunately, a Paragraph-Quote transform e2e test is failing for no apparent reason. There seems to be a lot of issues with the e2e tests lately; hopefully those get fixed soon.

Anyway, this is ready for review.

@ZebulanStanphill ZebulanStanphill force-pushed the update/polish-a11y-package branch from 1f39e28 to b5f381c Compare March 26, 2020 17:03
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Nice 👍

Regarding failing builds, should hopefully be improved somewhat as of #21175, #21174, #21180. Not sure if you will need to rebase. I restarted the build in any case.

*
* @return {HTMLDivElement} The ARIA live region HTML element.
*/
const addContainer = function( ariaLive ) {
ariaLive = ariaLive || 'polite';
Copy link
Member

Choose a reason for hiding this comment

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

Technically this would previously fall back to 'polite' for any falsy value given (null, false, 0, etc), and now it will only fall back when explicitly undefined (omitted). Generally speaking, I think this is a fine approach, but it may be something to consider for potential side-effects.

Copy link
Member Author

@ZebulanStanphill ZebulanStanphill Mar 26, 2020

Choose a reason for hiding this comment

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

Yeah, I generally lean towards enforcing that functions only be called with certain types, so in this case I'd consider calling this function with anything other than one of the 2 valid strings or undefined to be an error that should be caught by the linter/compiler.

I initially tried to use a type union of ('assertive'|'polite') as the ariaLive JSDoc type, but despite something similar appearing as an example in our docs, the auto-generated docs weren't handling it properly and was just outputting (|). I'm going to make another PR for that change later and see if anyone can figure out how to fix the issue.

Copy link
Member

Choose a reason for hiding this comment

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

I initially tried to use a type union of ('assertive'|'polite') as the ariaLive JSDoc type, but despite something similar appearing as an example in our docs, the auto-generated docs weren't handling it properly and was just outputting (|) I'm going to make another PR for that change later and see if anyone can figure out how to fix the issue.

Yeah, this is related to the issue described at #18045, and tangentially #19952 (#19952 (comment)).

Comment on lines 5 to 9
const regions = Array.from(
document.getElementsByClassName( 'a11y-speak-region' )
);
for ( const region of regions ) {
region.textContent = '';
}
Copy link
Member

Choose a reason for hiding this comment

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

Is Array.from necessary? As far as I can tell, getElementsByClassName is iterable without needing to be coerced to an array.

Apparently there are some interesting "gotchas" when using for ... in, not sure if those apply to for ... of.

I guess, generally speaking, I don't consider the previous implementation to be very problematic, at least enough that we'd do this dance of converting to an array just to use for ... of.

Copy link
Member Author

Choose a reason for hiding this comment

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

@aduth getElementsByClassName returns an HTMLCollection, which is not iterable. You may be thinking of a NodeList, which is iterable; that's the type returned from other functions like querySelectorAll. It's one of those weird inconsistencies caused by different browser APIs being created several years apart.

I went with getElementsByClassName because it's faster than querySelectorAll.

for ... of is essentially just the no-gotcha version of for ... in. The for ... of in JS is equivalent to the for ... in or foreach in most other languages. It's one of those cases where JavaScript implemented something weirdly early on (e.g. var), resulting in a non-weird alternative being added later (e.g. let).

I could switch back to querySelectorAll and then I wouldn't have to convert to an array before using for ... of. Alternatively, I could keep the switch to getElementsByClassName, but go back to the old for loop, and that also wouldn't require an array conversion. Which do you prefer? Personally, I prefer keeping the for ... of loop simply for ease of reading.

Copy link
Member

@aduth aduth Mar 26, 2020

Choose a reason for hiding this comment

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

I don't have a strong preference. It's fine.

But if part of the argument is that getElementsByClassName is more performant, I might imagine (not verified) that the cast to an Array type is possibly counteracting part of any benefit we'd get.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to switch to querySelectorAll and use the for ... of loop on that, but the TypeScript linting said that it wasn't iterable. And, because we're targeting ES5, that is correct. There is a TypeScript compiler option called downlevelIteration that allows you to use the for ... of syntax for ES5 targets by rewriting it to a standard for loops upon compilation.

However, that seems like a bit much effort for this one little thing, so I've decided to just revert to using an old-fashioned for loop while keeping the switch to getElementsByClassName.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, looking closer, it looks like we're actually targeting ES6? Argh, maybe I'll investigate this later, but I think this PR is good to go.

Copy link
Member

Choose a reason for hiding this comment

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

ES6 should be available in this module, yes. There's a few where we're running code directly, but those are typically not ones we're running the browser.

You can tell based on the main of the package's package.json. If it's pointing to a file in build, then it can be assumed to be safe to use ES6:

"main": "build/index.js",

If there's any issues from the tooling in using ES6 in these packages, that might be something we'd want to look into.

Copy link
Member Author

Choose a reason for hiding this comment

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

To be clear, what I meant by "targeting ES6" was that it appeared that this package was compiling to ES6, so using for ... of with querySelectorAll would not need to be rewritten to run in the browser.

But looking again, I can't remember what I saw that made me think the package was targeting an ES6 output. Maybe something changed recently, or maybe I just misread something earlier. Oh well. 🤷‍♂️

@ZebulanStanphill ZebulanStanphill force-pushed the update/polish-a11y-package branch from b5f381c to cc70110 Compare March 27, 2020 18:18
@ZebulanStanphill ZebulanStanphill force-pushed the update/polish-a11y-package branch from cc70110 to 3271da3 Compare March 27, 2020 19:55
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Still looking good.

@ZebulanStanphill ZebulanStanphill merged commit 00aa937 into master Mar 31, 2020
@ZebulanStanphill ZebulanStanphill deleted the update/polish-a11y-package branch March 31, 2020 21:14
@github-actions github-actions bot added this to the Gutenberg 7.9 milestone Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] A11y /packages/a11y [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants