Stabilize comments inside of ternaries (#666)

Now, all the variations of comments in ternaries are printed the same way.

Fixes #617
master
Christopher Chedeau 2017-02-13 07:12:31 -08:00 committed by James Long
parent 3464092cc4
commit 287db4a9b6
4 changed files with 118 additions and 3 deletions

View File

@ -157,9 +157,11 @@ function attach(comments, ast, text) {
addDanglingComment(ast, comment);
}
} else if (util.hasNewline(text, locEnd(comment))) {
// There is content before this comment on the same line, but
// none after it, so prefer a trailing comment of the previous node.
if (precedingNode) {
if (handleConditionalExpressionComments(enclosingNode, followingNode, comment)) {
// We're good
} else if (precedingNode) {
// There is content before this comment on the same line, but
// none after it, so prefer a trailing comment of the previous node.
addTrailingComment(precedingNode, comment);
} else if (followingNode) {
addLeadingComment(followingNode, comment);
@ -369,6 +371,14 @@ function handleMemberExpressionComment(enclosingNode, followingNode, comment) {
return false;
}
function handleConditionalExpressionComments(enclosingNode, followingNode, comment) {
if (enclosingNode && enclosingNode.type === 'ConditionalExpression' && followingNode) {
addLeadingComment(followingNode, comment);
return true;
}
return false;
}
function printComment(commentPath) {
const comment = commentPath.getValue();

View File

@ -0,0 +1,71 @@
exports[`test comments.js 1`] = `
"var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
const extractTextPluginOptions = shouldUseRelativeAssetPaths
// Making sure that the publicPath goes back to to build folder.
? { publicPath: Array(cssFilename.split(\'/\').length).join(\'../\') } :
{};
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split(\"/\").length).join(\"../\") }
: {};
const extractTextPluginOptions = shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
? { publicPath: Array(cssFilename.split(\"/\").length).join(\"../\") }
: {};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split(\"/\").length).join(\"../\") }
: {};
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split(\"/\").length).join(\"../\") }
: {};
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split(\"/\").length).join(\"../\") }
: {};
"
`;

View File

@ -0,0 +1,33 @@
var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
var inspect = 4 === util.inspect.length
? // node <= 0.8.x
(function(v, colors) {
return util.inspect(v, void 0, void 0, colors);
})
: // node > 0.8.x
(function(v, colors) {
return util.inspect(v, { colors: colors });
});
const extractTextPluginOptions = shouldUseRelativeAssetPaths
// Making sure that the publicPath goes back to to build folder.
? { publicPath: Array(cssFilename.split('/').length).join('../') } :
{};
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split("/").length).join("../") }
: {};
const extractTextPluginOptions = shouldUseRelativeAssetPaths // Making sure that the publicPath goes back to to build folder.
? { publicPath: Array(cssFilename.split("/").length).join("../") }
: {};

View File

@ -0,0 +1 @@
run_spec(__dirname);