super-slim 3.10.1 → 3.11.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: b0a7cbe26329fddff55fc5c358259f25f4e272650a5f8a0b30a17821979f4fa3
4
- data.tar.gz: 9d5311fd6a8b50633528f1575621c596bd72954ea01c2ba4fa789e36fec2dea2
3
+ metadata.gz: 9f696b4361ae01a8f5fefaa107ed07827abd2304e0129dc1702ec1c8c0be49bf
4
+ data.tar.gz: 1cdc4d8c4fbe568522716b0a1049177f3ec571521121c43de15d9fc095189ef0
5
5
  SHA512:
6
- metadata.gz: ea6ea592731f53a2330df2053fceb2a24febd77e28b2db00f0f76094be7f78f4f00939a30a864f6bd2dc7bc6a55f0789d4a779bcb173b531afa9708e82bf582f
7
- data.tar.gz: 32e8c758b9b4e79ac028da82b84353dacef9152534436521b89346213780c8184289706e547b2397f2ead652d1b8b90b6456969c498c27420287c1c20b5a9fae
6
+ metadata.gz: 36838ca199f226321a6987d90f328125de4706e02283b046ef9f919dffaa09fbf11ec2ae325666380d19578816df0830648d265aabb9b5e804fa661ab090985a
7
+ data.tar.gz: f40142d68a2fd49e7782d6b7319bfc7681de42e5439d3339f31a595ecc8d0a81cffc6dfa9a4b6ba8d49bacc9f9e652b8cd94ee4f676d92cdf3b2b93367cd0aa9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [3.11.0] — 2026-12-01
6
+
7
+ DatabaseQueries catches the reads it missed, and the linter reads every template as UTF-8.
8
+
9
+ **New**
10
+ - DatabaseQueries, off by default, reports more reads, so a template it passed may now be reported: `find_sole_by` and `average(:column)` on any receiver; `preload`, `eager_load`, `distinct`, `first!`, `last!` and `take!` on a model; `take` and `sole` on a model, unless the constant holds a value (`Post::IDS.take(3)` passes).
11
+
12
+ **Fixed**
13
+ - DatabaseQueries names a constant written across lines as code reads it (`Post::Comment.count`, without the line break and comment after `::`), keeps `self::`, and names a call under a namespace read from code (`record.class::Comment.where`) by its method alone.
14
+ - A `coding:` comment on the first line of a `ruby:` block no longer changes how the linter reads the block: Slim compiles it into the template, which is UTF-8. A name like `Café` was split in two before DatabaseQueries read it, and InstanceVariables crashed on `@café`.
15
+
5
16
  ## [3.10.1] — 2026-11-30
6
17
 
7
18
  DatabaseQueries tells a constant holding a value from a model.
@@ -427,7 +438,8 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
427
438
  - 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,
428
439
  newer than the one Ruby 3.3 ships.
429
440
 
430
- [Unreleased]: https://github.com/levups/super-slim/compare/v3.10.1...HEAD
441
+ [Unreleased]: https://github.com/levups/super-slim/compare/v3.11.0...HEAD
442
+ [3.11.0]: https://github.com/levups/super-slim/compare/v3.10.1...v3.11.0
431
443
  [3.10.1]: https://github.com/levups/super-slim/compare/v3.10.0...v3.10.1
432
444
  [3.10.0]: https://github.com/levups/super-slim/compare/v3.9.0...v3.10.0
433
445
  [3.9.0]: https://github.com/levups/super-slim/compare/v3.8.2...v3.9.0
data/README.md CHANGED
@@ -471,6 +471,7 @@ bundle exec rubocop
471
471
  FUZZ_SEED=42 FUZZ_CASES=20000 bundle exec ruby -Ilib -Itest test/fuzz_test.rb
472
472
  bundle exec ruby -Ilib bench/corpus.rb path/to/app # compile time and allocations against Slim
473
473
  bundle exec ruby --yjit -Ilib bench/render.rb # render speed and allocations against Slim
474
+ bundle exec ruby -Ilib bench/memory.rb path/to/app # memory of the compiled templates against Slim
474
475
  bundle exec rake docs # rebuild the linter rules page of the website
475
476
  ```
476
477
 
@@ -17,6 +17,15 @@ module SuperSlim
17
17
  class Rule
18
18
  DEFAULTS = {enabled: true}.freeze
19
19
  GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
20
+ # 2026-11: for Prism 0.19 (Ruby 3.3), which has no `encoding: false`: each `:` or `=` after `coding` in a comment
21
+ # on the first line, or the second after a shebang, which Prism reads as the source's encoding.
22
+ MAGIC_ENCODING = /(?:\A(?:[ \t]*#![^\n]*\n)?[ \t]*#|(?!\A)\G)[^\n]*?coding[ \t\v\f\r]*\K[:=]/i
23
+ # 2026-11: tried rather than read from the version: Prism 0.19's FFI backend takes the option and ignores it.
24
+ IGNORES_MAGIC_ENCODING = begin
25
+ Prism.parse("# encoding: us-ascii\nCafé", encoding: false).value.slice.encoding == Encoding::UTF_8
26
+ rescue TypeError
27
+ false
28
+ end
20
29
 
21
30
  # Not memoized: a Ractor other than the main one can't set a class's instance variable.
22
31
  def self.rule_name
@@ -132,13 +141,23 @@ module SuperSlim
132
141
  # Prism reads a `- if` or `- each do` line without its `end`, as it reads code with errors: the tree it gives
133
142
  # holds the calls written.
134
143
  def each_prism_node(code_or_node)
135
- stack = [code_or_node.is_a?(String) ? Prism.parse(code_or_node).value : code_or_node]
144
+ stack = [code_or_node.is_a?(String) ? prism_as_utf8(:parse, code_or_node).value : code_or_node]
136
145
  until stack.empty?
137
146
  node = stack.pop
138
147
  yield node
139
148
  stack.concat(node.compact_child_nodes)
140
149
  end
141
150
  end
151
+
152
+ # `# encoding: us-ascii` on the first line of a `ruby:` block makes Prism read the code in that encoding and
153
+ # split a name like `Café` in two. Slim compiles the block into the template, where Ruby reads it as UTF-8:
154
+ # Prism is told to as well. `method` is `:parse` or `:lex`.
155
+ def prism_as_utf8(method, code)
156
+ return Prism.public_send(method, code, encoding: false) if IGNORES_MAGIC_ENCODING
157
+
158
+ # 2026-11: Prism 0.19 (Ruby 3.3) has no `encoding: false`: the comment's `:` is blunted, offsets kept.
159
+ Prism.public_send(method, code.match?(MAGIC_ENCODING) ? code.gsub(MAGIC_ENCODING, " ") : code)
160
+ end
142
161
  end
143
162
 
144
163
  # Layout: everything `super-slim format` rewrites.
@@ -350,11 +369,12 @@ module SuperSlim
350
369
  variable_by_line = {}
351
370
  return variable_by_line unless code.include?("@")
352
371
 
353
- Prism.lex(code).value.each do |token,|
372
+ prism_as_utf8(:lex, code).value.each do |token,|
354
373
  # An `@` that no name follows is lexed as one too: `@<%= name %>` in a generator's template.
355
374
  next unless token.type == :INSTANCE_VARIABLE && token.value != "@"
356
375
 
357
- variable_by_line[token.location.start_line] ||= token.value
376
+ # 2026-11: as for constant names, a `coding` comment Prism 0.19 still reads leaves the name in its encoding.
377
+ variable_by_line[token.location.start_line] ||= token.value.dup.force_encoding(Encoding::UTF_8).scrub
358
378
  end
359
379
  variable_by_line
360
380
  end
@@ -1063,11 +1083,12 @@ module SuperSlim
1063
1083
  # Matched by method name: an off-by-default rule, since a few of these also run on arrays.
1064
1084
  class DatabaseQueries < Rule
1065
1085
  DEFAULTS = {enabled: false}.freeze
1066
- QUERY_METHODS = %i[where find_by find_by! pluck pick exists? find_each find_in_batches in_batches].to_set.freeze
1067
- # Called on a constant, these read a model's table: `Tag.order(:name)`.
1068
- RELATION_METHODS = %i[all order limit includes joins].to_set.freeze
1086
+ QUERY_METHODS = %i[where find_by find_by! find_sole_by pluck pick average exists? find_each find_in_batches in_batches].to_set.freeze
1087
+ # Called on a constant, these read a model's table: `Tag.order(:name)`. `first!` and the like are left alone
1088
+ # elsewhere, as `first` is: on records the controller loaded, they run no query.
1089
+ RELATION_METHODS = %i[all order limit includes preload eager_load joins distinct first! last! take!].to_set.freeze
1069
1090
  # These too, unless the constant holds a value, which arrays and hashes answer as well.
1070
- ENUMERABLE_METHODS = %i[find first last count sum minimum maximum].to_set.freeze
1091
+ ENUMERABLE_METHODS = %i[find first last take sole count sum minimum maximum].to_set.freeze
1071
1092
  CAPITALS = /\A\p{Lu}[\p{Lu}\d_]*\z/
1072
1093
 
1073
1094
  def offenses(document, _source)
@@ -1095,17 +1116,42 @@ module SuperSlim
1095
1116
 
1096
1117
  constant = receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
1097
1118
  if QUERY_METHODS.include?(node.name)
1098
- constant ? "#{utf8(receiver.slice)}.#{node.name}" : node.name.to_s
1119
+ # `average` always names a column: `stats.average` is someone else's.
1120
+ return if node.name == :average && node.arguments.nil?
1121
+
1122
+ constant ? labelled(receiver, node) : node.name.to_s
1099
1123
  elsif constant && (RELATION_METHODS.include?(node.name) ||
1100
1124
  (ENUMERABLE_METHODS.include?(node.name) && !(value?(receiver) && enumerable_call?(node))))
1101
- "#{utf8(receiver.slice)}.#{node.name}"
1125
+ labelled(receiver, node)
1102
1126
  end
1103
1127
  end
1104
1128
 
1105
- # `find(id)` and `sum(:amount)` only answer on a relation; an array finds with a block and sums its elements.
1129
+ def labelled(constant, node)
1130
+ (path = constant_path(constant)) ? "#{path}.#{node.name}" : node.name.to_s
1131
+ end
1132
+
1133
+ # `Post::Comment` from its nodes, without the line breaks and comments written after a `::`; nil under a
1134
+ # namespace read from code (`record.class::Comment`).
1135
+ def constant_path(constant)
1136
+ case constant
1137
+ when Prism::ConstantReadNode then constant_name(constant)
1138
+ when Prism::ConstantPathNode
1139
+ parent = constant.parent
1140
+ namespace = case parent
1141
+ when nil then ""
1142
+ when Prism::SelfNode then "self"
1143
+ else constant_path(parent)
1144
+ end
1145
+ "#{namespace}::#{constant_name(constant)}" if namespace
1146
+ end
1147
+ end
1148
+
1149
+ # `find(id)`, `sum(:amount)` and `take` only answer on a relation; an array finds with a block, sums its
1150
+ # elements and takes a count.
1106
1151
  def enumerable_call?(node)
1107
1152
  case node.name
1108
1153
  when :find then !node.block.nil?
1154
+ when :take then !node.arguments.nil?
1109
1155
  when :sum then node.arguments.nil? || node.arguments.arguments.none? { _1.is_a?(Prism::SymbolNode) || _1.is_a?(Prism::InterpolatedSymbolNode) }
1110
1156
  else true
1111
1157
  end
@@ -1125,12 +1171,8 @@ module SuperSlim
1125
1171
  elsif constant.child.is_a?(Prism::ConstantReadNode)
1126
1172
  constant.child.name
1127
1173
  end
1128
- utf8(name.to_s)
1129
- end
1130
-
1131
- # Templates are UTF-8, whatever a `# encoding:` comment in a `ruby:` block says.
1132
- def utf8(text)
1133
- text.dup.force_encoding(Encoding::UTF_8).scrub
1174
+ # 2026-11: Prism 0.19 still reads a `coding` comment the blunting misses: templates are UTF-8 all the same.
1175
+ name.to_s.dup.force_encoding(Encoding::UTF_8).scrub
1134
1176
  end
1135
1177
  end
1136
1178
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.10.1"
4
+ VERSION = "3.11.0"
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.10.1
4
+ version: 3.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions