Skip to main content

SQLFluff plugin with Jinja formatting rules: block tags on own lines and Black-based expression formatting.

Project description

sqlfluff-plugin-extended-jinja

SQLFluff plugin that adds extended Jinja formatting rules. Ensures block-level tags are isolated on their own lines and formats Python expressions inside Jinja tags using Black.

Uses the EJ (Extended Jinja) prefix to avoid conflicts with SQLFluff's built-in JJ rules.

Code Name Description
EJ01 extended_jinja.block_solo_line Block tags (if, for, else, endif, ...) must be on their own lines
EJ02 extended_jinja.content_format Format Python expressions inside Jinja tags with Black

Installation

pip install sqlfluff-plugin-extended-jinja

For Black-based formatting (EJ02):

pip install "sqlfluff-plugin-extended-jinja[jinjafmt]"

Usage

Once installed, the rules are automatically available to SQLFluff:

# Lint
sqlfluff lint my_model.sql --rules EJ01,EJ02

# Fix
sqlfluff fix my_model.sql --rules EJ01,EJ02

Configuration

Add to your .sqlfluff config file:

[sqlfluff:rules:extended_jinja.block_solo_line]
# Spaces per indent level for content inside block tags
jinja_indent_size = 2

[sqlfluff:rules:extended_jinja.content_format]
# Spaces per indent level inside formatted Jinja tags
jinja_indent_size = 2
# Max line length for Jinja content formatting
jinja_line_length = 120
# Enable/disable Black formatting
jinja_black_enabled = True

Rules

EJ01 — extended_jinja.block_solo_line

Block-level Jinja tags (if, for, else, elif, endif, endfor, macro, endmacro, etc.) must not share a line with SQL content or other tags.

Anti-pattern:

SELECT * FROM foo WHERE 1=1 {% if true %} AND x > 1 {% endif %}

Best practice:

SELECT * FROM foo WHERE 1=1
{% if true %}
  AND x > 1
{% endif %}

EJ02 — extended_jinja.content_format

Python expressions inside {{ }} and {% %} tags are formatted with Black for consistent style. Handles macro definitions, reserved-word identifiers (e.g. return()), and Jinja's tilde (~) concatenation operator.

Requires the black package. When Black is unavailable, the rule is silently skipped.

Anti-pattern:

{{ config(materialized="incremental", unique_key="order_id",
            partition_by={"field": "order_date", "data_type": "date"}) }}

Best practice:

{{
    config(
        materialized="incremental",
        unique_key="order_id",
        partition_by={"field": "order_date", "data_type": "date"},
    )
}}

Pre-commit

- repo: https://github.com/sqlfluff/sqlfluff
  rev: 4.2.1
  hooks:
    - id: sqlfluff-fix
      additional_dependencies:
        - sqlfluff-plugin-extended-jinja[jinjafmt]

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

Attribution

The Black formatting wrapper is adapted from sqlfmt (Copyright 2021 Ted Conbeer, Apache-2.0).

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlfluff_plugin_extended_jinja-0.1.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlfluff_plugin_extended_jinja-0.1.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlfluff_plugin_extended_jinja-0.1.1.tar.gz.

File metadata

File hashes

Hashes for sqlfluff_plugin_extended_jinja-0.1.1.tar.gz
Algorithm Hash digest
SHA256 41bdf66df479fd1e50ae3d73e14cda7b5c660ef8901c93f9c1b3452ba4c6aba7
MD5 067be2cd92b95ab2bae528db32c5d017
BLAKE2b-256 97fbf9be473b56be0ab1242c25edb28c57c7d5fa7fc41c0e5a72ee458527ea8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_plugin_extended_jinja-0.1.1.tar.gz:

Publisher: publish.yml on mauro-lanza/sqlfluff-plugin-extended-jinja

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlfluff_plugin_extended_jinja-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlfluff_plugin_extended_jinja-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ca02d300b5cd060090563229c993afabb9ca8254dae4149159896355d02a48c
MD5 8f657e958c89baf8e33a30aafe5f27d8
BLAKE2b-256 a6ef1a20c1a21f2b95ceab12cba457b0deaf8239df4e6261e803714400f831da

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlfluff_plugin_extended_jinja-0.1.1-py3-none-any.whl:

Publisher: publish.yml on mauro-lanza/sqlfluff-plugin-extended-jinja

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page