Skip to content

Commit e85963e

Browse files
committed
Fix regex to detect empty lines containing only whitespace
Addressed review feedback: The previous regex only detected consecutive newlines that were immediately adjacent (\n\n\n). It failed to detect multiple consecutive empty lines when those lines contained only spaces or tabs (\n \n \n). Updated regex to treat [ \t]*\r?\n (optional whitespace + newline) as an empty line, rather than just \r?\n. This ensures that lines containing only whitespace are properly treated as empty when checking for multiple consecutive empty lines. Changes: - Updated regex pattern: (?<=([ \t]*\r?\n)([ \t]*\r?\n))[ \t]*\r?\n - Added test file EmptyLinesWithWhitespace.cls with spaces on empty lines - Added specific test case for whitespace-only empty lines - Updated existing tests to include new violation
1 parent a848e74 commit e85963e

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

packages/code-analyzer-regex-engine/src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function validateEngineName(engineName: string) {
7272
export function createBaseRegexRules(now: Date): RegexRules {
7373
return {
7474
NoTrailingWhitespace: {
75-
regex: (/(?<target>((?<=\S.*)[ \t]+(?=\r?\n|$))|((?<=\r?\n\r?\n)\r?\n))/g).toString(),
75+
regex: (/(?<target>((?<=\S.*)[ \t]+(?=\r?\n|$))|((?<=([ \t]*\r?\n)([ \t]*\r?\n))[ \t]*\r?\n))/g).toString(),
7676
file_extensions: ['.cls', '.trigger'], // Currently restricted to apex files only... but we might want to extend where this rule applies in the future
7777
description: getMessage('TrailingWhitespaceRuleDescription'),
7878
violation_message: getMessage('TrailingWhitespaceRuleMessage'),

packages/code-analyzer-regex-engine/test/engine.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ describe('Tests for runRules', () => {
292292
endColumn: 1
293293
}]
294294
},
295+
{
296+
ruleName: "NoTrailingWhitespace",
297+
message: getMessage('TrailingWhitespaceRuleMessage'),
298+
primaryLocationIndex: 0,
299+
codeLocations: [{
300+
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
301+
startLine: 6,
302+
startColumn: 1,
303+
endLine: 7,
304+
endColumn: 1
305+
}]
306+
},
295307

296308
];
297309

@@ -330,6 +342,18 @@ describe('Tests for runRules', () => {
330342
endLine: 8,
331343
endColumn: 1
332344
}]
345+
},
346+
{
347+
ruleName: "NoTrailingWhitespace",
348+
message: getMessage('TrailingWhitespaceRuleMessage'),
349+
primaryLocationIndex: 0,
350+
codeLocations: [{
351+
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
352+
startLine: 6,
353+
startColumn: 1,
354+
endLine: 7,
355+
endColumn: 1
356+
}]
333357
}
334358
];
335359

@@ -347,6 +371,32 @@ describe('Tests for runRules', () => {
347371
expect(runResults.violations).toHaveLength(0);
348372
});
349373

374+
it("NoTrailingWhitespace rule should flag multiple empty lines that contain only whitespace", async () => {
375+
const runOptions: RunOptions = createRunOptions(
376+
new Workspace('id', [path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls")]));
377+
const runResults: EngineRunResults = await engine.runRules(["NoTrailingWhitespace"], runOptions);
378+
379+
const expectedViolations: Violation[] = [
380+
{
381+
ruleName: "NoTrailingWhitespace",
382+
message: getMessage('TrailingWhitespaceRuleMessage'),
383+
primaryLocationIndex: 0,
384+
codeLocations: [{
385+
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
386+
startLine: 6,
387+
startColumn: 1,
388+
endLine: 7,
389+
endColumn: 1
390+
}]
391+
}
392+
];
393+
394+
expect(runResults.violations).toHaveLength(expectedViolations.length);
395+
for (const expectedViolation of expectedViolations) {
396+
expect(runResults.violations).toContainEqual(expectedViolation);
397+
}
398+
});
399+
350400
it("Ensure runRules when called on a directory of Apex classes with getHeapSize in a loop, it properly emits violations", async () => {
351401
const runOptions: RunOptions = createRunOptions(
352402
new Workspace('id', [path.resolve(__dirname, "test-data", "apexClassGetLimitsInLoop")]));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class EmptyLinesWithWhitespace {
2+
public void method1() {
3+
System.debug('test1');
4+
}
5+
6+
7+
public void method2() {
8+
System.debug('test2');
9+
}
10+
}

0 commit comments

Comments
 (0)