Pass entire text through formatWithShebang() to format() (#1718)

* Verify shebang tests against Babylon as well as Flow/Typescript

* Pass entire text through formatWithShebang() to format()

That way, we won't have to do any arithmetic on range and cursor
locations when there is a shebang. See here for details:

https://github.com/prettier/prettier/pull/1637#issuecomment-303846507

This required changing comment printing such that comments that are
actually shebangs are just ignored.

* Add curly brace after `if`

See https://github.com/prettier/prettier/pull/1718#discussion_r118382574
master
Joseph Frazier 2017-05-24 18:59:25 -04:00 committed by GitHub
parent 7344a37b5c
commit 9d7515907e
3 changed files with 22 additions and 9 deletions

View File

@ -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 = {

View File

@ -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)))) {

View File

@ -1 +1 @@
run_spec(__dirname, null, ["typescript"]);
run_spec(__dirname, null, ["typescript", "babylon"]);