Tamas Hegedus
Tamas Hegedus

Reputation: 29936

Semicolon after default export

I read here that I don't need to put a semicolon after default exports. So this program has an unnecessary semicolon:

export default function() {};

But if my module continues like this:

export default function() {};

(() => {
  // creating a new function scope
})();

then I can't leave the semicolon.

So what is going on here? The grammar says I don't need the semicolon but if I leave it the code means something else?

UPDATE:

If I leave the semicolon:

export default function() {}

(() => {
  // creating a new function scope
})();

then the exported function gets called instead of being exported. babeljs.io compiles the latter into:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports["default"] = (function () {})(function () {
  // creating a new function scope
})();

;
module.exports = exports["default"];

More precisely after it gets called an error is thrown, because the return value of the first function also gets called (but that is not a function). The error I get in chrome is this:

Uncaught TypeError: (intermediate value)(...) is not a function(…)

Upvotes: 10

Views: 3937

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276496

You don't need to add a semicolon after a export default when it's followed by a function declaration, that's what the grammar says.

Babel is wrong, I've filed a bug against it. That code should be interpreted as exporting the function and then running the IIFE as an IIFE.

Upvotes: 13

Related Questions