Skip to content

Commit

Permalink
Issue 990 (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdewinter authored Feb 17, 2024
1 parent c130f98 commit 17ba688
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 60 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- [Issue 994](https://github.com/jackdewinter/pymarkdown/issues/994)
- Verified behavior of Rule Md029, adding configuration for starting ordered
lists from integers greater than 1
- [Issue 1001](https://github.com/jackdewinter/pymarkdown/issues/1001)
- Verified behavior of rules Md019 and Md021, fixing issues with Md021

### Changed

Expand Down
8 changes: 4 additions & 4 deletions publish/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"projectName": "pymarkdown",
"reportSource": "pytest",
"branchLevel": {
"totalMeasured": 4823,
"totalCovered": 4823
"totalMeasured": 4831,
"totalCovered": 4831
},
"lineLevel": {
"totalMeasured": 19417,
"totalCovered": 19417
"totalMeasured": 19442,
"totalCovered": 19442
}
}

6 changes: 3 additions & 3 deletions publish/test-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@
},
{
"name": "test.rules.test_md019",
"totalTests": 21,
"totalTests": 47,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand All @@ -1236,7 +1236,7 @@
},
{
"name": "test.rules.test_md021",
"totalTests": 20,
"totalTests": 53,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand Down Expand Up @@ -1604,7 +1604,7 @@
},
{
"name": "test.test_markdown_extra",
"totalTests": 114,
"totalTests": 115,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand Down
27 changes: 14 additions & 13 deletions pymarkdown/plugins/rule_md_019.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ def next_token(self, context: PluginScanContext, token: MarkdownToken) -> None:
self.__atx_heading_token = None
elif token.is_text:
text_token = cast(TextMarkdownToken, token)
resolved_extracted_whitespace = ParserHelper.remove_all_from_text(
text_token.extracted_whitespace
)
if self.__atx_heading_token and "\t" in resolved_extracted_whitespace:
start_index = (
self.__atx_heading_token.column_number
- 1
+ self.__atx_heading_token.hash_count
)
resolved_extracted_whitespace = TabHelper.detabify_string(
resolved_extracted_whitespace, start_index
if self.__atx_heading_token:
resolved_extracted_whitespace = ParserHelper.remove_all_from_text(
text_token.extracted_whitespace
)
if self.__atx_heading_token and len(resolved_extracted_whitespace) > 1:
self.__report(context, text_token)
if "\t" in resolved_extracted_whitespace:
start_index = (
self.__atx_heading_token.column_number
- 1
+ self.__atx_heading_token.hash_count
)
resolved_extracted_whitespace = TabHelper.detabify_string(
resolved_extracted_whitespace, start_index
)
if len(resolved_extracted_whitespace) > 1:
self.__report(context, text_token)
self.__atx_heading_token = None
69 changes: 52 additions & 17 deletions pymarkdown/plugins/rule_md_021.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from typing import Optional, cast

from pymarkdown.general.parser_helper import ParserHelper
from pymarkdown.general.tab_helper import TabHelper
from pymarkdown.plugin_manager.plugin_details import PluginDetailsV2
from pymarkdown.plugin_manager.plugin_scan_context import PluginScanContext
from pymarkdown.plugin_manager.rule_plugin import RulePlugin
Expand All @@ -21,9 +23,10 @@ class RuleMd021(RulePlugin):

def __init__(self) -> None:
super().__init__()
self.__atx_heading_token: Optional[MarkdownToken] = None
self.__atx_heading_token: Optional[AtxHeadingMarkdownToken] = None
self.__is_left_in_error = False
self.__first_text_token: Optional[TextMarkdownToken] = None
self.__last_token: Optional[TextMarkdownToken] = None

def get_details(self) -> PluginDetailsV2:
"""
Expand All @@ -45,13 +48,11 @@ def starting_new_file(self) -> None:
"""
self.__atx_heading_token = None
self.__first_text_token = None
self.__last_token = None
self.__is_left_in_error = False

def __report(
self,
context: PluginScanContext,
token: MarkdownToken,
end_token: EndMarkdownToken,
self, context: PluginScanContext, token: MarkdownToken, extra_end_data: str
) -> None:
assert self.__atx_heading_token is not None
if context.in_fix_mode:
Expand All @@ -64,10 +65,7 @@ def __report(
"extracted_whitespace",
" ",
)
if (
end_token.extra_end_data is not None
and len(end_token.extra_end_data) > 1
):
if len(extra_end_data) > 1:
self.register_fix_token_request(
context,
token,
Expand All @@ -78,24 +76,61 @@ def __report(
else:
self.report_next_token_error(context, self.__atx_heading_token)

def __handle_atx_end(
self, context: PluginScanContext, token: MarkdownToken
) -> None:
end_token = cast(EndMarkdownToken, token)
assert end_token.extra_end_data is not None
extra_end_data = end_token.extra_end_data
if "\t" in extra_end_data:
assert self.__last_token is not None
assert self.__last_token.is_text
resolved_token_text = ParserHelper.remove_all_from_text(
self.__last_token.token_text
)
start_index = self.__last_token.column_number - 1
resolved_token_text = TabHelper.detabify_string(
resolved_token_text, start_index
)
start_index_after_last_text_token = start_index + len(resolved_token_text)
extra_end_data = TabHelper.detabify_string(
extra_end_data, start_index_after_last_text_token
)
if self.__is_left_in_error or len(extra_end_data) > 1:
self.__report(context, token, extra_end_data)
self.__atx_heading_token = None
self.__first_text_token = None
self.__last_token = None

def next_token(self, context: PluginScanContext, token: MarkdownToken) -> None:
"""
Event that a new token is being processed.
"""
if token.is_atx_heading:
atx_token = cast(AtxHeadingMarkdownToken, token)
if atx_token.remove_trailing_count:
self.__atx_heading_token = token
self.__atx_heading_token = atx_token
self.__is_left_in_error = False
elif token.is_paragraph_end:
self.__atx_heading_token = None
elif token.is_atx_heading_end:
end_token = cast(EndMarkdownToken, token)
assert end_token.extra_end_data is not None
if self.__is_left_in_error or len(end_token.extra_end_data) > 1:
self.__report(context, token, end_token)
self.__handle_atx_end(context, token)
elif token.is_text:
text_token = cast(TextMarkdownToken, token)
self.__first_text_token = text_token
if self.__atx_heading_token and len(text_token.extracted_whitespace) > 1:
self.__is_left_in_error = True
if not self.__first_text_token and self.__atx_heading_token:
resolved_extracted_whitespace = ParserHelper.remove_all_from_text(
text_token.extracted_whitespace
)
if "\t" in resolved_extracted_whitespace:
start_index = (
self.__atx_heading_token.column_number
- 1
+ self.__atx_heading_token.hash_count
)
resolved_extracted_whitespace = TabHelper.detabify_string(
resolved_extracted_whitespace, start_index
)
if len(resolved_extracted_whitespace) > 1:
self.__is_left_in_error = True
self.__first_text_token = text_token
self.__last_token = text_token
10 changes: 7 additions & 3 deletions test/api/test_api_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,24 @@ def test_api_scan_with_multiple_scan_issues():

# Assert
assert scan_result
assert len(scan_result.scan_failures) == 5
assert len(scan_result.scan_failures) == 6
print(scan_result.scan_failures)
assert scan_result.scan_failures[0].partial_equals(
PyMarkdownScanFailure(source_path, 1, 1, "MD022", "", "", None)
)
assert scan_result.scan_failures[1].partial_equals(
PyMarkdownScanFailure(source_path, 1, 12, "MD010", "", "", None)
)
assert scan_result.scan_failures[2].partial_equals(
PyMarkdownScanFailure(source_path, 2, 2, "MD022", "", "", None)
PyMarkdownScanFailure(source_path, 2, 2, "MD021", "", "", None)
)
assert scan_result.scan_failures[3].partial_equals(
PyMarkdownScanFailure(source_path, 2, 2, "MD023", "", "", None)
PyMarkdownScanFailure(source_path, 2, 2, "MD022", "", "", None)
)
assert scan_result.scan_failures[4].partial_equals(
PyMarkdownScanFailure(source_path, 2, 2, "MD023", "", "", None)
)
assert scan_result.scan_failures[5].partial_equals(
PyMarkdownScanFailure(source_path, 2, 14, "MD010", "", "", None)
)

Expand Down
4 changes: 2 additions & 2 deletions test/rules/test_md005.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@
),
pluginRuleTest(
"mix_md005_md007_only_md005_1",
disable_rules="md007",
disable_rules=__plugin_disable_md007,
source_file_contents=""" + first
+ second
+ third
Expand All @@ -803,7 +803,7 @@
),
pluginRuleTest(
"mix_md005_md007_only_md005_2",
disable_rules="md007",
disable_rules=__plugin_disable_md007,
source_file_contents=""" + first
+ second
+ third
Expand Down
6 changes: 3 additions & 3 deletions test/rules/test_md006.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@
pluginRuleTest(
"mix_md006_md004",
is_mix_test=True,
enable_rules="MD006",
disable_rules="MD007",
enable_rules=plugin_enable_this_rule,
disable_rules=__plugin_disable_md007,
source_file_contents=""" + first
* second
- third
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_md006_scan(test: pluginRuleTest) -> None:
"""
Execute a parameterized scan test for plugin md001.
"""
execute_scan_test(test, "md006")
execute_scan_test(test, plugin_enable_this_rule)


@pytest.mark.parametrize("test", fixTests, ids=id_test_plug_rule_fn)
Expand Down
8 changes: 4 additions & 4 deletions test/rules/test_md007.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
),
pluginRuleTest(
"mix_md007_md005_only_md007_1",
disable_rules="md005",
disable_rules=__plugin_disable_md005,
source_file_contents=""" + first
+ second
+ third
Expand All @@ -553,7 +553,7 @@
),
pluginRuleTest(
"mix_md007_md005_only_md007_2",
disable_rules="md005",
disable_rules=__plugin_disable_md005,
source_file_contents=""" + first
+ second
+ third
Expand Down Expand Up @@ -621,7 +621,7 @@
),
pluginRuleTest(
"mix_md007_md030_xx",
disable_rules="MD030",
disable_rules=__plugin_disable_md030,
source_file_contents=""" * First
first paragraph
Expand All @@ -646,7 +646,7 @@
),
pluginRuleTest(
"mix_md007_md030",
disable_rules="md005",
disable_rules=__plugin_disable_md005,
source_file_contents=""" * First
first paragraph
Expand Down
Loading

0 comments on commit 17ba688

Please sign in to comment.