From abbf0b4c816f6b32f221b3991eeabe8be2240eab Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 13 Feb 2017 06:55:54 -0800 Subject: [PATCH] Stabilize comments inside of if/then/else before { (#672) We already have the logic for fixing if/then/else comments but those ones are triggering another codepath. Fixes #618 --- src/comments.js | 14 ++-- .../__snapshots__/jsfmt.spec.js.snap | 73 +++++++++++++++++++ tests/if_comments/if_comments.js | 37 ++++++++++ tests/if_comments/jsfmt.spec.js | 1 + 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 tests/if_comments/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/if_comments/if_comments.js create mode 100644 tests/if_comments/jsfmt.spec.js diff --git a/src/comments.js b/src/comments.js index d97dae76..6109ce83 100644 --- a/src/comments.js +++ b/src/comments.js @@ -170,12 +170,14 @@ function attach(comments, ast, text) { addDanglingComment(ast, comment); } } else { - // Otherwise, text exists both before and after the comment on - // the same line. If there is both a preceding and following - // node, use a tie-breaking algorithm to determine if it should - // be attached to the next or previous node. In the last case, - // simply attach the right node; - if (precedingNode && followingNode) { + if (handleIfStatementComments(enclosingNode, followingNode, comment)) { + // We're good + } else if (precedingNode && followingNode) { + // Otherwise, text exists both before and after the comment on + // the same line. If there is both a preceding and following + // node, use a tie-breaking algorithm to determine if it should + // be attached to the next or previous node. In the last case, + // simply attach the right node; const tieCount = tiesToBreak.length; if (tieCount > 0) { var lastTie = tiesToBreak[tieCount - 1]; diff --git a/tests/if_comments/__snapshots__/jsfmt.spec.js.snap b/tests/if_comments/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..56951037 --- /dev/null +++ b/tests/if_comments/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,73 @@ +exports[`test if_comments.js 1`] = ` +"async function f() { + if (untrackedChoice === 0) /* Cancel */ { + return null; + } else if (untrackedChoice === 1) /* Add */ { + await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) /* Allow Untracked */ { + allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) + /* Cancel */ { + return null; + } + else if (untrackedChoice === 1) + /* Add */ { + await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } + else if (untrackedChoice === 2) + /* Allow Untracked */ { + allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) { + /* Cancel */ return null; + } else if (untrackedChoice === 1) { + /* Add */ await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) { + /* Allow Untracked */ allowUntracked = true; + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +async function f() { + if (untrackedChoice === 0) { + /* Cancel */ return null; + } else if (untrackedChoice === 1) { + /* Add */ await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) { + /* Allow Untracked */ allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) { + /* Cancel */ return null; + } else if (untrackedChoice === 1) { + /* Add */ await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) { + /* Allow Untracked */ allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) { + /* Cancel */ return null; + } else if (untrackedChoice === 1) { + /* Add */ await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) { + /* Allow Untracked */ allowUntracked = true; + } +} +" +`; diff --git a/tests/if_comments/if_comments.js b/tests/if_comments/if_comments.js new file mode 100644 index 00000000..2b355299 --- /dev/null +++ b/tests/if_comments/if_comments.js @@ -0,0 +1,37 @@ +async function f() { + if (untrackedChoice === 0) /* Cancel */ { + return null; + } else if (untrackedChoice === 1) /* Add */ { + await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) /* Allow Untracked */ { + allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) + /* Cancel */ { + return null; + } + else if (untrackedChoice === 1) + /* Add */ { + await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } + else if (untrackedChoice === 2) + /* Allow Untracked */ { + allowUntracked = true; + } +} + +async function f() { + if (untrackedChoice === 0) { + /* Cancel */ return null; + } else if (untrackedChoice === 1) { + /* Add */ await repository.addAll(Array.from(untrackedChanges.keys())); + shouldAmend = true; + } else if (untrackedChoice === 2) { + /* Allow Untracked */ allowUntracked = true; + } +} diff --git a/tests/if_comments/jsfmt.spec.js b/tests/if_comments/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/if_comments/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname);