Add newline after shebang if necessary. (#215)

master
Benjamin Tan 2017-01-18 06:16:40 +08:00 committed by James Long
parent 92795a0fda
commit bc9b1fde19
4 changed files with 22 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const getStdin = require("get-stdin");

View File

@ -34,8 +34,8 @@ function format(text, opts) {
esproposal_export_star_as: true,
});
if (ast.errors.length > 0) {
let msg = ast.errors[(0)].message + " on line " +
ast.errors[(0)].loc.start.line;
let msg = ast.errors[0].message + " on line " +
ast.errors[0].loc.start.line;
if (opts.filename) {
msg += " in file " + opts.filename;
}
@ -65,7 +65,10 @@ function formatWithShebang(text, opts) {
const index = text.indexOf("\n");
const shebang = text.slice(0, index + 1);
const programText = text.slice(index + 1);
return shebang + format(programText, opts);
const nextChar = text.charAt(index + 1);
const addNewline = nextChar == "\n" || nextChar == "\r";
return shebang + (addNewline ? "\n" : "") + format(programText, opts);
}
module.exports = {

View File

@ -7,3 +7,15 @@ function a() {
}
"
`;
exports[`test shebang-newline.js 1`] = `
"#!/usr/bin/env node
function a() {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env node
function a() {
}
"
`;

View File

@ -0,0 +1,3 @@
#!/usr/bin/env node
function a() {}