diff --git a/lib/clojurefmt.js b/lib/clojurefmt.js index 520dfc0..392fd8b 100644 --- a/lib/clojurefmt.js +++ b/lib/clojurefmt.js @@ -702,6 +702,7 @@ let idx = 0 let outTxt = '' + let outputTxtContainsChars = false // FIXME: need a running paren stack of openers (who are not closed) // use this on a newline to determine indentation level @@ -715,6 +716,7 @@ const nextTextNode = nextNodeWithText(nodesArr, inc(idx)) const isLastNode = inc(idx) >= numNodes + const currentNodeIsWhitespace = isWhitespaceNode(node) const currentNodeIsNewline = isNewlineNode(node) let skipPrintingThisNode = false @@ -740,7 +742,12 @@ } // remove whitespace before a closer (remove-surrounding-whitespace?) - if (isWhitespaceNode(node) && !isNewlineNode(node) && isParenCloser(nextTextNode)) { + if (currentNodeIsWhitespace && !isNewlineNode(node) && isParenCloser(nextTextNode)) { + skipPrintingThisNode = true + } + + // do not print any initial whitespace nodes + if (currentNodeIsWhitespace && !outputTxtContainsChars) { skipPrintingThisNode = true } @@ -794,6 +801,7 @@ // add the text of this node to the output String if (!skipPrintingThisNode) { outTxt = strConcat(outTxt, node.text) + outputTxtContainsChars = true } if (addSpaceAfterThisNode) { diff --git a/test/format.test.js b/test/format.test.js index 171b38f..70ca282 100644 --- a/test/format.test.js +++ b/test/format.test.js @@ -66,20 +66,20 @@ test('All test_format/ cases should have unique names', () => { expect(uniqueTestCaseNames.size).toBe(allTestCases.length) }) +// dev convenience: set this to true and add specific test cases +// only those cases will run const onlyRunCertainTests = false const certainTests = new Set() -// certainTests.add('Simple Indentation') -certainTests.add('Reader conditional splicing syntax') +certainTests.add('Rule 3 Indentation') const ignoreSomeTests = true const ignoreTests = new Set() -ignoreTests.add('Reduce Indentation') ignoreTests.add('Rule 3 Indentation') allTestCases.forEach(testCase => { let runThisTest = true if (onlyRunCertainTests && !certainTests.has(testCase.name)) runThisTest = false - if (ignoreSomeTests && ignoreTests.has(testCase.name)) runThisTest = false + else if (ignoreSomeTests && ignoreTests.has(testCase.name)) runThisTest = false if (runThisTest) { test(testCase.filename + ': ' + testCase.name, () => {