Skip to content

Commit

Permalink
Print empty block semicolon with comments (#152)
Browse files Browse the repository at this point in the history
* print semicolon for if with comment

* tests
  • Loading branch information
helixbass authored Dec 31, 2019
1 parent 794dc45 commit ab59b56
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 17 deletions.
12 changes: 3 additions & 9 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,17 +1160,15 @@ function printPathNoParens(path, options, print) {
const hasDirectives = n.directives && n.directives.length > 0

const parent = path.getParentNode()
if (
const needsSemicolon =
!hasContent &&
!hasDirectives &&
!hasDanglingComments(n) &&
(((parent.type === 'IfStatement' ||
parent.type === 'ConditionalExpression') &&
n === parent.consequent &&
!parent.alternate) ||
((parent.type === 'WhileStatement' || parent.type === 'For') &&
n === parent.body))
) {
if (!hasDirectives && !hasDanglingComments(n) && needsSemicolon) {
return indent(concat([hardline, ';']))
}

Expand Down Expand Up @@ -1210,11 +1208,7 @@ function printPathNoParens(path, options, print) {

parts.push(comments.printDanglingComments(path, options))

if (
!hasContent &&
(parent.type === 'WhileStatement' || parent.type === 'For') &&
n === parent.body
) {
if (needsSemicolon) {
parts.push(indent(concat([hardline, ';'])))
}
return concat(parts)
Expand Down
24 changes: 16 additions & 8 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,23 @@ function hasSameStartLine(node, other) {
return node.loc.start.line === other.loc.start.line
}

function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
let oldIdx = null
let idx = locEnd(node)
while (idx !== oldIdx) {
oldIdx = idx
idx = skipSpaces(text, idx)
idx = skipInlineComment(text, idx)
idx = skipNewline(text, idx)
let nextIdx = idx
while (nextIdx !== oldIdx) {
oldIdx = nextIdx
nextIdx = skipSpaces(text, nextIdx)
nextIdx = skipInlineComment(text, nextIdx)
nextIdx = skipNewline(text, nextIdx)
}
return idx
return nextIdx
}

function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
locEnd(node)
)
}

function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
Expand All @@ -131,5 +138,6 @@ module.exports = {
isNextLineEmpty,
hasSameStartLine,
getNextNonSpaceNonCommentCharacter,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
isFunction,
}
100 changes: 100 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,106 @@ expect(=> {}).toTriggerReadyStateChanges [
`;
exports[`empty.coffee 1`] = `
if a
# preceding
;
if a
; # trailing
if a
;
# next line
while a
# preceding
;
while a
; # trailing
while a
;
# next line
for a in b
# preceding
;
for a in b
; # trailing
for a in b
;
# next line
switch
when b
# preceding
;
switch
when b
; # trailing
switch
when b
;
# next line
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if a
# preceding
;
if a
# trailing
;
if a
# next line
;
while a
# preceding
;
while a
# trailing
;
while a
# next line
;
for a in b
# preceding
;
for a in b
# trailing
;
for a in b
# next line
;
switch
when b
# preceding
;
switch
when b # trailing
;
switch
when b
# next line
;
`;
exports[`export.coffee 1`] = `
export ### comment ### {}
Expand Down
46 changes: 46 additions & 0 deletions tests/comments/empty.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if a
# preceding
;

if a
; # trailing

if a
;
# next line

while a
# preceding
;

while a
; # trailing

while a
;
# next line

for a in b
# preceding
;

for a in b
; # trailing

for a in b
;
# next line

switch
when b
# preceding
;

switch
when b
; # trailing

switch
when b
;
# next line

0 comments on commit ab59b56

Please sign in to comment.