sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.58k stars 208 forks source link

Cannot create syntax literal for class whose superclass is a member expression #686

Closed suchipi closed 7 years ago

suchipi commented 7 years ago

If you attempt to create a class declaration (or expression) inside a syntax template literal that extends a superclass which is accessed via a member expression, an error is thrown:

syntax cannot_use_member_expression_as_superclass = function(ctx) {
  return #`class Foo extends Bar.Baz {}`;
};
cannot_use_member_expression_as_superclass;
// Error: [assertion error]: Not a primary expression
//   at assert (node_modules/sweet.js/dist/errors.js:26:11)
//   at Enforester.enforestPrimaryExpression (node_modules/sweet.js/dist/enforester.js:1259:24)
//   at Enforester.enforestLeftHandSideExpression (node_modules/sweet.js/dist/enforester.js:1298:26)
//   at Enforester.enforestAssignmentExpression (node_modules/sweet.js/dist/enforester.js:1171:19)
//   at Enforester.enforestExpressionLoop (node_modules/sweet.js/dist/enforester.js:1072:23)
//   at Enforester.enforestClass (node_modules/sweet.js/dist/enforester.js:768:19)
//   at Enforester.enforestStatement (node_modules/sweet.js/dist/enforester.js:405:19)
//   at Enforester.enforestModuleItem (node_modules/sweet.js/dist/enforester.js:142:17)
//   at Enforester.enforestBody (node_modules/sweet.js/dist/enforester.js:129:17)
//   at Enforester.enforestModule (node_modules/sweet.js/dist/enforester.js:125:17)

Sweet.js version: 3.0.0

disnet commented 7 years ago

Not related to syntax literal, just a plain extends member expression is sufficient to exercise the bug:

class Foo extends Bar.Baz {} // throws error
gabejohnson commented 7 years ago

~I think enforestLeftHandSideExpression can just be called directly. That method didn't exist when the enforestClass was written.~

I was wrong. It shouldn't be called directly.

suchipi commented 7 years ago

Works great now, thanks!