Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Formatter] formatting not disabled inside {# djlint:off #} inside script tag #868

Open
3 tasks done
thacoon opened this issue Jul 21, 2024 · 2 comments
Open
3 tasks done
Labels
🦠 bug Something isn't working 🧽 formatter

Comments

@thacoon
Copy link

thacoon commented Jul 21, 2024

System Info

  • OS: macOS Sonoma
  • Python Version 3.11.9
  • djLint Version 1.34.1
  • template language: django default templating language

Issue

I am sometimes using django templating language inside the script tag. When I reformat the code this gets reformatted as well, so I want to ignore that part using {# djlint:off #}. However this is ignored by djlint and it tries to reformat the code resulting in broken javascript code, event {# djlint:off #} gets reformatted as well. Currently I can only ignore the whole script tag, then nothing gets reformated inside the script tag, but as soon as I only want to ignore a specific part inside it, everything gets reformatted, which then also breaks my JS code.

A similar issue was reported, which was fixed in version 1.0.0 #166, however in that previous error the {# djlint:off #} comments itself have not been affected, which is now the case.

How to Reproduce

<script>
    {# djlint:off #}
    const _steps = {
      {% for step in steps %}
        '{{ step.id }}': true
        {% if not forloop.last %},{% endif %}
      {% endfor %}
    };
    {# djlint:on #}

    document.addEventListener('alpine:init', () => {
        Alpine.data('steps-table', () => {
            return {
               steps: _steps, 
            };
        });
    });
</script>

I run djlint --reformat --format-js server/templates/ and get:

<script>
    {
        # djlint: off #
    }
    const _steps = {
        {
            %
            for step in steps %
        }
        '{{ step.id }}': true {
            %
            if not forloop.last %
        },
        {
            % endif %
        } {
            % endfor %
        }
    };
    {
        # djlint: on #
    }
    document.addEventListener('alpine:init', () => {
        Alpine.data('steps-table', () => {
            return {
                steps: _steps,
            };
        });
    });
</script>

Contents of .djlintrc/pyproject.toml [tool.djlint]

[tool.djlint]
ignore = "H006,H030,H031"
include = "H017,H035"
indent = 2
blank_line_after_tag = "load,extends"
profile = "django"
max_line_length = 120
format_attrite_template_tags = true
@thacoon thacoon added 🦠 bug Something isn't working 🧽 formatter labels Jul 21, 2024
@WellingtonNico
Copy link

WellingtonNico commented Aug 14, 2024

up, same problem here:

update: i bet it would happen with css also

before:

<script>

       {# djlint:off #}
       {% if not open_form %}
	    console.log('my code that should not be formatted')
       {% endif %}
       {# djlint:on #}

    function my_function_that_should_be_indented() {
	     $("#client_form").show();
             $("#list_users").hide();
    }
 </script>

after:

<script>
    {
      # djlint: off #
    } {
      %
      if not open_form %
    }
    console.log('my code that should not be formatted') {
      % endif %
    } {
      # djlint: on #
    }

    function my_function_that_should_be_indented() {
       $("#client_form").show();
       $("#list_users").hide();
    }
 </script>

@monosans
Copy link
Member

monosans commented Nov 4, 2024

#931 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🦠 bug Something isn't working 🧽 formatter
Projects
None yet
Development

No branches or pull requests

3 participants