estools / escodegen

ECMAScript code generator
BSD 2-Clause "Simplified" License
2.64k stars 334 forks source link

ExportDefaultDeclaration #228

Open xjamundx opened 9 years ago

xjamundx commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

pgilad commented 9 years ago

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

JuneChiu commented 8 years ago

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

xjamundx commented 8 years ago

Yay I think this was finally fixed!