Skip to content

CHANGE @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines#419

Merged
aruntyagiTutu merged 6 commits intodevfrom
arun.tyagi/enhance_no_trailing_whitespace_rule
Feb 20, 2026
Merged

CHANGE @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines#419
aruntyagiTutu merged 6 commits intodevfrom
arun.tyagi/enhance_no_trailing_whitespace_rule

Conversation

@aruntyagiTutu
Copy link
Contributor

@aruntyagiTutu aruntyagiTutu commented Feb 19, 2026

Summary

Enhanced the NoTrailingWhitespace regex rule to comprehensively detect
whitespace violations while eliminating false positives. The rule now
catches trailing whitespace after code content and multiple consecutive
empty lines, while correctly allowing single empty lines between code
sections.

What This Rule Now Catches (Violations)

1. Trailing whitespace after code content

Spaces or tabs at the end of lines that contain actual code.

Output for below code:

image

Example:

public void method() {    // ← violation: trailing spaces after '{'

2. Multiple consecutive empty lines

Two or more empty lines in a row anywhere in the file.

Example:
public void method1() {
    System.debug('test');
}


public void method2() {  // ← violation on the extra empty line above
    System.debug('test2');
}

3. Empty lines at end of file

Multiple newlines at EOF are caught as consecutive empty lines.

Example:
public class TestClass {
    // ... code ...
}


// ← violations on the extra empty lines above EOF

What This Rule Ignores (No Violations)

1. Single empty lines between code sections

One blank line separating methods, classes, or code blocks is allowed and
encouraged.

Example:
public void method1() {
    System.debug('test');
}

public void method2() {  // ← NO violation, single empty line is OK
    System.debug('test2');
}

2. Lines with only whitespace (when singular)

Empty lines that happen to contain spaces/tabs are treated as empty lines.
No false positives on whitespace-only lines between code sections. These are
 only flagged if they create multiple consecutive empty lines.

3. Properly formatted code

- Lines ending immediately at the last code character (no trailing
whitespace)
- Files ending with a single newline character



Technical Implementation

The regex pattern uses two alternation branches:
1. (?<=\S.*)(?<target>[ \t]+)(?=\r?\n|$) - Matches trailing spaces/tabs
after non-whitespace content
2. (?<=\r?\n\r?\n)(?<target>\r?\n) - Matches each newline appearing after
two consecutive newlines (i.e., extra empty lines)

This ensures precise violation boundaries and eliminates the false positives
 that occurred with the previous implementation where lines containing only
whitespace were incorrectly flagged.

## Changes

### code-analyzer-core (0.43.0-SNAPSHOT)
Updated packages with ^ symbol:
- semver: ^7.7.3 → ^7.7.4
- rimraf: ^6.1.2 → ^6.1.3
- typescript-eslint: ^8.53.0 → ^8.56.0

**Constraints followed:**
- ✅ Kept @types/node at ^20.0.0 (Node 20 is minimum customer version)
- ✅ Kept eslint at ^9.39.2 (v10 has breaking changes)
- ✅ Kept @eslint/js at ^9.39.2 (v10 has breaking changes)
- ✅ No version bump (already using 0.43.0-SNAPSHOT suffix)

## Testing
- ✅ Build successful
- ✅ Tests: 282 passed
- ✅ No breaking changes introduced

## Context
This update was done after merging all engine dependency updates:
- PMD engine upgrade (7.20.0 → 7.21.0)
- ESLint engines upgrade
- All other engines (engine-api, flow, regex, retirejs, sfge)

All engine references remain compatible with their updated versions.
## Issue
The NoTrailingWhitespace regex rule was flagging empty lines that contain
only whitespace (indentation). This created false positives when developers
use empty lines with indentation to separate code sections.

Example of false positive:
```apex
public void execute(SchedulableContext ctx) {

    Messaging.reserveSingleEmailCapacity(1);

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    List<Messaging.SendEmailResult> results = Messaging.sendEmail(...);
}
```

The empty lines with indentation (lines 3, 5, 7) were being flagged as
violations, even though they improve readability and are not counted
towards code character limits.

## Solution
Updated the regex pattern from:
```typescript
/(?<target>[ \t]+)((\r?\n)|$)/g
```

To:
```typescript
/(?<=\S.*)(?<target>[ \t]+)((\r?\n)|$)/g
```

The new pattern uses a positive lookbehind `(?<=\S.*)` to ensure there's
at least one non-whitespace character earlier on the line before matching
trailing whitespace.

## Behavior After Fix
- ✅ Still flags: `code content here    ` (trailing whitespace after content)
- ❌ No longer flags: `    ` (line with only whitespace/indentation)

## Testing
- ✅ All 65 existing tests pass
- ✅ Existing test cases still correctly flag legitimate trailing whitespace
  violations after actual code content
…y lines

Updated regex to detect both trailing whitespace after code and multiple
consecutive empty lines, while allowing single empty lines between code
sections. The rule now comprehensively handles all whitespace violations
without false positives on lines containing only whitespace.
@aruntyagiTutu aruntyagiTutu changed the title NEW @W-21310587 - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines NEW @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines Feb 19, 2026
@aruntyagiTutu aruntyagiTutu force-pushed the arun.tyagi/enhance_no_trailing_whitespace_rule branch from dee8afb to e12934d Compare February 19, 2026 09:45
- Added test data files with multiple consecutive empty lines
- Added test data file with empty lines at EOF
- Added test data file with single empty lines (valid case)
- Added test cases to verify multiple empty lines are flagged
- Added test case to verify single empty lines are allowed
- Updated existing test to include new violations from test data
@aruntyagiTutu aruntyagiTutu changed the title NEW @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines CHANGE @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines Feb 19, 2026
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
@aruntyagiTutu aruntyagiTutu merged commit 25d6644 into dev Feb 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants