diff --git a/.github/ISSUE_TEMPLATE/linter_bug_report.md b/.github/ISSUE_TEMPLATE/linter_bug_report.md index 8a541f1..e39991b 100644 --- a/.github/ISSUE_TEMPLATE/linter_bug_report.md +++ b/.github/ISSUE_TEMPLATE/linter_bug_report.md @@ -28,4 +28,7 @@ labels: [":microbe: bug", ":mag: linter"] ## How To Reproduce +## Contents of .djlintrc/pyproject.toml [tool.djlint] + + diff --git a/pyproject.toml b/pyproject.toml index c362257..2466c2c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/src/djlint/formatter/js.py b/src/djlint/formatter/js.py index a8c6497..ddb7fe3 100644 --- a/src/djlint/formatter/js.py +++ b/src/djlint/formatter/js.py @@ -18,15 +18,40 @@ def format_js(html: str, config: Config) -> 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 diff --git a/tests/test_config/test_scripts_styles/ignore.html b/tests/test_config/test_scripts_styles/ignore.html new file mode 100644 index 0000000..257f222 --- /dev/null +++ b/tests/test_config/test_scripts_styles/ignore.html @@ -0,0 +1,8 @@ + diff --git a/tests/test_config/test_scripts_styles/test_config.py b/tests/test_config/test_scripts_styles/test_config.py index 1f91e6f..73d97b8 100644 --- a/tests/test_config/test_scripts_styles/test_config.py +++ b/tests/test_config/test_scripts_styles/test_config.py @@ -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 @@ -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