super-slim 3.6.3 → 3.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bdda53d82fc2a91f77cf19258e1ee428e89b765df89a9e91e93af3517a6c2f0f
4
- data.tar.gz: 6998c577d2553c9ab39efc167c5bdbe576eecc891a959ec31d90b3ffc822c2bd
3
+ metadata.gz: c62c06c47157da25a9d318b8f1b29d044f3354034eb96321c6b884532866301f
4
+ data.tar.gz: 6587e5102eff9f73b36351dbbdec7cf419a85785dbb7ba4dc6fd85620f828aaf
5
5
  SHA512:
6
- metadata.gz: 2adce8b7a0578e7488852e04e267fd8ff964021f59719602be4e018583b1faae79a264c8f165148352ff1879c303957d582917c1d030eaa5d086d4404e505da4
7
- data.tar.gz: 6d40b5b0e0d8e68655c74237d82cf89f9d9afb0fd2053bf89cfb987a53d670b403de410055a711b640fa4f431529afc3e753fadbbe3fe09a36dc1a7fa5ca4bf4
6
+ metadata.gz: 069404facc261d86666252ef43a693760a262cd0d5974242be6a98d67c7ee6e188752216fe25000c1a3f6fa02e10f8233e908ceec625e9fdd911f916777431e4
7
+ data.tar.gz: bbd35b5adb251e1e0005b7308a3d91a832e7c85b64e2306d75b09e3195a2858ff19a72c2e6fc1c11d65b002911c6106fd0c9b4824f79cdcb29879140ac3c358b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [3.8.0] — 2026-11-26
6
+
7
+ SuperSlim now works from any Ractor, and the formatter and the linter load on their own.
8
+
9
+ **New**
10
+ - Compiling, rendering, formatting and linting work from any Ractor, with the same result as on the main one.
11
+ Outside the main Ractor, escaping needs erb 5.0.2 and parsing Prism 1.5, which Ruby 4.0 ships: Ruby 3.3 and 3.4
12
+ need `gem "erb"` in the Gemfile, and `gem "prism"` when theirs is older. Ruby 3.3, which can't autoload outside
13
+ the main Ractor, needs `require "super_slim/formatter"` on the main Ractor first. A `markdown:` block still
14
+ compiles on the main Ractor only. The constants under `SuperSlim` are now deeply frozen, nested values included.
15
+
16
+ **Fixed**
17
+ - `require "super_slim/formatter"` and `require "super_slim/linter"` work without `require "super_slim"` first: the
18
+ formatter raised `NameError` while loading, and the linter on its first call. Requiring the formatter after the
19
+ engine no longer warns of a circular require under `ruby -w`.
20
+
21
+ ## [3.7.0] — 2026-11-24
22
+
23
+ Faster rendering of splats and data hashes, faster compiling, and one fix.
24
+
25
+ **Faster**
26
+ - Tags with a splat (`*attrs`) or a `data=`/`aria=` hash allocate a third fewer objects per render: a form of twelve
27
+ fields went from 258 to 138 allocations per render, 4% faster without JIT and 9% with YJIT. Rails compiles
28
+ templates without `frozen_string_literal`, so each attribute name passed to the runtime allocated a string.
29
+ - Compiling allocates 30% fewer objects and takes 5% less time on 6,097 public templates, with and without YJIT.
30
+ The compiled Ruby is unchanged.
31
+
32
+ **Fixed**
33
+ - `SuperSlim.compile(source, html_safe: nil)` no longer compiles a dynamic class or a splat into code that raises
34
+ `ArgumentError` when it renders: `nil` works like `false`.
35
+
5
36
  ## [3.6.3] — 2026-11-21
6
37
 
7
38
  One fix: a line break inside an interpolation of a quoted attribute no longer shifts the lines that follow.
@@ -330,7 +361,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
330
361
  - 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,
331
362
  newer than the one Ruby 3.3 ships.
332
363
 
333
- [Unreleased]: https://github.com/levups/super-slim/compare/v3.6.3...HEAD
364
+ [Unreleased]: https://github.com/levups/super-slim/compare/v3.8.0...HEAD
365
+ [3.8.0]: https://github.com/levups/super-slim/compare/v3.7.0...v3.8.0
366
+ [3.7.0]: https://github.com/levups/super-slim/compare/v3.6.3...v3.7.0
334
367
  [3.6.3]: https://github.com/levups/super-slim/compare/v3.6.2...v3.6.3
335
368
  [3.6.2]: https://github.com/levups/super-slim/compare/v3.6.1...v3.6.2
336
369
  [3.6.1]: https://github.com/levups/super-slim/compare/v3.6.0...v3.6.1
data/README.md CHANGED
@@ -10,6 +10,9 @@ parser, so they never disagree with it about what a template means.
10
10
 
11
11
  Website and guide: https://levups.github.io/super-slim/
12
12
 
13
+ Coding agents can start from <https://levups.github.io/super-slim/llms.txt>, or read this README and the
14
+ website's pages as one Markdown file: <https://levups.github.io/super-slim/llms-full.txt>.
15
+
13
16
  ## Why SuperSlim
14
17
 
15
18
  A template engine does two jobs. It compiles each template into Ruby code, which Rails does the first time a
@@ -17,19 +20,19 @@ template renders and again after you edit it in development. Then it runs that c
17
20
  makes both cheaper.
18
21
 
19
22
  `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:
23
+ the same HTML. On the last run (November 2026, Ruby 4.0 with YJIT), SuperSlim compiled the page 11 times faster
24
+ than Slim and rendered it 2.2 times faster, with a quarter of the allocations:
22
25
 
23
26
  | Engine | Compiles per second | Renders per second | Objects allocated per render |
24
27
  |---|---:|---:|---:|
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
28
+ | SuperSlim 3.7.0 | 16,384 | 69,202 | 111 |
29
+ | Slim 5.2.2 | 1,441 | 31,452 | 480 |
30
+ | Haml 7.5.2 | 1,772 | 42,477 | 471 |
31
+ | Herb 0.11.2 | 6,478 | 19,513 | 830 |
32
+ | Erubi 1.13.1 (ERB) | 56,668 | 68,360 | 115 |
33
+
34
+ On whole apps, the 1,214 templates of a production Rails app compile in 69 ms instead of 361 ms, and 6,056
35
+ templates from 30 open-source projects in 421 ms instead of 2,309 ms. The linter takes about half the CPU
33
36
  time slim-lint takes on the same templates. The [benchmarks page](https://levups.github.io/super-slim/benchmarks.html)
34
37
  has the charts and the steps to reproduce them.
35
38
 
@@ -66,6 +69,11 @@ ruby_code = SuperSlim.compile(File.read("page.slim"), file: "page.slim") # file:
66
69
  html = eval(ruby_code, binding)
67
70
  ```
68
71
 
72
+ SuperSlim compiles, renders, formats and lints from any Ractor. Outside the main Ractor, escaping needs erb 5.0.2
73
+ or later and parsing Prism 1.5 or later: Ruby 4.0 ships both. Ruby 3.3 and 3.4 need `gem "erb"` in your Gemfile,
74
+ and `gem "prism"` too when theirs is older (Ruby 3.4.0 ships Prism 1.2, Ruby 3.4.8 1.5.2). Ruby 3.3 also can't autoload outside the main Ractor: `require "super_slim/formatter"` there
75
+ first. A `markdown:` block compiles on the main Ractor only, as markly can't leave it.
76
+
69
77
  ## A first template
70
78
 
71
79
  ```slim
@@ -97,6 +105,9 @@ defaults are different (`format: :xhtml, sort_attrs: true`), so if you never cha
97
105
  2. Replace `slim-rails` with `super-slim` in your Gemfile and remove any `Slim::Engine.set_options` or
98
106
  `with_options` call.
99
107
 
108
+ To hand the migration to a coding agent, the website has [a prompt](https://levups.github.io/super-slim/migrate.html#agents)
109
+ that takes it step by step, one commit each, through the linter and formatter switch.
110
+
100
111
  Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically, with `<link />` and
101
112
  `doctype xml`.
102
113
 
@@ -381,7 +392,8 @@ unquoted attribute of inline HTML, or in the URL of a `markdown:` link can still
381
392
 
382
393
  ## What SuperSlim leaves out
383
394
 
384
- These parts of Slim aren't supported. Each one fails when the template compiles, with an error that names it:
395
+ These parts of Slim aren't supported. Each one fails when the template compiles, with an error that names it, except a custom
396
+ shortcut written after a tag (`input&text`), which reads as text:
385
397
 
386
398
  - the XHTML format, the `sort_attrs`, `attr_quote`, `pretty` and `js_wrapper` options, and every Temple option;
387
399
  - the `coffee:`, `sass:`, `scss:`, `less:`, `erb:`, `builder:`, `textile:`, `rdoc:`, `asciidoc:` and wiki
@@ -395,7 +407,7 @@ come back if people ask for them, as long as they don't slow down rendering.
395
407
 
396
408
  ## Learn more
397
409
 
398
- The website has a [guide](https://levups.github.io/super-slim/guide.html) with every option, a page on the
410
+ The website has [a step-by-step switch from Slim](https://levups.github.io/super-slim/migrate.html), a [guide](https://levups.github.io/super-slim/guide.html) with every option, a page on the
399
411
  [formatter and linter](https://levups.github.io/super-slim/tools.html) with an example for each rule, the
400
412
  [benchmarks](https://levups.github.io/super-slim/benchmarks.html) and the
401
413
  [test setup](https://levups.github.io/super-slim/testing.html). [CHANGELOG.md](CHANGELOG.md) lists the changes in
data/lib/super-slim.rb CHANGED
@@ -2,3 +2,5 @@
2
2
 
3
3
  # Bundler requires a gem by its name: super-slim, then super/slim.
4
4
  require_relative "super_slim"
5
+ # Again here: when the linter or the formatter was required before Rails, super_slim.rb ran without Rails.
6
+ require_relative "super_slim/railtie" if defined?(Rails::Railtie)
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # shareable_constant_value: experimental_everything
2
3
 
3
4
  require "tempfile"
4
5
  require_relative "../super_slim"
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # shareable_constant_value: experimental_everything
2
3
 
3
4
  require "strscan"
4
5
 
@@ -8,6 +9,7 @@ module SuperSlim
8
9
 
9
10
  VOID_ELEMENTS = %w[area base br col embed hr img input keygen link menuitem meta param source track wbr].to_h { [_1, true] }.freeze
10
11
  HYPHENATED_ATTRIBUTES = %w[data aria].freeze
12
+ MAX_SCANNED_ATTRIBUTES = 8
11
13
  DOCTYPES = {
12
14
  html: {
13
15
  "5" => "<!DOCTYPE html>",
@@ -48,7 +50,7 @@ module SuperSlim
48
50
  @xml = format == :xml
49
51
  @doctypes = DOCTYPES.fetch(format)
50
52
  @rails = rails
51
- @html_safe = html_safe
53
+ @html_safe = html_safe ? true : false
52
54
  @escape_function = html_safe ? "::SuperSlim::Runtime.escape_html_safe" : "::ERB::Escape.html_escape"
53
55
  @preamble = preamble
54
56
  @postamble = postamble
@@ -226,29 +228,49 @@ module SuperSlim
226
228
  return if attributes.empty?
227
229
  return emit_output(compile_runtime_attributes(attributes), false) if attributes.any? { runtime_attribute?(_1) }
228
230
 
229
- if attributes.size > 1
230
- seen_names = {}
231
- attributes.each do |attribute|
232
- next if attribute.name == "class"
233
- if seen_names.key?(attribute.name)
234
- # Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
235
- lines_ahead = attributes.sum { |other| code_newlines(other) }
236
- compile_error!("Multiple #{attribute.name} attributes specified", lines_ahead:)
237
- end
238
-
239
- seen_names[attribute.name] = true
231
+ # A tag has a handful of attributes: scanning them again costs less than a hash of their names, which only a
232
+ # long list needs, as the scans grow with its square.
233
+ seen_names = {} if attributes.size > MAX_SCANNED_ATTRIBUTES
234
+ attributes.each_with_index do |attribute, index|
235
+ name = attribute.name
236
+ next if name == "class"
237
+
238
+ if seen_names
239
+ next seen_names[name] = true unless seen_names.key?(name)
240
+ elsif attributes.index { _1.name == name } == index
241
+ next
240
242
  end
243
+
244
+ # Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
245
+ lines_ahead = attributes.sum { |other| code_newlines(other) }
246
+ compile_error!("Multiple #{name} attributes specified", lines_ahead:)
241
247
  end
242
248
 
243
249
  # Every class value renders at the place of the first class attribute.
244
- class_attributes = nil
250
+ classes_compiled = false
245
251
  attributes.each do |attribute|
246
252
  next compile_attribute(attribute) unless attribute.name == "class"
247
- next if class_attributes
253
+ next if classes_compiled
254
+
255
+ classes_compiled = true
256
+ if attributes.all? { _1.name != "class" || _1.is_a?(StaticAttribute) }
257
+ compile_static_classes(attributes)
258
+ else
259
+ compile_class_attribute(attributes.filter_map { attribute_parts(_1) if _1.name == "class" })
260
+ end
261
+ end
262
+ end
263
+
264
+ # Shortcuts, the most common classes: their values join at compile time.
265
+ def compile_static_classes(attributes)
266
+ opened = false
267
+ attributes.each do |attribute|
268
+ next if attribute.name != "class" || attribute.value.empty?
248
269
 
249
- class_attributes = attributes.select { _1.name == "class" }
250
- compile_class_attribute(class_attributes.map { attribute_parts(_1) })
270
+ @static << (opened ? " " : ' class="') << attribute.value
271
+ opened = true
251
272
  end
273
+ @static << '"' if opened
252
274
  end
253
275
 
254
276
  def runtime_attribute?(attribute)
@@ -259,6 +281,7 @@ module SuperSlim
259
281
  case attribute
260
282
  when BooleanAttribute then emit_boolean_attribute(attribute.name)
261
283
  when CodeAttribute then compile_code_attribute(attribute)
284
+ when StaticAttribute then @static << " " << attribute.name << '="' << attribute.value << '"'
262
285
  else emit_attribute(attribute.name, attribute_parts(attribute))
263
286
  end
264
287
  end
@@ -321,15 +344,15 @@ module SuperSlim
321
344
 
322
345
  def compile_runtime_attributes(attributes)
323
346
  builder = new_variable
324
- emit_code("#{builder} = ::SuperSlim::Runtime::Attributes.new(#{@html_safe})")
347
+ emit_code("#{builder} = ::SuperSlim::Runtime::#{@html_safe ? "HtmlSafeAttributes" : "Attributes"}.new")
325
348
  attributes.each do |attribute|
326
349
  case attribute
327
350
  when Splat
328
351
  emit_code("#{builder}.splat_attrs((#{attribute.code}))")
329
352
  when CodeAttribute
330
- emit_code("#{builder}.code_attr(#{attribute.name.inspect}, #{attribute.escape}, (#{attribute.code}))")
353
+ emit_code("#{builder}.code_attr(#{frozen_literal(attribute.name)}, #{attribute.escape}, (#{attribute.code})#{unchecked_name(attribute.name)})")
331
354
  else
332
- emit_code("#{builder}.attr(#{attribute.name.inspect}, #{string_expression(attribute_parts(attribute))})")
355
+ emit_code("#{builder}.attr(#{frozen_literal(attribute.name)}, #{string_expression(attribute_parts(attribute))}#{unchecked_name(attribute.name)})")
333
356
  end
334
357
  end
335
358
  builder
@@ -351,13 +374,14 @@ module SuperSlim
351
374
  end
352
375
 
353
376
  def emit_attribute(name, parts)
354
- emit_static(%( #{name}="))
377
+ @static << " " << name << '="'
355
378
  emit_parts(parts)
356
- emit_static('"')
379
+ @static << '"'
357
380
  end
358
381
 
359
382
  def emit_boolean_attribute(name)
360
- emit_static(@xml ? %( #{name}="") : " #{name}")
383
+ @static << " " << name
384
+ @static << '=""' if @xml
361
385
  end
362
386
 
363
387
  def emit_parts(parts)
@@ -376,6 +400,7 @@ module SuperSlim
376
400
  return "#{@escape_function}((#{value}))" if kind == :dynamic && escape
377
401
  return value if kind == :raw
378
402
  end
403
+ return frozen_literal(parts.map { _1[1] }.join) if parts.all? { _1[0] == :static }
379
404
 
380
405
  literal = parts.map do |kind, value, escape|
381
406
  case kind
@@ -387,8 +412,19 @@ module SuperSlim
387
412
  %("#{literal.join}")
388
413
  end
389
414
 
415
+ # A name found valid now needs no check at render time; the parser lets through some the runtime refuses, such
416
+ # as control characters, which still raise when they render.
417
+ def unchecked_name(name)
418
+ ", false" unless name.match?(Runtime::Attributes::INVALID_NAME)
419
+ end
420
+
421
+ # Rails compiles templates without `frozen_string_literal`: a bare literal would allocate on every render.
422
+ def frozen_literal(text)
423
+ "#{text.inspect}.freeze"
424
+ end
425
+
390
426
  def emit_interpolated(text)
391
- emit_parts(Compiler.interpolation_parts(text))
427
+ text.include?('#{') ? emit_parts(Compiler.interpolation_parts(text)) : emit_static(text)
392
428
  end
393
429
 
394
430
  def compile_embedded(embedded)
@@ -488,25 +524,29 @@ module SuperSlim
488
524
  @static << text
489
525
  end
490
526
 
527
+ # The buffers write their statements straight into the compiled code: no string for each one.
491
528
  def emit_output(code, escape)
492
- emit_code(escape ? @buffer.escaped(code) : @buffer.raw(code))
529
+ flush_static
530
+ separate_statement
531
+ escape ? @buffer.escaped(@code, code) : @buffer.raw(@code, code)
493
532
  end
494
533
 
495
534
  def emit_code(code)
496
535
  flush_static
497
- append(code)
536
+ separate_statement
537
+ @code << code
498
538
  end
499
539
 
500
540
  def flush_static
501
541
  return if @static.empty?
502
542
 
503
- append(@buffer.static(@static))
504
- @static = +""
543
+ separate_statement
544
+ @buffer.static(@code, @static)
545
+ @static.clear
505
546
  end
506
547
 
507
- def append(code)
548
+ def separate_statement
508
549
  @code << "; " unless @code.empty? || @code.end_with?("\n")
509
- @code << code
510
550
  end
511
551
 
512
552
  def new_variable
@@ -521,17 +561,35 @@ module SuperSlim
521
561
 
522
562
  def open = "#{@name} = +\"\""
523
563
  def close = @name
524
- def static(text) = "#{@name} << #{text.inspect}.freeze"
525
- def escaped(code) = "#{@name} << #{@escape_function}((#{code}))"
526
- def raw(code) = "#{@name} << (#{code}).to_s"
564
+
565
+ def static(code, text)
566
+ code << @name << " << " << text.inspect << ".freeze"
567
+ end
568
+
569
+ def escaped(code, ruby)
570
+ code << @name << " << " << @escape_function << "((" << ruby << "))"
571
+ end
572
+
573
+ def raw(code, ruby)
574
+ code << @name << " << (" << ruby << ").to_s"
575
+ end
527
576
  end
528
577
 
529
578
  class OutputBuffer
530
579
  def open = "@output_buffer = output_buffer || ActionView::OutputBuffer.new"
531
580
  def close = "@output_buffer"
532
- def static(text) = "@output_buffer.safe_concat(#{text.inspect}.freeze)"
533
- def escaped(code) = "@output_buffer << (#{code})"
534
- def raw(code) = "@output_buffer.safe_concat((#{code}).to_s)"
581
+
582
+ def static(code, text)
583
+ code << "@output_buffer.safe_concat(" << text.inspect << ".freeze)"
584
+ end
585
+
586
+ def escaped(code, ruby)
587
+ code << "@output_buffer << (" << ruby << ")"
588
+ end
589
+
590
+ def raw(code, ruby)
591
+ code << "@output_buffer.safe_concat((" << ruby << ").to_s)"
592
+ end
535
593
  end
536
594
  end
537
595
  end
@@ -1,6 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "prism"
4
+ require_relative "../super_slim"
5
+
6
+ module SuperSlim
7
+ # Defined here before the files below reopen it: from them, the autoload of SuperSlim::Formatter would load this
8
+ # file again, a circular require.
9
+ class Formatter; end
10
+ end
11
+
4
12
  require_relative "formatter/attribute_arranger"
5
13
  require_relative "formatter/code_wrapper"
6
14
  require_relative "formatter/line_wrapper"
@@ -410,13 +418,14 @@ module SuperSlim
410
418
  # a deep template would exhaust Ruby's.
411
419
  def same_node?(node, other_node)
412
420
  pending_values = [node, other_node]
421
+ comparator_by_node_class = Formatter.send(:ractor_cache, :COMPARATOR_BY_NODE_CLASS)
413
422
  until pending_values.empty?
414
423
  other_value = pending_values.pop
415
424
  value = pending_values.pop
416
425
  case value
417
426
  when Prism::Node
418
427
  if value.instance_of?(other_value.class)
419
- return false unless node_comparator(value).call(value, other_value, pending_values)
428
+ return false unless node_comparator(value, comparator_by_node_class).call(value, other_value, pending_values)
420
429
  else
421
430
  pairs = negated_if_pairs(value, other_value) or return false
422
431
  pending_values.concat(pairs)
@@ -451,13 +460,21 @@ module SuperSlim
451
460
  [negation.receiver, unless_node.predicate, if_node.statements, unless_node.statements]
452
461
  end
453
462
 
454
- def node_comparator(node)
455
- COMPARATOR_BY_NODE_CLASS[node.class] ||= Formatter.send(:comparator_for, node.class, *node_fields(node))
463
+ def node_comparator(node, comparator_by_node_class)
464
+ comparator_by_node_class[node.class] ||= Formatter.send(:comparator_for, node.class, *node_fields(node))
456
465
  end
457
466
 
458
467
  class << self
459
468
  private
460
469
 
470
+ # The constant on the main Ractor, shared by every formatter. Another Ractor can't read a mutable constant: it
471
+ # fills caches of its own. TruffleRuby has no Ractor.
472
+ def ractor_cache(name)
473
+ return const_get(name) if !defined?(Ractor) || Ractor.current == Ractor.main
474
+
475
+ Ractor.current[:"super_slim_#{name.downcase}"] ||= {}
476
+ end
477
+
461
478
  # Compares what two nodes of one class hold besides their children, and pushes their children on `pending_values`.
462
479
  def comparator_for(node_class, field_names, located_names, flagged)
463
480
  if node_class <= Prism::SourceLineNode
@@ -503,7 +520,7 @@ module SuperSlim
503
520
  # 2026-11: Prism 0.19 (Ruby 3.3) has no reflection API: the field names come from the first node of each
504
521
  # class. Prism 1.x keeps flags out of deconstruct_keys.
505
522
  def node_fields(node)
506
- FIELDS_BY_NODE_CLASS[node.class] ||= begin
523
+ Formatter.send(:ractor_cache, :FIELDS_BY_NODE_CLASS)[node.class] ||= begin
507
524
  keys = node.deconstruct_keys(nil).keys
508
525
  located_names, field_names = (keys - %i[node_id flags location])
509
526
  .reject { _1.end_with?("_loc") }
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # shareable_constant_value: experimental_everything
2
3
 
3
4
  module SuperSlim
4
5
  class Linter
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # shareable_constant_value: experimental_everything
2
3
 
3
4
  require "prism"
4
5
 
@@ -17,8 +18,9 @@ module SuperSlim
17
18
  DEFAULTS = {enabled: true}.freeze
18
19
  GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
19
20
 
21
+ # Not memoized: a Ractor other than the main one can't set a class's instance variable.
20
22
  def self.rule_name
21
- @rule_name ||= name.split("::").last
23
+ name.split("::").last
22
24
  end
23
25
 
24
26
  def initialize(options)
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
+ # shareable_constant_value: experimental_everything
2
3
 
3
4
  require "yaml"
5
+ require_relative "../super_slim"
4
6
 
5
7
  module SuperSlim
6
8
  class Linter
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "set" # rubocop:disable Lint/RedundantRequireStatement -- 2026-11: Ruby 3.3 can't autoload Set outside the main Ractor
3
4
  require "strscan"
4
5
  require_relative "parser/ruby_brackets"
5
6
 
@@ -47,6 +48,7 @@ module SuperSlim
47
48
  QUOTED_CHUNK_BY_QUOTE = %w[" '].to_h { |quote| [quote, /(?:[^{}\\#{quote}]|\\(?!\z))+/] }.freeze
48
49
  SLASH, PIPE, APOSTROPHE, LESS_THAN, GREATER_THAN, DASH, EQUALS = "/|'<>-=".bytes
49
50
  OPENING_BRACE, CLOSING_BRACE = "{}".bytes
51
+ HASH, DOT, STAR = "#.*".bytes
50
52
  WHITESPACE_MARKER_BYTES = "<>'".bytes.freeze
51
53
  BOOLEAN_ATTRIBUTE_BY_CLOSING = DELIMITERS.values.to_h { |closing| [closing, /#{ATTRIBUTE_NAME}(?=(\s|#{Regexp.escape(closing)}|\z))/] }.freeze
52
54
  ATTRIBUTE_LIST_END_BY_CLOSING = DELIMITERS.values.to_h { |closing| [closing, /\s*#{Regexp.escape(closing)}/] }.freeze
@@ -364,11 +366,18 @@ module SuperSlim
364
366
  end
365
367
  end
366
368
 
367
- # A tag name is consumed with its match; a shortcut is left for parse_tag to read as an attribute.
369
+ # A tag name is consumed with its match; a shortcut is left for parse_tag to read as an attribute of a div, and
370
+ # a splat as the attributes of a dynamic tag.
368
371
  def scan_tag
369
- tag = @scanner.check(Shortcuts::TAG_PATTERN) or return
370
- @scanner.pos += tag.bytesize if @scanner[1]
371
- tag
372
+ length = @scanner.match?(Shortcuts::TAG_PATTERN) or return
373
+ case @scanner.peek_byte
374
+ when HASH, DOT then Shortcuts::DEFAULT_TAG
375
+ when STAR then "*"
376
+ else
377
+ tag = @scanner.peek(length)
378
+ @scanner.pos += length
379
+ tag
380
+ end
372
381
  end
373
382
 
374
383
  def parse_embedded(engine, attributes_source, text)
@@ -473,18 +482,18 @@ module SuperSlim
473
482
 
474
483
  def parse_tag(tag)
475
484
  tag_lineno = @lineno
476
- tag = Shortcuts::DEFAULT_TAG if Shortcuts::ATTRIBUTE_BY_SHORTCUT.key?(tag)
477
485
 
478
486
  attributes = []
479
487
  # Read before it is consumed, so that an illegal shortcut is reported where it starts.
480
488
  while (shortcut_length = @scanner.match?(@attribute_shortcut))
481
- shortcut = @scanner[1]
482
- value = @scanner[2]
483
- syntax_error!("Illegal shortcut") unless (name = Shortcuts::ATTRIBUTE_BY_SHORTCUT[shortcut])
489
+ # A shortcut is a single `#` or `.`: read by bytes, without a string for it.
490
+ next_byte = @scanner.string.getbyte(@scanner.pos + 1)
491
+ syntax_error!("Illegal shortcut") if next_byte == HASH || next_byte == DOT
484
492
 
493
+ value = @scanner[1]
485
494
  # Tailwind arbitrary values may hold quotes and ampersands, which Slim names never do.
486
495
  value = ERB::Escape.html_escape(value) if value.match?(SHORTCUT_VALUE_TO_ESCAPE)
487
- attributes << StaticAttribute.new(name, value)
496
+ attributes << StaticAttribute.new((@scanner.peek_byte == HASH) ? "id" : "class", value)
488
497
  @scanner.pos += shortcut_length
489
498
  end
490
499
 
@@ -17,10 +17,10 @@ module SuperSlim
17
17
 
18
18
  def class_value(value, escape, html_safe)
19
19
  if value.is_a?(Array)
20
- value = value.flatten.filter_map do |element|
21
- string = element.to_s
22
- string unless string.empty?
23
- end.join(" ")
20
+ # `flatten` returns a new array: changing it in place spares one more.
21
+ classes = value.flatten.map!(&:to_s)
22
+ classes.reject!(&:empty?)
23
+ value = classes.join(" ")
24
24
  end
25
25
  return value.to_s unless escape
26
26
 
@@ -28,11 +28,13 @@ module SuperSlim
28
28
  end
29
29
 
30
30
  def join_classes(*values)
31
- values.reject(&:empty?).join(" ")
31
+ values.reject!(&:empty?)
32
+ values.join(" ")
32
33
  end
33
34
 
34
35
  # Attributes only known at render time: splats (`*hash`), `data=`/`aria=` hashes and dynamic tags (`*{tag: "a"}`).
35
- class Attributes
36
+ # A hash of each attribute's rendered value by name, in the order they render: one object per tag.
37
+ class Attributes < Hash
36
38
  # Slim lets tabs and line breaks through: they end a name, so `data: {"x\tonclick" => code}` set onclick.
37
39
  INVALID_NAME = /[[:cntrl:] "'>\/=]/
38
40
  # Read after escaping, which turns markup into entities: `&` is refused with the rest.
@@ -41,19 +43,15 @@ module SuperSlim
41
43
  # Far deeper than any page needs, and short of the stack a hash referring to itself would exhaust.
42
44
  MAX_HYPHENATED_DEPTH = 100
43
45
 
44
- def initialize(html_safe)
45
- @html_safe = html_safe
46
- @value_by_name = {}
47
- end
48
-
49
- def code_attr(name, escape, value)
46
+ # Names are checked unless the compiler already found them valid: those from render-time data always are.
47
+ def code_attr(name, escape, value, check = true)
50
48
  if name == "class"
51
49
  value = value.is_a?(Array) ? value.join(" ") : value.to_s
52
- attr(name, escape(escape, value)) unless value.empty?
50
+ attr(name, escape(escape, value), false) unless value.empty?
53
51
  elsif HYPHENATED.include?(name) && value.is_a?(Hash)
54
52
  hyphenated_attr(name, escape, value)
55
53
  elsif value != false && !value.nil?
56
- attr(name, escape(escape, value))
54
+ attr(name, escape(escape, value), check)
57
55
  end
58
56
  end
59
57
 
@@ -61,20 +59,20 @@ module SuperSlim
61
59
  splat.each { |name, value| code_attr(name.is_a?(Symbol) ? name.name : name.to_s, true, value) }
62
60
  end
63
61
 
64
- def attr(name, value)
65
- raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if name.empty? || name.match?(INVALID_NAME)
62
+ def attr(name, value, check = true)
63
+ raise InvalidAttributeNameError, "Invalid attribute name '#{name}' was rendered" if check && (name.empty? || name.match?(INVALID_NAME))
66
64
 
67
- if !@value_by_name.key?(name)
68
- @value_by_name[name] = value
65
+ if !key?(name)
66
+ self[name] = value
69
67
  elsif name == "class"
70
- @value_by_name[name] = "#{@value_by_name[name]} #{value}"
68
+ self[name] = "#{self[name]} #{value}"
71
69
  else
72
70
  raise "Multiple #{name} attributes specified"
73
71
  end
74
72
  end
75
73
 
76
74
  def build_tag(content = nil)
77
- tag = @value_by_name.delete("tag").to_s
75
+ tag = delete("tag").to_s
78
76
  tag = "div" if tag.empty?
79
77
  raise InvalidTagNameError, "Invalid tag name '#{tag}' was rendered" unless tag.match?(VALID_TAG)
80
78
  content ? "<#{tag}#{self}>#{content}</#{tag}>" : "<#{tag}#{self} />"
@@ -82,7 +80,7 @@ module SuperSlim
82
80
 
83
81
  def to_s
84
82
  html = +""
85
- @value_by_name.each do |name, value|
83
+ each do |name, value|
86
84
  html << " " << name
87
85
  html << %(=") << value.to_s << %(") unless value == true
88
86
  end
@@ -102,9 +100,16 @@ module SuperSlim
102
100
  end
103
101
 
104
102
  def escape(escape, value)
105
- return value if !escape || value == true
103
+ (!escape || value == true) ? value : Runtime.escape_html(value)
104
+ end
105
+ end
106
+
107
+ # Values that are `html_safe?` render as they are.
108
+ class HtmlSafeAttributes < Attributes
109
+ private
106
110
 
107
- @html_safe ? Runtime.escape_html_safe(value) : Runtime.escape_html(value)
111
+ def escape(escape, value)
112
+ (!escape || value == true) ? value : Runtime.escape_html_safe(value)
108
113
  end
109
114
  end
110
115
  end
@@ -3,7 +3,6 @@
3
3
  module SuperSlim
4
4
  # `#name` sets the id and `.name` a class, on a div unless a tag name comes first.
5
5
  module Shortcuts
6
- ATTRIBUTE_BY_SHORTCUT = {"#" => "id", "." => "class"}.freeze
7
6
  DEFAULT_TAG = "div"
8
7
 
9
8
  SLIM_NAME_PART = /\p{Word}|-|\/\d+|:(?:\w|-)+/
@@ -21,8 +20,8 @@ module SuperSlim
21
20
  | (?<=-)\( (?:[^()\s] | \([^()\s]*\))+ \)
22
21
  }x
23
22
 
24
- ATTRIBUTE_PATTERN = /\A([#.]+)((?:#{SLIM_NAME_PART})*)/
25
- TAILWIND_ATTRIBUTE_PATTERN = /\A([#.]+)((?:#{TAILWIND_NAME_PART})*)/
26
- TAG_PATTERN = /\A(?:[#.]|\*(?=[^\s]+)|(\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+))/
23
+ ATTRIBUTE_PATTERN = /\A[#.]+((?:#{SLIM_NAME_PART})*)/
24
+ TAILWIND_ATTRIBUTE_PATTERN = /\A[#.]+((?:#{TAILWIND_NAME_PART})*)/
25
+ TAG_PATTERN = /\A(?:[#.]|\*(?=[^\s]+)|(?:\p{Word}(?:\p{Word}|:|-)*\p{Word}|\p{Word}+))/
27
26
  end
28
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.6.3"
4
+ VERSION = "3.8.0"
5
5
  end
metadata CHANGED
@@ -1,90 +1,90 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super-slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.3
4
+ version: 3.8.0
5
5
  platform: ruby
6
6
  authors:
7
- - Level UP Solutions
7
+ - Level UP Solutions
8
8
  bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: strscan
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: 3.1.1
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: 3.1.1
26
- description: A Slim template engine written from scratch, without Temple. It renders
27
- the same HTML as Slim 5, compiles and renders faster with fewer allocations, and
28
- comes with a formatter that cannot change a page, a linter with HTML and accessibility
29
- rules, and a RuboCop plugin for the Ruby inside templates.
12
+ - !ruby/object:Gem::Dependency
13
+ name: strscan
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.1.1
28
+ description: "A Slim template engine written from scratch, without Temple. It renders the same HTML as Slim 5, compiles and renders faster with fewer allocations, and comes with a formatter that cannot change a page, a linter with HTML and accessibility rules, and a RuboCop plugin for the Ruby inside templates."
30
29
  executables:
31
- - super-slim
30
+ - super-slim
32
31
  extensions: []
33
32
  extra_rdoc_files: []
34
33
  files:
35
- - CHANGELOG.md
36
- - LICENSE
37
- - LICENSE-slim
38
- - README.md
39
- - config/rubocop.yml
40
- - exe/super-slim
41
- - lib/super-slim.rb
42
- - lib/super_slim.rb
43
- - lib/super_slim/ast.rb
44
- - lib/super_slim/cli.rb
45
- - lib/super_slim/compiler.rb
46
- - lib/super_slim/formatter.rb
47
- - lib/super_slim/formatter/attribute_arranger.rb
48
- - lib/super_slim/formatter/code_wrapper.rb
49
- - lib/super_slim/formatter/line_wrapper.rb
50
- - lib/super_slim/linter.rb
51
- - lib/super_slim/linter/presets.rb
52
- - lib/super_slim/linter/rules.rb
53
- - lib/super_slim/parallel.rb
54
- - lib/super_slim/parser.rb
55
- - lib/super_slim/parser/ruby_brackets.rb
56
- - lib/super_slim/rails_handler.rb
57
- - lib/super_slim/railtie.rb
58
- - lib/super_slim/rubocop.rb
59
- - lib/super_slim/runtime.rb
60
- - lib/super_slim/shortcuts.rb
61
- - lib/super_slim/version.rb
62
- homepage: https://github.com/levups/super-slim
34
+ - CHANGELOG.md
35
+ - LICENSE
36
+ - LICENSE-slim
37
+ - README.md
38
+ - config/rubocop.yml
39
+ - exe/super-slim
40
+ - lib/super-slim.rb
41
+ - lib/super_slim.rb
42
+ - lib/super_slim/ast.rb
43
+ - lib/super_slim/cli.rb
44
+ - lib/super_slim/compiler.rb
45
+ - lib/super_slim/formatter.rb
46
+ - lib/super_slim/formatter/attribute_arranger.rb
47
+ - lib/super_slim/formatter/code_wrapper.rb
48
+ - lib/super_slim/formatter/line_wrapper.rb
49
+ - lib/super_slim/linter.rb
50
+ - lib/super_slim/linter/presets.rb
51
+ - lib/super_slim/linter/rules.rb
52
+ - lib/super_slim/parallel.rb
53
+ - lib/super_slim/parser.rb
54
+ - lib/super_slim/parser/ruby_brackets.rb
55
+ - lib/super_slim/rails_handler.rb
56
+ - lib/super_slim/railtie.rb
57
+ - lib/super_slim/rubocop.rb
58
+ - lib/super_slim/runtime.rb
59
+ - lib/super_slim/shortcuts.rb
60
+ - lib/super_slim/version.rb
61
+ homepage: "https://github.com/levups/super-slim"
63
62
  licenses:
64
- - MIT
63
+ - MIT
65
64
  metadata:
66
- source_code_uri: https://github.com/levups/super-slim
67
- changelog_uri: https://github.com/levups/super-slim/blob/main/CHANGELOG.md
68
- documentation_uri: https://levups.github.io/super-slim/
69
- bug_tracker_uri: https://github.com/levups/super-slim/issues
70
- rubygems_mfa_required: 'true'
71
- default_lint_roller_plugin: SuperSlim::RuboCop::Plugin
65
+ source_code_uri: "https://github.com/levups/super-slim"
66
+ changelog_uri: "https://github.com/levups/super-slim/blob/main/CHANGELOG.md"
67
+ documentation_uri: "https://levups.github.io/super-slim/"
68
+ bug_tracker_uri: "https://github.com/levups/super-slim/issues"
69
+ rubygems_mfa_required: "true"
70
+ default_lint_roller_plugin: "SuperSlim::RuboCop::Plugin"
72
71
  rdoc_options: []
73
72
  require_paths:
74
- - lib
73
+ - lib
75
74
  required_ruby_version: !ruby/object:Gem::Requirement
76
75
  requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- version: '3.3'
76
+ -
77
+ - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "3.3"
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
82
+ -
83
+ - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
85
86
  requirements: []
86
- rubygems_version: 4.0.20
87
+ rubygems_version: 4.1.1
87
88
  specification_version: 4
88
- summary: A Slim template engine that compiles and renders faster, with a formatter
89
- and a linter
89
+ summary: "A Slim template engine that compiles and renders faster, with a formatter and a linter"
90
90
  test_files: []