Skip to content

Feat/format json js in attributes#1337

Open
oliverhaas wants to merge 25 commits intodjlint:masterfrom
oliverhaas:feat/format-json-js-in-attributes
Open

Feat/format json js in attributes#1337
oliverhaas wants to merge 25 commits intodjlint:masterfrom
oliverhaas:feat/format-json-js-in-attributes

Conversation

@oliverhaas
Copy link
Contributor

@oliverhaas oliverhaas commented Jul 17, 2025

Pull Request Check List

Resolves: #1336

  • Added tests for changed code.
  • Updated documentation for changed code.

Please use the issue for information and discussion for now.

oliverhaas and others added 20 commits July 14, 2025 17:49
- Add --format-js-attributes flag to enable JSON/JS attribute formatting
- Add --js-attribute-pattern for custom JavaScript attribute matching
- Implement JSON object formatting with proper indentation relative to HTML
- Support dynamic indentation based on attribute name length
- Skip formatting for single-property objects to avoid unnecessary changes
- Add comprehensive test coverage for different attribute name lengths
- Fix max_attribute_length=0 handling for testing scenarios

Features implemented:
- JSON detection and validation using json.loads()
- Multi-line JSON formatting with proper closing brace alignment
- Configurable JavaScript attribute pattern matching
- Integration with existing djlint formatting pipeline

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…ibutes

- Extended format_js_with_indent to handle both JS objects and general JS code
- Added logic to differentiate between objects and non-object JavaScript
- Adjusted indentation for general JS code (1 space less than objects)
- Removed object-only restriction from JS attribute formatting
- All tests passing including new js_code_block_multiline test

This completes the feature implementation for formatting:
1. JSON objects (using Python's json module)
2. JavaScript objects (using jsbeautifier)
3. General JavaScript code (using jsbeautifier)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…butes

Feature/format json js in attributes
- Replace console.log() with foo(), baz() for cleaner test cases
- Update test IDs to be more descriptive and unique
- Add additional test case for JavaScript under max_attribute_length

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…e quality

- Pre-compile JS attribute regex pattern for better performance
- Refactor helper functions to follow project patterns (config as first parameter)
- Fix linting errors by using proper try-except-else blocks in all helper functions
- Fix MyPy error for proper return type handling
- Remove problematic regex pattern that was causing non-JS attributes to be formatted
- Add comprehensive test for non-JS attributes to ensure they remain unformatted
- Consolidate repetitive test cases into single parameterized test
- Eliminate is_js_attribute function and use pre-compiled regex directly
- Replace has_object_braces with pre-compiled regex for better performance
- Add Django template tag exclusion to prevent formatting {{}} and {%} tags
- Improve code organization by removing redundant helper functions
- All tests passing with enhanced performance and robustness
… formatting

- Fix unused variable warning by removing unused indent_size
- Fix return type annotation for format_js_with_indent function
- Fix mypy type inference by adding explicit type annotation
- Remove trailing whitespace to pass ruff formatting

All pre-commit hooks now pass successfully.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@netlify
Copy link

netlify bot commented Jul 17, 2025

Deploy Preview for djlint failed.

Name Link
🔨 Latest commit 355d3a2
🔍 Latest deploy log https://app.netlify.com/projects/djlint/deploys/6888ee8d2d7cf7000887bec5

This comment was marked as outdated.

oliverhaas and others added 4 commits July 20, 2025 07:30
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@monosans monosans requested a review from Copilot July 29, 2025 13:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds JavaScript and JSON formatting functionality for HTML attributes, resolving issue #1336. The feature allows formatting of JavaScript objects and code within HTML attributes like onclick, data-*, and other JS-related attributes to improve code readability.

  • Adds new configuration options for enabling JS/JSON attribute formatting with customizable patterns and minimum property thresholds
  • Implements formatting logic that handles both JSON objects and JavaScript code with proper indentation relative to HTML structure
  • Provides comprehensive test coverage for various scenarios including different attribute types, indentation levels, and edge cases

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_config/test_format_js_json_attributes.py Comprehensive test suite covering JSON/JS formatting scenarios with different configurations
src/djlint/settings.py Configuration options and regex patterns for JS/JSON attribute formatting
src/djlint/formatter/attributes.py Core formatting logic for JavaScript and JSON content in HTML attributes
src/djlint/__init__.py CLI options for the new JS/JSON attribute formatting feature
pyproject.toml Version bump from 1.36.4 to 1.37.0
docs/src/_data/configuration.json Documentation for the new configuration options
Comments suppressed due to low confidence (2)

tests/test_config/test_format_js_json_attributes.py:164

  • The test case 'non_js_attributes_no_formatting' verifies that non-JS attributes are not formatted, but it doesn't test what happens when format_attribute_js_json is disabled. Consider adding a test case that verifies JS attributes are not formatted when the feature is disabled.
    pytest.param(


opts = BeautifierOptions(js_config)
formatted: str = jsbeautifier.beautify(value, opts)
except (jsbeautifier.BeautifierError, ValueError):
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider catching more specific exceptions from jsbeautifier.beautify(). The current catch block handles jsbeautifier.BeautifierError and ValueError, but the broad Exception catch in count_object_properties suggests there might be other exceptions to consider.

Suggested change
except (jsbeautifier.BeautifierError, ValueError):
except (jsbeautifier.BeautifierError, ValueError, TypeError, KeyError):
# Handle known exceptions from jsbeautifier

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@oliverhaas oliverhaas Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know which exceptions exactly could be raised by the code in the try block? Because I'm not sure. Please research if you can.

EDIT: Apparently this does not trigger a copilot answer. As written in the issue, let me know if you're overall fine with the feature and I can give the PR another pass to clean it up @monosans .

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Format JSON and JS inside html attributes

1 participant