mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-16 21:40:24 +00:00
feat(ignore rules): added option to ignore linter rules with inline comments
closes #113
This commit is contained in:
parent
1731ee830a
commit
1b3159f661
12 changed files with 298 additions and 122 deletions
|
|
@ -129,5 +129,10 @@ module.exports = {
|
|||
'en-US': 'Edit this page',
|
||||
ru: 'Редактировать эту страницу',
|
||||
fr: 'Modifier cette page',
|
||||
},
|
||||
ignoring_code: {
|
||||
'en-US': 'Ignoring Code',
|
||||
ru: 'Игнорирование Контент',
|
||||
fr: 'Ignorer le Contenu',
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ layout: layout.njk
|
|||
<a class="{% if page.url == "lang_code_url" | i18n + "/docs/getting-started/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/getting-started/"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "getting_started" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/formatter/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/formatter/"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "formatter" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/linter/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/linter"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "linter" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/ignoring-code/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/ignoring-code/"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "ignoring_code" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/configuration/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/configuration/"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "configuration" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/integrations/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/integrations"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "integrations" | i18n }}</a></li>
|
||||
<li><a class="{% if page.url == "lang_code_url" | i18n + "/docs/best-practices/" %}is-active{% endif %}" href="{{ "lang_code_url" | i18n }}/docs/best-practices/"><span class="icon mr-1"><i class="fa-solid fa-code-commit"></i></span>{{ "best_practices" | i18n }}</a></li>
|
||||
|
|
|
|||
|
|
@ -38,46 +38,6 @@ djlint . --reformat
|
|||
"djLint is not an html parser or syntax validator."
|
||||
%}
|
||||
|
||||
## Ignoring Code
|
||||
|
||||
Code can be ignored by wrapping it in `djlint` tags:
|
||||
|
||||
{% raw %}
|
||||
|
||||
For plain old html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<bad html to ignore> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
or as a comment -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <bad html to ignore> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
or as a long comment -
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<bad html to ignore> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
or as a javascript style comment -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <bad html to ignore> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
or as a golang style comment -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <bad html to ignore> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Here's an example!
|
||||
|
||||
### Before
|
||||
|
|
|
|||
74
docs/src/docs/ignoring-code.md
Normal file
74
docs/src/docs/ignoring-code.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
---
|
||||
description: How to prevent djLint from formatting a block of code. How ignore djLint rules inline.
|
||||
title: Ignoring Code
|
||||
keywords: template linter, template formatter, djLint, HTML, templates, formatter, linter, formatter usage, ignore code, ignore rules
|
||||
date: Last Modified
|
||||
---
|
||||
|
||||
# Ignoring Code
|
||||
|
||||
Code can be ignored by wrapping it in `djlint` tags:
|
||||
|
||||
{% raw %}
|
||||
|
||||
For plain old html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<bad html to ignore> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
or as a comment -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <bad html to ignore> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
or as a long comment -
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<bad html to ignore> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
or as a javascript style comment -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <bad html to ignore> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
or as a golang style comment -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <bad html to ignore> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Ignoring Rules
|
||||
|
||||
Specific linter rules can also be ignored by adding the rule name into the ignored block opening tag.
|
||||
|
||||
{% raw %}
|
||||
```html
|
||||
{# djlint:off H025,H026 #}
|
||||
<p>
|
||||
{# djlint:on #}
|
||||
|
||||
<!-- djlint:off H025-->
|
||||
<p>
|
||||
<!-- djlint:on -->
|
||||
|
||||
{% comment %} djlint:off H025 {% endcomment %}
|
||||
<p>
|
||||
{% comment %} djlint:on {% endcomment %}
|
||||
|
||||
{{!-- djlint:off H025 --}}
|
||||
<p>
|
||||
{{!-- djlint:on --}}
|
||||
|
||||
{{ /* djlint:off H025 */ }}
|
||||
<p>
|
||||
{{ /* djlint:on */ }}
|
||||
```
|
||||
{% endraw %}
|
||||
|
|
@ -38,45 +38,6 @@ djlint . --reformat
|
|||
"djLint n'est pas un analyseur html ou un validateur de syntaxe."
|
||||
%}
|
||||
|
||||
## Ignorer le code
|
||||
|
||||
Le code peut être ignoré en l'entourant de balises `djlint` :
|
||||
|
||||
{% raw %}
|
||||
|
||||
Pour le simple html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<mauvais html à ignorer> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
ou comme un long commentaire -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <mauvais html à ignorer> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
ou comme un long commentaire -
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<mauvais html à ignorer> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
ou comme un commentaire de style javascript -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <mauvais html à ignorer> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
ou comme un commentaire de style golang -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <mauvais html à ignorer> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Voici un exemple !
|
||||
|
||||
|
|
|
|||
74
docs/src/fr/docs/ignoring-code.md
Normal file
74
docs/src/fr/docs/ignoring-code.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
---
|
||||
description: Comment empêcher djLint de formater un bloc de code. Comment ignorer les règles de djLint en ligne.
|
||||
title: Ignorer le code
|
||||
keywords: template linter, template formatter, djLint, HTML, templates, formatter, linter, formatter usage, ignorer le code, ignorer les règles
|
||||
date: Last Modified
|
||||
---
|
||||
|
||||
## Ignorer le code
|
||||
|
||||
Le code peut être ignoré en l'entourant de balises `djlint` :
|
||||
|
||||
{% raw %}
|
||||
|
||||
Pour le simple html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<mauvais html à ignorer> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
ou comme un long commentaire -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <mauvais html à ignorer> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
ou comme un long commentaire -
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<mauvais html à ignorer> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
ou comme un commentaire de style javascript -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <mauvais html à ignorer> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
ou comme un commentaire de style golang -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <mauvais html à ignorer> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Ignorer les règles
|
||||
|
||||
Des règles spécifiques de linter peuvent également être ignorées en ajoutant le nom de la règle dans la balise d'ouverture du bloc ignoré.
|
||||
|
||||
{% raw %}
|
||||
```html
|
||||
{# djlint:off H025,H026 #}
|
||||
<p>
|
||||
{# djlint:on #}
|
||||
|
||||
<!-- djlint:off H025-->
|
||||
<p>
|
||||
<!-- djlint:on -->
|
||||
|
||||
{% comment %} djlint:off H025 {% endcomment %}
|
||||
<p>
|
||||
{% comment %} djlint:on {% endcomment %}
|
||||
|
||||
{{!-- djlint:off H025 --}}
|
||||
<p>
|
||||
{{!-- djlint:on --}}
|
||||
|
||||
{{ /* djlint:off H025 */ }}
|
||||
<p>
|
||||
{{ /* djlint:on */ }}
|
||||
```
|
||||
{% endraw %}
|
||||
|
|
@ -38,46 +38,6 @@ djlint . --reformat
|
|||
"djLint не является парсером html или синтаксическим валидатором."
|
||||
%}
|
||||
|
||||
## Игнорирование кодекса
|
||||
|
||||
Код можно игнорировать, обернув его в теги `djlint`:
|
||||
|
||||
{% raw %}
|
||||
|
||||
Для простого старого html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<плохой html, который следует игнорировать> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
или как комментарий -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <плохой html, который следует игнорировать> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
или в виде длинного комментария
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<плохой html, который следует игнорировать> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
или как комментарий в стиле javascript -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <плохой html, который следует игнорировать> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
или как комментарий в стиле golang -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <плохой html, который следует игнорировать> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Вот пример!
|
||||
|
||||
### До
|
||||
|
|
|
|||
74
docs/src/ru/docs/ignoring-code.md
Normal file
74
docs/src/ru/docs/ignoring-code.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
---
|
||||
description: Как запретить djLint форматировать блок кода. Как игнорировать правила djLint в строке.
|
||||
title: Игнорирование кодекса
|
||||
keywords: облицовка шаблонов, форматер шаблонов, djLint, HTML, шаблоны, форматер, линтер, использование, Использование форматера
|
||||
date: Last Modified
|
||||
---
|
||||
|
||||
## Игнорирование кодекса
|
||||
|
||||
Код можно игнорировать, обернув его в теги `djlint`:
|
||||
|
||||
{% raw %}
|
||||
|
||||
Для простого старого html -
|
||||
|
||||
```html
|
||||
<!-- djlint:off -->
|
||||
<плохой html, который следует игнорировать> <!-- djlint:on --></bad>
|
||||
```
|
||||
|
||||
или как комментарий -
|
||||
|
||||
```html
|
||||
{# djlint:off #} <плохой html, который следует игнорировать> {# djlint:on #}</bad>
|
||||
```
|
||||
|
||||
или в виде длинного комментария
|
||||
|
||||
```html
|
||||
{% comment %} djlint:off {% endcomment %}
|
||||
<плохой html, который следует игнорировать> {% comment %} djlint:on {% endcomment %}</bad>
|
||||
```
|
||||
|
||||
или как комментарий в стиле javascript -
|
||||
|
||||
```html
|
||||
{{ /* djlint:off */ }} <плохой html, который следует игнорировать> {{ /* djlint:on */ }}</bad>
|
||||
```
|
||||
|
||||
или как комментарий в стиле golang -
|
||||
|
||||
```html
|
||||
{{!-- djlint:off --}} <плохой html, который следует игнорировать> {{!-- djlint:on --}}</bad>
|
||||
```
|
||||
|
||||
{% endraw %}
|
||||
|
||||
## Игнорирование правил
|
||||
|
||||
Определенные правила linter можно игнорировать, добавив имя правила в открывающий тег игнорируемого блока.
|
||||
|
||||
{% raw %}
|
||||
```html
|
||||
{# djlint:off H025,H026 #}
|
||||
<p>
|
||||
{# djlint:on #}
|
||||
|
||||
<!-- djlint:off H025-->
|
||||
<p>
|
||||
<!-- djlint:on -->
|
||||
|
||||
{% comment %} djlint:off H025 {% endcomment %}
|
||||
<p>
|
||||
{% comment %} djlint:on {% endcomment %}
|
||||
|
||||
{{!-- djlint:off H025 --}}
|
||||
<p>
|
||||
{{!-- djlint:on --}}
|
||||
|
||||
{{ /* djlint:off H025 */ }}
|
||||
<p>
|
||||
{{ /* djlint:on */ }}
|
||||
```
|
||||
{% endraw %}
|
||||
|
|
@ -100,3 +100,21 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
|
|||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def inside_ignored_rule(config: Config, html: str, match: re.Match, rule: str) -> bool:
|
||||
"""Check if match is inside an ignored pattern."""
|
||||
for rule_regex in config.ignored_rules:
|
||||
for ignored_match in list(
|
||||
re.finditer(
|
||||
re.compile(rule_regex, re.DOTALL | re.IGNORECASE | re.VERBOSE),
|
||||
html,
|
||||
)
|
||||
):
|
||||
if (
|
||||
rule in list(set(re.split(r"\s|,", ignored_match.group(1).strip())))
|
||||
and ignored_match.start(0) <= match.start()
|
||||
and match.end(0) <= ignored_match.end()
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from typing import Dict, List
|
|||
|
||||
import regex as re
|
||||
|
||||
from .helpers import inside_ignored_block
|
||||
from .helpers import inside_ignored_block, inside_ignored_rule
|
||||
from .settings import Config
|
||||
|
||||
flags = {
|
||||
|
|
@ -88,7 +88,11 @@ def lint_file(config: Config, this_file: Path) -> Dict:
|
|||
open_tags.insert(0, match)
|
||||
|
||||
for match in open_tags:
|
||||
if inside_ignored_block(config, html, match) is False:
|
||||
if (
|
||||
inside_ignored_block(config, html, match) is False
|
||||
and inside_ignored_rule(config, html, match, rule["name"])
|
||||
is False
|
||||
):
|
||||
errors[filename].append(
|
||||
{
|
||||
"code": rule["name"],
|
||||
|
|
@ -105,7 +109,11 @@ def lint_file(config: Config, this_file: Path) -> Dict:
|
|||
),
|
||||
html,
|
||||
):
|
||||
if inside_ignored_block(config, html, match) is False:
|
||||
if (
|
||||
inside_ignored_block(config, html, match) is False
|
||||
and inside_ignored_rule(config, html, match, rule["name"])
|
||||
is False
|
||||
):
|
||||
errors[filename].append(
|
||||
{
|
||||
"code": rule["name"],
|
||||
|
|
|
|||
|
|
@ -579,6 +579,18 @@ class Config:
|
|||
| ^---[\s\S]+?---
|
||||
"""
|
||||
|
||||
self.ignored_rules: List[str] = [
|
||||
# html comment
|
||||
r"<!--\s*djlint\:off(.+?)-->.*?(?=<!--\s*djlint\:on\s*-->)",
|
||||
# django/jinja/nunjucks
|
||||
r"{\#\s*djlint\:\s*off(.+?)\#}.*?(?={\#\s*djlint\:\s*on\s*\#})",
|
||||
r"{%\s*comment\s*%\}\s*djlint\:off(.*?)\{%\s*endcomment\s*%\}.*?(?={%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\})",
|
||||
# handlebars
|
||||
r"{{!--\s*djlint\:off(.*?)--}}.*?(?={{!--\s*djlint\:on\s*--}})",
|
||||
# golang
|
||||
r"{{-?\s*/\*\s*djlint\:off(.*?)\*/\s*-?}}.*?(?={{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}})",
|
||||
]
|
||||
|
||||
self.ignored_inline_blocks: str = r"""
|
||||
<!--.*?-->
|
||||
| <(script|style).*?\</(?:\1)>
|
||||
|
|
|
|||
|
|
@ -762,3 +762,32 @@ H025 2:4 Tag seems to be an orphan. <h3>
|
|||
H025 2:12 Tag seems to be an orphan. </h4>"""
|
||||
in result.output
|
||||
)
|
||||
|
||||
|
||||
def test_ignoring_rules(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
write_to_file(
|
||||
tmp_file.name,
|
||||
b"""{# djlint:off H025,H026 #}
|
||||
<p>
|
||||
{# djlint:on #}
|
||||
|
||||
<!-- djlint:off H025-->
|
||||
<p>
|
||||
<!-- djlint:on -->
|
||||
|
||||
{% comment %} djlint:off H025 {% endcomment %}
|
||||
<p>
|
||||
{% comment %} djlint:on {% endcomment %}
|
||||
|
||||
{{!-- djlint:off H025 --}}
|
||||
<p>
|
||||
{{!-- djlint:on --}}
|
||||
|
||||
{{ /* djlint:off H025 */ }}
|
||||
<p>
|
||||
{{ /* djlint:on */ }}
|
||||
|
||||
""",
|
||||
)
|
||||
result = runner.invoke(djlint, [tmp_file.name])
|
||||
assert "H025" not in result.output
|
||||
|
|
|
|||
Loading…
Reference in a new issue