-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
core(hreflang): remove eval, import axe valid-langs.js directly #13385
Changes from 1 commit
bc735b6
1f808b7
f94ccbd
4bdd212
20c0a06
33fc5cb
0d6d0c6
3fb89f0
6fea81d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,9 +11,7 @@ | |||||||||||||
|
||||||||||||||
const Audit = require('../audit.js'); | ||||||||||||||
const i18n = require('../../lib/i18n/i18n.js'); | ||||||||||||||
const axeLibSource = require('../../lib/axe.js').source; | ||||||||||||||
|
||||||||||||||
const VALID_LANGS = importValidLangs(); | ||||||||||||||
const NO_LANGUAGE = 'x-default'; | ||||||||||||||
|
||||||||||||||
const UIStrings = { | ||||||||||||||
|
@@ -33,20 +31,6 @@ const UIStrings = { | |||||||||||||
|
||||||||||||||
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Import list of valid languages from axe core. | ||||||||||||||
* This is a huge array of language codes that can be stored more efficiently if we will need to | ||||||||||||||
* shrink the bundle size. | ||||||||||||||
* @return {Array<string>} | ||||||||||||||
*/ | ||||||||||||||
function importValidLangs() { | ||||||||||||||
// Define a window-ish object so axe will export to it. | ||||||||||||||
const window = {getComputedStyle: () => {}}; | ||||||||||||||
eval(axeLibSource); | ||||||||||||||
// @ts-expect-error | ||||||||||||||
return window.axe.utils.validLangs(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {string} href | ||||||||||||||
* @return {boolean} | ||||||||||||||
|
@@ -57,16 +41,17 @@ function isFullyQualified(href) { | |||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {string} hreflang | ||||||||||||||
* @param {(lang: string) => boolean} isValidLang From axe. | ||||||||||||||
* @return {boolean} | ||||||||||||||
*/ | ||||||||||||||
function isExpectedLanguageCode(hreflang) { | ||||||||||||||
function isExpectedLanguageCode(hreflang, isValidLang) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to make this a callback? Could import where needed:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to limit the async from going where it made less sense. |
||||||||||||||
if (hreflang.toLowerCase() === NO_LANGUAGE) { | ||||||||||||||
return true; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// hreflang can consist of language-script-region, we are validating only language | ||||||||||||||
const [lang] = hreflang.split('-'); | ||||||||||||||
return VALID_LANGS.includes(lang.toLowerCase()); | ||||||||||||||
return isValidLang(lang.toLowerCase()); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
class Hreflang extends Audit { | ||||||||||||||
|
@@ -86,9 +71,11 @@ class Hreflang extends Audit { | |||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {LH.Artifacts} artifacts | ||||||||||||||
* @return {LH.Audit.Product} | ||||||||||||||
* @return {Promise<LH.Audit.Product>} | ||||||||||||||
*/ | ||||||||||||||
static audit({LinkElements}) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to be async anymore |
||||||||||||||
static async audit({LinkElements}) { | ||||||||||||||
const isValidLang = (await import('../../../third-party/axe/valid-langs.js')).default; | ||||||||||||||
|
||||||||||||||
/** @type {InvalidHreflang[]} */ | ||||||||||||||
const invalidHreflangs = []; | ||||||||||||||
|
||||||||||||||
|
@@ -105,7 +92,7 @@ class Hreflang extends Audit { | |||||||||||||
/** @type {Source} */ | ||||||||||||||
let source; | ||||||||||||||
|
||||||||||||||
if (!isExpectedLanguageCode(link.hreflang)) { | ||||||||||||||
if (!isExpectedLanguageCode(link.hreflang, isValidLang)) { | ||||||||||||||
reasons.push(str_(UIStrings.unexpectedLanguage)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
#!/usr/bin/env bash | ||||||
|
||||||
## | ||||||
# @license Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||||||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||||||
## | ||||||
|
||||||
# Necessary because axe-core does not publish as type: 'module', even though axe-core/lib/ is esmodules. | ||||||
|
||||||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||||||
LH_ROOT_DIR="$SCRIPT_DIR/../.." | ||||||
|
||||||
OUT_FILE="$LH_ROOT_DIR"/third-party/axe/valid-langs.js | ||||||
|
||||||
echo '// @ts-nocheck' > "$OUT_FILE" | ||||||
echo '// Auto-generated by lighthouse-core/scripts/copy-axe-valid-langs.sh' >> "$OUT_FILE" | ||||||
sed 's/Sting/string/' "$LH_ROOT_DIR"/node_modules/axe-core/lib/core/utils/valid-langs.js >> "$OUT_FILE" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I think they fixed it so it is |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "module", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. directory needs a license/readme (or metadata but readme is fine, honestly): go/thirdparty/non-google3 |
||
"//": "Any directory that uses `import ... from` or `export ...` must be type module. Temporary file until root package.json is type: module" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no |
||
} |
Large diffs are not rendered by default.
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.
can we skip introducing machinery to update? The list of langs hasn't changed since March 2018. The third-party directory needs a readme anyways, we could just put update instructions in there too. Ideally long term we just get the file to be importable from the node_modules/ dependency.