Repository navigation
Issue 810 - #1260
Issue 810#1260
Conversation
Reviewer's Guide by SourceryThis pull request implements a fix for Rule MD012 (no-multiple-blanks) and fixes issues with container handling in blank lines. The changes primarily focus on improving the handling of consecutive blank lines in Markdown documents and fixing related container parsing issues. Sequence diagram for handling excess blank linessequenceDiagram
participant RuleMd012
participant PluginScanContext
participant MarkdownToken
participant LeadingSpaceIndexTracker
Note over RuleMd012: Start processing a new file
RuleMd012->>RuleMd012: starting_new_file()
Note over RuleMd012: Process each token
RuleMd012->>MarkdownToken: next_token(context, token)
alt token is blank line
RuleMd012->>RuleMd012: __check_for_excess_blank_lines(context)
RuleMd012->>RuleMd012: __captured_blank_line.append(token)
else token is not blank line
RuleMd012->>RuleMd012: __check_for_excess_blank_lines(context)
end
alt token is block quote or list end
RuleMd012->>RuleMd012: __process_pending_container_end_fixes(context, token)
end
RuleMd012->>LeadingSpaceIndexTracker: track_since_last_non_end_token(token)
Class diagram for RuleMd012 and related classesclassDiagram
class RuleMd012 {
- int __blank_lines_maximum
- int __blank_line_count
- MarkdownToken __last_blank_line
- List<MarkdownToken> __captured_blank_line
- LeadingSpaceIndexTracker __leading_space_index_tracker
- Dict<MarkdownToken, List<PendingContainerFixes>> __container_fix_map
+ void starting_new_file()
+ void __check_for_excess_blank_lines(PluginScanContext context)
+ void __process_pending_container_end_fixes(PluginScanContext context, MarkdownToken token)
+ void completed_file(PluginScanContext context)
+ void next_token(PluginScanContext context, MarkdownToken token)
}
class PendingContainerFixes {
int leading_space_index
int repeat_count
}
class LeadingSpaceIndexTracker {
+ void clear()
+ void open_container(MarkdownToken token)
+ void register_container_end(MarkdownToken token)
+ void track_since_last_non_end_token(MarkdownToken token)
+ int get_container_stack_size()
+ MarkdownToken get_container_stack_item(int index)
+ int get_tokens_block_quote_bleading_space_index(MarkdownToken token, int alternate_index)
+ int get_tokens_list_leading_space_index(MarkdownToken token, int alternate_index)
}
RuleMd012 --> LeadingSpaceIndexTracker
RuleMd012 --> PendingContainerFixes
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @jackdewinter - I've reviewed your changes - here's some feedback:
Overall Comments:
- Please add more descriptive PR description explaining the changes made rather than just referencing issue numbers.
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| self.__container_fix_map[key] = value_list | ||
| value_list.append(value) | ||
|
|
||
| def __fix_containers(self) -> None: |
There was a problem hiding this comment.
suggestion: Consider extracting duplicate container handling logic into helper methods
The block quote and list handling branches share very similar structure. Consider extracting the common pattern into helper methods to reduce duplication and improve maintainability.
def __fix_containers(self) -> None:
self.__fix_block_quotes()
self.__fix_lists()
def __fix_block_quotes(self) -> None:
outer_container_index = (
self.__leading_space_index_tracker.get_container_stack_size() - 1| def __handle_block_quote_end_calc( | ||
| actual_tokens: List[MarkdownToken], search_index: int, current_token_index: int | ||
| ) -> Tuple[bool, bool]: | ||
| ) -> bool: |
There was a problem hiding this comment.
suggestion: Consider keeping tuple return type for better self-documentation
The change from returning a tuple to a single boolean loses semantic meaning. Consider keeping the tuple return type with named variables to better document the intent of the return value.
) -> Tuple[bool, bool]: # type: ignore| md009 no-trailing-spaces True True 0.6.0 Yes | ||
| md010 no-hard-tabs True True 0.6.0 Yes | ||
| md011 no-reversed-links True True 0.5.0 No | ||
| md012 no-multiple-blanks True True 0.6.0 No |
There was a problem hiding this comment.
suggestion (testing): Consider adding tests for new fixable rule functionality
The change indicates that rule md012 is now fixable (Yes) and has a version bump. Should there be additional tests added to verify the new fix functionality?
def test_md012_fix():
"""Test that md012 rule correctly fixes multiple blank lines."""
input_text = "Line 1\n\n\n\nLine 2"
expected = "Line 1\n\nLine 2"
result = fix_multiple_blanks(input_text)
assert result == expected| index -= adjust | ||
| return index | ||
|
|
||
| def __calculate_adjust(self, initial_index: int, container_index: int) -> int: |
There was a problem hiding this comment.
issue (complexity): Consider extracting the adjustment logic into a dedicated method with clear intent
The __calculate_adjust method introduces unnecessary complexity with nested conditions and unclear intent. Consider simplifying it to directly express the adjustment logic:
def __should_skip_adjustment(self, initial_index: int, container_index: int) -> bool:
"""Return True if we should skip the standard adjustment (returning 0 instead of 1)"""
last_closed_info = self.get_closed_container_info(-1)
return (
initial_index >= 1
and not container_index
and last_closed_info.adjustment
)
def get_tokens_list_leading_space_index(
self, token: MarkdownToken, alternate_index: int = -1
) -> int:
initial_index = self.get_container_stack_size() - 1
container_index = alternate_index
last_closed_info = self.get_closed_container_info(-1)
assert not last_closed_info.adjustment
# Determine if we need the standard +1 adjustment
adjustment = 0 if self.__should_skip_adjustment(initial_index, container_index) else 1
return (
LeadingSpaceIndexTracker.calculate_token_line_number(token)
- self.get_container_stack_item(initial_index).line_number
- last_closed_info.adjustment
- adjustment
)This refactoring:
- Gives the adjustment logic a clear, descriptive name
- Removes the complex assertion in favor of more explicit logic
- Simplifies the index calculation by clearly separating the adjustment determination
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1260 +/- ##
===========================================
+ Coverage 99.98% 100.00% +0.01%
===========================================
Files 191 191
Lines 21197 21304 +107
Branches 2716 2731 +15
===========================================
+ Hits 21193 21304 +111
+ Misses 1 0 -1
+ Partials 3 0 -3 ☔ View full report in Codecov by Sentry. |
closes #810
closes #1259
Summary by Sourcery
Enhance Rule Md012 to support automatic fixing of multiple consecutive blank lines and add comprehensive test coverage for various Markdown structures.
Bug Fixes:
Enhancements:
Tests: