Package: j2lint
Jinja2-LinterAVD Ecosystem - Jinja2 LinterProject GoalsBuild a Jinja2 linter that will provide the following capabilities:Validate syntax according toAVD style guide.Capability to run as part of a CI pipeline to enforce j2lint rules.Develop an extension that works with VSCode and potentially other IDEs i.e PyCharm.Syntax and code style issuesCodeShort DescriptionDescriptionS0jinja-syntax-errorJinja2 syntax should be correctS1single-space-decoratorA single space should be added between Jinja2 curly brackets and a variable's nameS2operator-enclosed-by-spacesWhen variables are used in combination with an operator, the operator shall be enclosed by spaceS3jinja-statements-indentationNested jinja code block should follow next rules:- All J2 statements must be enclosed by 1 space- All J2 statements must be indented by 4 more spaces within jinja delimiter- To close a control, end tag must have same indentation levelS4jinja-statements-single-spaceJinja statement should have at least a single space after '{%' and a single space before '%}'S5jinja-statements-no-tabsIndentation should not use tabulation but 4 spacesS6jinja-statements-delimiterJinja statements should not have {%- or {%+ or -%} as delimitersS7single-statement-per-lineJinja statements should be on separate linesV1jinja-variable-lower-caseAll variables should use lower caseV2jinja-variable-formatIf variable is multi-words, underscore_should be used as a separatorGetting StartedRequirementsPython version 3.8+Install with pipTo get started, you can use Python pip to install j2lint:Install the latest stable version:pip3installj2lintInstall the latest development version:pip3installgit+https://github.com/aristanetworks/j2lint.gitRunning the linterj2lint<path-to-directory-of-templates>Running the linter on a specific filej2lint<path-to-directory-of-templates>/template.j2Listing linting rulesj2lint--listRunning the linter with verbose linter error outputj2lint<path-to-directory-of-templates>--verboseRunning the linter with logs enabled. Logs saved in jinja2-linter.log in the current directoryj2lint<path-to-directory-of-templates>--logTo enable debug logs, use both options:j2lint<path-to-directory-of-templates>--log--debugRunning the linter with JSON format for linter error outputj2lint<path-to-directory-of-templates>--jsonIgnoring rulesThe --ignore option can have one or more of these values: syntax-error, single-space-decorator, filter-enclosed-by-spaces, jinja-statement-single-space, jinja-statements-indentation, no-tabs, single-statement-per-line, jinja-delimiter, jinja-variable-lower-case, jinja-variable-format.If multiple rules are to be ignored, use the --ignore option along with rule descriptions separated by space.j2lint<path-to-directory-of-templates>--ignore<rule_description1><rule_desc>NoteThis runs the custom linting rules in addition to the default linting rules.
When using the-i/--ignoreor-w/--warnoptions, the arguments MUST either:Be entered at the end of the CLI as in the example aboveBe entered as the last options before the<path-to-directory-of-templates>with--separator.  e.g.j2lint--ignore<rule_description1><rule_desc>--<path-to-directory-of-templates>If one or more linting rules are to be ignored only for a specific jinja template file, add a Jinja comment at the top of the file. The rule can be disabled using the short description of the rule or the id of the rule.{# j2lint: disable=S6}

# OR
{# j2lint: disable=jinja-delimiter #}Disabling multiple rules{# j2lint: disable=jinja-delimiter j2lint: disable=S1 #}Adding custom rulesCreate a new rules directory under j2lint folder.Add custom rule classes which are similar to classes in j2lint/rules directory:
The file name of rules should be in snake_case and the class name should be the PascalCase version of the file name. For example:File name:jinja_operator_has_spaces_rule.pyClass name:JinjaOperatorHasSpacesRuleRun the jinja2 linter using --rules-dir optionj2lint<path-to-directory-of-templates>--rules-dir<custom-rules-directory>NoteThis runs the custom linting rules in addition to the default linting rules.Running jinja2 linter help commandj2lint--helpRunning jinja2 linter on STDIN template. This option can be used with VS Code.j2lint--stdinUsing j2lint as a pre-commit-hookAdd j2lint pre-commit hook inside your repository in .pre-commit-config.yaml.-repo:https://github.com/aristanetworks/j2lint.gitrev:<release_tag/sha>hooks:-id:j2lintRun pre-commit ->pre-commit run --all-filesNoteWhen using-i/--ignoreor-w/--warnargument in pre-commit, use the
following syntax-repo:https://github.com/aristanetworks/j2lint.gitrev:<release_tag/sha>hooks:-id:j2lint# Using -- to separate the end of ignore from the positional arguments# passed to j2lintargs:[--ignore,S3,jinja-statements-single-space,--]AcknowledgmentsThis project is based onsalt-lintandjinjalint