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

feature: add more types that get logged via console methods #294

Merged
merged 1 commit into from
Jan 26, 2022

Conversation

threepointone
Copy link
Contributor

This PR adds more special logic for some data types that get logged via console methods. Types like Promise, Date, WeakMaps, and some more, now get logged correctly (or at least, better than they used to).

This PR also fixes a sinister bug - the type of the ConsoleAPICalled events don't match 1:1 with actual console methods (eg: console.warn message type is warning). This PR adds a mapping between those types and method names. Some methods don't seem to have a message type, I'm not sure why, but we'll get to them later.

@changeset-bot
Copy link

changeset-bot bot commented Jan 25, 2022

🦋 Changeset detected

Latest commit: aff14b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -539,5 +575,7 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
}
}

console[evt.type](...args);
const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
//@ts-expect-error I dunno how to make this type pass lol
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤪 halp

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to exclude the constructor from the list of keys:

export const mapConsoleAPIMessageTypeToConsoleMethod: {
  [key in Protocol.Runtime.ConsoleAPICalledEvent["type"]]: Exclude<
    keyof Console,
    "Console"
  >;

But then TS complains that args must be a tuple or be passed to a rest argument.
We can work around that by using apply() (telling eslint not to stress):

  // eslint-disable-next-line prefer-spread
  console[method].apply(console, args);

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Tests needed.... 🙏

This PR adds more special logic for some data types that get logged via `console` methods. Types like `Promise`, `Date`, `WeakMaps`, and some more, now get logged correctly (or at least, better than they used to).

This PR also fixes a sinister bug - the `type` of the `ConsoleAPICalled` events don't match 1:1 with actual console methods (eg: `console.warn` message type is `warning`). This PR adds a mapping between those types and method names. Some methods don't seem to have a message type, I'm not sure why, but we'll get to them later.
@threepointone
Copy link
Contributor Author

Working on tests right now, in a separate PR, based on the description here #188 (comment). Going to be a bit involved, don't want to block this bugfix here.

@threepointone threepointone merged commit 7746fba into main Jan 26, 2022
@threepointone threepointone deleted the feat-console-types branch January 26, 2022 08:20
@github-actions github-actions bot mentioned this pull request Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants