Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(…)
like image 994
Tamas Hegedus Avatar asked Dec 05 '15 22:12

Tamas Hegedus


1 Answers

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.

like image 131
Benjamin Gruenbaum Avatar answered Oct 22 '22 13:10

Benjamin Gruenbaum