Closed guybedford closed 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.
The spec is clear about this. export default function() {}
is a complete ExportDeclaration
.
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.
The answer is not. It is a syntax error.
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).
@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] ;
.
@UltCombo thanks so much for the confirmation, I've posted a Babel issue at https://phabricator.babeljs.io/T7136.
The following is not supported in Traceur but is supported in Babel as a self-invoking 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?