Skip to content

Commit

Permalink
fix(js formatting): added support for jsbeautify ignore blocks
Browse files Browse the repository at this point in the history
closes #367
  • Loading branch information
christopherpickering committed Sep 7, 2022
1 parent 2d8948f commit b8bf071
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/linter_bug_report.md
Expand Up @@ -28,4 +28,7 @@ labels: [":microbe: bug", ":mag: linter"]
## How To Reproduce
<!-- Steps to reproduce the behavior -->

## Contents of .djlintrc/pyproject.toml [tool.djlint]
<!-- please include your config -->

<!-- Thanks! 🤠 -->
3 changes: 3 additions & 0 deletions pyproject.toml
Expand Up @@ -69,3 +69,6 @@ disable = "E1120, R0914, E0401, R0912, R0916, R0913, W0104, R0801, W1404, R0902,

[tool.pylint.TYPECHECK]
ignored-modules = "regex"

[tool.djlint]
format_js = true
39 changes: 32 additions & 7 deletions src/djlint/formatter/js.py
Expand Up @@ -18,15 +18,40 @@ def launch_formatter(config: Config, match: re.Match) -> str:

indent = len(match.group(1)) * " "
inner_indent = indent + config.indent

opts = BeautifierOptions(config.js_config)

beautified = (
"\n"
+ inner_indent
+ ("\n" + inner_indent).join(
jsbeautifier.beautify(match.group(3), opts).splitlines()
)
)
beautified_lines = jsbeautifier.beautify(match.group(3), opts).splitlines()
beautified = ""

# add indent back
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

Expand Down
8 changes: 8 additions & 0 deletions tests/test_config/test_scripts_styles/ignore.html
@@ -0,0 +1,8 @@
<script>
/* beautify preserve:start */
function(){}
function(){}
function(){}

/* beautify preserve:end */
</script>
16 changes: 13 additions & 3 deletions tests/test_config/test_scripts_styles/test_config.py
Expand Up @@ -2,10 +2,10 @@
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
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
Expand All @@ -23,5 +23,15 @@ def test_config(runner: CliRunner) -> None:
"--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

0 comments on commit b8bf071

Please sign in to comment.