ES6 spec: `export default` HoistableDeclaration
On Nov 19, 2014 6:54 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
Quoting people.mozilla.org/~jorendorff/es6-draft.html#sec-exports
export default
HoistableDeclaration
Note that the actual spec. text has a [Default] grammar parameter after HoistableDeclaration. That,'s important.
- Do these grammar rules mean that you have to put anonymous function expressions in parentheses? Is that desirable (given that it’s a frequent use case)?
No, HoistableDeclaration[Default] includes FunctionDeclaration that lacks a BindingIdentifier.
- If the purpose of the first rule is to enable default-exporting of declarations (= no semicolon) – shouldn’t classes be included?
It has nothing to do with semicolons. It's so a function that is a default export can also have a local name binding.
We should also allow:
export default class Foo {};
where Foo is a module local binding.
Please file a bug.
No, HoistableDeclaration[Default] includes FunctionDeclaration that lacks a BindingIdentifier.
OK, I take it the following wasn’t viable?
export default
[lookahead ≠function (
] HoistableDeclaration
export default
AssignmentExpression;
It has nothing to do with semicolons. It's so a function that is a default export can also have a local name binding.
Ah, interesting and useful.
We should also allow:
export default class Foo {};
where Foo is a module local binding.
Without the semicolon, though? The “operand” of export default
is also a declaration, right?
Please file a bug.
Will do, once I fully understand the issue.
On Nov 19, 2014, at 1:59 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
OK, I take it the following wasn’t viable?
that’s correct. We wanted the initialization of a function like:
export default function () {}
to be hoisted, just like:
export function f() {};
Without the semicolon, though? The “operand” of
export default
is also a declaration, right?
right, ClassDeclaration doesn’t need to end of a semicolon.
I suspect that that will confuse people: they will expect an anonymous function to be an expression, not a declaration (even more so because the operand of export default
is usually an expression). Does hoisting even matter if the function doesn’t have a name? Is it worth it to introduce a completely new construct (an anonymous function declaration) just for export default
?
On Nov 19, 2014, at 4:42 PM, Axel Rauschmayer <axel at rauschma.de> wrote:
I suspect that that will confuse people: they will expect an anonymous function to be an expression, not a declaration (even more so because the operand of
export default
is usually an expression). Does hoisting even matter if the function doesn’t have a name? Is it worth it to introduce a completely new construct (an anonymous function declaration) just forexport default
?
It can make a difference to circular modules that reference each other’s default export. Hosted function iinitialization occurs before anything in the module body is evaluated. export default expression isn’t initialized at the point of the export declaration in the module body.
Interesting that that matters. I wouldn’t have thought so – given that modules export references, not values.
Quoting people.mozilla.org/~jorendorff/es6-draft.html#sec-exports
Questions: