Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1005 #1006

Merged
merged 6 commits into from
Feb 17, 2024
Merged

Issue 1005 #1006

merged 6 commits into from
Feb 17, 2024

Conversation

jackdewinter
Copy link
Owner

#1005

Closes #1005

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

PR Type: Enhancement

PR Summary: This pull request addresses Issue 1005 by extending the MD039 rule to include link reference definitions in its scope. It updates the rule's logic, documentation, and tests to ensure that spaces inside link text are correctly identified and handled, not only for direct links and images but also for link reference definitions. This enhancement ensures a more comprehensive and consistent application of the MD039 rule across different types of Markdown links.

Decision: Comment

📝 Type: 'Enhancement' - not supported yet.
  • Sourcery currently only approves 'Typo fix' PRs.
✅ Issue addressed: this change correctly addresses the issue or implements the desired feature.
No details provided.
📝 Complexity: the changes are too large or complex for Sourcery to approve.
  • Unsupported files: the diff contains files that Sourcery does not currently support during reviews.

General suggestions:

  • While the implementation and documentation updates are thorough, it might be beneficial to include more context or comments in the code where unique identifiers or properties are used (e.g., link_name_debug). This could improve maintainability and understanding for future contributors.
  • Ensure that all new code paths introduced by this enhancement are covered by the added tests. It's crucial that the tests not only cover typical cases but also edge cases that might arise with link reference definitions.
  • Consider reviewing the naming conventions used in the tests to ensure they are consistent and descriptive. Clear test names can significantly aid in understanding the purpose and scope of each test case.
pymarkdown/plugins/rule_md_039.py: Refactor to reduce duplication and simplify `next_token` by abstracting shared logic into a separate method.

While the added functionality for handling LinkReferenceDefinitionMarkdownToken tokens is a valuable addition, the current implementation introduces some complexity that could be streamlined. Specifically, the logic for stripping whitespace and comparing the stripped value to the original is duplicated, which increases the cognitive load and the risk of future inconsistencies.

A more maintainable approach could involve abstracting the shared logic into a separate method. This would not only reduce duplication but also make the next_token method simpler and more focused. Here's a suggested refactoring:

class RuleMd039(RulePlugin):
    # Other methods...

    def next_token(self, context: PluginScanContext, token: MarkdownToken) -> None:
        if token.is_inline_link or token.is_inline_image or token.is_link_reference_definition:
            self.handle_link_token(context, token)

    def handle_link_token(self, context: PluginScanContext, token: MarkdownToken) -> None:
        link_text = self.get_link_text(token)
        if link_text is not None:
            stripped_text = link_text.strip(Constants.ascii_whitespace)
            if link_text != stripped_text:
                if context.in_fix_mode:
                    self.register_fix_token_request(
                        context, token, "next_token", "text_from_blocks", stripped_text,
                    )
                else:
                    self.report_next_token_error(context, token)

    def get_link_text(self, token: MarkdownToken) -> str:
        if isinstance(token, LinkStartMarkdownToken) or isinstance(token, LinkReferenceDefinitionMarkdownToken):
            return token.link_name_debug  # Assuming link_name_debug exists in both token types
        return None

This refactoring reduces the complexity by consolidating the shared logic, making the code easier to maintain and extend in the future.

pymarkdown/tokens/link_reference_definition_markdown_token.py: Consider refactoring to reduce repetition and enhance maintainability by adopting a more generic approach for field handling.

While reviewing the recent changes, I noticed that the addition of the link_name_debug handling introduces a pattern of repetition similar to the existing blocks for other fields. This approach, although functional, increases the method's complexity and reduces its maintainability, especially as more fields might be added in the future.

To enhance the code's simplicity and maintainability, consider adopting a more generic approach for setting these attributes. Here's a suggested refactor that reduces repetition and makes it easier to manage and extend:

def _modify_token(self, field_name, field_value):
    valid_fields = ["link_title_whitespace", "link_name_debug"]
    if field_name in valid_fields and isinstance(field_value, str):
        setattr(self, f"__{field_name}", field_value)
        extra_data = self.__validate_proper_fields_are_valid(
            self.extracted_whitespace
        )
        super()._set_extra_data(extra_data)
        return True
    return super()._modify_token(field_name, field_value)

This refactor checks if the field_name is within a predefined list of valid fields and dynamically sets the attribute. It simplifies adding new fields by including them in the valid_fields list, adheres to the DRY principle by centralizing the field handling logic, and overall, makes the codebase easier to maintain.

Thanks for using Sourcery. We offer it for free for open source projects and would be very grateful if you could help us grow. If you like it, would you consider sharing Sourcery on your favourite social media? ✨

Share Sourcery

Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

pymarkdown/plugins/rule_md_039.py Show resolved Hide resolved
Copy link

codecov bot commented Feb 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (22e6025) to head (a8afb4c).
Report is 50 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1006   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          189       189           
  Lines        19442     19456   +14     
  Branches      2415      2419    +4     
=========================================
+ Hits         19442     19456   +14     

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

@jackdewinter jackdewinter merged commit 32ca849 into main Feb 17, 2024
15 checks passed
@jackdewinter jackdewinter deleted the issue-1003 branch February 17, 2024 19:37
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.

Verify Md039
1 participant