mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-04 03:44:43 +00:00
fix(js formatting): added support for jsbeautify ignore blocks
closes #367
This commit is contained in:
parent
2d8948f288
commit
b8bf071d16
5 changed files with 59 additions and 10 deletions
3
.github/ISSUE_TEMPLATE/linter_bug_report.md
vendored
3
.github/ISSUE_TEMPLATE/linter_bug_report.md
vendored
|
|
@ -28,4 +28,7 @@ labels: [":microbe: bug", ":mag: linter"]
|
||||||
## How To Reproduce
|
## How To Reproduce
|
||||||
<!-- Steps to reproduce the behavior -->
|
<!-- Steps to reproduce the behavior -->
|
||||||
|
|
||||||
|
## Contents of .djlintrc/pyproject.toml [tool.djlint]
|
||||||
|
<!-- please include your config -->
|
||||||
|
|
||||||
<!-- Thanks! 🤠 -->
|
<!-- Thanks! 🤠 -->
|
||||||
|
|
|
||||||
|
|
@ -69,3 +69,6 @@ disable = "E1120, R0914, E0401, R0912, R0916, R0913, W0104, R0801, W1404, R0902,
|
||||||
|
|
||||||
[tool.pylint.TYPECHECK]
|
[tool.pylint.TYPECHECK]
|
||||||
ignored-modules = "regex"
|
ignored-modules = "regex"
|
||||||
|
|
||||||
|
[tool.djlint]
|
||||||
|
format_js = true
|
||||||
|
|
|
||||||
|
|
@ -18,15 +18,40 @@ def format_js(html: str, config: Config) -> str:
|
||||||
|
|
||||||
indent = len(match.group(1)) * " "
|
indent = len(match.group(1)) * " "
|
||||||
inner_indent = indent + config.indent
|
inner_indent = indent + config.indent
|
||||||
|
|
||||||
opts = BeautifierOptions(config.js_config)
|
opts = BeautifierOptions(config.js_config)
|
||||||
|
|
||||||
beautified = (
|
beautified_lines = jsbeautifier.beautify(match.group(3), opts).splitlines()
|
||||||
"\n"
|
beautified = ""
|
||||||
+ inner_indent
|
|
||||||
+ ("\n" + inner_indent).join(
|
# add indent back
|
||||||
jsbeautifier.beautify(match.group(3), opts).splitlines()
|
ignore_indent = False
|
||||||
)
|
for line in beautified_lines:
|
||||||
)
|
|
||||||
|
if re.search(
|
||||||
|
re.compile(
|
||||||
|
r"\/\*[ ]*?beautify[ ]+?preserve:end[ ]*?\*\/",
|
||||||
|
re.DOTALL | re.IGNORECASE | re.MULTILINE,
|
||||||
|
),
|
||||||
|
line,
|
||||||
|
):
|
||||||
|
line = line.lstrip()
|
||||||
|
ignore_indent = False
|
||||||
|
|
||||||
|
if ignore_indent is False:
|
||||||
|
|
||||||
|
beautified += "\n" + inner_indent + line
|
||||||
|
else:
|
||||||
|
beautified += "\n" + line
|
||||||
|
|
||||||
|
if re.search(
|
||||||
|
re.compile(
|
||||||
|
r"\/\*[ ]*?beautify[ ]+?preserve:start[ ]*?\*\/",
|
||||||
|
re.DOTALL | re.IGNORECASE | re.MULTILINE,
|
||||||
|
),
|
||||||
|
line,
|
||||||
|
):
|
||||||
|
ignore_indent = True
|
||||||
|
|
||||||
return match.group(1) + match.group(2) + beautified + "\n" + indent
|
return match.group(1) + match.group(2) + beautified + "\n" + indent
|
||||||
|
|
||||||
|
|
|
||||||
8
tests/test_config/test_scripts_styles/ignore.html
Normal file
8
tests/test_config/test_scripts_styles/ignore.html
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<script>
|
||||||
|
/* beautify preserve:start */
|
||||||
|
function(){}
|
||||||
|
function(){}
|
||||||
|
function(){}
|
||||||
|
|
||||||
|
/* beautify preserve:end */
|
||||||
|
</script>
|
||||||
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
run::
|
run::
|
||||||
|
|
||||||
pytest tests/test_config/test_json/test_config.py --cov=src/djlint --cov-branch \
|
pytest tests/test_config/test_scripts_styles/test_config.py --cov=src/djlint --cov-branch \
|
||||||
--cov-report xml:coverage.xml --cov-report term-missing
|
--cov-report xml:coverage.xml --cov-report term-missing
|
||||||
|
|
||||||
pytest tests/test_config/test_json/test_config.py::test_config
|
pytest tests/test_config/test_scripts_styles/test_config.py::test_ignore
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# pylint: disable=C0116
|
# pylint: disable=C0116
|
||||||
|
|
@ -23,5 +23,15 @@ def test_config(runner: CliRunner) -> None:
|
||||||
"--check",
|
"--check",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
print(result.output)
|
assert result.exit_code == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_ignore(runner: CliRunner) -> None:
|
||||||
|
result = runner.invoke(
|
||||||
|
djlint,
|
||||||
|
[
|
||||||
|
"tests/test_config/test_scripts_styles/ignore.html",
|
||||||
|
"--check",
|
||||||
|
],
|
||||||
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue