diff --git a/index.js b/index.js index 7ed976467..748438441 100644 --- a/index.js +++ b/index.js @@ -235,11 +235,10 @@ function formatWithShebang(text, opts) { const index = text.indexOf("\n"); const shebang = text.slice(0, index + 1); - const programText = text.slice(index + 1); const nextChar = text.charAt(index + 1); const newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : ""; - return shebang + newLine + format(programText, opts); + return shebang + newLine + format(text, opts); } module.exports = { diff --git a/src/comments.js b/src/comments.js index 6612495e9..e057707fe 100644 --- a/src/comments.js +++ b/src/comments.js @@ -791,7 +791,7 @@ function handleVariableDeclaratorComments( return false; } -function printComment(commentPath) { +function printComment(commentPath, options) { const comment = commentPath.getValue(); comment.printed = true; @@ -801,6 +801,10 @@ function printComment(commentPath) { return "/*" + comment.value + "*/"; case "CommentLine": case "Line": + // Don't print the shebang, it's taken care of in index.js + if (options.originalText.slice(util.locStart(comment)).startsWith("#!")) { + return ""; + } return "//" + comment.value; default: throw new Error("Not a comment: " + JSON.stringify(comment)); @@ -832,7 +836,10 @@ function getQuasiRange(expr) { function printLeadingComment(commentPath, print, options) { const comment = commentPath.getValue(); - const contents = printComment(commentPath); + const contents = printComment(commentPath, options); + if (!contents) { + return ""; + } const isBlock = util.isBlockComment(comment); // Leading block comments should see if they need to stay on the @@ -849,7 +856,10 @@ function printLeadingComment(commentPath, print, options) { function printTrailingComment(commentPath, print, options) { const comment = commentPath.getValue(); - const contents = printComment(commentPath); + const contents = printComment(commentPath, options); + if (!contents) { + return ""; + } const isBlock = util.isBlockComment(comment); if ( @@ -895,8 +905,8 @@ function printDanglingComments(path, options, sameIndent) { path.each(commentPath => { const comment = commentPath.getValue(); - if (!comment.leading && !comment.trailing) { - parts.push(printComment(commentPath)); + if (comment && !comment.leading && !comment.trailing) { + parts.push(printComment(commentPath, options)); } }, "comments"); @@ -930,7 +940,11 @@ function printComments(path, print, options, needsSemi) { const trailing = types.getFieldValue(comment, "trailing"); if (leading) { - leadingParts.push(printLeadingComment(commentPath, print, options)); + const contents = printLeadingComment(commentPath, print, options); + if (!contents) { + return; + } + leadingParts.push(contents); const text = options.originalText; if (util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))) { diff --git a/tests/shebang/jsfmt.spec.js b/tests/shebang/jsfmt.spec.js index 7580dfab0..320b4cfe1 100644 --- a/tests/shebang/jsfmt.spec.js +++ b/tests/shebang/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, null, ["typescript"]); +run_spec(__dirname, null, ["typescript", "babylon"]);