Module.exports syntax for classes #175

Closed
watzon opened this Issue Sep 3, 2016 · 2 comments

Projects

None yet

2 participants

@watzon
watzon commented Sep 3, 2016

Classes don't get generated if the javascript format is as follows

var ClassName = module.exports = function() {...}

This is basically the same as

export default class ClassName {}
@nene
Collaborator
nene commented Sep 3, 2016

That's a tricky syntax to catch. Lebab implements two separate transforms which don't really know about each other:

  • class transform will not recognize this as a class because of the module.exports in the middle of an assignment.
  • commonjs transform will not recognize this as export because it only looks at assignments to module.exports at top-level.

I suppose one cold improve commonjs transform, so it would output the following:

export default var ClassName = function() {...}

Then the class transform should recognize this function as a class.

Additional question is whether code like this is a widespread practice or something just you personally use. Some references to projects using this would be helpful.

@nene nene added the enhancement label Sep 3, 2016
@nene
Collaborator
nene commented Jan 21, 2017

The solution proposed above isn't really good, as one would need to know that commonjs transform needs to be run before class transform. But given the state of support for the import/export syntax, it's likely that one wants to apply the commonjs transform as the very last.

This also looks like a very specific case. We could come back to this, when more requests for supporting such a pattern appear.

Closing for now.

@nene nene closed this Jan 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment