Improve printing of comments
This commit is contained in:
1 parent
542c0ee7b9
commit
994432c193
5 files changed
+76
-158
No files matched your search
+13
-65
@@ -258,82 +258,32 @@ function addTrailingComment(node, comment) {
|
||||
function printLeadingComment(commentPath, print) {
|
||||
var comment = commentPath.getValue();
|
||||
n.Comment.assert(comment);
|
||||
|
||||
var loc = comment.loc;
|
||||
var lines = loc && loc.lines;
|
||||
var parts = [print(commentPath)];
|
||||
|
||||
if (comment.trailing) {
|
||||
// When we print trailing comments as leading comments, we don't
|
||||
// want to bring any trailing spaces along.
|
||||
parts.push(hardline);
|
||||
|
||||
}
|
||||
|
||||
// else if (lines instanceof Lines) {
|
||||
// var trailingSpace = lines.slice(
|
||||
// loc.end,
|
||||
// lines.skipSpaces(loc.end)
|
||||
// );
|
||||
|
||||
// if (trailingSpace.length === 1) {
|
||||
// // If the trailing space contains no newlines, then we want to
|
||||
// // preserve it exactly as we found it.
|
||||
// parts.push(trailingSpace);
|
||||
// } else {
|
||||
// // If the trailing space contains newlines, then replace it
|
||||
// // with just that many newlines, with all other spaces removed.
|
||||
// parts.push(new Array(trailingSpace.length).join("\n"));
|
||||
// }
|
||||
|
||||
// }
|
||||
else {
|
||||
parts.push(hardline);
|
||||
}
|
||||
|
||||
return concat(parts);
|
||||
return concat([print(commentPath), hardline]);
|
||||
}
|
||||
|
||||
function printTrailingComment(commentPath, print) {
|
||||
var comment = commentPath.getValue(commentPath);
|
||||
function printTrailingComment(commentPath, print, options) {
|
||||
const comment = commentPath.getValue(commentPath);
|
||||
n.Comment.assert(comment);
|
||||
const text = options.originalText;
|
||||
|
||||
var loc = comment.loc;
|
||||
var lines = loc && loc.lines;
|
||||
var parts = [];
|
||||
|
||||
// if (lines instanceof Lines) {
|
||||
// var fromPos = lines.skipSpaces(loc.start, true) || lines.firstPos();
|
||||
// var leadingSpace = lines.slice(fromPos, loc.start);
|
||||
|
||||
// if (leadingSpace.length === 1) {
|
||||
// // If the leading space contains no newlines, then we want to
|
||||
// // preserve it exactly as we found it.
|
||||
// parts.push(leadingSpace);
|
||||
// } else {
|
||||
// // If the leading space contains newlines, then replace it
|
||||
// // with just that many newlines, sans all other spaces.
|
||||
// parts.push(new Array(leadingSpace.length).join("\n"));
|
||||
// }
|
||||
// }
|
||||
|
||||
parts.push(print(commentPath), hardline);
|
||||
|
||||
return concat(parts);
|
||||
return concat([
|
||||
util.newlineExistsBefore(text, comment.start) ? hardline : " ",
|
||||
print(commentPath)
|
||||
]);
|
||||
}
|
||||
|
||||
exports.printComments = function(path, print) {
|
||||
exports.printComments = function(path, print, options) {
|
||||
var value = path.getValue();
|
||||
var innerLines = print(path);
|
||||
var printed = print(path);
|
||||
var comments = n.Node.check(value) &&
|
||||
types.getFieldValue(value, "comments");
|
||||
|
||||
if (!comments || comments.length === 0) {
|
||||
return innerLines;
|
||||
return printed;
|
||||
}
|
||||
|
||||
var leadingParts = [];
|
||||
var trailingParts = [innerLines];
|
||||
var trailingParts = [printed];
|
||||
|
||||
path.each(function(commentPath) {
|
||||
var comment = commentPath.getValue();
|
||||
@@ -345,12 +295,10 @@ exports.printComments = function(path, print) {
|
||||
comment.type === "CommentBlock"))) {
|
||||
leadingParts.push(printLeadingComment(commentPath, print));
|
||||
} else if (trailing) {
|
||||
trailingParts.push(printTrailingComment(commentPath, print));
|
||||
trailingParts.push(printTrailingComment(commentPath, print, options));
|
||||
}
|
||||
}, "comments");
|
||||
|
||||
leadingParts.push.apply(leadingParts, trailingParts);
|
||||
// TODO: Optimize this; if there are no comments we shouldn't
|
||||
// touch the structure
|
||||
return concat(leadingParts);
|
||||
};
|
||||
+29
-87
@@ -71,54 +71,9 @@ function Printer(originalOptions) {
|
||||
// can be confused by) options.sourceFileName, so we null it out.
|
||||
options.sourceFileName = null;
|
||||
|
||||
function printWithComments(path) {
|
||||
assert.ok(path instanceof FastPath);
|
||||
|
||||
return printComments(path, print);
|
||||
}
|
||||
|
||||
function print(path, includeComments) {
|
||||
if (includeComments)
|
||||
return printWithComments(path);
|
||||
|
||||
assert.ok(path instanceof FastPath);
|
||||
|
||||
if (!explicitTabWidth) {
|
||||
var oldTabWidth = options.tabWidth;
|
||||
var loc = path.getNode().loc;
|
||||
|
||||
if (loc && loc.lines && loc.lines.guessTabWidth) {
|
||||
options.tabWidth = loc.lines.guessTabWidth();
|
||||
|
||||
var lines = maybeReprint(path);
|
||||
|
||||
options.tabWidth = oldTabWidth;
|
||||
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
return maybeReprint(path);
|
||||
}
|
||||
|
||||
function maybeReprint(path) {
|
||||
// TODO: remove this function entirely as we don't ever keep the
|
||||
// previous formatting
|
||||
return printRootGenerically(path);
|
||||
}
|
||||
|
||||
// Print the root node generically, but then resume reprinting its
|
||||
// children non-generically.
|
||||
function printRootGenerically(path, includeComments) {
|
||||
return includeComments
|
||||
? printComments(path, printRootGenerically)
|
||||
: genericPrint(path, options, printWithComments);
|
||||
}
|
||||
|
||||
// Print the entire AST generically.
|
||||
function printGenerically(path) {
|
||||
// return genericPrint(path, options, printGenerically);
|
||||
return printComments(path, p => genericPrint(p, options, printGenerically));
|
||||
return printComments(path, p => genericPrint(p, options, printGenerically), options);
|
||||
}
|
||||
|
||||
this.print = function(ast) {
|
||||
@@ -349,13 +304,15 @@ function genericPrintNoParens(path, options, print) {
|
||||
|
||||
parts.push(
|
||||
path.call(print, "typeParameters"),
|
||||
printFunctionParams(path, print, options),
|
||||
printReturnType(path, print),
|
||||
multilineGroup(concat([
|
||||
printFunctionParams(path, print, options),
|
||||
printReturnType(path, print)
|
||||
])),
|
||||
" ",
|
||||
path.call(print, "body")
|
||||
);
|
||||
|
||||
return group(concat(parts));
|
||||
return concat(parts);
|
||||
case "ArrowFunctionExpression":
|
||||
if (n.async)
|
||||
parts.push("async ");
|
||||
@@ -373,15 +330,15 @@ function genericPrintNoParens(path, options, print) {
|
||||
) {
|
||||
parts.push(path.call(print, "params", 0));
|
||||
} else {
|
||||
parts.push(
|
||||
parts.push(multilineGroup(concat([
|
||||
printFunctionParams(path, print, options),
|
||||
printReturnType(path, print)
|
||||
);
|
||||
])));
|
||||
}
|
||||
|
||||
parts.push(" => ", path.call(print, "body"));
|
||||
|
||||
return group(concat(parts));
|
||||
return concat(parts);
|
||||
case "MethodDefinition":
|
||||
if (n.static) {
|
||||
parts.push("static ");
|
||||
@@ -1265,12 +1222,12 @@ function genericPrintNoParens(path, options, print) {
|
||||
case "CommentBlock":
|
||||
case // Esprima block comment.
|
||||
"Block":
|
||||
return concat([ "/*", fromString(n.value, options), "*/" ]);
|
||||
return concat([ "/*", n.value, "*/" ]);
|
||||
// Babel line comment.
|
||||
case "CommentLine":
|
||||
case // Esprima line comment.
|
||||
"Line":
|
||||
return concat([ "//", fromString(n.value, options) ]);
|
||||
return concat([ "//", n.value ]);
|
||||
// Type Annotations for Facebook Flow, typically stripped out or
|
||||
// transformed away before printing.
|
||||
case "TypeAnnotation":
|
||||
@@ -1351,7 +1308,7 @@ function genericPrintNoParens(path, options, print) {
|
||||
|
||||
parts.push(path.call(print, "typeParameters"));
|
||||
|
||||
parts.push(printFunctionParams(path, print, options));
|
||||
parts.push(multilineGroup(printFunctionParams(path, print, options)));
|
||||
|
||||
// The returnType is not wrapped in a TypeAnnotation, so the colon
|
||||
// needs to be added separately.
|
||||
@@ -1363,7 +1320,7 @@ function genericPrintNoParens(path, options, print) {
|
||||
);
|
||||
}
|
||||
|
||||
return group(concat(parts));
|
||||
return concat(parts);
|
||||
case "FunctionTypeParam":
|
||||
return concat([
|
||||
path.call(print, "name"),
|
||||
@@ -1659,18 +1616,20 @@ function printMethod(path, options, print) {
|
||||
parts.push(
|
||||
key,
|
||||
path.call(print, "value", "typeParameters"),
|
||||
path.call(
|
||||
function(valuePath) {
|
||||
return printFunctionParams(valuePath, print, options);
|
||||
},
|
||||
"value"
|
||||
),
|
||||
path.call(p => printReturnType(p, print), "value"),
|
||||
multilineGroup(concat([
|
||||
path.call(
|
||||
function(valuePath) {
|
||||
return printFunctionParams(valuePath, print, options);
|
||||
},
|
||||
"value"
|
||||
),
|
||||
path.call(p => printReturnType(p, print), "value")
|
||||
])),
|
||||
" ",
|
||||
path.call(print, "value", "body")
|
||||
);
|
||||
|
||||
return group(concat(parts));
|
||||
return concat(parts);
|
||||
}
|
||||
|
||||
function printArgumentsList(path, options, print) {
|
||||
@@ -1797,13 +1756,15 @@ function printObjectMethod(path, options, print) {
|
||||
}
|
||||
|
||||
parts.push(
|
||||
printFunctionParams(path, print, options),
|
||||
printReturnType(path, print),
|
||||
multilineGroup([
|
||||
printFunctionParams(path, print, options),
|
||||
printReturnType(path, print)
|
||||
]),
|
||||
" ",
|
||||
path.call(print, "body")
|
||||
);
|
||||
|
||||
return group(concat(parts));
|
||||
return concat(parts);
|
||||
}
|
||||
|
||||
function printReturnType(path, print) {
|
||||
@@ -2045,26 +2006,7 @@ function nodeStr(str, options) {
|
||||
|
||||
function shouldAddSpacing(node, options) {
|
||||
const text = options.originalText;
|
||||
const length = text.length;
|
||||
let cursor = node.end + 1;
|
||||
// Look forward and see if there is a blank line after this code by
|
||||
// scanning up to the next non-indentation character.
|
||||
while (cursor < length) {
|
||||
const c = text.charAt(cursor);
|
||||
// Skip any indentation characters (this will detect lines with
|
||||
// spaces or tabs as blank)
|
||||
if (c !== " " && c !== "\t") {
|
||||
// If the next character is a newline, this line is blank so we
|
||||
// should add a blank line.
|
||||
if (c === "\n" || c === "\r") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
cursor++;
|
||||
}
|
||||
|
||||
return false;
|
||||
return util.newlineExistsAfter(text, node.end);
|
||||
}
|
||||
|
||||
function isFirstStatement(path) {
|
||||
|
||||
+26
@@ -311,3 +311,29 @@ util.getLast = function(arr) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function _findNewline(text, index, backwards) {
|
||||
const length = text.length;
|
||||
let cursor = backwards ? (index - 1) : (index + 1);
|
||||
// Look forward and see if there is a newline after/before this code
|
||||
// by scanning up/back to the next non-indentation character.
|
||||
while (cursor > 0 && cursor < length) {
|
||||
const c = text.charAt(cursor);
|
||||
// Skip any indentation characters
|
||||
if (c !== " " && c !== "\t") {
|
||||
// Check if the next non-indentation character is a newline or
|
||||
// not.
|
||||
return c === "\n" || c === "\r";
|
||||
}
|
||||
backwards ? cursor-- : cursor++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
util.newlineExistsBefore = function(text, index) {
|
||||
return _findNewline(text, index, true);
|
||||
}
|
||||
|
||||
util.newlineExistsAfter = function(text, index) {
|
||||
return _findNewline(text, index);
|
||||
}
|
||||
@@ -63,9 +63,11 @@ let tests = [
|
||||
function(copyProperties: Object$Assign) {
|
||||
let result = {};
|
||||
result.baz = false;
|
||||
(copyProperties(result, { foo: \"a\" }, {
|
||||
bar: 123
|
||||
}): { foo: string, bar: number, baz: boolean });
|
||||
(copyProperties(result, { foo: \"a\" }, { bar: 123 }): {
|
||||
foo: string,
|
||||
bar: number,
|
||||
baz: boolean
|
||||
});
|
||||
},
|
||||
function() {
|
||||
const copyProperties = require(\"copyProperties\");
|
||||
|
||||
@@ -76,9 +76,9 @@ declare class Promise<R> {
|
||||
static cast<T>(object?: T): Promise<T>,
|
||||
static all<T>(promises: Array<?Promise<T> | T>): Promise<Array<T>>,
|
||||
static race<T>(promises: Array<Promise<T>>): Promise<T>,
|
||||
static allObject<T: Object>(
|
||||
promisesByKey: T
|
||||
): Promise<{ [key: $Keys<T>]: any }>
|
||||
static allObject<T: Object>(promisesByKey: T): Promise<{
|
||||
[key: $Keys<T>]: any
|
||||
}>
|
||||
}
|
||||
"
|
||||
`;
|
||||
Reference in new issue
Block a user