eslint-plugin-twig-lint 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +294 -2
- package/dist/index.d.ts +64 -0
- package/dist/index.js +53 -0
- package/dist/language/html.d.ts +34 -0
- package/dist/language/html.js +107 -0
- package/dist/language/lexer.d.ts +34 -0
- package/dist/language/lexer.js +219 -0
- package/dist/language/parse.d.ts +46 -0
- package/dist/language/parse.js +254 -0
- package/dist/language/twig-language.d.ts +31 -0
- package/dist/language/twig-language.js +34 -0
- package/dist/language/twig-source-code.d.ts +70 -0
- package/dist/language/twig-source-code.js +121 -0
- package/dist/rules/attribute-spacing.d.ts +3 -0
- package/dist/rules/attribute-spacing.js +41 -0
- package/dist/rules/attribute-wrap.d.ts +3 -0
- package/dist/rules/attribute-wrap.js +15 -0
- package/dist/rules/blank-line-after-block.d.ts +6 -0
- package/dist/rules/blank-line-after-block.js +39 -0
- package/dist/rules/block-indent.d.ts +3 -0
- package/dist/rules/block-indent.js +14 -0
- package/dist/rules/defined-check.d.ts +2 -0
- package/dist/rules/defined-check.js +91 -0
- package/dist/rules/delimiter-spacing.d.ts +3 -0
- package/dist/rules/delimiter-spacing.js +31 -0
- package/dist/rules/deprecated-spaceless.d.ts +6 -0
- package/dist/rules/deprecated-spaceless.js +33 -0
- package/dist/rules/drupal/attach-library-format.d.ts +6 -0
- package/dist/rules/drupal/attach-library-format.js +37 -0
- package/dist/rules/drupal/component-reference.d.ts +6 -0
- package/dist/rules/drupal/component-reference.js +37 -0
- package/dist/rules/drupal/index.d.ts +6 -0
- package/dist/rules/drupal/index.js +9 -0
- package/dist/rules/drupal/no-translate-variable.d.ts +7 -0
- package/dist/rules/drupal/no-translate-variable.js +28 -0
- package/dist/rules/empty-lines.d.ts +2 -0
- package/dist/rules/empty-lines.js +23 -0
- package/dist/rules/endblock-name.d.ts +6 -0
- package/dist/rules/endblock-name.js +43 -0
- package/dist/rules/expression-wrap.d.ts +3 -0
- package/dist/rules/expression-wrap.js +12 -0
- package/dist/rules/final-newline.d.ts +2 -0
- package/dist/rules/final-newline.js +17 -0
- package/dist/rules/hash-key-quotes.d.ts +6 -0
- package/dist/rules/hash-key-quotes.js +30 -0
- package/dist/rules/hash-spacing.d.ts +3 -0
- package/dist/rules/hash-spacing.js +18 -0
- package/dist/rules/include-style.d.ts +7 -0
- package/dist/rules/include-style.js +140 -0
- package/dist/rules/index.d.ts +26 -0
- package/dist/rules/index.js +50 -0
- package/dist/rules/isolated-include.d.ts +6 -0
- package/dist/rules/isolated-include.js +78 -0
- package/dist/rules/named-arguments.d.ts +6 -0
- package/dist/rules/named-arguments.js +31 -0
- package/dist/rules/nested-indent.d.ts +3 -0
- package/dist/rules/nested-indent.js +14 -0
- package/dist/rules/no-raw.d.ts +3 -0
- package/dist/rules/no-raw.js +47 -0
- package/dist/rules/operator-spacing.d.ts +6 -0
- package/dist/rules/operator-spacing.js +21 -0
- package/dist/rules/punctuation-spacing.d.ts +3 -0
- package/dist/rules/punctuation-spacing.js +18 -0
- package/dist/rules/single-quotes.d.ts +6 -0
- package/dist/rules/single-quotes.js +22 -0
- package/dist/rules/trailing-comma.d.ts +8 -0
- package/dist/rules/trailing-comma.js +47 -0
- package/dist/rules/trailing-whitespace.d.ts +2 -0
- package/dist/rules/trailing-whitespace.js +17 -0
- package/dist/rules/variable-name.d.ts +3 -0
- package/dist/rules/variable-name.js +76 -0
- package/dist/util/indent.d.ts +16 -0
- package/dist/util/indent.js +101 -0
- package/dist/util/rule.d.ts +99 -0
- package/dist/util/rule.js +65 -0
- package/dist/util/spacing.d.ts +15 -0
- package/dist/util/spacing.js +44 -0
- package/dist/util/syntax.d.ts +26 -0
- package/dist/util/syntax.js +107 -0
- package/dist/util/wrap.d.ts +16 -0
- package/dist/util/wrap.js +382 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anders Niemi Brundin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,295 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center" alt="title">eslint-plugin-twig-lint</h1>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/eslint-plugin-twig-lint"><img src="https://img.shields.io/npm/v/eslint-plugin-twig-lint?style=flat&colorA=111111&colorB=000000" alt="Version" /></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/eslint-plugin-twig-lint"><img src="https://img.shields.io/npm/dt/eslint-plugin-twig-lint.svg?style=flat&colorA=222222&colorB=000000" alt="Downloads" /></a>
|
|
6
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-000000?style=flat&colorA=333333" alt="License" /></a>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
Lint and format Twig templates with ESLint.
|
|
10
|
+
|
|
11
|
+
## Table of Contents
|
|
12
|
+
|
|
13
|
+
- [Features](#features)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Install](#install)
|
|
16
|
+
- [Setup](#setup)
|
|
17
|
+
- [Editor Integration](#editor-integration)
|
|
18
|
+
- [Presets](#presets)
|
|
19
|
+
- [Settings](#settings)
|
|
20
|
+
- [Rules](#rules)
|
|
21
|
+
- [Layout](#layout)
|
|
22
|
+
- [Syntax](#syntax)
|
|
23
|
+
- [Best Practices](#best-practices)
|
|
24
|
+
- [Drupal](#drupal)
|
|
25
|
+
- [Examples](#examples)
|
|
26
|
+
- [Disabling Rules](#disabling-rules)
|
|
27
|
+
- [FAQ](#faq)
|
|
28
|
+
- [Disclaimer](#disclaimer)
|
|
29
|
+
- [Contributing](#contributing)
|
|
30
|
+
- [License](#license)
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- **Respectful**: Fixes what is wrong and keeps the rest of your style. Multi-line expressions are re-indented,
|
|
35
|
+
never collapsed into one line.
|
|
36
|
+
- **Safe**: Only changes whitespace that does not affect the rendered page. The content of `<pre>`, `<textarea>`,
|
|
37
|
+
`<script>`, `<style>`, comments and `{% trans %}` is never touched, and a file that cannot be parsed is left
|
|
38
|
+
untouched.
|
|
39
|
+
- **Native ESLint**: Twig is a real ESLint language, so you get ESLint's CLI, config, `--fix`, disable comments,
|
|
40
|
+
output formats and editor integration.
|
|
41
|
+
- **Configurable**: Every rule can be turned off or set to `warn` or `error`, and shared settings control
|
|
42
|
+
indentation and line width.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
> [!NOTE]
|
|
47
|
+
> eslint-plugin-twig-lint requires the following minimum versions:
|
|
48
|
+
>
|
|
49
|
+
> - Node.js: 20.19.0
|
|
50
|
+
> - ESLint: 9.15.0
|
|
51
|
+
|
|
52
|
+
### Install
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
npm install --save-dev eslint eslint-plugin-twig-lint
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Setup
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
// eslint.config.js
|
|
62
|
+
|
|
63
|
+
import twigLint from "eslint-plugin-twig-lint";
|
|
64
|
+
|
|
65
|
+
export default [
|
|
66
|
+
// Parse **/*.twig as Twig and enable the general rules.
|
|
67
|
+
twigLint.configs.recommended,
|
|
68
|
+
|
|
69
|
+
// Optional: rules for Drupal themes and modules.
|
|
70
|
+
twigLint.configs.drupal,
|
|
71
|
+
|
|
72
|
+
{
|
|
73
|
+
files: ["**/*.twig"],
|
|
74
|
+
|
|
75
|
+
// Optional shared settings (defaults shown).
|
|
76
|
+
settings: {
|
|
77
|
+
"twig-lint": { indentSize: 2, useTabs: false, printWidth: 80 },
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
// Custom rule overrides (change rule levels or turn rules off).
|
|
81
|
+
rules: {
|
|
82
|
+
"twig-lint/blank-line-after-block": "warn",
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Then run ESLint as usual:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
npx eslint "templates/**/*.twig"
|
|
92
|
+
npx eslint --fix "templates/**/*.twig"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Editor Integration
|
|
96
|
+
|
|
97
|
+
In VS Code, install the [ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
|
|
98
|
+
and add this to `.vscode/settings.json`:
|
|
99
|
+
|
|
100
|
+
```jsonc
|
|
101
|
+
{
|
|
102
|
+
"eslint.validate": ["twig", "html.twig"],
|
|
103
|
+
"eslint.format.enable": true,
|
|
104
|
+
"[twig]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The language id depends on which Twig extension you use. Check the language mode in the status bar.
|
|
109
|
+
[`examples/vscode-demo`](examples/vscode-demo) is a ready-made setup.
|
|
110
|
+
|
|
111
|
+
## Presets
|
|
112
|
+
|
|
113
|
+
- `recommended`\
|
|
114
|
+
Parse `**/*.twig` with the Twig language and enable the general rules. Layout and syntax rules are `error`,
|
|
115
|
+
best practices are `warn`.
|
|
116
|
+
|
|
117
|
+
- `drupal`\
|
|
118
|
+
Enable the [Drupal rules](#drupal). Use it together with `recommended`. Opt-in rules such as
|
|
119
|
+
`drupal/component-reference` stay off until you enable them.
|
|
120
|
+
|
|
121
|
+
## Settings
|
|
122
|
+
|
|
123
|
+
Shared settings go in `settings["twig-lint"]`. Every rule also accepts them as options, which take precedence.
|
|
124
|
+
|
|
125
|
+
| Setting | Default | Description |
|
|
126
|
+
|---|---|---|
|
|
127
|
+
| `indentSize` | `2` | Columns per indentation step |
|
|
128
|
+
| `useTabs` | `false` | Indent with tabs instead of spaces |
|
|
129
|
+
| `printWidth` | `80` | Line width used by `attribute-wrap` and `expression-wrap` |
|
|
130
|
+
|
|
131
|
+
## Rules
|
|
132
|
+
|
|
133
|
+
All general rules are in `recommended`. Fixable rules are fixed by `--fix` and by formatting in the editor.
|
|
134
|
+
|
|
135
|
+
### Layout
|
|
136
|
+
|
|
137
|
+
Enabled as `error`.
|
|
138
|
+
|
|
139
|
+
| Rule | Description | Fixable |
|
|
140
|
+
|---|---|---|
|
|
141
|
+
| [`twig-lint/block-indent`](docs/rules/block-indent.md) | Indentation of Twig blocks and HTML elements | yes |
|
|
142
|
+
| [`twig-lint/nested-indent`](docs/rules/nested-indent.md) | Indentation inside multi-line expressions: one step per line that opens brackets | yes |
|
|
143
|
+
| [`twig-lint/attribute-spacing`](docs/rules/attribute-spacing.md) | `<p{{ attrs }}>` → `<p {{ attrs }}>`. Option `mode`: `space` (default) or `tight` | yes |
|
|
144
|
+
| [`twig-lint/attribute-wrap`](docs/rules/attribute-wrap.md) | One attribute per line when an opening tag is longer than `printWidth`. Option `minAttributes` (default 2) | yes |
|
|
145
|
+
| [`twig-lint/expression-wrap`](docs/rules/expression-wrap.md) | Breaks Twig expressions longer than `printWidth`: maps, `set`, `if` and call arguments | yes |
|
|
146
|
+
| [`twig-lint/delimiter-spacing`](docs/rules/delimiter-spacing.md) | Exactly one space inside `{{ }}`, `{% %}` and `{# #}` | yes |
|
|
147
|
+
| [`twig-lint/blank-line-after-block`](docs/rules/blank-line-after-block.md) | An empty line after `{% endif %}`, `{% endfor %}` and similar closing tags | yes |
|
|
148
|
+
| [`twig-lint/empty-lines`](docs/rules/empty-lines.md) | At most one empty line in a row | yes |
|
|
149
|
+
| [`twig-lint/trailing-whitespace`](docs/rules/trailing-whitespace.md) | No whitespace at the end of lines | yes |
|
|
150
|
+
| [`twig-lint/final-newline`](docs/rules/final-newline.md) | Exactly one newline at the end of the file | yes |
|
|
151
|
+
|
|
152
|
+
### Syntax
|
|
153
|
+
|
|
154
|
+
Enabled as `error`.
|
|
155
|
+
|
|
156
|
+
| Rule | Description | Fixable |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| [`twig-lint/operator-spacing`](docs/rules/operator-spacing.md) | One space around binary operators, none around `.`, `\|` and `..` | yes |
|
|
159
|
+
| [`twig-lint/punctuation-spacing`](docs/rules/punctuation-spacing.md) | One space after commas, none inside brackets | yes |
|
|
160
|
+
| [`twig-lint/hash-spacing`](docs/rules/hash-spacing.md) | `{key: value}` and `name: value`: no space before the colon, one after | yes |
|
|
161
|
+
| [`twig-lint/single-quotes`](docs/rules/single-quotes.md) | `'single'` quotes unless the string contains `'` or uses interpolation | yes |
|
|
162
|
+
| [`twig-lint/hash-key-quotes`](docs/rules/hash-key-quotes.md) | No quotes around plain hash keys: `{'class': x}` → `{class: x}` | yes |
|
|
163
|
+
| [`twig-lint/trailing-comma`](docs/rules/trailing-comma.md) | A trailing comma when the closing bracket is on its own line. Option `multiline` | yes |
|
|
164
|
+
| [`twig-lint/named-arguments`](docs/rules/named-arguments.md) | `with_context: false` instead of the deprecated `with_context = false` | yes |
|
|
165
|
+
| [`twig-lint/endblock-name`](docs/rules/endblock-name.md) | `{% endblock content %}` and `{% endmacro card %}`. Options `mode`: `always` (default) or `never`, and `minLines` | yes |
|
|
166
|
+
| [`twig-lint/include-style`](docs/rules/include-style.md) | `{{ include() }}` or `{% include %}`. Option `style`: `both` (default), `function` or `tag` | yes |
|
|
167
|
+
|
|
168
|
+
### Best Practices
|
|
169
|
+
|
|
170
|
+
Enabled as `warn`. These rules point out code that works but may cause problems, so they are never fixed
|
|
171
|
+
automatically. Two of them offer a suggestion that you can apply in the editor.
|
|
172
|
+
|
|
173
|
+
| Rule | Description | Fixable |
|
|
174
|
+
|---|---|---|
|
|
175
|
+
| [`twig-lint/isolated-include`](docs/rules/isolated-include.md) | `include()` with `with_context: false` and `{% include %}` with `only`. Option `embed`. The editor suggests the fix | no |
|
|
176
|
+
| [`twig-lint/defined-check`](docs/rules/defined-check.md) | `x is defined and x is not empty` → `x is not empty`. Option `mode`: `simplify`, `require` or `allow`. The editor suggests the fix | no |
|
|
177
|
+
| [`twig-lint/variable-name`](docs/rules/variable-name.md) | `snake_case` for variables, loop variables, macros and arrow function parameters created in the template | no |
|
|
178
|
+
| [`twig-lint/no-raw`](docs/rules/no-raw.md) | Warns about `\|raw`, which turns off escaping. Option `allow` for known-safe values | no |
|
|
179
|
+
| [`twig-lint/deprecated-spaceless`](docs/rules/deprecated-spaceless.md) | Warns about the `spaceless` filter, deprecated since Twig 3.12 | no |
|
|
180
|
+
|
|
181
|
+
### Drupal
|
|
182
|
+
|
|
183
|
+
Enabled by the `drupal` preset.
|
|
184
|
+
|
|
185
|
+
| Rule | Description | Fixable | Level |
|
|
186
|
+
|---|---|---|---|
|
|
187
|
+
| [`twig-lint/drupal/attach-library-format`](docs/rules/drupal/attach-library-format.md) | `attach_library('extension/library')`, not a component ID like `'theme:button'` | no | error |
|
|
188
|
+
| [`twig-lint/drupal/no-translate-variable`](docs/rules/drupal/no-translate-variable.md) | `\|t` on a variable cannot be extracted for translation | no | warn |
|
|
189
|
+
| [`twig-lint/drupal/component-reference`](docs/rules/drupal/component-reference.md) | Include components by SDC ID, not by file path. Option `folders` | no | off (opt-in) |
|
|
190
|
+
|
|
191
|
+
### Examples
|
|
192
|
+
|
|
193
|
+
Nested indentation is fixed without collapsing the expression:
|
|
194
|
+
|
|
195
|
+
```twig
|
|
196
|
+
{# before #}
|
|
197
|
+
{% set heading_attributes = create_attribute({
|
|
198
|
+
class: [
|
|
199
|
+
'teaser__heading',
|
|
200
|
+
],
|
|
201
|
+
}, title_attributes) %}
|
|
202
|
+
|
|
203
|
+
{# after #}
|
|
204
|
+
{% set heading_attributes = create_attribute({
|
|
205
|
+
class: [
|
|
206
|
+
'teaser__heading',
|
|
207
|
+
],
|
|
208
|
+
}, title_attributes) %}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Long opening tags get one attribute per line, and an `{% if %}` between attributes becomes a block:
|
|
212
|
+
|
|
213
|
+
```twig
|
|
214
|
+
<li
|
|
215
|
+
class="card-grid__item card-grid__item--featured"
|
|
216
|
+
{% if is_mixed %}
|
|
217
|
+
style="--card-grid-index: {{ loop.index }};"
|
|
218
|
+
{% endif %}
|
|
219
|
+
>
|
|
220
|
+
{{ item }}
|
|
221
|
+
</li>
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Long conditions break at the lowest-precedence operator:
|
|
225
|
+
|
|
226
|
+
```twig
|
|
227
|
+
{% if has_choices
|
|
228
|
+
or (visible_choices is iterable
|
|
229
|
+
and visible_choices|length > 0
|
|
230
|
+
)
|
|
231
|
+
%}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
More examples are in [`test/fixtures`](test/fixtures), where each `input.twig` has an `expected.twig`.
|
|
235
|
+
|
|
236
|
+
### Disabling Rules
|
|
237
|
+
|
|
238
|
+
ESLint's disable comments work inside Twig comments:
|
|
239
|
+
|
|
240
|
+
```twig
|
|
241
|
+
{# eslint-disable-next-line twig-lint/attribute-wrap -- generated markup #}
|
|
242
|
+
<div class="…very long…">
|
|
243
|
+
|
|
244
|
+
{# eslint-disable twig-lint/blank-line-after-block #}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## FAQ
|
|
248
|
+
|
|
249
|
+
**Is it only for Drupal?**\
|
|
250
|
+
No. The general rules work on any Twig template, and Drupal-style templates (`create_attribute()`,
|
|
251
|
+
`<p {{ attributes }}>`, `{% trans %}`) are well covered by the tests. Rules that only make sense in Drupal are
|
|
252
|
+
under `twig-lint/drupal/` and only run with the `drupal` preset.
|
|
253
|
+
|
|
254
|
+
**What happens with a file that cannot be parsed?**\
|
|
255
|
+
ESLint reports a fatal parsing error with the line and column, and `--fix` leaves the file untouched.
|
|
256
|
+
|
|
257
|
+
**Does `--fix` change the rendered HTML?**\
|
|
258
|
+
It should not. The fixtures are checked by rendering both `input.twig` and `expected.twig` with Twig and
|
|
259
|
+
comparing the HTML trees (see [Contributing](#contributing)).
|
|
260
|
+
|
|
261
|
+
## Disclaimer
|
|
262
|
+
|
|
263
|
+
This project is not affiliated with or endorsed by Symfony, the Twig project or the ESLint project.
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
Contributions are welcome! Open an issue or a pull request.
|
|
268
|
+
|
|
269
|
+
```sh
|
|
270
|
+
npm install
|
|
271
|
+
npm test # all fixtures and plugin tests via ESLint's API
|
|
272
|
+
npm run coverage # the same, with a coverage report
|
|
273
|
+
npm run typecheck
|
|
274
|
+
npm run build # compiles to dist/
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
CI runs the tests on Node 22 and 24 with ESLint 9.15, 9 and 10, checks that the packed package works on its
|
|
278
|
+
own, and runs the PHP checks below.
|
|
279
|
+
|
|
280
|
+
The source is TypeScript that Node runs directly (type stripping, Node 22.18+). The published package
|
|
281
|
+
contains compiled JavaScript in `dist/`.
|
|
282
|
+
|
|
283
|
+
A new formatting case is a folder in [`test/fixtures`](test/fixtures) with an `input.twig` and an
|
|
284
|
+
`expected.twig` (or an `expected-error.txt` for a file that must be left untouched). Two PHP scripts check
|
|
285
|
+
that every `expected.twig` is valid Twig and renders the same HTML as its `input.twig`. They need a Composer
|
|
286
|
+
project with `twig/twig`:
|
|
287
|
+
|
|
288
|
+
```sh
|
|
289
|
+
TWIG_AUTOLOAD=/path/to/vendor/autoload.php php test/parse-check.php test/fixtures/*/expected.twig
|
|
290
|
+
TWIG_AUTOLOAD=/path/to/vendor/autoload.php php test/render-check.php test/fixtures/*/
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { TwigLanguage } from './language/twig-language.ts';
|
|
2
|
+
import type { TwigRuleDefinition } from './util/rule.ts';
|
|
3
|
+
export { TwigLanguage } from './language/twig-language.ts';
|
|
4
|
+
export { TwigSourceCode, type TwigNode } from './language/twig-source-code.ts';
|
|
5
|
+
export type { SharedSettings } from './util/rule.ts';
|
|
6
|
+
type Level = 'error' | 'warn';
|
|
7
|
+
declare const plugin: {
|
|
8
|
+
meta: {
|
|
9
|
+
name: string;
|
|
10
|
+
version: string;
|
|
11
|
+
namespace: string;
|
|
12
|
+
};
|
|
13
|
+
languages: {
|
|
14
|
+
twig: TwigLanguage;
|
|
15
|
+
};
|
|
16
|
+
rules: {
|
|
17
|
+
'drupal/attach-library-format': TwigRuleDefinition;
|
|
18
|
+
'drupal/component-reference': TwigRuleDefinition;
|
|
19
|
+
'drupal/no-translate-variable': TwigRuleDefinition;
|
|
20
|
+
'attribute-spacing': TwigRuleDefinition;
|
|
21
|
+
'attribute-wrap': TwigRuleDefinition;
|
|
22
|
+
'blank-line-after-block': TwigRuleDefinition;
|
|
23
|
+
'block-indent': TwigRuleDefinition;
|
|
24
|
+
'defined-check': TwigRuleDefinition;
|
|
25
|
+
'delimiter-spacing': TwigRuleDefinition;
|
|
26
|
+
'deprecated-spaceless': TwigRuleDefinition;
|
|
27
|
+
'empty-lines': TwigRuleDefinition;
|
|
28
|
+
'endblock-name': TwigRuleDefinition;
|
|
29
|
+
'expression-wrap': TwigRuleDefinition;
|
|
30
|
+
'final-newline': TwigRuleDefinition;
|
|
31
|
+
'hash-key-quotes': TwigRuleDefinition;
|
|
32
|
+
'hash-spacing': TwigRuleDefinition;
|
|
33
|
+
'include-style': TwigRuleDefinition;
|
|
34
|
+
'isolated-include': TwigRuleDefinition;
|
|
35
|
+
'named-arguments': TwigRuleDefinition;
|
|
36
|
+
'nested-indent': TwigRuleDefinition;
|
|
37
|
+
'no-raw': TwigRuleDefinition;
|
|
38
|
+
'operator-spacing': TwigRuleDefinition;
|
|
39
|
+
'punctuation-spacing': TwigRuleDefinition;
|
|
40
|
+
'single-quotes': TwigRuleDefinition;
|
|
41
|
+
'trailing-comma': TwigRuleDefinition;
|
|
42
|
+
'trailing-whitespace': TwigRuleDefinition;
|
|
43
|
+
'variable-name': TwigRuleDefinition;
|
|
44
|
+
};
|
|
45
|
+
configs: {
|
|
46
|
+
/** Parses `**\/*.twig` as Twig and enables the general rules. */
|
|
47
|
+
recommended: {
|
|
48
|
+
name: string;
|
|
49
|
+
files: string[];
|
|
50
|
+
plugins: Record<string, unknown>;
|
|
51
|
+
language: string;
|
|
52
|
+
rules: Record<string, Level>;
|
|
53
|
+
};
|
|
54
|
+
/** Enables the `twig-lint/drupal/*` rules. Use it together with `recommended`. */
|
|
55
|
+
drupal: {
|
|
56
|
+
name: string;
|
|
57
|
+
files: string[];
|
|
58
|
+
plugins: Record<string, unknown>;
|
|
59
|
+
language: string;
|
|
60
|
+
rules: Record<string, Level>;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
export default plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// eslint-plugin-twig-lint: Twig as an ESLint language, plus rules for formatting and linting.
|
|
2
|
+
//
|
|
3
|
+
// import twigLint from 'eslint-plugin-twig-lint';
|
|
4
|
+
// export default [twigLint.configs.recommended]; // any Twig
|
|
5
|
+
// export default [twigLint.configs.recommended, twigLint.configs.drupal]; // Drupal themes and modules
|
|
6
|
+
import { TwigLanguage } from "./language/twig-language.js";
|
|
7
|
+
import { drupalRules } from "./rules/drupal/index.js";
|
|
8
|
+
import { rules as generalRules } from "./rules/index.js";
|
|
9
|
+
export { TwigLanguage } from "./language/twig-language.js";
|
|
10
|
+
export { TwigSourceCode } from "./language/twig-source-code.js";
|
|
11
|
+
/** `twig-lint/<id>` → the rule's level in its preset. Opt-in rules (`off`) are left out. */
|
|
12
|
+
function presetRules(rules) {
|
|
13
|
+
const out = {};
|
|
14
|
+
for (const [id, rule] of Object.entries(rules)) {
|
|
15
|
+
const level = rule.meta.docs.level;
|
|
16
|
+
if (level !== 'off')
|
|
17
|
+
out[`twig-lint/${id}`] = level;
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
const rules = { ...generalRules, ...drupalRules };
|
|
22
|
+
const plugin = {
|
|
23
|
+
meta: {
|
|
24
|
+
name: 'eslint-plugin-twig-lint',
|
|
25
|
+
version: '0.1.0',
|
|
26
|
+
namespace: 'twig-lint',
|
|
27
|
+
},
|
|
28
|
+
languages: {
|
|
29
|
+
twig: new TwigLanguage(),
|
|
30
|
+
},
|
|
31
|
+
rules,
|
|
32
|
+
configs: {
|
|
33
|
+
/** Parses `**\/*.twig` as Twig and enables the general rules. */
|
|
34
|
+
recommended: {
|
|
35
|
+
name: 'twig-lint/recommended',
|
|
36
|
+
files: ['**/*.twig'],
|
|
37
|
+
plugins: {},
|
|
38
|
+
language: 'twig-lint/twig',
|
|
39
|
+
rules: presetRules(generalRules),
|
|
40
|
+
},
|
|
41
|
+
/** Enables the `twig-lint/drupal/*` rules. Use it together with `recommended`. */
|
|
42
|
+
drupal: {
|
|
43
|
+
name: 'twig-lint/drupal',
|
|
44
|
+
files: ['**/*.twig'],
|
|
45
|
+
plugins: {},
|
|
46
|
+
language: 'twig-lint/twig',
|
|
47
|
+
rules: presetRules(drupalRules),
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
plugin.configs.recommended.plugins['twig-lint'] = plugin;
|
|
52
|
+
plugin.configs.drupal.plugins['twig-lint'] = plugin;
|
|
53
|
+
export default plugin;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Segment } from './lexer.ts';
|
|
2
|
+
export interface Range {
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
}
|
|
6
|
+
export interface HtmlTag {
|
|
7
|
+
start: number;
|
|
8
|
+
end: number;
|
|
9
|
+
name: string;
|
|
10
|
+
nameStart: number;
|
|
11
|
+
nameEnd: number;
|
|
12
|
+
closing: boolean;
|
|
13
|
+
selfClosing: boolean;
|
|
14
|
+
void: boolean;
|
|
15
|
+
/** The tag name is a Twig expression, e.g. `<{{ type }}>`. */
|
|
16
|
+
dynamic: boolean;
|
|
17
|
+
/** Position of `>`, or of `/` in `/>`. */
|
|
18
|
+
closeStart: number;
|
|
19
|
+
/** Quoted attribute values, including the quotes. */
|
|
20
|
+
attrValues: Range[];
|
|
21
|
+
}
|
|
22
|
+
export interface RawElement {
|
|
23
|
+
open: HtmlTag;
|
|
24
|
+
close: HtmlTag;
|
|
25
|
+
}
|
|
26
|
+
export interface HtmlScan {
|
|
27
|
+
tags: HtmlTag[];
|
|
28
|
+
raw: RawElement[];
|
|
29
|
+
comments: Range[];
|
|
30
|
+
}
|
|
31
|
+
export declare const VOID_ELEMENTS: Set<string>;
|
|
32
|
+
export declare const RAW_ELEMENTS: Set<string>;
|
|
33
|
+
export declare function maskTwig(src: string, segments: Segment[]): string;
|
|
34
|
+
export declare function scanHtml(src: string, masked: string): HtmlScan;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// HTML scanning on a masked copy of the source. Twig segments are replaced with \0 (newlines are kept),
|
|
2
|
+
// so positions match the original and Twig expressions do not interfere with the scan.
|
|
3
|
+
import { TwigParseError, lineAt } from "./lexer.js";
|
|
4
|
+
export const VOID_ELEMENTS = new Set([
|
|
5
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
6
|
+
]);
|
|
7
|
+
export const RAW_ELEMENTS = new Set(['pre', 'textarea', 'script', 'style']);
|
|
8
|
+
export function maskTwig(src, segments) {
|
|
9
|
+
const chars = src.split('');
|
|
10
|
+
for (const seg of segments) {
|
|
11
|
+
if (seg.kind === 'text' && !seg.verbatim)
|
|
12
|
+
continue;
|
|
13
|
+
for (let i = seg.start; i < seg.end; i++)
|
|
14
|
+
if (chars[i] !== '\n')
|
|
15
|
+
chars[i] = '\0';
|
|
16
|
+
}
|
|
17
|
+
return chars.join('');
|
|
18
|
+
}
|
|
19
|
+
const NAME_CHAR = /[a-zA-Z0-9:_.-]/;
|
|
20
|
+
export function scanHtml(src, masked) {
|
|
21
|
+
const tags = [];
|
|
22
|
+
const raw = [];
|
|
23
|
+
const comments = [];
|
|
24
|
+
const lower = masked.toLowerCase();
|
|
25
|
+
let i = 0;
|
|
26
|
+
while ((i = masked.indexOf('<', i)) >= 0) {
|
|
27
|
+
if (masked.startsWith('<!--', i)) {
|
|
28
|
+
const e = masked.indexOf('-->', i + 4);
|
|
29
|
+
const end = e < 0 ? masked.length : e + 3;
|
|
30
|
+
comments.push({ start: i, end });
|
|
31
|
+
i = end;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const next = masked[i + 1];
|
|
35
|
+
if (next === '!' || next === '?') {
|
|
36
|
+
const e = masked.indexOf('>', i);
|
|
37
|
+
i = e < 0 ? masked.length : e + 1;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const closing = next === '/';
|
|
41
|
+
const nameStart = i + (closing ? 2 : 1);
|
|
42
|
+
let j = nameStart;
|
|
43
|
+
const first = masked[j];
|
|
44
|
+
let dynamic = false;
|
|
45
|
+
if (first !== undefined && /[a-zA-Z]/.test(first)) {
|
|
46
|
+
while (j < masked.length && NAME_CHAR.test(masked[j]))
|
|
47
|
+
j++;
|
|
48
|
+
}
|
|
49
|
+
else if (first === '\0') {
|
|
50
|
+
dynamic = true;
|
|
51
|
+
while (j < masked.length && masked[j] === '\0')
|
|
52
|
+
j++;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
i++;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const nameEnd = j;
|
|
59
|
+
const attrValues = [];
|
|
60
|
+
let lastNonWs = '';
|
|
61
|
+
while (j < masked.length && masked[j] !== '>') {
|
|
62
|
+
const ch = masked[j];
|
|
63
|
+
if ((ch === '"' || ch === "'") && lastNonWs === '=') {
|
|
64
|
+
const e = masked.indexOf(ch, j + 1);
|
|
65
|
+
if (e < 0)
|
|
66
|
+
break;
|
|
67
|
+
attrValues.push({ start: j, end: e + 1 });
|
|
68
|
+
j = e + 1;
|
|
69
|
+
lastNonWs = ch;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!/\s/.test(ch))
|
|
73
|
+
lastNonWs = ch;
|
|
74
|
+
j++;
|
|
75
|
+
}
|
|
76
|
+
if (j >= masked.length) {
|
|
77
|
+
throw new TwigParseError(`HTML tag "${src.slice(i, nameEnd)}" on line ${lineAt(src, i)} is never closed`, i);
|
|
78
|
+
}
|
|
79
|
+
const selfClosing = masked[j - 1] === '/';
|
|
80
|
+
const name = dynamic ? src.slice(nameStart, nameEnd) : src.slice(nameStart, nameEnd).toLowerCase();
|
|
81
|
+
const tag = {
|
|
82
|
+
start: i, end: j + 1, name, nameStart, nameEnd, closing, selfClosing,
|
|
83
|
+
void: !dynamic && VOID_ELEMENTS.has(name), dynamic,
|
|
84
|
+
closeStart: selfClosing ? j - 1 : j, attrValues,
|
|
85
|
+
};
|
|
86
|
+
tags.push(tag);
|
|
87
|
+
i = j + 1;
|
|
88
|
+
if (!closing && !selfClosing && !dynamic && RAW_ELEMENTS.has(name)) {
|
|
89
|
+
const c = lower.indexOf(`</${name}`, i);
|
|
90
|
+
if (c < 0) {
|
|
91
|
+
throw new TwigParseError(`<${name}> on line ${lineAt(src, tag.start)} is never closed`, tag.start);
|
|
92
|
+
}
|
|
93
|
+
const e = masked.indexOf('>', c);
|
|
94
|
+
if (e < 0) {
|
|
95
|
+
throw new TwigParseError(`</${name} on line ${lineAt(src, c)} is never closed`, c);
|
|
96
|
+
}
|
|
97
|
+
const close = {
|
|
98
|
+
start: c, end: e + 1, name, nameStart: c + 2, nameEnd: c + 2 + name.length, closing: true,
|
|
99
|
+
selfClosing: false, void: false, dynamic: false, closeStart: e, attrValues: [],
|
|
100
|
+
};
|
|
101
|
+
tags.push(close);
|
|
102
|
+
raw.push({ open: tag, close });
|
|
103
|
+
i = e + 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { tags, raw, comments };
|
|
107
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type TokenType = 'name' | 'number' | 'string' | 'operator' | 'punctuation';
|
|
2
|
+
export interface Token {
|
|
3
|
+
type: TokenType;
|
|
4
|
+
value: string;
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
/** For opening brackets: index of the matching closing bracket in the same segment. */
|
|
8
|
+
match?: number;
|
|
9
|
+
}
|
|
10
|
+
export type SegmentKind = 'text' | 'output' | 'block' | 'comment';
|
|
11
|
+
export interface Segment {
|
|
12
|
+
kind: SegmentKind;
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
/** Twig segments: position right after the opening delimiter (including `-`/`~`). */
|
|
16
|
+
innerStart: number;
|
|
17
|
+
/** Twig segments: start of the closing delimiter (including `-`/`~`). */
|
|
18
|
+
closeStart: number;
|
|
19
|
+
/** The opening delimiter, e.g. `{{`, `{%-`. Empty for text. */
|
|
20
|
+
open: string;
|
|
21
|
+
/** The closing delimiter, e.g. `}}`, `-%}`. Empty for text. */
|
|
22
|
+
close: string;
|
|
23
|
+
tokens: Token[];
|
|
24
|
+
/** Block segments: the tag name (first name token). */
|
|
25
|
+
tag: string;
|
|
26
|
+
/** Text inside {% verbatim %}/{% raw %}. */
|
|
27
|
+
verbatim: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare class TwigParseError extends Error {
|
|
30
|
+
offset: number;
|
|
31
|
+
constructor(message: string, offset: number);
|
|
32
|
+
}
|
|
33
|
+
export declare function lineAt(src: string, pos: number): number;
|
|
34
|
+
export declare function lex(src: string): Segment[];
|