Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,21 @@ function moveOpeningBracket(allLines, variables) {
if (allLines[realPos - 1].trim().replace(/ /g, "").includes("){")) {
throw new Error("Fix not applied: probably a CodeNarc false positive");
}

// Check if the line contains a comment and insert bracket before it
const targetLine = allLines[realPos - 2];
const commentIndex = targetLine.indexOf("//");
let addedBracketLine;

if (commentIndex !== -1) {
// Insert bracket before the comment
const beforeComment = targetLine.substring(0, commentIndex).trimEnd();
const comment = targetLine.substring(commentIndex);
addedBracketLine = beforeComment + " { " + comment;
} else {
// Add bracket after if
const addedBracketLine = allLines[realPos - 2].trimEnd() + " {";
addedBracketLine = targetLine.trimEnd() + " {";
}
allLines[realPos - 2] = addedBracketLine;
// Remove bracket which was on the wrong line
const removedBracketLine = allLines[realPos - 1].substring(allLines[realPos - 1].indexOf("{") + 1).trimEnd();
Expand Down
22 changes: 20 additions & 2 deletions test/lint-format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ async function checkRule(key, check, checkType) {
const result = linter.lintResult.files[0].updatedSource;
const expectedResult = normalizeNewLines(check.after);
const effectiveDiff = getDiff(expectedResult, result, source);
assert(linter.status === 0, `Linter status is 0 (${linter.status} returned)`);
assert(effectiveDiff.length === 0, "Code has been formatted correctly");
assert(linter.status === 0, `Expected linter status to be 0, but status was ${linter.status}`);
assert(effectiveDiff.length === 0, "Code was not formatted correctly");
checkCodeNarcCallsCounter(check.codeNarcCallsCounter);
}

Expand Down Expand Up @@ -289,6 +289,24 @@ pipeline {
}
` */
}
],
[
"MoveOpeningCurlyBracketShouldNotCommentOutBracket",
{
totalFixed: 1,
codeNarcCallsCounter: 1,
before: `
void foo() // comment
{
doSomething()
}
`,
after: `
void foo() { // comment
doSomething()
}
`,
}
]
]);
}
Loading