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
This commit is contained in:
1 parent
7344a37b5c
commit
9d7515907e
3 files changed
+22
-9
No files matched your search
@@ -235,11 +235,10 @@ function formatWithShebang(text, opts) {
|
|||||||
|
|
||||||
const index = text.indexOf("\n");
|
const index = text.indexOf("\n");
|
||||||
const shebang = text.slice(0, index + 1);
|
const shebang = text.slice(0, index + 1);
|
||||||
const programText = text.slice(index + 1);
|
|
||||||
const nextChar = text.charAt(index + 1);
|
const nextChar = text.charAt(index + 1);
|
||||||
const newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : "";
|
const newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : "";
|
||||||
|
|
||||||
return shebang + newLine + format(programText, opts);
|
return shebang + newLine + format(text, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
+20
-6
@@ -791,7 +791,7 @@ function handleVariableDeclaratorComments(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function printComment(commentPath) {
|
function printComment(commentPath, options) {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
comment.printed = true;
|
comment.printed = true;
|
||||||
|
|
||||||
@@ -801,6 +801,10 @@ function printComment(commentPath) {
|
|||||||
return "/*" + comment.value + "*/";
|
return "/*" + comment.value + "*/";
|
||||||
case "CommentLine":
|
case "CommentLine":
|
||||||
case "Line":
|
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;
|
return "//" + comment.value;
|
||||||
default:
|
default:
|
||||||
throw new Error("Not a comment: " + JSON.stringify(comment));
|
throw new Error("Not a comment: " + JSON.stringify(comment));
|
||||||
@@ -832,7 +836,10 @@ function getQuasiRange(expr) {
|
|||||||
|
|
||||||
function printLeadingComment(commentPath, print, options) {
|
function printLeadingComment(commentPath, print, options) {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
const contents = printComment(commentPath);
|
const contents = printComment(commentPath, options);
|
||||||
|
if (!contents) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
const isBlock = util.isBlockComment(comment);
|
const isBlock = util.isBlockComment(comment);
|
||||||
|
|
||||||
// Leading block comments should see if they need to stay on the
|
// 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) {
|
function printTrailingComment(commentPath, print, options) {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
const contents = printComment(commentPath);
|
const contents = printComment(commentPath, options);
|
||||||
|
if (!contents) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
const isBlock = util.isBlockComment(comment);
|
const isBlock = util.isBlockComment(comment);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -895,8 +905,8 @@ function printDanglingComments(path, options, sameIndent) {
|
|||||||
|
|
||||||
path.each(commentPath => {
|
path.each(commentPath => {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
if (!comment.leading && !comment.trailing) {
|
if (comment && !comment.leading && !comment.trailing) {
|
||||||
parts.push(printComment(commentPath));
|
parts.push(printComment(commentPath, options));
|
||||||
}
|
}
|
||||||
}, "comments");
|
}, "comments");
|
||||||
|
|
||||||
@@ -930,7 +940,11 @@ function printComments(path, print, options, needsSemi) {
|
|||||||
const trailing = types.getFieldValue(comment, "trailing");
|
const trailing = types.getFieldValue(comment, "trailing");
|
||||||
|
|
||||||
if (leading) {
|
if (leading) {
|
||||||
leadingParts.push(printLeadingComment(commentPath, print, options));
|
const contents = printLeadingComment(commentPath, print, options);
|
||||||
|
if (!contents) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
leadingParts.push(contents);
|
||||||
|
|
||||||
const text = options.originalText;
|
const text = options.originalText;
|
||||||
if (util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))) {
|
if (util.hasNewline(text, util.skipNewline(text, util.locEnd(comment)))) {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
run_spec(__dirname, null, ["typescript"]);
|
run_spec(__dirname, null, ["typescript", "babylon"]);
|
||||||
Reference in new issue
Block a user