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: strict typing for audit requested artifacts #9946

Closed
wants to merge 12 commits into from

Conversation

connorjclark
Copy link
Collaborator

@connorjclark connorjclark commented Nov 8, 2019

We pass only the artifacts that an audit declares via .meta.requiredArtifacts. However, the audit function takes the entire artifact type (LH.Artifacts) as a parameter, and assumes you declared things properly. This is a little scary when you consider boolean artifacts, since failure to declare one results in the audit not crashing at runtime, but always acting like it was false.

We could type this fairly easily. It's not as nice as it could be, since there's no direct way to use an array of property names in Pick<T, K> without declaring the array as a const, which is not yet supported in JSDoc. So there's duplication in the declaration - one for the type comment and one for the value itself.

EDIT: found a good way to remove duplication without loss of typing. I keep both implementations in the draft for comparison.

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.

I like the function approach, seems like we could put this onto Audit as .artfiacts(), still have it accept an array and do one massive replace

requiredArtifacts: Audit.artifacts([ ]),

@connorjclark
Copy link
Collaborator Author

@patrickhulce would need to do const requiredArtifacts = ... so it can be used in the jsdoc for .audit

@connorjclark
Copy link
Collaborator Author

@patrickhulce this shows how to pull const requiredArtifacts = ... into meta. But the lack of inferrable type parameters means we are stuck with an additional ugly typeof requiredArtifacts[number]

playground

@connorjclark
Copy link
Collaborator Author

I'm happy with this:

  1. static get requiredArtifacts. Deprecate defining from meta, but still support indefinitely.
  2. export type Select<T extends typeof _Audit> = Pick<LH.Artifacts, T['requiredArtifacts'][number]>;
  3. usage in audit: * @param {LH.Artifacts.Select<typeof FirstCPUIdle>} artifacts

see first-cpu-idle.js

};
}

static get requiredArtifacts() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could define this in the Axe audit base class, instead of in each axe audit. sg?

/**
* @param {LH.Artifacts} artifacts
* @param {LH.Artifacts.Select<typeof module.exports>} artifacts
Copy link
Collaborator Author

@connorjclark connorjclark Nov 14, 2019

Choose a reason for hiding this comment

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

this works! And made the global find & replace feasible.

@connorjclark
Copy link
Collaborator Author

@patrickhulce would you be the reviewer for this? It's mostly 140 mechanical changes :)

@patrickhulce
Copy link
Collaborator

oof, mostly except for the part where we decide to alter our requiredArtifacts properties to getters forever ;)

I'm happy to review this change in its entirety if we get cursory buy-in to this new approach from the entire team first.

I'm fine with it, personally, but also somewhat skeptical of its usefulness compared to the noise. Is it nice to have it typed? Ya, but generally getting this wrong results in colossal obvious failure when missed, so 🤷‍♂

@patrickhulce
Copy link
Collaborator

summoning @GoogleChrome/lighthouse-hackers , OK with

get meta() { return {requiredArtifacts: ['MyArtifact']}; } ->
get requiredArtifacts() { return this.artifacts('MyArtifact'); }

?

@connorjclark connorjclark changed the title core: strict typing for audit required artifacts core: strict typing for audit requested artifacts Feb 23, 2020
@connorjclark
Copy link
Collaborator Author

Let's discuss in Monday eng meeting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants