CHANGE @W-21310587@ - Enhance NoTrailingWhitespace rule to detect multiple consecutive empty lines#419
Merged
aruntyagiTutu merged 6 commits intodevfrom Feb 20, 2026
Conversation
## 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.
…trailing_whitespace_rule
dee8afb to
e12934d
Compare
- 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
nikhil-mittal-165
approved these changes
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enhanced the
NoTrailingWhitespaceregex rule to comprehensively detectwhitespace 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:
Example: