super-slim 3.6.0 → 3.6.2

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: 80dad2f87ffeca714cec78be16f94bea2271a13f0c095a9d4b6809f1fd256b85
4
- data.tar.gz: 7b7da5b1350e4ddb1e27519d2bf6d80136ce27b9ce55b492ec06b6e8fb67deaa
3
+ metadata.gz: cdd1bc27d23e754183cb4f4e4963a935d56d030a1b73d60bccfc4ee97f8972bc
4
+ data.tar.gz: 2419f8247b33000daa959e96470679eb54ef778e5c82aa6c0555323e68e7ed02
5
5
  SHA512:
6
- metadata.gz: f5836cf9c4c31086aee7e4de49fce6179796ff8858e5f3f78851a091b0489fdc31e39b66dfcf32ae0513cb622af8299d4bc125aaec1b8a5af3c113adfeb6698f
7
- data.tar.gz: 396e25566bd11a16442861680472c09b5deffccac68b0c47ed5fbc49917422802e3d04646853a78e08b67ca74f47b6f13f2e7a87ca5e406df2102c39c4f6f121
6
+ metadata.gz: 545d8dd22428f0a94ccb6e36c0f1108427ca607d6242abf1abe1dc69c9969ba6db04cef1404262017214599499813f0a028b3e2392fe17f48eb9a25e5746ef50
7
+ data.tar.gz: 339168144fd87e1b8a6ef09e293c1f645d31c0da65c30c3fab686fa63dd6fb9debcbf5f63e92396c244031e75ca0c7aba202c9f9ea0cf0d94a6dd69ceca47018
data/CHANGELOG.md CHANGED
@@ -2,6 +2,28 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [3.6.2] — 2026-11-20
6
+
7
+ Template errors found while compiling now point at their template line, in Rails too.
8
+
9
+ **Fixed**
10
+ - `Explicit end statements are forbidden`, `Multiple … attributes specified`, `Invalid xml directive in html mode`
11
+ and `Invalid doctype` are raised as `SuperSlim::SyntaxError` with the file, the line and a caret, like parse
12
+ errors, so Rails' error page shows the template line instead of `compiler.rb`. They are still a `SuperSlim::Error`.
13
+ - Code on the `ruby:` line itself, or an empty `ruby:` block, no longer moves every later line two lines down in
14
+ backtraces and `__LINE__`.
15
+ - Without `file:`, parse errors and `Invalid UTF-8` start their backtrace at `(__TEMPLATE__):<line>`, like the
16
+ others.
17
+
18
+ ## [3.6.1] — 2026-11-11
19
+
20
+ One fix: `InstanceVariables` no longer reports an `@` that no name follows.
21
+
22
+ **Fixed**
23
+ - `InstanceVariables` no longer reports ``Pass `@` to the partial as a local`` on an `@` that no name follows, as in
24
+ the `_form.html.slim` scaffold template simple_form installs, `= simple_form_for(@<%= singular_table_name %>)`. 3.6.0
25
+ did, 3.5.0 did not.
26
+
5
27
  ## [3.6.0] — 2026-11-11
6
28
 
7
29
  The linter reads all the Ruby of a template and `.super-slim.yml` is checked when it is read: `super-slim lint` may
@@ -299,7 +321,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
299
321
  - 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,
300
322
  newer than the one Ruby 3.3 ships.
301
323
 
302
- [Unreleased]: https://github.com/levups/super-slim/compare/v3.6.0...HEAD
324
+ [Unreleased]: https://github.com/levups/super-slim/compare/v3.6.2...HEAD
325
+ [3.6.2]: https://github.com/levups/super-slim/compare/v3.6.1...v3.6.2
326
+ [3.6.1]: https://github.com/levups/super-slim/compare/v3.6.0...v3.6.1
303
327
  [3.6.0]: https://github.com/levups/super-slim/compare/v3.5.0...v3.6.0
304
328
  [3.5.0]: https://github.com/levups/super-slim/compare/v3.4.1...v3.5.0
305
329
  [3.4.1]: https://github.com/levups/super-slim/compare/v3.4.0...v3.4.1
data/README.md CHANGED
@@ -100,6 +100,12 @@ defaults are different (`format: :xhtml, sort_attrs: true`), so if you never cha
100
100
  Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically, with `<link />` and
101
101
  `doctype xml`.
102
102
 
103
+ Errors keep Slim's messages, but those found while compiling also name their file and line, which Slim leaves out:
104
+ `Explicit end statements are forbidden`, `Multiple id attributes specified`, an `xml` directive in HTML and an
105
+ unknown doctype. SuperSlim raises them as `SuperSlim::SyntaxError`, like a parse error, so Rails' error page shows
106
+ the template line. Every line of a `ruby:` block also stays on its template line, where Slim moves the lines after
107
+ it, so an error or a `__LINE__` there reports the line you see in the template.
108
+
103
109
  ## Formatter
104
110
 
105
111
  `super-slim format` rewrites templates in one layout: two spaces per level, a space after `-` and `=`, no `div`
@@ -112,7 +112,7 @@ module SuperSlim
112
112
  block_open = false
113
113
  nodes.each do |node|
114
114
  if node.is_a?(Control)
115
- raise Error, "Explicit end statements are forbidden" if node.code.match?(EXPLICIT_END)
115
+ compile_error!("Explicit end statements are forbidden") if node.code.match?(EXPLICIT_END)
116
116
 
117
117
  emit_code("end") if block_open && !node.code.match?(BLOCK_CONTINUATION)
118
118
  block_open = with_do(node.code, node.children).match?(BLOCK_OPENING)
@@ -223,7 +223,11 @@ module SuperSlim
223
223
  seen_names = {}
224
224
  attributes.each do |attribute|
225
225
  next if attribute.name == "class"
226
- raise Error, "Multiple #{attribute.name} attributes specified" if seen_names.key?(attribute.name)
226
+ if seen_names.key?(attribute.name)
227
+ # Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
228
+ code_newlines = attributes.sum { _1.is_a?(CodeAttribute) ? _1.code.count("\n") : 0 }
229
+ compile_error!("Multiple #{attribute.name} attributes specified", lines_ahead: code_newlines)
230
+ end
227
231
 
228
232
  seen_names[attribute.name] = true
229
233
  end
@@ -391,11 +395,15 @@ module SuperSlim
391
395
 
392
396
  # Keeps every line of the block on its template line, blank lines included. The code must end with a
393
397
  # newline, as its last line may be a comment: when no blank line follows, that newline is the one of the
394
- # newline node that comes next.
398
+ # newline node that comes next. Code on the `ruby:` line itself has no newline before it, an empty block none
399
+ # at all.
395
400
  def compile_ruby(children)
396
401
  code = children.grep(Text).map(&:text).join
397
- trailing_newline_count = children.count(NEWLINE) - 1 - code.count("\n")
398
- @code << "\n"
402
+ leading_newline_count = children.index { _1 != NEWLINE } || children.size
403
+ trailing_newline_count = children.count(NEWLINE) - leading_newline_count - code.count("\n")
404
+ @code << ("\n" * leading_newline_count)
405
+ return if code.empty?
406
+
399
407
  emit_code(code)
400
408
  @code << ("\n" * [trailing_newline_count, 1].max)
401
409
  @newline_emitted = trailing_newline_count.zero?
@@ -423,14 +431,21 @@ module SuperSlim
423
431
  def doctype(type)
424
432
  type = type.downcase
425
433
  if (xml = type.match(/\Axml(\s+(.+))?\z/))
426
- raise Error, "Invalid xml directive in html mode" unless @xml
434
+ compile_error!("Invalid xml directive in html mode") unless @xml
427
435
 
428
436
  %(<?xml version="1.0" encoding="#{xml[2] || "utf-8"}" ?>)
429
437
  else
430
- @doctypes[type] || raise(Error, "Invalid doctype #{type}")
438
+ @doctypes[type] || compile_error!("Invalid doctype #{type}")
431
439
  end
432
440
  end
433
441
 
442
+ # The compiled code keeps each template line on its own line, so the newlines written so far give the line of
443
+ # the node being compiled: no node carries its line, which would cost every template line. Code not written
444
+ # yet may still hold `lines_ahead` of them.
445
+ def compile_error!(message, lines_ahead: 0)
446
+ raise CompileError.new(message, @code.count("\n") + lines_ahead + 1)
447
+ end
448
+
434
449
  def capture_into(variable)
435
450
  if @rails
436
451
  emit_code("#{variable} = capture do")
@@ -319,7 +319,10 @@ module SuperSlim
319
319
  return variable_by_line unless code.include?("@")
320
320
 
321
321
  Prism.lex(code).value.each do |token,|
322
- variable_by_line[token.location.start_line] ||= token.value if token.type == :INSTANCE_VARIABLE
322
+ # An `@` that no name follows is lexed as one too: `@<%= name %>` in a generator's template.
323
+ next unless token.type == :INSTANCE_VARIABLE && token.value != "@"
324
+
325
+ variable_by_line[token.location.start_line] ||= token.value
323
326
  end
324
327
  variable_by_line
325
328
  end
@@ -148,7 +148,9 @@ module SuperSlim
148
148
  def invalid_encoding!(source)
149
149
  lines = source.b.split("\n")
150
150
  lineno = lines.index { |line| !line.dup.force_encoding(Encoding::UTF_8).valid_encoding? } || 0
151
- raise SyntaxError.new("Invalid UTF-8", @file, lines[lineno].to_s.force_encoding(Encoding::UTF_8).scrub, lineno + 1, 0)
151
+ error = SyntaxError.new("Invalid UTF-8", @file, lines[lineno].to_s.force_encoding(Encoding::UTF_8).scrub, lineno + 1, 0)
152
+ error.set_backtrace(["#{error.file}:#{lineno + 1}", *caller])
153
+ raise error
152
154
  end
153
155
 
154
156
  def line_starts(source, byte_order_mark_size)
@@ -659,7 +661,7 @@ module SuperSlim
659
661
 
660
662
  def syntax_error!(message)
661
663
  error = SyntaxError.new(message, @file, @line, @lineno, (@line && @scanner) ? column : 0)
662
- error.set_backtrace(["#{@file}:#{@lineno}", *caller])
664
+ error.set_backtrace(["#{error.file}:#{@lineno}", *caller])
663
665
  raise error
664
666
  end
665
667
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.6.0"
4
+ VERSION = "3.6.2"
5
5
  end
data/lib/super_slim.rb CHANGED
@@ -42,6 +42,17 @@ module SuperSlim
42
42
  end
43
43
  end
44
44
 
45
+ # A template error found while compiling, at its template line: `compile` adds the file and the line's text, and
46
+ # raises it as a SyntaxError.
47
+ class Compiler::CompileError < Error
48
+ attr_reader :lineno
49
+
50
+ def initialize(message, lineno)
51
+ @lineno = lineno
52
+ super(message)
53
+ end
54
+ end
55
+
45
56
  class << self
46
57
  # Shortcuts also read Tailwind class names, such as `.rounded-b-[3px]` or `.lg:hover:underline`. Off by
47
58
  # default so that switching from Slim changes no template: Slim renders `.size-2.5` as two classes.
@@ -49,6 +60,23 @@ module SuperSlim
49
60
 
50
61
  def compile(source, file: nil, tailwind_classes: self.tailwind_classes, **compiler_options)
51
62
  Compiler.new(**compiler_options).call(Parser.new(file:, tailwind_classes:).call(source))
63
+ rescue Compiler::CompileError => exception
64
+ # Without its cause: Rails would show the compiler's backtrace below the template line.
65
+ raise compile_syntax_error(exception, source, file), cause: nil
66
+ end
67
+
68
+ private
69
+
70
+ # Located like the parser's errors, its backtrace starting at the template line, which Rails reports.
71
+ def compile_syntax_error(exception, source, file)
72
+ lineno = exception.lineno
73
+ # The source parsed, so it is valid UTF-8 whatever its encoding says. A construct whose compiled code takes
74
+ # more lines than its template can move the count past the end: the error still raises, without that line.
75
+ line = String.new(Parser.split_lines(source.b)[lineno - 1].to_s, encoding: Encoding::UTF_8)
76
+ line = line.delete_prefix("") if lineno == 1
77
+ error = SyntaxError.new(exception.message, file, line, lineno, line[/\A[ \t]*/].size)
78
+ error.set_backtrace(["#{error.file}:#{lineno}", *exception.backtrace])
79
+ error
52
80
  end
53
81
  end
54
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super-slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.0
4
+ version: 3.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions