mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-31 12:40:24 +00:00
parent
aaf1e2dea8
commit
1bcff422db
6 changed files with 93 additions and 11 deletions
|
|
@ -19,7 +19,7 @@ Linter Usage
|
|||
djlint src -e html.dj
|
||||
|
||||
Formatter Usage
|
||||
-----------------
|
||||
---------------
|
||||
|
||||
Foramtting is a beta tool. ``--check`` the output before applying changes.
|
||||
|
||||
|
|
@ -37,6 +37,17 @@ To format code run:
|
|||
|
||||
djlint . --reformat --indent=3
|
||||
|
||||
Ignoring Code
|
||||
-------------
|
||||
|
||||
Code can be skipped by the linter and formatter by wrapping in djlint tags:
|
||||
|
||||
.. code:: html
|
||||
|
||||
{% djlint:off %}
|
||||
<bad html to ignore>
|
||||
{% djlint:on %}
|
||||
|
||||
Stdin vs Path
|
||||
-------------
|
||||
|
||||
|
|
|
|||
|
|
@ -26,9 +26,8 @@ def _should_ignore(config: Config, html: str, match: re.Match) -> bool:
|
|||
return any(
|
||||
ignored_match.start() < match.start(1) and ignored_match.end() > match.end(1)
|
||||
for ignored_match in re.finditer(
|
||||
config.ignored_blocks,
|
||||
re.compile(config.ignored_blocks, re.DOTALL | re.IGNORECASE | re.VERBOSE),
|
||||
html,
|
||||
re.DOTALL | re.IGNORECASE | re.VERBOSE,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,18 @@ def get_line(start: int, line_ends: List) -> str:
|
|||
return "%d:%d" % (line_ends.index(line) + 1, start - line["start"])
|
||||
|
||||
|
||||
# filter out matches that are inside ignored blocks
|
||||
def _should_ignore(config: Config, html: str, match: re.Match) -> bool:
|
||||
"""Do not add whitespace if the tag is in a non indent block."""
|
||||
return any(
|
||||
ignored_match.start() < match.start() and ignored_match.end() > match.end()
|
||||
for ignored_match in re.finditer(
|
||||
re.compile(config.ignored_blocks, re.DOTALL | re.IGNORECASE | re.VERBOSE),
|
||||
html,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def lint_file(config: Config, this_file: Path) -> Dict:
|
||||
"""Check file for formatting errors."""
|
||||
file_name = str(this_file)
|
||||
|
|
@ -73,14 +85,16 @@ def lint_file(config: Config, this_file: Path) -> Dict:
|
|||
re.compile(pattern, flags=build_flags(rule.get("flags", "re.DOTALL"))),
|
||||
html,
|
||||
):
|
||||
errors[file_name].append(
|
||||
{
|
||||
"code": rule["name"],
|
||||
"line": get_line(match.start(), line_ends),
|
||||
"match": match.group().strip()[:20],
|
||||
"message": rule["message"],
|
||||
}
|
||||
)
|
||||
|
||||
if _should_ignore(config, html, match) is False:
|
||||
errors[file_name].append(
|
||||
{
|
||||
"code": rule["name"],
|
||||
"line": get_line(match.start(), line_ends),
|
||||
"match": match.group().strip()[:20],
|
||||
"message": rule["message"],
|
||||
}
|
||||
)
|
||||
|
||||
# remove duplicate matches
|
||||
for file_name, error_dict in errors.items():
|
||||
|
|
|
|||
|
|
@ -147,6 +147,7 @@ class Config:
|
|||
| [^\{]{\#
|
||||
| <pre
|
||||
| <textarea
|
||||
| {%[ ]djlint:off[ ]%}
|
||||
"""
|
||||
|
||||
self.ignored_group_closing: str = r"""
|
||||
|
|
@ -154,6 +155,7 @@ class Config:
|
|||
| \#}
|
||||
| </pre
|
||||
| </textarea
|
||||
| {%[ ]djlint:on[ ]%}
|
||||
"""
|
||||
|
||||
# the contents of these tag blocks will be indented, then unindented
|
||||
|
|
@ -402,6 +404,7 @@ class Config:
|
|||
|
||||
self.ignored_blocks: str = r"""
|
||||
<(script|style|pre|textarea).*?</(\1)>
|
||||
| {%[ ]djlint:off[ ]%}.*?{%[ ]djlint:on[ ]%}
|
||||
| <!--.*?-->
|
||||
| {\*.*?\*}
|
||||
| {\#.*?\#}
|
||||
|
|
|
|||
|
|
@ -240,5 +240,45 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
</p>
|
||||
</div>
|
||||
<!-- <div> -->
|
||||
"""
|
||||
)
|
||||
|
||||
# check custom ignore tag {# djlint:off #} {# djlint:on #}
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""{% djlint:off %}
|
||||
<div><p><span></span></p></div>
|
||||
{% djlint:on %}
|
||||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== """{% djlint:off %}
|
||||
<div><p><span></span></p></div>
|
||||
{% djlint:on %}
|
||||
"""
|
||||
)
|
||||
|
||||
# check script tag
|
||||
output = reformat(
|
||||
tmp_file,
|
||||
runner,
|
||||
b"""<script>
|
||||
<div><p><span></span></p></div>
|
||||
</script>
|
||||
""",
|
||||
)
|
||||
|
||||
assert output["exit_code"] == 0
|
||||
|
||||
assert (
|
||||
output["text"]
|
||||
== """<script>
|
||||
<div><p><span></span></p></div>
|
||||
</script>
|
||||
"""
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@ run::
|
|||
pytest tests/test_linter.py --cov=src/djlint --cov-branch \
|
||||
--cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
# for a single test
|
||||
|
||||
pytest tests/test_linter.py::test_rules_not_matched_in_ignored_block --cov=src/djlint --cov-branch \
|
||||
--cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
"""
|
||||
# pylint: disable=C0116,C0103
|
||||
|
||||
|
|
@ -187,3 +192,13 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
assert result.exit_code == 1
|
||||
assert "D018 1:" in result.output
|
||||
assert "J018 1:" in result.output
|
||||
|
||||
|
||||
def test_rules_not_matched_in_ignored_block(
|
||||
runner: CliRunner, tmp_file: TextIO
|
||||
) -> None:
|
||||
write_to_file(tmp_file.name, b"<script><div class=test></script>")
|
||||
result = runner.invoke(djlint, [tmp_file.name])
|
||||
print(result.output)
|
||||
assert result.exit_code == 0
|
||||
assert "H011 1:" not in result.output
|
||||
|
|
|
|||
Loading…
Reference in a new issue