Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull out inline enums as types in protocol.d.ts (#216)
* Emit real TypeScript enums for inline protocol enums Note: this change is taken directly from this DevTools CL (we should explore getting DevTools to depend on this module to avoid the duplication): https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2113374 This change is motivated by moving Puppeteer to TypeScript and wanting to control types more strictly rather than declaring arguments as strings even though really only a subset are supported. I've copied the commit message from the above here: Commands, Events and Object types can declare "inline enums" to restrict the possible values of a 'string' field. Example field: referrerPolicy: ('unsafe-url'|'...'|'...') To enable type-checking with TypeScript and stay compatible with existing code, we now generate explicit enums. for the enum names is adapted from code_generator_frontend.py and needs to always match. Example generated enum for the above code: export enum RequestReferrerPolicy { UnsafeUrl = 'unsafe-url', NoReferrerWhenDowngrade = 'no-referrer-when-downgrade', NoReferrer = 'no-referrer', Origin = 'origin', OriginWhenCrossOrigin = 'origin-when-cross-origin', SameOrigin = 'same-origin', StrictOrigin = 'strict-origin', StrictOriginWhenCrossOrigin = 'strict-origin-when-cross-origin', } * Generate protocol with inline enums * Export enums as const enums but leave original strings intact. This avoids a breaking change for existing consumers whilst new consumers can still pass in enum values and typecheck. E.g. both of these work: ``` const message: ConsoleMessage = { source: 'xml', level: 'log', text: 'foo' } ``` ``` const message2: ConsoleMessage = { source: ConsoleMessageSource.XML, level: ConsoleMessageLevel.Log, text: 'foo' } ```
- Loading branch information