3

I am getting various unexpected empty lines when I transpile a JavaScript snippet with Babel. Here is the source code:

/**
 * Header.
 */
function header() {
  const header = 'header';
  console.log(header);
};

/**
 * Navigation.
 */
function navigation() {
  const navigation = 'navigation';
  console.log(navigation);
};

And this is what I get after I transpile it:

"use-strict";

/**
 * Header.
 */
function header() {
    var header = 'header';
    console.log(header);
}

;
/**
 * Navigation.
 */

function navigation() {
    var navigation = 'navigation';
    console.log(navigation);
}

;

Notice empty lines before semicolons and after the second function comment.

Dependencies:

"devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3"
}

.babelrc

{
    "presets": ["@babel/preset-env"]
}

So the question is: How do I get a clean output without those empty lines?

Interestingly, if you put the same snippet to https://babeljs.io/repl it doesn't output empty lines.

Update

As suggested by Felix Kling, after removing the semicolons I do get this:

"use strict";

/**
 * Header.
 */
function header() {
  var header = 'header';
  console.log(header);
}
/**
 * Navigation.
 */


function navigation() {
  var navigation = 'navigation';
  console.log(navigation);
}
Dmitry Mayorov
  • 216
  • 2
  • 9
  • That doesn't happen on https://babeljs.io/repl. But note that the `;` in your source code are unnecessary: [You have a function declaration followed by an empty statement.](https://astexplorer.net/#/gist/08225a3a285d6ce33ca2d18091f7b918/fd72d39b3f146fc5c4503a42e4d0bbd24ff4a02a) That could be the reason for the strange formatting. However: You shouldn't generally care about the format that Babel outputs. It's a build artifact. – Felix Kling Jan 11 '19 at 02:01
  • 1
    Thanks for the comment. Just tried without semicolons. Better but still there are two new lines after the second comment and no new line after the first function. Obviously, this is not a huge deal, but still would be great to understand how to get the same clean output as on babeljs.io/repl. – Dmitry Mayorov Jan 11 '19 at 02:12

0 Answers0