Skip to content

Commit

Permalink
formatter: reduce indentation test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Mar 9, 2024
1 parent b536b75 commit 4023d8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/clojurefmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down

0 comments on commit 4023d8b

Please sign in to comment.