-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block Serialization Default Parser: Include TypeScript type declarations #43722
Conversation
16cec77
to
9ddd499
Compare
Size Change: -12 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
9ddd499
to
31033aa
Compare
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.
Nice!
const next = nextToken(); | ||
if ( next === null ) { |
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.
This refactoring was driven by TypeScript that wasn't quite happy about the token being an array with 1 item or 5 items. It turns out that when using null
instead the array with 1 string item, the behavior remains the same, and we can bail out earlier to avoid other operations that aren't necessary for this case anyway.
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.
did you look at using [TokenType, string?, Attributes?, number?, number?]
?
the refactor looks fine but I'd rather we return 'no-more-tokens'
than null
because of the self-documenting nature of the string value.
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.
Yes, that was my initial try, but TS was complaining about the number of items in the array returned: 1 instead of 5. Maybe return [ 'no-more-tokens', , , , ];
would work then?
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.
I opened #44459 to address it.
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.
Thanks for adding type definitions here @gziolo
A couple broad questions:
- Did we consider creating a
types.d.ts
file instead for the public API to avoid having to litter the JSDoc comments throughout the code? That is, maintain full internal type unity vs. document the public API? - If we are going to fully-type the code, and the code is fairly straight-forward, why not go ahead and write it with full TypeScript support and avoid the more awkward JSDoc comments?
const next = nextToken(); | ||
if ( next === null ) { |
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.
did you look at using [TokenType, string?, Attributes?, number?, number?]
?
the refactor looks fine but I'd rather we return 'no-more-tokens'
than null
because of the self-documenting nature of the string value.
That seems like an improvement to what we have now.
However, that would be my preferred approach. My initial idea was just to get it to work, and JSDoc approach seemed like the most straightforward path forward for both the reviewers (smaller diff) and me. |
What?
This PR adds types to the
@wordpres/block-serialization-default-parser
and exposes TypeScript types for the package.Why?
To increase the code quality and the documentation.
It's also part of an effort to add types to
@wordpress/blocks
package that depends on this package.How?
Adds JSDoc comment with types for
@wordpres/block-serialization-default-parser
.Testing Instructions
All CI check should pass. Locally,
npm run build:package-types
should finish with no errors.