mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-28 14:18:16 +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
|
djlint src -e html.dj
|
||||||
|
|
||||||
Formatter Usage
|
Formatter Usage
|
||||||
-----------------
|
---------------
|
||||||
|
|
||||||
Foramtting is a beta tool. ``--check`` the output before applying changes.
|
Foramtting is a beta tool. ``--check`` the output before applying changes.
|
||||||
|
|
||||||
|
|
@ -37,6 +37,17 @@ To format code run:
|
||||||
|
|
||||||
djlint . --reformat --indent=3
|
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
|
Stdin vs Path
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,8 @@ def _should_ignore(config: Config, html: str, match: re.Match) -> bool:
|
||||||
return any(
|
return any(
|
||||||
ignored_match.start() < match.start(1) and ignored_match.end() > match.end(1)
|
ignored_match.start() < match.start(1) and ignored_match.end() > match.end(1)
|
||||||
for ignored_match in re.finditer(
|
for ignored_match in re.finditer(
|
||||||
config.ignored_blocks,
|
re.compile(config.ignored_blocks, re.DOTALL | re.IGNORECASE | re.VERBOSE),
|
||||||
html,
|
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"])
|
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:
|
def lint_file(config: Config, this_file: Path) -> Dict:
|
||||||
"""Check file for formatting errors."""
|
"""Check file for formatting errors."""
|
||||||
file_name = str(this_file)
|
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"))),
|
re.compile(pattern, flags=build_flags(rule.get("flags", "re.DOTALL"))),
|
||||||
html,
|
html,
|
||||||
):
|
):
|
||||||
errors[file_name].append(
|
|
||||||
{
|
if _should_ignore(config, html, match) is False:
|
||||||
"code": rule["name"],
|
errors[file_name].append(
|
||||||
"line": get_line(match.start(), line_ends),
|
{
|
||||||
"match": match.group().strip()[:20],
|
"code": rule["name"],
|
||||||
"message": rule["message"],
|
"line": get_line(match.start(), line_ends),
|
||||||
}
|
"match": match.group().strip()[:20],
|
||||||
)
|
"message": rule["message"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# remove duplicate matches
|
# remove duplicate matches
|
||||||
for file_name, error_dict in errors.items():
|
for file_name, error_dict in errors.items():
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,7 @@ class Config:
|
||||||
| [^\{]{\#
|
| [^\{]{\#
|
||||||
| <pre
|
| <pre
|
||||||
| <textarea
|
| <textarea
|
||||||
|
| {%[ ]djlint:off[ ]%}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.ignored_group_closing: str = r"""
|
self.ignored_group_closing: str = r"""
|
||||||
|
|
@ -154,6 +155,7 @@ class Config:
|
||||||
| \#}
|
| \#}
|
||||||
| </pre
|
| </pre
|
||||||
| </textarea
|
| </textarea
|
||||||
|
| {%[ ]djlint:on[ ]%}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# the contents of these tag blocks will be indented, then unindented
|
# the contents of these tag blocks will be indented, then unindented
|
||||||
|
|
@ -402,6 +404,7 @@ class Config:
|
||||||
|
|
||||||
self.ignored_blocks: str = r"""
|
self.ignored_blocks: str = r"""
|
||||||
<(script|style|pre|textarea).*?</(\1)>
|
<(script|style|pre|textarea).*?</(\1)>
|
||||||
|
| {%[ ]djlint:off[ ]%}.*?{%[ ]djlint:on[ ]%}
|
||||||
| <!--.*?-->
|
| <!--.*?-->
|
||||||
| {\*.*?\*}
|
| {\*.*?\*}
|
||||||
| {\#.*?\#}
|
| {\#.*?\#}
|
||||||
|
|
|
||||||
|
|
@ -240,5 +240,45 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<!-- <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 \
|
pytest tests/test_linter.py --cov=src/djlint --cov-branch \
|
||||||
--cov-report xml:coverage.xml --cov-report term-missing
|
--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
|
# pylint: disable=C0116,C0103
|
||||||
|
|
||||||
|
|
@ -187,3 +192,13 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||||
assert result.exit_code == 1
|
assert result.exit_code == 1
|
||||||
assert "D018 1:" in result.output
|
assert "D018 1:" in result.output
|
||||||
assert "J018 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