Fix comment style when placed after if statement test (#2675)

* Fix comment style when placed after the test of an if statement

* Add tests

* Do a bit of refactoring

* Fix lint errors

* Handle skipping comments

* Account for multiple-line comments

* Add more tests

* Change function name

* Add more tests

* Refactor

* Add some comments

* Do away with getPreviousNonSpaceNonCommentCharacter

* Improve comment and code on handling if statement comments

* Refactor

* Fix a bug
master
jackyho112 2017-09-08 10:12:10 -07:00 committed by Christopher Chedeau
parent 85f1351366
commit 302de60bdc
5 changed files with 203 additions and 11 deletions

View File

@ -466,12 +466,12 @@ function handleIfStatementComments(
}
// We unfortunately have no way using the AST or location of nodes to know
// if the comment is positioned before or after the condition parenthesis:
// if the comment is positioned before the condition parenthesis:
// if (a /* comment */) {}
// if (a) /* comment */ {}
// The only workaround I found is to look at the next character to see if
// it is a ).
if (getNextNonSpaceNonCommentCharacter(text, comment) === ")") {
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
return true;
}
@ -486,6 +486,16 @@ function handleIfStatementComments(
return true;
}
// For comments positioned after the condition parenthesis in an if statement
// before the consequent with or without brackets on, such as
// if (a) /* comment */ {} or if (a) /* comment */ true,
// we look at the next character to see if it is a { or if the following node
// is the consequent for the if statement
if (nextCharacter === "{" || enclosingNode.consequent === followingNode) {
addLeadingComment(followingNode, comment);
return true;
}
return false;
}

View File

@ -328,10 +328,45 @@ else if (4) {
else {
}
if (1)
{}
if (5) // comment
true
if (6) // comment
{true}
else if (7) // comment
true
else // comment
{}
{true}
if (8) // comment
// comment
{true}
else if (9) // comment
// comment
true
else // comment
// comment
{true}
if (10) /* comment */ // comment
{true}
else if (11) /* comment */
true
else if (12) // comment /* comment */ // comment
true
else if (13) /* comment */ /* comment */ // comment
true
else /* comment */
{true}
if (14) // comment
/* comment */
// comment
{true}
else if (15) // comment
/* comment */
/* comment */ // comment
true
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (1) {
// comment
@ -352,11 +387,63 @@ else if (4) {
// comment
}
if (1) {
} else {
if (5)
// comment
true;
if (6) {
// comment
true;
} else if (7)
// comment
true;
else {
// comment
true;
}
if (8) {
// comment
// comment
true;
} else if (9)
// comment
// comment
true;
else {
// comment
// comment
true;
}
if (10) {
/* comment */ // comment
true;
} else if (11)
/* comment */
true;
else if (12)
// comment /* comment */ // comment
true;
else if (13)
/* comment */ /* comment */ // comment
true;
else {
/* comment */
true;
}
if (14) {
// comment
/* comment */
// comment
true;
} else if (15)
// comment
/* comment */
/* comment */ // comment
true;
`;
exports[`issues.js 1`] = `

View File

@ -20,7 +20,42 @@ else if (4) {
else {
}
if (1)
{}
if (5) // comment
true
if (6) // comment
{true}
else if (7) // comment
true
else // comment
{}
{true}
if (8) // comment
// comment
{true}
else if (9) // comment
// comment
true
else // comment
// comment
{true}
if (10) /* comment */ // comment
{true}
else if (11) /* comment */
true
else if (12) // comment /* comment */ // comment
true
else if (13) /* comment */ /* comment */ // comment
true
else /* comment */
{true}
if (14) // comment
/* comment */
// comment
{true}
else if (15) // comment
/* comment */
/* comment */ // comment
true

View File

@ -54,6 +54,24 @@ async function f() {
}
}
async function f() {
if (untrackedChoice === 0) /* Cancel */
null;
else if (untrackedChoice === 1) /* Add */
shouldAmend = true;
else if (untrackedChoice === 2) /* Allow Untracked */
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0) /* Cancel */ // Cancel
null;
else if (untrackedChoice === 1) /* Add */ // Add
shouldAmend = true;
else if (untrackedChoice === 2) /* Allow Untracked */ // Allow Untracked
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0)
/* Cancel */ {
@ -92,6 +110,30 @@ async function f() {
}
}
async function f() {
if (untrackedChoice === 0)
/* Cancel */
null;
else if (untrackedChoice === 1)
/* Add */
shouldAmend = true;
else if (untrackedChoice === 2)
/* Allow Untracked */
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0)
/* Cancel */ // Cancel
null;
else if (untrackedChoice === 1)
/* Add */ // Add
shouldAmend = true;
else if (untrackedChoice === 2)
/* Allow Untracked */ // Allow Untracked
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0) {
/* Cancel */ return null;

View File

@ -9,6 +9,24 @@ async function f() {
}
}
async function f() {
if (untrackedChoice === 0) /* Cancel */
null;
else if (untrackedChoice === 1) /* Add */
shouldAmend = true;
else if (untrackedChoice === 2) /* Allow Untracked */
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0) /* Cancel */ // Cancel
null;
else if (untrackedChoice === 1) /* Add */ // Add
shouldAmend = true;
else if (untrackedChoice === 2) /* Allow Untracked */ // Allow Untracked
allowUntracked = true;
}
async function f() {
if (untrackedChoice === 0)
/* Cancel */ {