Fix files with comments only (#813)
* Add new handleOnlyComments function. * Update tests. * Update test as the printer forces a trailing newline if there were any contents. * Implement a different heuristic. * Update tests. * Add directives checking in handleOnlyComments function. * Add directives checking in handleOnlyComments function (amend to retrigger CI). * Remove duplicate.master
parent
3045829a61
commit
008ac1c479
|
@ -132,13 +132,15 @@ function attach(comments, ast, text, options) {
|
||||||
|
|
||||||
var tiesToBreak = [];
|
var tiesToBreak = [];
|
||||||
|
|
||||||
comments.forEach(function(comment) {
|
comments.forEach((comment, i) => {
|
||||||
decorateComment(ast, comment, text);
|
decorateComment(ast, comment, text);
|
||||||
|
|
||||||
const precedingNode = comment.precedingNode;
|
const precedingNode = comment.precedingNode;
|
||||||
const enclosingNode = comment.enclosingNode;
|
const enclosingNode = comment.enclosingNode;
|
||||||
const followingNode = comment.followingNode;
|
const followingNode = comment.followingNode;
|
||||||
|
|
||||||
|
const isLastComment = comments.length - 1 === i;
|
||||||
|
|
||||||
if (util.hasNewline(text, locStart(comment), { backwards: true })) {
|
if (util.hasNewline(text, locStart(comment), { backwards: true })) {
|
||||||
// If a comment exists on its own line, prefer a leading comment.
|
// If a comment exists on its own line, prefer a leading comment.
|
||||||
// We also need to check if it's the first line of the file.
|
// We also need to check if it's the first line of the file.
|
||||||
|
@ -152,14 +154,15 @@ function attach(comments, ast, text, options) {
|
||||||
handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
|
handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
|
||||||
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
||||||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
||||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
|
||||||
handleClassComments(enclosingNode, comment) ||
|
handleClassComments(enclosingNode, comment) ||
|
||||||
|
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||||
handleUnionTypeComments(
|
handleUnionTypeComments(
|
||||||
precedingNode,
|
precedingNode,
|
||||||
enclosingNode,
|
enclosingNode,
|
||||||
followingNode,
|
followingNode,
|
||||||
comment
|
comment
|
||||||
)
|
) ||
|
||||||
|
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
|
||||||
) {
|
) {
|
||||||
// We're good
|
// We're good
|
||||||
} else if (followingNode) {
|
} else if (followingNode) {
|
||||||
|
@ -184,10 +187,11 @@ function attach(comments, ast, text, options) {
|
||||||
) ||
|
) ||
|
||||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||||
handleTemplateLiteralComments(enclosingNode, comment) ||
|
handleTemplateLiteralComments(enclosingNode, comment) ||
|
||||||
handleCallExpressionComments(precedingNode, enclosingNode, comment) ||
|
|
||||||
handleClassComments(enclosingNode, comment) ||
|
handleClassComments(enclosingNode, comment) ||
|
||||||
|
handleCallExpressionComments(precedingNode, enclosingNode, comment) ||
|
||||||
handlePropertyComments(enclosingNode, comment) ||
|
handlePropertyComments(enclosingNode, comment) ||
|
||||||
handleExportNamedDeclarationComments(enclosingNode, comment)
|
handleExportNamedDeclarationComments(enclosingNode, comment) ||
|
||||||
|
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
|
||||||
) {
|
) {
|
||||||
// We're good
|
// We're good
|
||||||
} else if (precedingNode) {
|
} else if (precedingNode) {
|
||||||
|
@ -207,7 +211,8 @@ function attach(comments, ast, text, options) {
|
||||||
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
||||||
handleObjectProperty(enclosingNode, precedingNode, comment) ||
|
handleObjectProperty(enclosingNode, precedingNode, comment) ||
|
||||||
handleTemplateLiteralComments(enclosingNode, comment) ||
|
handleTemplateLiteralComments(enclosingNode, comment) ||
|
||||||
handleFunctionDeclarationComments(enclosingNode, comment)
|
handleFunctionDeclarationComments(enclosingNode, comment) ||
|
||||||
|
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
|
||||||
) {
|
) {
|
||||||
// We're good
|
// We're good
|
||||||
} else if (precedingNode && followingNode) {
|
} else if (precedingNode && followingNode) {
|
||||||
|
@ -584,6 +589,30 @@ function handleExportNamedDeclarationComments(enclosingNode, comment) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleOnlyComments(enclosingNode, ast, comment, isLastComment) {
|
||||||
|
// With Flow the enclosingNode is undefined so use the AST instead.
|
||||||
|
if (ast && ast.body && ast.body.length === 0) {
|
||||||
|
if (isLastComment) {
|
||||||
|
addDanglingComment(ast, comment);
|
||||||
|
} else {
|
||||||
|
addLeadingComment(ast, comment);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else if (
|
||||||
|
enclosingNode && enclosingNode.type === 'Program' &&
|
||||||
|
enclosingNode.body.length === 0 && enclosingNode.directives &&
|
||||||
|
enclosingNode.directives.length === 0
|
||||||
|
) {
|
||||||
|
if (isLastComment) {
|
||||||
|
addDanglingComment(enclosingNode, comment);
|
||||||
|
} else {
|
||||||
|
addLeadingComment(enclosingNode, comment);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function printComment(commentPath) {
|
function printComment(commentPath) {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
comment.printed = true;
|
comment.printed = true;
|
||||||
|
|
|
@ -44,12 +44,44 @@ exports[`blank.js 1`] = `
|
||||||
// should still exist
|
// should still exist
|
||||||
//
|
//
|
||||||
// when printed.
|
// when printed.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {DataDrivenMapping|ConstantMapping} Mapping
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {Object.<String, Mapping>} ConfigurationMapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Function} D3Scale - a D3 scale
|
||||||
|
* @property {Function} ticks
|
||||||
|
* @property {Function} tickFormat
|
||||||
|
*/
|
||||||
|
// comment
|
||||||
|
|
||||||
|
// comment
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
// This file only
|
// This file only
|
||||||
// has comments. This comment
|
// has comments. This comment
|
||||||
// should still exist
|
// should still exist
|
||||||
//
|
//
|
||||||
// when printed.
|
// when printed.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {DataDrivenMapping|ConstantMapping} Mapping
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {Object.<String, Mapping>} ConfigurationMapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Function} D3Scale - a D3 scale
|
||||||
|
* @property {Function} ticks
|
||||||
|
* @property {Function} tickFormat
|
||||||
|
*/
|
||||||
|
// comment
|
||||||
|
|
||||||
|
// comment
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
@ -59,12 +91,44 @@ exports[`blank.js 2`] = `
|
||||||
// should still exist
|
// should still exist
|
||||||
//
|
//
|
||||||
// when printed.
|
// when printed.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {DataDrivenMapping|ConstantMapping} Mapping
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {Object.<String, Mapping>} ConfigurationMapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Function} D3Scale - a D3 scale
|
||||||
|
* @property {Function} ticks
|
||||||
|
* @property {Function} tickFormat
|
||||||
|
*/
|
||||||
|
// comment
|
||||||
|
|
||||||
|
// comment
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
// This file only
|
// This file only
|
||||||
// has comments. This comment
|
// has comments. This comment
|
||||||
// should still exist
|
// should still exist
|
||||||
//
|
//
|
||||||
// when printed.
|
// when printed.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {DataDrivenMapping|ConstantMapping} Mapping
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {Object.<String, Mapping>} ConfigurationMapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Function} D3Scale - a D3 scale
|
||||||
|
* @property {Function} ticks
|
||||||
|
* @property {Function} tickFormat
|
||||||
|
*/
|
||||||
|
// comment
|
||||||
|
|
||||||
|
// comment
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|
|
@ -3,3 +3,19 @@
|
||||||
// should still exist
|
// should still exist
|
||||||
//
|
//
|
||||||
// when printed.
|
// when printed.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {DataDrivenMapping|ConstantMapping} Mapping
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @typedef {Object.<String, Mapping>} ConfigurationMapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Function} D3Scale - a D3 scale
|
||||||
|
* @property {Function} ticks
|
||||||
|
* @property {Function} tickFormat
|
||||||
|
*/
|
||||||
|
// comment
|
||||||
|
|
||||||
|
// comment
|
||||||
|
|
|
@ -586,6 +586,7 @@ export default {
|
||||||
* @providesModule ES6Default
|
* @providesModule ES6Default
|
||||||
* @flow
|
* @flow
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
export default {
|
export default {
|
||||||
numberValue: 42,
|
numberValue: 42,
|
||||||
|
|
|
@ -686,6 +686,7 @@ export default {
|
||||||
* @providesModule ES6Default
|
* @providesModule ES6Default
|
||||||
* @flow
|
* @flow
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
export default {
|
export default {
|
||||||
numberValue: 42,
|
numberValue: 42,
|
||||||
|
|
Loading…
Reference in New Issue