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.0.tar.gz (16.8 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.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for sqlfluff_plugin_extended_jinja-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d1aa506c5961eb0a1923a372396d9c8d45fedefd72277945960cd2a1c15da535
MD5 63a3bc2633c91b6d2e8f12d441065c15
BLAKE2b-256 270048716d5597ca253f2259dc28738be67145adc1f8966200e7da56b0e533b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlfluff_plugin_extended_jinja-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b07cac41703e40f76bbec16109cdf7e3fa4669a0bb490696bbe4fa2bd8dbafc
MD5 e0967b62be0c610ea890f7752d133d20
BLAKE2b-256 d3d571d2a39687283c04eb451cabdb8222dfda313171173d2a0b150ebf3faf64

See more details on using hashes here.

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