krikri-jinja-linter v0.2.0

Static linter for Jinja2 templates, built on the krikri-jinja engine (Crystal)

krikri-jinja-linter

A static linter for Jinja2 templates, written in Crystal on top of the krikri-jinja engine (our clean-room Jinja2 implementation).

It is inspired by aristanetworks/j2lint, not a clone: we start from a similar rule set, but rule behavior, output format, configuration and exit codes are this project's own. Matching upstream's findings exactly is a non-goal.

Binary name: jinja-lint.

Status

Version 0.3.0 (pre-1.0: rules, output format and config may still change).

All eleven rules are implemented. S0 is the only rule that needs the engine; the rest are source-text checks over the tags the scanner finds. The AST is still unused, and it is what later rules (unused {% set %}, undefined variables) will build on.

Rule IDs follow aristanetworks/j2lint v1.3.0, so a j2lint suppression list carries over. The one exception is S8, this project's own addition (a statement written in {{ }}).

S0 accepts every tag the krikri-jinja engine parses (engine v1.2.0), including {% spaceless %}, {% debug %} and the i18n tags ({% trans %}, {% pluralize %}, {% endtrans %}, with the trimmed/notrimmed markers). No known engine gap remains.

Usage

$ jinja-lint --help
$ jinja-lint templates/                      # directory: *.j2, *.jinja, *.jinja2
$ jinja-lint playbook/templates/site.j2      # explicit files, any extension
$ jinja-lint --format json templates/        # machine-readable output (-j)
$ jinja-lint --extensions j2,html templates/ # more suffixes for a directory scan
$ jinja-lint --stdin < site.j2               # one template from standard input
$ jinja-lint --list                          # rules (-l)
$ jinja-lint --ignore S6 -- templates/       # skip rules (IDs or names)
$ jinja-lint --warn S2 templates/            # report them, but do not fail

Text output is one finding per line, path:line[:column]: severity[RULE] message:

templates/site.j2:12: error[S0] unclosed "{{" tag
1 problem(s) in 3 template(s)

--verbose groups the findings under each file instead, and --format json emits one object per finding.

Exit codes:

Code Meaning
0 no findings, or only rules named by --warn
1 at least one finding from a rule that is not warned
2 usage error (bad flag, unknown format, missing path)

Rules

ID Name Checks
S0 jinja-syntax-error the template parses as valid Jinja2
S1 single-space-decorator exactly one space inside {{ }}
S2 operator-enclosed-by-spaces {{ a + b }}, not {{ a+b }}; = is exempt
S3 jinja-statements-indentation closing and mid-block tags line up with their opener
S4 jinja-statements-single-space exactly one space inside {% %}
S5 jinja-statements-no-tabs no tabs in tags or in their indentation
S6 jinja-statements-delimiter no {%-, {%+ or -%} on statements
S7 single-statement-per-line at most one {% %} per line
S8 jinja-statements-output-delimiter statements use {% %}, not {{ }}
V1 jinja-variable-lower-case variable names have no upper case letters
V2 jinja-variable-format lower case variable names are snake_case

Rule details worth knowing:

  • S6 owns the whitespace-trim markers on statements: {%- if x %}, {%+ if x %} and {% endif -%} are findings. The marker is still part of the delimiter for S4, so one tag is reported once by each rule, for its own reason.
  • Trim markers on an expression are fine: {{- name -}} is clean, and S1 keeps its inner spacing honest.
  • S1 and S4 skip an inner side whose whitespace holds a tab: tabs are S5's finding, and each problem is reported once.
  • S5 reports once per line, at the first offending tab. Plain text in a template and comments are not linted for tabs.
  • S7 counts {% %} statements only; several {{ }} values on one line are fine.
  • S3 compares a block's closing and mid-block tags with the column of its opener. Tags that do not own their line are left to S7, and mismatched blocks to S0. j2lint's S3 also flags a statement nested inside a block; it reports every such statement whatever its indentation (a correctly 4-space-indented one included), so that part is not copied.
  • V1 and V2 check the variables referenced in {{ }}. Attribute names after a dot, filters after a pipe, keyword arguments, dict keys, test names and keywords are not variables and are skipped.
  • A name with an upper case letter is V1's finding, not V2's, so one badly named variable is one finding.
  • Tags inside {% raw %}...{% endraw %} are copied verbatim by the engine and are not linted.
  • {% spaceless %} is a block: S3 checks that its {% endspaceless %} lines up with it, and S8 reports {{ spaceless }} as a statement keyword. {% debug %} is standalone, and {{ debug }} stays a variable.
  • {% trans %} is a block too: S3 lines up its {% pluralize %} and {% endtrans %} with it, and S8 reports {{ trans }}, {{ pluralize }} and {{ endtrans }} as statement keywords. The variables a body substitutes ({{ count }}) are checked by V1 and V2 like any other expression.

Configuration

  • --ignore (or -i) drops rules for the run, and --warn (or -w) keeps their findings but leaves the exit code at 0. Both take rule IDs or names, repeated or separated by commas: --ignore S6,single-space-decorator -- templates/.

  • A template can switch rules off for itself with a Jinja comment, which applies to the whole file wherever it sits:

    {# j2lint: disable=S6 #}
    {# j2lint: disable=jinja-statements-no-tabs #}
    {# j2lint: disable=S5,S6 j2lint: disable=V1 #}
    
  • --extensions (or -e) lists the suffixes a directory scan treats as templates, j2,jinja,jinja2 by default. A file named on the command line is always linted, whatever its extension.

Licensing

This project is MIT, see LICENSE.md.

j2lint is MIT (Copyright (c) 2021 Arista Networks). This project is written from the rule descriptions; no upstream code is copied. If any upstream code or text is ever copied verbatim, keep Arista's notice in the repo and ship it with the binaries.

Repository

krikri-jinja-linter

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 3
  • about 1 hour ago
  • December 5, 2026
License

MIT License

Links
Synced at

Sat, 05 Dec 2026 05:35:45 GMT

Languages