google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 580 forks source link

export default self-invoking function #2083

Closed guybedford closed 8 years ago

guybedford commented 8 years ago

The following is not supported in Traceur but is supported in Babel as a self-invoking function:

export default function() {
}();

This is causing issues because I'm piping the output of Babel to Traceur, where Babel will remove any enclosing brackets from export default (function() {})() to look like the above causing the error to be throw in Traceur.

Which is right by the spec?

UltCombo commented 8 years ago

IIRC export default function() {} exports a function declaration that does not introduce a referenceable binding in the containing module scope, while export default (function() {}) exports a function expression.

arv commented 8 years ago

The spec is clear about this. export default function() {} is a complete ExportDeclaration.

guybedford commented 8 years ago

The question here was specifically whether 'export default function() {}()' with trailing brackets is a self-invoking function or not? On Fri, 19 Feb 2016 at 23:43, Erik Arvidsson notifications@github.com wrote:

Closed #2083 https://github.com/google/traceur-compiler/issues/2083.

— Reply to this email directly or view it on GitHub https://github.com/google/traceur-compiler/issues/2083#event-557150442.

arv commented 8 years ago

The answer is not. It is a syntax error.

UltCombo commented 8 years ago

Just adding some references to my previous comment.

As per NOTE in ES2016 draft § 14.1.20 Runtime Semantics: InstantiateFunctionObject:

An anonymous FunctionDeclaration can only occur as part of an export default declaration.

As per § 15.2.1.16 Source Text Module Records—Table 43 (Informative): Export Forms Mappings to ExportEntry Records, an exported anonymous FunctionDeclaration gets a local name ("binding") of *default*, which is not a valid identifier and thus unaccessible from user code.

(I know notes and informative sections are non-normative, but they explain it much more clearly than the normative sections).

UltCombo commented 8 years ago

@guybedford Babel removing the grouping operator on export default (function() {})() looks like a bug to me, that breaks the negative lookahead assertion in § 15.2.3 Exports—Syntax—ExportDeclaration: export default [lookahead ∉ { function, class }] AssignmentExpression[In] ;.

guybedford commented 8 years ago

@UltCombo thanks so much for the confirmation, I've posted a Babel issue at https://phabricator.babeljs.io/T7136.