Skip to content

https://github.com/jackdewinter/pymarkdown/issues/1274 - #1278

Merged
jackdewinter merged 10 commits into
mainfrom
issue-810
Dec 7, 2024
Merged

jackdewinter merged 10 commits into
mainfrom
issue-810

Conversation

@jackdewinter

@jackdewinter jackdewinter commented Dec 7, 2024 •

Copy link
Copy Markdown
Owner

Work to eliminate asserts, leaving Fixes for Md031 that produce the wrong Markdown.

Summary by Sourcery

Fix issues with Md031 rule handling of fenced code blocks and resolve remaining assert issues. Enhance inline processing logic for better handling of leading spaces and container tokens. Add new test cases to improve coverage and ensure correct behavior.

Bug Fixes:

  • Fix issues with Md031 rule that incorrectly handled fenced code blocks, ensuring they are surrounded by blank lines as intended.
  • Resolve remaining assert issues in the code, ensuring valid Markdown is produced.

Enhancements:

  • Refactor inline processing to improve handling of leading spaces and container tokens, enhancing the parsing logic for Markdown tokens.

Tests:

  • Add new test cases to cover various scenarios involving nested lists and block quotes, improving test coverage and ensuring correct behavior.

@sourcery-ai

sourcery-ai Bot commented Dec 7, 2024 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide by Sourcery

The changes focus on fixing issues with fenced code blocks (FCB) in Markdown parsing, particularly around indentation and column position reporting. The main changes involve correcting text token positions within code blocks and removing assert statements while preserving functionality that produces valid Markdown.

Class diagram for LeadingSpaceIndexTracker and MarkdownToken changes

classDiagram
    class LeadingSpaceIndexTracker {
        +open_container(token: MarkdownToken)
        +track_since_last_non_end_token(token: MarkdownToken)
        +have_any_registered_container_ends() bool
        +process_container_end(token: MarkdownToken)
        +get_tokens_block_quote_bleading_space_index(text_token: TextMarkdownToken) int
        +get_tokens_list_leading_space_index(text_token: TextMarkdownToken) int
    }

    class MarkdownToken {
        +is_block_quote_start bool
        +is_list_start bool
        +is_block_quote_end bool
        +is_list_end bool
        +is_end_token bool
        +is_end_of_stream bool
    }

    LeadingSpaceIndexTracker --> MarkdownToken : uses
    note for LeadingSpaceIndexTracker "Handles tracking of leading spaces in Markdown tokens"
    note for MarkdownToken "Represents a token in Markdown parsing"
Loading

Class diagram for FencedCodeBlockMarkdownToken changes

classDiagram
    class FencedCodeBlockMarkdownToken {
        +fence_character char
        +fence_count int
        +extracted_text string
        +pre_extracted_text string
        +text_after_extracted_text string
        +pre_text_after_extracted_text string
        +extracted_whitespace string
        +extracted_whitespace_before_info_string string
        +position_marker PositionMarker
        +is_fenced_code_block bool
    }

    class PositionMarker {
        +line_number int
        +index_number int
        +text_to_parse string
        +index_indent int
    }

    FencedCodeBlockMarkdownToken --> PositionMarker : uses
    note for FencedCodeBlockMarkdownToken "Represents a fenced code block in Markdown"
    note for PositionMarker "Tracks position information for tokens"
Loading

File-Level Changes

Change Details Files
Fixed text token position calculations for fenced code blocks
  • Corrected column number calculations for text tokens within fenced code blocks
  • Added handling for container-specific indentation adjustments
  • Implemented position marker adjustments for fenced code block tokens
pymarkdown/inline/inline_processor.py
pymarkdown/leaf_blocks/fenced_leaf_block_processor.py
Refactored container transformation logic
  • Split complex container transformation logic into smaller functions
  • Added handling for remaining tokens in container transformations
  • Fixed prefix calculations for container transformations
pymarkdown/transform_markdown/transform_containers.py
Removed assert statements while preserving functionality
  • Removed assert statements that were blocking valid Markdown generation
  • Added proper condition checks to replace assertions
  • Updated test cases to reflect removed assertions
pymarkdown/plugins/rule_md_031.py
test/rules/test_md031.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jackdewinter - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread newdocs/src/changelog.md
- Fixed issue with Md027 not reporting line numbers properly within
anything except the first paragraph.
- [Issue 1272](https://github.com/jackdewinter/pymarkdown/issues/1272)
- Parsing of the FCB in certain cases was off, as was the text token

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider expanding the abbreviation 'FCB' for clarity

While experienced developers might be familiar with this abbreviation, expanding it on first use would improve documentation clarity for all readers.

Suggested change
- Parsing of the FCB in certain cases was off, as was the text token
- Parsing of the Fenced Code Block (FCB) in certain cases was off, as was the text token

# pylint: enable=too-many-arguments, too-many-boolean-expressions

@staticmethod
def __abcd_remaining(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the whitespace handling logic into smaller, well-named methods with clear responsibilities

The __abcd_remaining method introduces unnecessary complexity. Consider refactoring to:

  1. Rename methods to describe their purpose:
def __adjust_container_leading_spaces(self, ...) -> Optional[str]:  # instead of __abcd
def __process_remaining_whitespace(self, ...) -> Optional[str]:     # instead of __abcd_remaining
  1. Extract whitespace handling logic:
def __get_token_leading_spaces(token: MarkdownToken) -> Tuple[List[str], int]:
    if token.is_block_quote_start:
        bq_token = cast(BlockQuoteMarkdownToken, token)
        spaces = bq_token.bleading_spaces.split("\n")
        return spaces, len(spaces) if bq_token.weird_kludge_five else 0
    else:
        list_token = cast(ListStartMarkdownToken, token)
        spaces = list_token.leading_spaces.split("\n")
        return spaces, len(spaces)

def __process_remaining_whitespace(self, removed_tokens: List[MarkdownToken],
                                 removed_indices: List[int],
                                 container_line: str,
                                 prefix: str) -> str:
    while removed_tokens:
        spaces, space_count = self.__get_token_leading_spaces(removed_tokens[0])
        if not self.__validate_token_spacing(spaces, space_count, removed_indices[0]):
            break
        removed_tokens.pop(0)
        removed_indices.pop(0)

    return "" if container_line.startswith(prefix) else prefix

This improves readability while maintaining the same functionality and edge case handling.

@codecov

codecov Bot commented Dec 7, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (6c8bf89) to head (7df8eda).
Report is 28 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1278   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          191       191           
  Lines        21362     21390   +28     
  Branches      2744      2751    +7     
=========================================
+ Hits         21362     21390   +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jackdewinter
jackdewinter merged commit 72523ad into main Dec 7, 2024
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.

1 participant