diff --git a/src/comments.ts b/src/comments.ts index 33f359e..a95c970 100644 --- a/src/comments.ts +++ b/src/comments.ts @@ -375,7 +375,7 @@ function handleIfStatementsWithNoBodyComments(precedingNode: luaparse.Node, return true; } - if (precedingNode && precedingNode.type === 'ElseClause') { + if (precedingNode && (precedingNode.type === 'ElseClause' || precedingNode.type === 'IfClause')) { addDanglingComment(precedingNode, comment); return true; } diff --git a/src/docPrinter.ts b/src/docPrinter.ts index 624616e..76c375b 100644 --- a/src/docPrinter.ts +++ b/src/docPrinter.ts @@ -142,12 +142,10 @@ function printDocToStringWithState(doc: Doc, state: State) { break; case 'indent': - { - state.indentation++; - printDocToStringWithState(doc.content, state); - state.indentation--; - break; - } + state.indentation++; + printDocToStringWithState(doc.content, state); + state.indentation--; + break; case 'lineSuffix': state.lineSuffixes.push(doc); diff --git a/test/comments/__snapshots__/comments.test.ts.snap b/test/comments/__snapshots__/comments.test.ts.snap index 8597bb2..ec07750 100644 --- a/test/comments/__snapshots__/comments.test.ts.snap +++ b/test/comments/__snapshots__/comments.test.ts.snap @@ -101,6 +101,10 @@ else -- dangling ElseClause -- body ElseClause end -- dangling End IfStatement -- trailing IfStatement + +if 1 then + -- body IfClause +end " `; diff --git a/test/comments/if.lua b/test/comments/if.lua index 71068ca..f400b64 100644 --- a/test/comments/if.lua +++ b/test/comments/if.lua @@ -21,3 +21,7 @@ else -- dangling ElseClause -- body ElseClause end -- dangling End IfStatement -- trailing IfStatement + +if 1 then + -- body IfClause +end