Skip to content

Commit

Permalink
fix(js indent): fixed an issue where literal strings were double inde…
Browse files Browse the repository at this point in the history
…nted

closes #614
  • Loading branch information
christopherpickering committed May 5, 2023
1 parent f335e3b commit b1e8ab2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 65 deletions.
48 changes: 20 additions & 28 deletions src/djlint/formatter/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,30 @@ def launch_formatter(config: Config, match: re.Match) -> str:
return match.group()

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

beautified_lines = cssbeautifier.beautify(match.group(3), opts).splitlines()
beautified = ""
# because of the param options for js-beautifier we cannot pass
# in a fixed space leading.
# so, call formatter twice, once with a fake indent.
# check which lines changed (these are the formattable lines)
# and add the leading space to them.

# add indent back
ignore_indent = False
for line in beautified_lines:
if re.search(
re.compile(
r"\/\*[ ]*?beautify[ ]+?ignore:end[ ]*?\*\/",
re.DOTALL | re.IGNORECASE | re.MULTILINE,
),
line,
):
line = line.lstrip()
ignore_indent = False
config.css_config["indent_level"] = 1
opts = BeautifierOptions(config.css_config)
beautified_lines = cssbeautifier.beautify(match.group(3), opts).splitlines()

if ignore_indent is False and line:
beautified += "\n" + inner_indent + line
else:
beautified += "\n" + line
config.js_config["indent_level"] = 2
opts = BeautifierOptions(config.js_config)
beautified_lines_test = cssbeautifier.beautify(
match.group(3), opts
).splitlines()

if re.search(
re.compile(
r"\/\*[ ]*?beautify[ ]+?ignore:start[ ]*?\*\/",
re.DOTALL | re.IGNORECASE | re.MULTILINE,
),
line,
):
ignore_indent = True
beautified = ""
for line, test in zip(beautified_lines, beautified_lines_test):
beautified += "\n"
if line == test:
beautified += line
continue
beautified += indent + line

return match.group(1) + match.group(2) + beautified + "\n" + indent

Expand Down
46 changes: 17 additions & 29 deletions src/djlint/formatter/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,28 @@ def launch_formatter(config: Config, match: re.Match) -> str:
return match.group()

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

opts = BeautifierOptions(config.js_config)
# because of the param options for js-beautifier we cannot pass
# in a fixed space leading.
# so, call formatter twice, once with a fake indent.
# check which lines changed (these are the formattable lines)
# and add the leading space to them.

config.js_config["indent_level"] = 1
opts = BeautifierOptions(config.js_config)
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|ignore):end[ ]*?\*\/",
re.DOTALL | re.IGNORECASE | re.MULTILINE,
),
line,
):
line = line.lstrip()
ignore_indent = False

if ignore_indent is False and line:
beautified += "\n" + inner_indent + line

else:
beautified += "\n" + line
config.js_config["indent_level"] = 2
opts = BeautifierOptions(config.js_config)
beautified_lines_test = jsbeautifier.beautify(match.group(3), opts).splitlines()

if re.search(
re.compile(
r"\/\*[ ]*?beautify[ ]+?(?:preserve|ignore):start[ ]*?\*\/",
re.DOTALL | re.IGNORECASE | re.MULTILINE,
),
line,
):
ignore_indent = True
beautified = ""
for line, test in zip(beautified_lines, beautified_lines_test):
beautified += "\n"
if line == test:
beautified += line
continue
beautified += indent + line

return match.group(1) + match.group(2) + beautified + "\n" + indent

Expand Down
4 changes: 2 additions & 2 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ def __init__(

self.js_config = (
{"indent_size": indent_js} if indent_js else djlint_settings.get("js")
)
) or {}

self.css_config = (
{"indent_size": indent_css} if indent_css else djlint_settings.get("css")
)
) or {}

self.format_css: bool = format_css or djlint_settings.get("format_css", False)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_config/test_format_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
(
"<div>\n"
" <style>\n"
" body {\n"
" color: red\n"
" }\n"
" body {\n"
" color: red\n"
" }\n"
" </style>\n"
"</div>\n"
),
Expand Down
34 changes: 31 additions & 3 deletions tests/test_config/test_format_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
(
"<div>\n"
" <script>\n"
" () => {\n"
' console.log("hi")\n'
" }\n"
" () => {\n"
' console.log("hi")\n'
" }\n"
" </script>\n"
"</div>\n"
),
Expand Down Expand Up @@ -166,6 +166,34 @@
({"format_js": True}),
id="ignore",
),
pytest.param(
(
'<script type="text/javascript">\n'
" let s = `\n"
" <p>Text.</p>`;\n"
"</script>\n"
"<div>"
'<script type="text/javascript">\n'
" let s = `\n"
" <p>Text.\n"
" </p>`;\n"
"</script></div>"
),
(
'<script type="text/javascript">\n'
" let s = `\n"
" <p>Text.</p>`;\n"
"</script>\n"
"<div>\n"
' <script type="text/javascript">\n'
" let s = `\n"
" <p>Text.</p>`;\n"
" </script>\n"
"</div>\n"
),
({"format_js": True, "indent_js": 3}),
id="literals",
),
]


Expand Down

0 comments on commit b1e8ab2

Please sign in to comment.