escodegen icon indicating copy to clipboard operation
escodegen copied to clipboard

ExportDefaultDeclaration

Open xjamundx opened this issue 9 years ago • 5 comments

It seems this project uses ExportDeclaration for both export and export default. As far as I can tell most projects (including espree, estree, and estraverse) are now using ExportDefaultDeclaration for these nodes, but escodegen is complaining with the following when I try to use it:

TypeError: Object #<CodeGenerator> has no method 'ExportDefaultDeclaration'
    at CodeGenerator.generateExpression (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:2408:28)
    at CodeGenerator.Statement.ExpressionStatement (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:1313:28)
    at CodeGenerator.generateStatement (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:2421:33)
    at CodeGenerator.Statement.Program (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:1695:43)
    at CodeGenerator.generateStatement (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:2421:33)
    at generateInternal (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:2442:28)
    at Object.generate (/Users/jamuferguson/dev/5to6/node_modules/escodegen/escodegen.js:2510:18)

This hacky code will actually unblock the common use case... https://github.com/xjamundx/escodegen/blob/export-default/escodegen.js#L1268

ExportDefaultDeclaration: function (stmt, flags) {
    stmt.default = true;
    return this.ExportDeclaration(stmt, flags);
},

See also: https://github.com/estree/estree/blob/master/es6.md#exportdefaultdeclaration

xjamundx avatar Apr 14 '15 16:04 xjamundx

I have a few other nitpicky things. I'll file separate issues if you like but for now I'll just add them here, so as not to spam your repo :)

xjamundx avatar Apr 14 '15 17:04 xjamundx

As per Espree spec https://github.com/estree/estree/blob/master/es6.md#modulespecifier the ImportSpecifier should expect local instead of an id. This can be easily fixed by adding || expr.local below:

ImportDefaultSpecifier: function (expr, precedence, flags) {
    return generateIdentifier(expr.id || expr.local);
}

Here is my copy with it fixed: https://github.com/xjamundx/escodegen/blob/export-default/escodegen.js#L2236

xjamundx avatar Apr 14 '15 17:04 xjamundx

Hey @xjamundx your implementations were great. Can you add them as PRs?

pgilad avatar Jun 18 '15 11:06 pgilad

@xjamundx oh i realy love your idea,and it fixed my bugs!

JuneChiu avatar Aug 20 '15 12:08 JuneChiu

Yay I think this was finally fixed!

xjamundx avatar Sep 02 '15 17:09 xjamundx