super-slim 3.2.0 → 3.2.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: 4f7d1e689786582fc22547722bf3b3ef03847b976163b3560c48a9ab06e8178e
4
- data.tar.gz: 7b422a5d70567feef50061078f19bceb966fb7d6363c9fc48040fae004335f34
3
+ metadata.gz: 2dd1011e0ac6372401bfc8b9f1ae5dad00cfc9208fc845e892eb1d0b7ebf2aae
4
+ data.tar.gz: 2ceb2fe0dc641bd9ccbd49c7841cdaf1f8124c7b2b1340ec7961be80b30a9421
5
5
  SHA512:
6
- metadata.gz: e5578534152295f0f13694febb4b5f54fa2c6b72a3e9ce0852f0ea45cbbe200f782bf51d6eb1ec19f3f798d9117351e62d3b2fdd9096d9e4cb016c12da51f198
7
- data.tar.gz: dcef262dc4ff56c2e908d9a57e55b77818370fe2057685e4084b31e1f1d056e9bf66108487000e2f5ecec4f5a00a0027c4127c311e611cc0f7aa5718a0f5a413
6
+ metadata.gz: ad7f7c44edd4bb989722855e7baa362ca0285fcb3168eedc474205f7f9817b0f7bf296ebb566edaa867f8fe7f73d850aa20881859b88a1e32bab76f58f6123da
7
+ data.tar.gz: 6842ed4614918cd699dcaae1d9115257dc5755e343699bfcdf2c387da90bb731bc106cac062f9c0cd84f75cea351e13c82b388c9f46053382819f91ba3b08b8d
data/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [3.2.2] — 2026-11-06
6
+
7
+ `super-slim format` no longer joins code spread by hand into lines past `max_line_length`.
8
+
9
+ **Fixed**
10
+ - With `max_line_length`, an argument that still passes the limit once its call is spread one argument per line
11
+ spreads its own elements or arguments in turn, an array before `.join` included, instead of coming back on one
12
+ long line. Code spread by hand, such as a string continued with a backslash, keeps its layout whenever the
13
+ formatted one would hold a longer line.
14
+
15
+ ## [3.2.1] — 2026-11-06
16
+
17
+ A comment after Ruby code no longer breaks the template.
18
+
19
+ **Fixed**
20
+ - A comment that ends a line of Ruby code, as in
21
+ `= render CardComponent.new(post:) # rubocop:disable Some/Cop -- reason`,
22
+ no longer makes the template a syntax error: it is left out of the
23
+ compiled Ruby, after `=`, `==` and `-` alike, so a RuboCop directive can
24
+ sit on the line it is about. Slim refuses these lines.
25
+
5
26
  ## [3.2.0] — 2026-11-06
6
27
 
7
28
  One layout per line and per tag in `super-slim format`, cache digests that follow Slim partials, and a faster lint.
@@ -172,7 +193,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
172
193
  - 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,
173
194
  newer than the one Ruby 3.3 ships.
174
195
 
175
- [Unreleased]: https://github.com/levups/super-slim/compare/v3.2.0...HEAD
196
+ [Unreleased]: https://github.com/levups/super-slim/compare/v3.2.2...HEAD
197
+ [3.2.2]: https://github.com/levups/super-slim/compare/v3.2.1...v3.2.2
198
+ [3.2.1]: https://github.com/levups/super-slim/compare/v3.2.0...v3.2.1
176
199
  [3.2.0]: https://github.com/levups/super-slim/compare/v3.1.1...v3.2.0
177
200
  [3.1.1]: https://github.com/levups/super-slim/compare/v3.1.0...v3.1.1
178
201
  [3.1.0]: https://github.com/levups/super-slim/compare/v3.0.0...v3.1.0
data/README.md CHANGED
@@ -249,7 +249,7 @@ attributes and whitespace markers (`span> title="x" = name`), so you may see a f
249
249
 
250
250
  ## Additions to Slim
251
251
 
252
- Two additions read templates that Slim refuses or misreads. Neither changes a template that already works with
252
+ Three additions read templates that Slim refuses or misreads. None changes a template that already works with
253
253
  Slim.
254
254
 
255
255
  ### Ruby code over several lines
@@ -271,6 +271,13 @@ an opening `(`, `[` or `{` at the end of a line, until the bracket is closed:
271
271
  A bracket opened in the middle of a line, as in `- items.each { |item|`, works as before: the lines below it are
272
272
  its children.
273
273
 
274
+ ### Comments after Ruby code
275
+
276
+ Slim compiles `= render CardComponent.new(post:) # rubocop:disable Some/Cop -- reason` into a syntax error: the
277
+ comment swallows the parenthesis that closes the output, or the `do` of a block. SuperSlim leaves a comment that
278
+ follows code on its line out of the compiled Ruby, after `=`, `==` and `-` alike, so a RuboCop directive can sit on
279
+ the line it is about.
280
+
274
281
  ### Tailwind class names
275
282
 
276
283
  Slim stops a `.class` shortcut at characters such as `[`, `:` or `/`, which Tailwind uses all the time. It renders
@@ -149,9 +149,22 @@ module SuperSlim
149
149
  end
150
150
 
151
151
  def with_do(code, children)
152
+ code = without_trailing_comment(code)
152
153
  (code.match?(BLOCK_SELF_OPENING) || AST.empty?(children)) ? code : "#{code} do"
153
154
  end
154
155
 
156
+ # A comment that ends the code would swallow what the compiler writes after it, such as the closing
157
+ # parenthesis of an output or the `do` of a block. It never runs, so it is left out. One alone on a line
158
+ # stays: a backslash before it carries the code on to that line.
159
+ def without_trailing_comment(code)
160
+ return code unless code.include?("#")
161
+
162
+ comment_offset = Parser::RubyBrackets.new(code).trailing_comment_offset or return code
163
+
164
+ code_before = code.byteslice(0, comment_offset).sub(/[ \t]+\z/, "")
165
+ (code_before.empty? || code_before.end_with?("\n")) ? code : code_before
166
+ end
167
+
155
168
  def compile_tag(tag)
156
169
  return compile_dynamic_tag(tag) if tag.name == "*"
157
170
 
@@ -11,9 +11,10 @@ module SuperSlim
11
11
  TEMPLATE_METHOD = "def template\n"
12
12
  CODE_OFFSET = TEMPLATE_METHOD.bytesize
13
13
 
14
- # Returns the lines after the first one's indentation, or nil when the code has nothing to spread.
15
- def self.call(code, indent, opens_block:)
16
- new(code, indent, opens_block:).call
14
+ # Returns the lines after the first one's indentation, or nil when the code has nothing to spread. An item
15
+ # still longer than max_line_length spreads its own arguments or elements.
16
+ def self.call(code, indent, opens_block:, max_line_length:)
17
+ new(code, indent, opens_block:, max_line_length:).call
17
18
  end
18
19
 
19
20
  # `render Card.new title:` reads as if title went to render: arguments to `new` go in parentheses.
@@ -68,17 +69,19 @@ module SuperSlim
68
69
  end
69
70
 
70
71
  # opens_block: whether the line has children, which Slim passes to its call as a block even without `do`.
71
- def initialize(code, indent, opens_block:)
72
+ def initialize(code, indent, opens_block:, max_line_length:)
72
73
  @code = code
73
74
  @indent = indent
74
75
  @opens_block = opens_block
76
+ @max_line_length = max_line_length
75
77
  end
76
78
 
77
79
  def call
78
80
  statements, opens_block = self.class.parse(@code)
79
81
  return unless statements&.size == 1 && (node = spreadable(statements.first))
80
82
 
81
- spread(node, @opens_block || opens_block)
83
+ closing_indent = @indent + (" " * ((@opens_block || opens_block) ? 2 * INDENT_WIDTH : INDENT_WIDTH))
84
+ spread(node, CODE_OFFSET, CODE_OFFSET + @code.bytesize, closing_indent)
82
85
  end
83
86
 
84
87
  private
@@ -89,37 +92,48 @@ module SuperSlim
89
92
  when Prism::CallNode
90
93
  items = self.class.arguments_of(node)
91
94
  # An operator such as `+` has no parentheses to add, and `list[index] = value` holds value in its arguments.
92
- return if items.nil? || node.name.end_with?("=") || (node.opening_loc.nil? && !METHOD_NAME.match?(node.name.to_s))
93
- return spreadable(items.first) if items.size == 1
95
+ return if node.name.end_with?("=") || (node.opening_loc.nil? && !METHOD_NAME.match?(node.name.to_s))
96
+ return node if items && items.size > 1
94
97
 
95
- node
98
+ # `[a, b].join(" ")` spreads its array.
99
+ (spreadable(items.first) if items) || (spreadable(node.receiver) if node.receiver.is_a?(Prism::ArrayNode) || node.receiver.is_a?(Prism::HashNode))
96
100
  when Prism::ArrayNode, Prism::HashNode
97
101
  # `%w[a b]` takes no commas.
98
102
  node if ["[", "{"].include?(node.opening) && node.elements.size > 1
99
103
  end
100
104
  end
101
105
 
102
- def spread(node, opens_block)
106
+ # The lines of the code from first to last, node's items spread between them, the first line unindented.
107
+ def spread(node, first, last, closing_indent)
103
108
  items = node.is_a?(Prism::CallNode) ? self.class.arguments_of(node) : node.elements
104
109
  # `hint: -> do` ends past the line, with the children of the line.
105
110
  return if self.class.end_offset(items.last) > CODE_OFFSET + @code.bytesize
106
111
 
107
- closing_indent = @indent + (" " * (opens_block ? 2 * INDENT_WIDTH : INDENT_WIDTH))
108
112
  item_indent = closing_indent + (" " * INDENT_WIDTH)
109
113
  head, tail =
110
114
  if node.opening_loc
111
- [slice(CODE_OFFSET, node.opening_loc.end_offset), slice(node.closing_loc.start_offset)]
115
+ [slice(first, node.opening_loc.end_offset), slice(node.closing_loc.start_offset, last)]
112
116
  else
113
- ["#{slice(CODE_OFFSET, node.message_loc.end_offset)}(", ")#{slice(self.class.end_offset(items.last))}"]
117
+ ["#{slice(first, node.message_loc.end_offset)}(", ")#{slice(self.class.end_offset(items.last), last)}"]
114
118
  end
115
- item_lines = items.map do |item|
116
- # A comma after `&block` is a syntax error.
117
- comma = "," unless item.is_a?(Prism::BlockArgumentNode)
118
- "#{item_indent}#{slice(item.location.start_offset, self.class.end_offset(item))}#{comma}"
119
- end
119
+ item_lines = items.flat_map { spread_item(_1, item_indent) }
120
120
  [head, *item_lines, closing_indent + tail]
121
121
  end
122
122
 
123
+ def spread_item(item, indent)
124
+ # A comma after `&block` is a syntax error.
125
+ comma = "," unless item.is_a?(Prism::BlockArgumentNode)
126
+ item_end = self.class.end_offset(item)
127
+ line = "#{indent}#{slice(item.location.start_offset, item_end)}#{comma}"
128
+ return [line] if line.size <= @max_line_length
129
+
130
+ nested = spreadable(item.is_a?(Prism::AssocNode) ? item.value : item)
131
+ lines = nested && spread(nested, item.location.start_offset, item_end, indent)
132
+ return [line] unless lines
133
+
134
+ [indent + lines.first, *lines[1...-1], "#{lines.last}#{comma}"]
135
+ end
136
+
123
137
  # From offsets in the parsed source, up to the end of the line by default.
124
138
  def slice(first, last = CODE_OFFSET + @code.bytesize)
125
139
  @code.byteslice((first - CODE_OFFSET)...(last - CODE_OFFSET))
@@ -63,8 +63,8 @@ module SuperSlim
63
63
  rejoined = joined_code(broken, info)
64
64
  return [rejoined] if rejoined && rejoined.size <= @max_line_length
65
65
 
66
- # Only the outer call is spread: a hash spread inside it by hand would come back longer than the limit.
67
- return lines if lines.size > 1 && lines.none? { _1.size > @max_line_length } && broken.any? { _1.size > @max_line_length }
66
+ # A string continued with `\` comes back on one line, longer than the author's.
67
+ return lines if lines.size > 1 && broken.max_by(&:size).size > lines.max_by(&:size).size
68
68
 
69
69
  broken
70
70
  end
@@ -121,7 +121,7 @@ module SuperSlim
121
121
  return lines unless code_start && lines.last.size > @max_line_length
122
122
 
123
123
  last_indent = lines.last[/\A */]
124
- code_lines = CodeWrapper.call(lines.last[code_start..], last_indent, opens_block:)
124
+ code_lines = CodeWrapper.call(lines.last[code_start..], last_indent, opens_block:, max_line_length: @max_line_length)
125
125
  return lines unless code_lines
126
126
 
127
127
  lines[0...-1] + [lines.last[0...code_start] + code_lines.first, *code_lines.drop(1)]
@@ -3,7 +3,7 @@
3
3
  module SuperSlim
4
4
  class Parser
5
5
  # Read by hand, a line at a time: Prism would cost every process that renders templates its load time and memory.
6
- # Misread, all rare after a bracket that ends its line: heredocs, `?(`, `$'`, a regexp after a method name or a
6
+ # Misread, all rare after a bracket that ends its line or before a comment that ends the code: heredocs, `?(`, `$'`, a regexp after a method name or a
7
7
  # keyword (`when /'/`), `%` glued to a value (`"%d"%[a]`), interpolation in a %-literal and %-literals between
8
8
  # other delimiters (`%|a(|`).
9
9
  class RubyBrackets
@@ -28,7 +28,11 @@ module SuperSlim
28
28
  read
29
29
  end
30
30
 
31
+ # The byte offset of the comment that ends the code, if one does.
32
+ attr_reader :trailing_comment_offset
33
+
31
34
  def <<(line)
35
+ @trailing_comment_offset = nil
32
36
  @scanner << "\n" << line
33
37
  read
34
38
  self
@@ -63,7 +67,10 @@ module SuperSlim
63
67
  when '"', "'", "`" then return @literals << character
64
68
  when "/" then return @literals << character if regexp_start?
65
69
  when "%" then return open_percent_literal if @scanner.scan(PERCENT_LITERAL_OPENING)
66
- when "#" then @scanner.skip_until(LINE_END)
70
+ when "#"
71
+ comment_offset = @scanner.pos - 1
72
+ @scanner.skip_until(LINE_END)
73
+ @trailing_comment_offset = comment_offset if @scanner.eos?
67
74
  end
68
75
  end
69
76
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.2.0"
4
+ VERSION = "3.2.2"
5
5
  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.2.0
4
+ version: 3.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions