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

core(hreflang): remove eval, import axe valid-langs.js directly #13385

Merged
merged 9 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ docs/
lighthouse-core/lib/sd-validation/
lighthouse-core/scripts/*
lighthouse-core/test/
lighthouse-core/third_party/src/
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

drive by: this is not a folder

viewer/
treemap/
clients/
Expand Down
5 changes: 3 additions & 2 deletions docs/recipes/integration-test/example-lh-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ expect.extend({
const status = audit.score === 1 ?
this.utils.EXPECTED_COLOR('○') :
this.utils.RECEIVED_COLOR('✕');
const weight = this.utils.DIM_COLOR(`[weight: ${auditRef.weight}]`);
return `\t${status} ${weight} ${audit.id}`;
const attrs = this.utils.DIM_COLOR(`[weight: ${auditRef.weight}, score: ${audit.score}]`);
const error = audit.errorMessage ? ` ${audit.errorMessage}` : '';
return `\t${status} ${attrs} ${audit.id}${error}`;
}).join('\n');

if (score >= threshold) {
Expand Down
19 changes: 2 additions & 17 deletions lighthouse-core/audits/seo/hreflang.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

const Audit = require('../audit.js');
const i18n = require('../../lib/i18n/i18n.js');
const axeLibSource = require('../../lib/axe.js').source;
const {isValidLang} = require('../../../third-party/axe/valid-langs.js');

const VALID_LANGS = importValidLangs();
const NO_LANGUAGE = 'x-default';

const UIStrings = {
Expand All @@ -33,20 +32,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}
Expand All @@ -66,7 +51,7 @@ function isExpectedLanguageCode(hreflang) {

// 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 {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ yarn type-check

### Docs

Some of our docs have tests that run only in CI by default. If you end up needing to modify our documentation, you'll need to run `yarn test-docs` locally to make sure they pass.
Some of our docs have tests that run only in CI by default. If you end up needing to modify our documentation, you'll need to run `yarn build-pack && yarn test-docs` locally to make sure they pass.

**Additional Dependencies**
- `brew install jq`
Expand Down
Loading