super-slim 3.0.0 → 3.1.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -14
- data/README.md +289 -117
- data/lib/super_slim/cli.rb +45 -9
- data/lib/super_slim/compiler.rb +29 -10
- data/lib/super_slim/formatter/line_wrapper.rb +19 -18
- data/lib/super_slim/formatter.rb +91 -21
- data/lib/super_slim/linter/presets.rb +72 -0
- data/lib/super_slim/linter/rules.rb +20 -18
- data/lib/super_slim/linter.rb +68 -6
- data/lib/super_slim/parallel.rb +31 -11
- data/lib/super_slim/parser/ruby_brackets.rb +115 -0
- data/lib/super_slim/parser.rb +60 -10
- data/lib/super_slim/rubocop.rb +27 -5
- data/lib/super_slim/runtime.rb +12 -4
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +2 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8a3894df5f66ffe80daaf131669b5457659e414579256020b670719ec7bddf8b
|
|
4
|
+
data.tar.gz: 696bb115aa3776422a79dcde2f31984f35690ccf08af50cf43c9655c8cc2fc08
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ee4af8fa3325f7dcbea6a271776987caef9817a97a2e579ad38f01b4fc054a2c4e98ef04b03a10f8ca8efefd8415f8fc37431381aebe6facf8474c03b4eca2e5
|
|
7
|
+
data.tar.gz: 33e7e5274420e92acfe0c824ee5f710dd544c466f3e6ae2be22b386949d6d3917d607d8a4787ab2c6a639308002f1165c02ca4b3ead9be2e3eaf69328c311986
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.1.1] — 2026-11-06
|
|
6
|
+
|
|
7
|
+
Two ways for data to inject markup closed, hostile templates read in linear time, and Prism kept out of rendering.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- Loading SuperSlim no longer loads Prism, which only `super-slim format` needs: since 3.1.0, every process that
|
|
11
|
+
loaded the gem, each Puma worker included, paid Prism's load time and about 7 MB.
|
|
12
|
+
- A tab, line feed, form feed or carriage return in a `*splat` or `data`/`aria` key raises
|
|
13
|
+
`InvalidAttributeNameError` instead of ending the attribute name, and `*{tag: …}` raises the new
|
|
14
|
+
`InvalidTagNameError` when its value is not a tag name: both let data inject markup, as in Slim 5.2.2. No valid
|
|
15
|
+
page changes.
|
|
16
|
+
- Hostile templates no longer hang or crash the compiler, `lint`, `format` or the RuboCop plugin: nested `#{`, long
|
|
17
|
+
continued code and deep nesting are read in linear time, and tags nested more than 500 levels deep are a syntax
|
|
18
|
+
error.
|
|
19
|
+
- On a hostile checkout, the CLI skips symlinks, FIFOs and devices, writes atomically and escapes control
|
|
20
|
+
characters in what it prints; a malformed `.super-slim.yml` is a usage error that names the setting.
|
|
21
|
+
- `super-slim format` refuses a wrap of a template that doesn't compile and a move of `__LINE__`, and its check
|
|
22
|
+
works on Prism 0.19 (Ruby 3.3) and Prism 2.
|
|
23
|
+
|
|
24
|
+
## [3.1.0] — 2026-11-06
|
|
25
|
+
|
|
26
|
+
A Bootstrap 5 preset for the linter, Ruby code that continues after an open bracket, and a faster formatter.
|
|
27
|
+
|
|
28
|
+
**New**
|
|
29
|
+
- `preset: bootstrap5` on DeprecatedClasses and DeprecatedAttributes: the Bootstrap 4 classes and component data
|
|
30
|
+
attributes Bootstrap 5.3 renamed, breakpoints included (`text-md-right`). The rule's own entries go over it, and
|
|
31
|
+
`~` drops one of the preset.
|
|
32
|
+
- A `(`, `[` or `{` that ends a line of code (`-`, `=`, `tag =`) continues it until the bracket closes, one argument
|
|
33
|
+
per line; Slim refused these lines.
|
|
34
|
+
|
|
35
|
+
**Faster**
|
|
36
|
+
- `super-slim format` takes 3.5 s instead of 4.6 s on 7,500 templates at 120 columns.
|
|
37
|
+
|
|
38
|
+
**Fixed**
|
|
39
|
+
- `super-slim format` no longer refuses a trailing comma or call parentheses, and no longer takes `link_to(a) + b`
|
|
40
|
+
for `link_to (a) + b`.
|
|
41
|
+
- In DeprecatedClasses, the longest prefix wins: `badge-promo-*` over `badge-*`.
|
|
42
|
+
- An empty `classes:` or `attributes:` no longer crashes the linter, and an unknown preset exits 64 with its name.
|
|
43
|
+
|
|
5
44
|
## [3.0.0] — 2026-11-05
|
|
6
45
|
|
|
7
46
|
SuperSlim is published as super-slim, the name of its command.
|
|
@@ -49,14 +88,14 @@ Two fixes to `super-slim`: braces around wrapped attributes, and `--help`.
|
|
|
49
88
|
|
|
50
89
|
## [2.0.0] — 2026-11-05
|
|
51
90
|
|
|
52
|
-
|
|
91
|
+
The gem takes the name SuperSlim, sheds what a Rails app doesn't need, and `super-slim fmt` wraps long lines.
|
|
53
92
|
|
|
54
93
|
**Breaking**
|
|
55
94
|
- The gem is renamed: `super_slim` in the Gemfile, the `SuperSlim` module, the `super-slim lint|fmt` command,
|
|
56
95
|
`.super-slim.yml` and `plugins: [super_slim]` for RuboCop. Compiled templates name their variables
|
|
57
|
-
`_super_slim1
|
|
58
|
-
- No more Tilt adapter: `require
|
|
59
|
-
- No more strict mode (`
|
|
96
|
+
`_super_slim1`, after the new name.
|
|
97
|
+
- No more Tilt adapter: its `require` has no replacement. Rails and `SuperSlim.compile` are unchanged.
|
|
98
|
+
- No more strict mode (`strict: true`): run `super-slim fmt --check` in CI instead.
|
|
60
99
|
- No more custom shortcuts: only `#id` and `.class`.
|
|
61
100
|
- Only `doctype 5`, `doctype html` and `doctype xml` remain; HTML 4 and XHTML doctypes, such as
|
|
62
101
|
`doctype transitional`, raise `Invalid doctype`.
|
|
@@ -69,10 +108,10 @@ SlimX becomes SuperSlim, sheds what a Rails app doesn't need, and `super-slim fm
|
|
|
69
108
|
|
|
70
109
|
## [1.1.0] — 2026-11-04
|
|
71
110
|
|
|
72
|
-
A RuboCop plugin that lints the Ruby inside templates with
|
|
111
|
+
A RuboCop plugin that lints the Ruby inside templates with SuperSlim's own parser, and templates compiled up to 10% faster.
|
|
73
112
|
|
|
74
113
|
**New**
|
|
75
|
-
- A RuboCop plugin (`plugins: [
|
|
114
|
+
- A RuboCop plugin (`plugins: [super-slim]`) lints the Ruby of templates: `-` and `=` lines, code attributes, splats,
|
|
76
115
|
`#{}` interpolations and `ruby:` blocks. Offenses point at their line and column in the `.slim` file, and
|
|
77
116
|
autocorrection writes back into it. It replaces rubocop-slim, and with it slimi and Temple; unlike slimi, it
|
|
78
117
|
also reads the output of a tag with whitespace markers and attributes (`span> title="x" = name`).
|
|
@@ -82,7 +121,7 @@ A RuboCop plugin that lints the Ruby inside templates with SlimX's own parser, a
|
|
|
82
121
|
public templates, Ruby 3.3 and 4.0). Rendering is unchanged.
|
|
83
122
|
|
|
84
123
|
**Fixed**
|
|
85
|
-
- `
|
|
124
|
+
- `super-slim lint` reports the offenses of one line in the same order on every Ruby version.
|
|
86
125
|
|
|
87
126
|
## [1.0.0] — 2026-11-04
|
|
88
127
|
|
|
@@ -91,12 +130,12 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
91
130
|
`.atom`) render as XML.
|
|
92
131
|
|
|
93
132
|
**New**
|
|
94
|
-
- Rails through a railtie, Tilt through `require
|
|
133
|
+
- Rails through a railtie, Tilt through a `require`, and `SuperSlim.compile` for plain Ruby.
|
|
95
134
|
- Every `-` and `=` stays on its template line, so backtraces point at the right one.
|
|
96
|
-
- `
|
|
135
|
+
- `super-slim fmt`, which refuses any change to the compiled Ruby, and strict mode, which
|
|
97
136
|
refuses unformatted templates at compile time.
|
|
98
|
-
- `
|
|
99
|
-
configured in `.
|
|
137
|
+
- `super-slim lint` with 29 rules from slim-lint, haml-lint, erb_lint, Herb and eslint-plugin-jsx-a11y,
|
|
138
|
+
configured in `.super-slim.yml`.
|
|
100
139
|
- `tailwind_classes`, off by default: class shortcuts read Tailwind class names.
|
|
101
140
|
|
|
102
141
|
**Left out**
|
|
@@ -108,11 +147,13 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
108
147
|
- Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
|
|
109
148
|
newer than the one Ruby 3.3 ships.
|
|
110
149
|
|
|
111
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.
|
|
150
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.1.1...HEAD
|
|
151
|
+
[3.1.1]: https://github.com/levups/super-slim/compare/v3.1.0...v3.1.1
|
|
152
|
+
[3.1.0]: https://github.com/levups/super-slim/compare/v3.0.0...v3.1.0
|
|
112
153
|
[3.0.0]: https://github.com/levups/super-slim/compare/v2.2.0...v3.0.0
|
|
113
154
|
[2.2.0]: https://github.com/levups/super-slim/compare/v2.1.0...v2.2.0
|
|
114
155
|
[2.1.0]: https://github.com/levups/super-slim/compare/v2.0.1...v2.1.0
|
|
115
156
|
[2.0.1]: https://github.com/levups/super-slim/compare/v2.0.0...v2.0.1
|
|
116
157
|
[2.0.0]: https://github.com/levups/super-slim/compare/v1.1.0...v2.0.0
|
|
117
|
-
[1.1.0]: https://github.com/
|
|
118
|
-
[1.0.0]: https://github.com/
|
|
158
|
+
[1.1.0]: https://github.com/levups/super-slim/compare/v1.0.0...v1.1.0
|
|
159
|
+
[1.0.0]: https://github.com/levups/super-slim/releases/tag/v1.0.0
|
data/README.md
CHANGED
|
@@ -1,112 +1,178 @@
|
|
|
1
1
|
# SuperSlim
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
SuperSlim renders [Slim](https://github.com/slim-template/slim) templates in Ruby and Rails apps. Slim is a template
|
|
4
|
+
language that writes HTML with indentation instead of closing tags, and SuperSlim is a new engine for it, written
|
|
5
|
+
from scratch: your existing `.slim` files produce the same HTML, but they compile about nine times faster and
|
|
6
|
+
render about twice as fast.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
It also comes with a formatter, a linter and a RuboCop plugin. All three read templates with the engine's own
|
|
9
|
+
parser, so they never disagree with it about what a template means.
|
|
10
|
+
|
|
11
|
+
Website and guide: https://levups.github.io/super-slim/
|
|
12
|
+
|
|
13
|
+
## Why SuperSlim
|
|
14
|
+
|
|
15
|
+
A template engine does two jobs. It compiles each template into Ruby code, which Rails does the first time a
|
|
16
|
+
template renders and again after you edit it in development. Then it runs that code on every request. SuperSlim
|
|
17
|
+
makes both cheaper.
|
|
18
|
+
|
|
19
|
+
`bench/compare.rb` renders the same blog page (24 post cards) with five engines and checks that they all produce
|
|
20
|
+
the same HTML. On the last run (November 2026, Ruby 4.0 with YJIT), SuperSlim compiled the page 9.3 times faster
|
|
21
|
+
than Slim and rendered it 2.3 times faster, with a quarter of the allocations:
|
|
22
|
+
|
|
23
|
+
| Engine | Compiles per second | Renders per second | Objects allocated per render |
|
|
24
|
+
|---|---:|---:|---:|
|
|
25
|
+
| SuperSlim 1.0.0 | 12,423 | 60,725 | 111 |
|
|
26
|
+
| Slim 5.2.2 | 1,341 | 26,786 | 480 |
|
|
27
|
+
| Haml 7.5.2 | 1,559 | 37,704 | 471 |
|
|
28
|
+
| Herb 0.11.1 | 5,600 | 17,496 | 830 |
|
|
29
|
+
| Erubi 1.13.1 (ERB) | 51,144 | 62,746 | 115 |
|
|
30
|
+
|
|
31
|
+
On whole apps, the 1,399 templates of a production Rails app compile in 108 ms instead of 522 ms, and 6,053
|
|
32
|
+
templates from 30 open-source projects in 529 ms instead of 2,472 ms. The linter takes about a third of the CPU
|
|
33
|
+
time slim-lint takes on the same templates. The [benchmarks page](https://levups.github.io/super-slim/benchmarks.html)
|
|
34
|
+
has the charts and the steps to reproduce them.
|
|
8
35
|
|
|
9
36
|
## Installation
|
|
10
37
|
|
|
38
|
+
SuperSlim needs Ruby 3.3 or later. In a Rails app, add it to your Gemfile, and remove `slim-rails` if you have it:
|
|
39
|
+
SuperSlim replaces it.
|
|
40
|
+
|
|
11
41
|
```ruby
|
|
12
|
-
|
|
13
|
-
gem "super-slim" # instead of slim-rails
|
|
42
|
+
gem "super-slim"
|
|
14
43
|
```
|
|
15
44
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
Rails picks it up through its railtie. Outside Rails, `SuperSlim.compile(source)` returns the Ruby code.
|
|
19
|
-
|
|
20
|
-
## What SuperSlim renders
|
|
45
|
+
Rails then renders every `.slim` view with SuperSlim. There is nothing to configure. Rails itself is optional, and
|
|
46
|
+
SuperSlim is tested on Rails 8.0 and later.
|
|
21
47
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
(`<link />`, `doctype xml`) from their format alone.
|
|
48
|
+
Without Rails, `SuperSlim.compile` turns a template into Ruby code, which you evaluate where the template's
|
|
49
|
+
variables and methods are defined:
|
|
25
50
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
markers, `|` and `'` text, inline HTML, `/` and `/!` comments, `-` control code, `=`/`==` output, `#{}` and
|
|
29
|
-
`#{{}}` interpolation, Slim's doctypes (`html`, `transitional` for email clients, their XHTML versions in XML
|
|
30
|
-
templates), and the `ruby:`, `javascript:`, `css:` and `markdown:` (through
|
|
31
|
-
[Markly](https://github.com/ioquatix/markly)) embedded engines.
|
|
51
|
+
```ruby
|
|
52
|
+
require "super_slim"
|
|
32
53
|
|
|
33
|
-
|
|
54
|
+
ruby_code = SuperSlim.compile(File.read("page.slim"), file: "page.slim") # file: is shown in errors and backtraces
|
|
55
|
+
html = eval(ruby_code, binding)
|
|
56
|
+
```
|
|
34
57
|
|
|
35
|
-
|
|
36
|
-
`tailwind_classes` and the `.` shortcut takes any Tailwind class name:
|
|
58
|
+
## A first template
|
|
37
59
|
|
|
38
60
|
```slim
|
|
39
|
-
.
|
|
40
|
-
|
|
41
|
-
|
|
61
|
+
ul.posts
|
|
62
|
+
- posts.each do |post|
|
|
63
|
+
li
|
|
64
|
+
a href=post.url = post.title
|
|
65
|
+
- if post.draft?
|
|
66
|
+
span.badge Draft
|
|
42
67
|
```
|
|
43
68
|
|
|
44
|
-
|
|
45
|
-
|
|
69
|
+
With two posts, the second one a draft, this renders:
|
|
70
|
+
|
|
71
|
+
```html
|
|
72
|
+
<ul class="posts"><li><a href="/posts/1">Hello & welcome</a></li><li><a href="/posts/2">Next steps</a><span class="badge">Draft</span></li></ul>
|
|
46
73
|
```
|
|
47
74
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
75
|
+
Lines starting with `-` run Ruby, `=` outputs the result of Ruby code (escaped for HTML), and everything else is a
|
|
76
|
+
tag, text or an attribute. The [Slim documentation](https://github.com/slim-template/slim#readme) describes the whole
|
|
77
|
+
language. SuperSlim reads all of it, except the few parts listed in [What SuperSlim leaves out](#what-superslim-leaves-out).
|
|
78
|
+
|
|
79
|
+
## Switching from Slim
|
|
80
|
+
|
|
81
|
+
SuperSlim renders what Slim renders with the options `format: :html, sort_attrs: false`, byte for byte. Slim's
|
|
82
|
+
defaults are different (`format: :xhtml, sort_attrs: true`), so if you never changed them, switch in two steps:
|
|
83
|
+
|
|
84
|
+
1. Set those two options on Slim and run your test suite. Fix anything that depended on XHTML output or on
|
|
85
|
+
sorted attributes.
|
|
86
|
+
2. Replace `slim-rails` with `super-slim` in your Gemfile and remove any `Slim::Engine.set_options` or
|
|
87
|
+
`with_options` call.
|
|
88
|
+
|
|
89
|
+
Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically, with `<link />` and
|
|
90
|
+
`doctype xml`.
|
|
91
|
+
|
|
92
|
+
## Formatter
|
|
93
|
+
|
|
94
|
+
`super-slim format` rewrites templates in one layout: two spaces per level, a space after `-` and `=`, no `div`
|
|
95
|
+
where a `.class` or `#id` is enough, no trailing whitespace, single blank lines and a final newline. This template:
|
|
96
|
+
|
|
97
|
+
```slim
|
|
98
|
+
div.card
|
|
99
|
+
-if post.draft?
|
|
100
|
+
span.badge Draft
|
|
101
|
+
=link_to post.title, post
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
img src=post.image_url
|
|
51
105
|
```
|
|
52
106
|
|
|
53
|
-
|
|
54
|
-
`a.btn[href="/"]` is still a link with a class and an `href`. A colon followed by a space still nests a tag
|
|
55
|
-
(`li.item: a`), and a final `/` still closes one. What changes is what Slim mangles: it renders `.size-2.5` as
|
|
56
|
-
`class="size-2 5"`, the option as `class="size-2.5"`. On 7,505 templates from 31 apps, 16 compile differently
|
|
57
|
-
with the option, all of them Tailwind classes Slim breaks. It stays off by default all the same, so that
|
|
58
|
-
turning SuperSlim on changes nothing.
|
|
107
|
+
comes out as:
|
|
59
108
|
|
|
60
|
-
|
|
61
|
-
|
|
109
|
+
```slim
|
|
110
|
+
.card
|
|
111
|
+
- if post.draft?
|
|
112
|
+
span.badge Draft
|
|
113
|
+
= link_to post.title, post
|
|
62
114
|
|
|
63
|
-
|
|
115
|
+
img src=post.image_url
|
|
116
|
+
```
|
|
64
117
|
|
|
65
|
-
|
|
118
|
+
```sh
|
|
119
|
+
super-slim format # every .slim file under the current directory (except .git, node_modules, tmp, vendor)
|
|
120
|
+
super-slim format --check # list the files that would change, and fail if there are any
|
|
121
|
+
super-slim format app/views/posts
|
|
122
|
+
```
|
|
66
123
|
|
|
67
|
-
|
|
68
|
-
- the `coffee:`, `sass:`, `scss:`, `less:`, `erb:`, `builder:`, `textile:`, `rdoc:`, `asciidoc:` and wiki
|
|
69
|
-
engines, and `/[if IE]` conditional comments;
|
|
70
|
-
- the `logic_less`, `translator`, `include` and `smart` plugins, and custom shortcuts: only `#id` and `.class`.
|
|
124
|
+
`fmt` is an alias of `format`.
|
|
71
125
|
|
|
72
|
-
|
|
126
|
+
The formatter can't change what a page renders. Before writing a file, it compiles the old and the new version and
|
|
127
|
+
compares the Ruby code they produce, using [Prism](https://github.com/ruby/prism). Line breaks, spaces, a trailing
|
|
128
|
+
comma or the parentheses of a call may differ. If anything else does, it leaves the file alone and reports it. Text
|
|
129
|
+
blocks move together with their whitespace for the same reason.
|
|
73
130
|
|
|
74
|
-
|
|
131
|
+
To also wrap long lines, set a maximum length in `.super-slim.yml`:
|
|
75
132
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
133
|
+
```yaml
|
|
134
|
+
format:
|
|
135
|
+
max_line_length: 120
|
|
136
|
+
attribute_delimiters: "()" # or "[]" or "{}"
|
|
137
|
+
```
|
|
79
138
|
|
|
80
|
-
|
|
139
|
+
The formatter then puts a long tag's attributes one per line inside those delimiters, and breaks the arguments
|
|
140
|
+
of long Ruby calls after their commas. Lines you already split over several lines stay as they are.
|
|
81
141
|
|
|
82
|
-
|
|
142
|
+
## Linter
|
|
83
143
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
144
|
+
```sh
|
|
145
|
+
super-slim lint # every .slim file under the current directory
|
|
146
|
+
super-slim lint app/views/posts
|
|
147
|
+
```
|
|
88
148
|
|
|
89
|
-
|
|
90
|
-
per line inside `attribute_delimiters` (`"()"` by default, `"[]"` or `"{}"`), then the arguments of its Ruby code after each comma, four spaces deep when the line opens a block. It keeps a wrap only
|
|
91
|
-
when the compiled Ruby differs by whitespace alone, and leaves lines already spread over several lines alone.
|
|
149
|
+
On the formatted template above, it still finds the image without a text alternative:
|
|
92
150
|
|
|
93
|
-
|
|
151
|
+
```
|
|
152
|
+
_card.html.slim:6: [ImageAlt] Describe the image with `alt`, or mark it decorative with `alt=""`
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The linter reads its settings from `.super-slim.yml`, in the directory you run it from. Its rules come from other
|
|
156
|
+
linters, so most will look familiar:
|
|
94
157
|
|
|
95
|
-
| Rule |
|
|
158
|
+
| Rule | Comes from | Default |
|
|
96
159
|
|---|---|---|
|
|
97
|
-
| Indentation, Tab, TrailingWhitespace, EmptyLines, TrailingBlankLines, ControlStatementSpacing, RedundantDiv | slim-lint
|
|
160
|
+
| Indentation, Tab, TrailingWhitespace, EmptyLines, TrailingBlankLines, ControlStatementSpacing, RedundantDiv | slim-lint (`super-slim format` fixes them) | on |
|
|
98
161
|
| LineLength (`max: 120`), CommentControlStatement, EmptyControlStatement, ConsecutiveControlStatements (`max: 2`), TagCase | slim-lint | on |
|
|
99
162
|
| InstanceVariables (in partials) | haml-lint, Rails conventions | on |
|
|
100
163
|
| StrictLocals (partials declare `/# locals:`) | Rails 7.1 strict locals | off |
|
|
101
164
|
| DuplicateId, ImageAlt, IframeTitle, AnchorHref, NestedLinks, PositiveTabindex, BooleanAttributeValue, AriaRole, AriaAttribute | Herb, eslint-plugin-jsx-a11y | on |
|
|
102
165
|
| ButtonType, InlineStyles | eslint-plugin-react, haml-lint | off |
|
|
103
|
-
| DeprecatedClasses (`classes: {old: new}`, `old-*: new-*` for a prefix), DeprecatedAttributes (`attributes: {old: new}`) | erb_lint | on, empty |
|
|
166
|
+
| DeprecatedClasses (`classes: {old: new}`, `old-*: new-*` for a prefix), DeprecatedAttributes (`attributes: {old: new}`), both with `preset: bootstrap5` | erb_lint | on, but empty |
|
|
104
167
|
| UnescapedOutput (`==`, `html_safe`, `raw`, `#{{}}`) | Brakeman, erb_lint | off |
|
|
105
168
|
| EmbeddedEngines (`forbidden_engines: [javascript, css]`) | slim-lint | off |
|
|
106
|
-
| HardcodedText (text
|
|
169
|
+
| HardcodedText (text that should be translated) | erb_lint | off |
|
|
107
170
|
| UnnecessaryInterpolation, UnnecessaryStringOutput | haml-lint | on |
|
|
108
171
|
| Zwsp (invisible characters) | slim-lint | on |
|
|
109
172
|
|
|
173
|
+
The website shows [each rule with an example](https://levups.github.io/super-slim/tools.html). A configuration
|
|
174
|
+
file looks like this:
|
|
175
|
+
|
|
110
176
|
```yaml
|
|
111
177
|
# .super-slim.yml
|
|
112
178
|
linters:
|
|
@@ -115,9 +181,12 @@ linters:
|
|
|
115
181
|
ButtonType:
|
|
116
182
|
enabled: true
|
|
117
183
|
DeprecatedClasses:
|
|
184
|
+
preset: bootstrap5 # Bootstrap 4 classes that Bootstrap 5.3 renamed or dropped
|
|
118
185
|
classes:
|
|
119
|
-
|
|
120
|
-
|
|
186
|
+
media: ~ # removes this entry from the preset
|
|
187
|
+
legacy-*: modern-*
|
|
188
|
+
DeprecatedAttributes:
|
|
189
|
+
preset: bootstrap5 # data-toggle becomes data-bs-toggle, and so on
|
|
121
190
|
InlineStyles:
|
|
122
191
|
enabled: true
|
|
123
192
|
exclude: ["app/views/*_mailer/**/*"]
|
|
@@ -126,16 +195,23 @@ linters:
|
|
|
126
195
|
include: ["app/views/shop/**/*.slim"]
|
|
127
196
|
```
|
|
128
197
|
|
|
129
|
-
Every rule
|
|
130
|
-
(`**/`
|
|
198
|
+
Every rule accepts `include:` and `exclude:`, lists of glob patterns matched against paths relative to
|
|
199
|
+
`.super-slim.yml` (`**/` matches any number of directories).
|
|
200
|
+
|
|
201
|
+
The `bootstrap5` preset lists the classes and data attributes that Bootstrap 5.3 renamed, including the ones with
|
|
202
|
+
breakpoints such as `text-md-right`. Its data attributes are only those of Bootstrap's own components
|
|
203
|
+
(`data-toggle`, `data-target` and the like). If your app also uses generic ones such as `data-content`, add them
|
|
204
|
+
under `attributes:`. If your own JavaScript still reads an attribute the preset renames, remove that entry with `~`.
|
|
205
|
+
|
|
206
|
+
## RuboCop plugin
|
|
131
207
|
|
|
132
|
-
The
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
208
|
+
The linter checks the Slim side of a template. For the Ruby inside it, SuperSlim has a RuboCop plugin, which
|
|
209
|
+
replaces rubocop-slim (and with it slimi and Temple). It passes every `-` and `=` line, Ruby attribute, splat,
|
|
210
|
+
`#{}` interpolation and `ruby:` block to RuboCop. Offenses point at the right line and column of the `.slim` file,
|
|
211
|
+
and `rubocop -a` writes its corrections back into it.
|
|
136
212
|
|
|
137
213
|
```yaml
|
|
138
|
-
# .rubocop-slim.yml, run with rubocop -c .rubocop-slim.yml
|
|
214
|
+
# .rubocop-slim.yml, run with: rubocop -c .rubocop-slim.yml
|
|
139
215
|
inherit_from: .rubocop.yml
|
|
140
216
|
plugins:
|
|
141
217
|
- super-slim
|
|
@@ -144,74 +220,170 @@ AllCops:
|
|
|
144
220
|
- "**/*.slim"
|
|
145
221
|
```
|
|
146
222
|
|
|
147
|
-
|
|
148
|
-
`Lint/UselessAssignment` on a variable a later line
|
|
149
|
-
does.
|
|
150
|
-
(`span> title="x" = name`), so a few offenses
|
|
223
|
+
Some cops make no sense inside a template, such as indentation and alignment across lines, the frozen string
|
|
224
|
+
comment, or `Lint/UselessAssignment` on a variable that a later line uses. The plugin turns them off for `.slim`
|
|
225
|
+
files, as rubocop-slim does. SuperSlim also reads code that slimi skips, for example the output of a tag with
|
|
226
|
+
attributes and whitespace markers (`span> title="x" = name`), so you may see a few new offenses after switching.
|
|
151
227
|
|
|
152
|
-
##
|
|
228
|
+
## Additions to Slim
|
|
229
|
+
|
|
230
|
+
Two additions read templates that Slim refuses or misreads. Neither changes a template that already works with
|
|
231
|
+
Slim.
|
|
232
|
+
|
|
233
|
+
### Ruby code over several lines
|
|
153
234
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
- A seeded fuzzer generates thousands of random templates from the grammar and compares both engines; CI adds
|
|
157
|
-
10,000 from a new seed on every run.
|
|
158
|
-
- Golden snapshots (`test/golden`) hold the rendered HTML, compiled Ruby, formatting and lint offenses of
|
|
159
|
-
templates covering the grammar, its errors and Tailwind class names: `UPDATE_GOLDEN=1` rewrites them.
|
|
160
|
-
- `bench/compiled_digests.rb` proves a refactoring leaves the compiled Ruby of a whole corpus unchanged.
|
|
235
|
+
Slim lets a line of Ruby continue on the next line after a comma or a backslash. SuperSlim also continues it after
|
|
236
|
+
an opening `(`, `[` or `{` at the end of a line, until the bracket is closed:
|
|
161
237
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
238
|
+
```slim
|
|
239
|
+
= render ProductCardComponent.new(
|
|
240
|
+
product:,
|
|
241
|
+
variant: :compact,
|
|
242
|
+
)
|
|
243
|
+
- columns = %w[
|
|
244
|
+
name
|
|
245
|
+
price
|
|
246
|
+
]
|
|
247
|
+
```
|
|
167
248
|
|
|
168
|
-
|
|
249
|
+
A bracket opened in the middle of a line, as in `- items.each { |item|`, works as before: the lines below it are
|
|
250
|
+
its children.
|
|
169
251
|
|
|
170
|
-
|
|
171
|
-
produce the same HTML, and writes compile and render speed and allocations to `docs/benchmarks.json`, which
|
|
172
|
-
the website reads. Run it before each release, on a quiet machine:
|
|
252
|
+
### Tailwind class names
|
|
173
253
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
254
|
+
Slim stops a `.class` shortcut at characters such as `[`, `:` or `/`, which Tailwind uses all the time. It renders
|
|
255
|
+
`.size-2.5` as `class="size-2 5"`, and refuses `.lg:[&:nth-child(3)]:hover:underline`. With `tailwind_classes` on,
|
|
256
|
+
the shortcut takes any Tailwind class name:
|
|
257
|
+
|
|
258
|
+
```slim
|
|
259
|
+
.p-1.rounded-b-[3px].lg:[&:nth-child(3)]:hover:underline
|
|
260
|
+
.font-['Open_Sans'].bg-(--brand).mx-0.5.w-1/2.!font-bold.@lg:flex
|
|
261
|
+
ul.*:p-2
|
|
178
262
|
```
|
|
179
263
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
264
|
+
Turn it on for the engine, in `config/initializers/super_slim.rb` for example (or per call with
|
|
265
|
+
`SuperSlim.compile(source, tailwind_classes: true)`):
|
|
266
|
+
|
|
267
|
+
```ruby
|
|
268
|
+
SuperSlim.tailwind_classes = true
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
And for the formatter and the linter, in `.super-slim.yml`:
|
|
272
|
+
|
|
273
|
+
```yaml
|
|
274
|
+
tailwind_classes: true
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Everything else reads as before. Tailwind never puts a bracket right after a letter or a digit, so in
|
|
278
|
+
`a.btn[href="/"]` the brackets are still an attribute list. `li.item: a` still nests a tag, and a final `/` still
|
|
279
|
+
closes one. On 7,505 templates from 31 apps, 16 compiled differently with the option on, and each time it was a
|
|
280
|
+
Tailwind class that Slim broke. The option is off by default anyway, so that switching to SuperSlim changes
|
|
281
|
+
nothing. It costs nothing measurable: compile time on 1,408 templates moved by 0.5%, within noise, and the
|
|
282
|
+
compiled code is the same, so render time is too.
|
|
283
|
+
|
|
284
|
+
## Escaping
|
|
285
|
+
|
|
286
|
+
Like Slim, SuperSlim escapes for HTML everything that `=`, `#{}` and attribute values output. `==` and `#{{}}`
|
|
287
|
+
output raw HTML, so only use them on content you trust. In Rails, strings marked `html_safe` aren't escaped again.
|
|
288
|
+
|
|
289
|
+
Some names are only known when the page renders: the keys of a `*splat` hash, of a `data:` or `aria:` hash, and
|
|
290
|
+
the tag of `*{tag: …}`. SuperSlim checks those names and raises an error instead of rendering one that would break
|
|
291
|
+
the HTML. Slim does not check them. With Slim, `data: {"x\tonclick" => code}` renders an `onclick` attribute, and
|
|
292
|
+
`*{tag: "img src=x onerror=…"}` renders an image with an `onerror` handler. SuperSlim raises
|
|
293
|
+
`SuperSlim::InvalidAttributeNameError` and `SuperSlim::InvalidTagNameError` instead.
|
|
294
|
+
|
|
295
|
+
HTML escaping protects HTML, and nothing else. A `#{}` of user input inside `javascript:` or `css:`, inside an
|
|
296
|
+
unquoted attribute of inline HTML, or in the URL of a `markdown:` link can still inject code. In scripts, write
|
|
297
|
+
`#{{json_escape(value.to_json)}}` (Rails), always quote attributes, and check URLs before they reach a template.
|
|
298
|
+
|
|
299
|
+
## What SuperSlim leaves out
|
|
300
|
+
|
|
301
|
+
These parts of Slim aren't supported. Each one fails when the template compiles, with an error that names it:
|
|
302
|
+
|
|
303
|
+
- the XHTML format, the `sort_attrs`, `attr_quote`, `pretty` and `js_wrapper` options, and every Temple option;
|
|
304
|
+
- the `coffee:`, `sass:`, `scss:`, `less:`, `erb:`, `builder:`, `textile:`, `rdoc:`, `asciidoc:` and wiki
|
|
305
|
+
engines, and `/[if IE]` conditional comments;
|
|
306
|
+
- the `logic_less`, `translator`, `include` and `smart` plugins, and custom shortcuts (only `#id` and `.class`
|
|
307
|
+
exist).
|
|
308
|
+
|
|
309
|
+
The embedded engines that do work are `ruby:`, `javascript:`, `css:` and `markdown:`. `markdown:` uses
|
|
310
|
+
[Markly](https://github.com/ioquatix/markly), so add `gem "markly"` to your Gemfile if you use it. The rest can
|
|
311
|
+
come back if people ask for them, as long as they don't slow down rendering.
|
|
312
|
+
|
|
313
|
+
## Learn more
|
|
314
|
+
|
|
315
|
+
The website has a [guide](https://levups.github.io/super-slim/guide.html) with every option, a page on the
|
|
316
|
+
[formatter and linter](https://levups.github.io/super-slim/tools.html) with an example for each rule, the
|
|
317
|
+
[benchmarks](https://levups.github.io/super-slim/benchmarks.html) and the
|
|
318
|
+
[test setup](https://levups.github.io/super-slim/testing.html). [CHANGELOG.md](CHANGELOG.md) lists the changes in
|
|
319
|
+
each version.
|
|
320
|
+
|
|
321
|
+
## How it is tested
|
|
322
|
+
|
|
323
|
+
Slim is the reference. Its own test suite, including the examples of its specification (`TESTS.md`), runs against
|
|
324
|
+
SuperSlim: every template is rendered by both engines, and the HTML has to match.
|
|
325
|
+
|
|
326
|
+
On top of that:
|
|
327
|
+
|
|
328
|
+
- A fuzzer builds thousands of random templates from the grammar and compares the output of both engines. CI runs
|
|
329
|
+
10,000 new ones on every build.
|
|
330
|
+
- Snapshots in `test/golden` record the HTML, the compiled Ruby, the formatted template and the lint offenses for
|
|
331
|
+
templates covering the whole grammar, its errors and Tailwind class names. `UPDATE_GOLDEN=1` rewrites them after
|
|
332
|
+
a deliberate change.
|
|
333
|
+
- `bench/compiled_digests.rb` checks that a refactoring leaves the compiled Ruby of a whole set of templates
|
|
334
|
+
unchanged.
|
|
335
|
+
|
|
336
|
+
Real templates come from 30 open-source apps, pinned to a commit in `corpus/manifest.yml`. `rake corpus:fetch`
|
|
337
|
+
downloads only their `.slim` files, into `corpus/cache` (ignored by Git). `rake corpus:check` compiles, formats
|
|
338
|
+
and lints each of them and compares the result with Slim. `rake corpus:diff BASE=rev` shows what a change does to
|
|
339
|
+
them: compiled Ruby, formatting and lint offenses. These templates are never rendered, because rendering would run
|
|
340
|
+
their Ruby code. Your own templates can be rendered too: pass their directories in `SUPER_SLIM_CORPUS`.
|
|
183
341
|
|
|
184
342
|
## Development
|
|
185
343
|
|
|
186
344
|
```sh
|
|
187
345
|
bundle install
|
|
188
|
-
bundle exec rake test
|
|
346
|
+
bundle exec rake test # SuperSlim's tests, then Slim's own suites
|
|
189
347
|
BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test # the same on Rails 8.0
|
|
190
348
|
bundle exec rubocop
|
|
191
|
-
FUZZ_SEED=42 FUZZ_CASES=20000 bundle exec ruby -Ilib test/fuzz_test.rb
|
|
192
|
-
bundle exec ruby -Ilib bench/corpus.rb path/to/app
|
|
193
|
-
bundle exec ruby --yjit -Ilib bench/render.rb
|
|
349
|
+
FUZZ_SEED=42 FUZZ_CASES=20000 bundle exec ruby -Ilib -Itest test/fuzz_test.rb
|
|
350
|
+
bundle exec ruby -Ilib bench/corpus.rb path/to/app # compile time and allocations against Slim
|
|
351
|
+
bundle exec ruby --yjit -Ilib bench/render.rb # render speed and allocations against Slim
|
|
352
|
+
bundle exec rake docs # rebuild the linter rules page of the website
|
|
194
353
|
```
|
|
195
354
|
|
|
355
|
+
The website's numbers come from `docs/benchmarks.json`, which `rake bench` rewrites by running `bench/compare.rb`.
|
|
356
|
+
If `corpus/cache` is there, it also times the open-source templates, and compares `super-slim lint` and `format`
|
|
357
|
+
with slim-lint when slim-lint is installed. Run it on a quiet machine:
|
|
358
|
+
|
|
359
|
+
```sh
|
|
360
|
+
bundle exec rake bench # with YJIT, 5 seconds per measurement
|
|
361
|
+
SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench # also times compiling a whole app against Slim
|
|
362
|
+
BENCH_LOCK=path/to/.bench.lock bundle exec rake bench # waits while another benchmark holds the lock
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
[CONTRIBUTING.md](CONTRIBUTING.md) explains how to propose a change. Please report security issues privately, as
|
|
366
|
+
[SECURITY.md](SECURITY.md) describes.
|
|
367
|
+
|
|
196
368
|
### Releasing
|
|
197
369
|
|
|
198
|
-
Pull requests
|
|
199
|
-
pull requests merged since the last tag, without committing them. Refresh the website
|
|
200
|
-
(`SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench`) and merge them
|
|
201
|
-
up-to-date `main`
|
|
370
|
+
Pull requests don't touch `CHANGELOG.md`. A release starts by writing its notes under `## [Unreleased]`, from the
|
|
371
|
+
pull requests merged since the last tag, without committing them. Refresh the numbers on the website first, on a
|
|
372
|
+
quiet machine (`SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench`), and merge them in their own pull
|
|
373
|
+
request. Then, from an up-to-date `main` with green CI:
|
|
202
374
|
|
|
203
375
|
```sh
|
|
204
|
-
bin/release minor --dry-run # the notes and the changes, nothing
|
|
376
|
+
bin/release minor --dry-run # shows the notes and the changes, writes nothing
|
|
205
377
|
bin/release minor # or patch, major, or an exact version such as 1.2.0
|
|
206
378
|
```
|
|
207
379
|
|
|
208
|
-
`main`
|
|
209
|
-
`release/vX.Y.Z
|
|
210
|
-
signed
|
|
211
|
-
the Release workflow, which publishes the gem to RubyGems through trusted publishing and
|
|
212
|
-
with the
|
|
380
|
+
Nobody pushes to `main` directly, so `bin/release` dates the Unreleased section and bumps the version on a
|
|
381
|
+
`release/vX.Y.Z` branch, opens a pull request that merges itself once CI passes, and waits for it. It then pushes
|
|
382
|
+
a signed `vX.Y.Z` tag on the merge commit (if you stopped it while it waited, `bin/release --tag X.Y.Z` does that
|
|
383
|
+
step). The tag starts the Release workflow, which publishes the gem to RubyGems through trusted publishing and
|
|
384
|
+
creates the GitHub release with the notes and the `.gem` file.
|
|
213
385
|
|
|
214
386
|
## License
|
|
215
387
|
|
|
216
|
-
MIT, by Level UP Solutions
|
|
217
|
-
|
|
388
|
+
MIT, by [Level UP Solutions](https://github.com/levups). The parser follows the grammar and the parsing algorithm
|
|
389
|
+
of [Slim](https://github.com/slim-template/slim), by the Slim Team (MIT).
|