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

fix(engine-core): remove dev-only errors for decorators #3409

Merged
merged 4 commits into from
Apr 13, 2023

Conversation

nolanlawson
Copy link
Collaborator

Details

Partially addresses #3245

Anytime we throw an error in dev mode but not in prod mode, that's a bug. This PR gets us one step closer to fixing that, by making decorator validation errors log rather than throw.

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ✅ No, it does not introduce an observable change.

assert.invariant(
isFunction(get),
`Invalid compiler output for public accessor ${toString(key)} decorated with @api`
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you look at the logic above, this is doing the same thing. It was throwing in both dev mode and prod mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please update the error message to say "the property is missing a getter."? It will improve the DX.

Currently the message is abstract because it does not exactly say what is wrong, it also seems to indicate that the compiler output is incorrect. If the user does not look at the dev console, they might miss the cause.

@@ -81,17 +80,13 @@ function validateObservedField(
fieldName: string,
descriptor: PropertyDescriptor | undefined
) {
assertNotProd(); // this method should never leak to prod
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added assertNotProds for my own sanity

// This line of code does not seem possible to reach. Maybe when native decorators are supported?
logError(
`Invalid @wire ${methodName} field. The field should have a valid writable descriptor.`
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A bunch of these validations seem impossible to hit. (I tried.) So I just noted it in the comment.

The main reason they can't be hit is that decorators append the registerDecorators() call right after the class declaration, so there's no chance to do any funny business with Object.defineProperty(), because that would occur after the registerDecorators().

I guess someone could do funny business after registerDecorators, but if someone's doing that, then they're already broken today.

logError(
`@wire on method "${fieldOrMethodName}": adapter id must be truthy.`
);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Despite the name, assert.isTrue() actually asserts truthiness, not === true. So this logic is the same as before.

@nolanlawson
Copy link
Collaborator Author

/nucleus ignore --reason 'lds-lightning-platform is not passing right now'

@nolanlawson
Copy link
Collaborator Author

/nucleus test

Copy link
Contributor

@ravijayaramappa ravijayaramappa left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -155,15 +152,20 @@ function validateAccessorDecoratedWithApi(
fieldName: string,
descriptor: PropertyDescriptor | undefined
) {
assertNotProd(); // this method should never leak to prod
if (isUndefined(descriptor)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

registerDecorators() which is the only caller of this function already checks if descriptor is undefined and it throws. However it does it after invoking this function. You can switch the order there and eliminate this undefined check.

assert.invariant(
isFunction(get),
`Invalid compiler output for public accessor ${toString(key)} decorated with @api`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please update the error message to say "the property is missing a getter."? It will improve the DX.

Currently the message is abstract because it does not exactly say what is wrong, it also seems to indicate that the compiler output is incorrect. If the user does not look at the dev console, they might miss the cause.

@nolanlawson
Copy link
Collaborator Author

/nucleus test

@nolanlawson
Copy link
Collaborator Author

/nucleus test

@nolanlawson
Copy link
Collaborator Author

/nucleus ignore --reason 'lwc-platform needs updating'

@nolanlawson nolanlawson merged commit 4a66fe4 into master Apr 13, 2023
@nolanlawson nolanlawson deleted the nolan/dev-errors-1 branch April 13, 2023 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/dev-only-errors Removing all dev-only errors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants