-
-
Notifications
You must be signed in to change notification settings - Fork 258
Throw error when exporting non-declaration #241
Conversation
return this.parseStatement(true); | ||
const statement = this.parseStatement(true); | ||
if (!this.isDeclaration(statement)) { | ||
this.unexpected(statement.start); |
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.
Since we know what the problem is here (an export
that doesn't have a valid declaration or export clause) maybe we can use this.raise
and hint to the user what the problem was?
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.
After discussing a bit with @hzoo, I changed it to an ahead of time check rather than checking the export statement post-parse. This actually changes the error message in some cases, so wanted to see what do you think.
If we want a more descriptive error message I could leave the original conditional and do the shouldParseExportDeclaration
check (maybe now called isDeclaration
?) inside that block, throwing a more descriptive error if isDeclaration
returns false
.
My only concern with the error message is that I'm not sure we can be sure of what the intention of the code is. If Babylon is trying to parse export typeof foo
, can we surmise they intended to export a declaration? Or did you mean a more generic "Not a valid export" type of message? Let me know if I'm missing something!
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.
Or did you mean a more generic "Not a valid export" type of message
Yep! Just some message indicating that we know it's an export, it's just not a valid one.
Hmmm, actually, going to explore checking token values ahead of time rather than the node type afterwards |
Updated |
Sorry, one last update - added return value flow type and moved the |
Nice thanks |
Currently, Babylon will parse any export that starts with a keyword as a declaration. These changes will throw when the statement following the
export
keyword is not a declaration.