rubocop-katalyst 3.3.2 → 3.4.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: a6b16c5d6d98da6573092a04c8e314e51ebc59c3529b4a318dfb25908bf812c5
4
- data.tar.gz: ef7528f898b148a08fffbc22afa54d9045f112b31d44c395dfce08ca5807c2ad
3
+ metadata.gz: f9732097a9a8c67e05871b2af9d42fb9ff3f5e8078e2312b2553ce0492465e37
4
+ data.tar.gz: 5df1a7c025c139e3949f5f1d642c1442c436f23e26fdbe505a15e30278c75554
5
5
  SHA512:
6
- metadata.gz: f594757e43455794d5653bfa1f1e3ae87e19e089cbc60d577920c1965836f6b0f6bc25a42c8912fd645045dd08af6eb49c59a1c29d8ee323d2b55fe04f279e48
7
- data.tar.gz: ed4ab5aee111a7236bf63402b6bbfc58ba7fbacb2579152530d40beb379e02eee6cb62e422ff0eaf087346cd4687cfdf545fc41d3f9199260717666233a56cb1
6
+ metadata.gz: f3e1daa8424301a929e8cac0134e547680e04897e2b43bbc72650c5526beade267f4b1d52f5a37573a816ee391f70ea57b8d7406298731436530c8e9369b2313
7
+ data.tar.gz: 8a83fcb086f4d0f1ef87dcde47bcda8e5e3ba20b1652467ab5b17e90ac27cc2586bf70ce1a9a284fc0433d913d3576e35712d195423a18f51c2fc8514de4bdad
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [3.4.0] - 2026-11-17
2
+
3
+ - Add Katalyst/RenderInsideRespondTo cop: controllers should render templates
4
+ inside `respond_to`, so unsupported formats answer 406 instead of a 500.
5
+ Pending: runs in projects with `NewCops: enable`, which this gem's defaults
6
+ set.
7
+
1
8
  ## [3.3.0] - 2026-08-18
2
9
 
3
10
  - Add Koi/TableAsset cop: admin index tables (index/archived views and row
data/config/default.yml CHANGED
@@ -15,6 +15,7 @@ inherit_mode:
15
15
  inherit_from:
16
16
  - "./rubocop-bundler.yml"
17
17
  - "./rubocop-gemspec.yml"
18
+ - "./rubocop-katalyst.yml"
18
19
  - "./rubocop-koi.yml"
19
20
  - "./rubocop-layout.yml"
20
21
  - "./rubocop-lint.yml"
@@ -0,0 +1,15 @@
1
+ # Custom cops for Katalyst Rails projects.
2
+ Katalyst:
3
+ Enabled: true
4
+
5
+ # Admin controllers sit behind sign in, so crawlers asking for other formats
6
+ # don't reach them.
7
+ Katalyst/RenderInsideRespondTo:
8
+ Description: "Render templates inside respond_to so unsupported formats answer 406, not a 500."
9
+ Enabled: pending
10
+ Safe: false
11
+ VersionAdded: "3.4"
12
+ Include:
13
+ - "**/app/controllers/**/*.rb"
14
+ Exclude:
15
+ - "**/app/controllers/admin/**/*"
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Katalyst
6
+ # Controllers should render templates inside `respond_to`, listing the
7
+ # formats that have a template.
8
+ #
9
+ # An action without a `render` call renders implicitly, and Rails answers
10
+ # a format it has no template for with 406 Not Acceptable. An explicit
11
+ # `render` skips that check: a request for markdown or JSON looks up a
12
+ # template that does not exist and raises ActionView::MissingTemplate, a
13
+ # 500. Inside `respond_to`, an unlisted format raises
14
+ # ActionController::UnknownFormat instead, which Rails answers with 406.
15
+ #
16
+ # Renders that bring their own body (`json:`, `plain:`, `turbo_stream:`
17
+ # and so on), renders that force a format with `formats:`, and renders of
18
+ # a component instance never look up a template by the request's format,
19
+ # so they are not registered.
20
+ #
21
+ # The cop does not check which formats are listed. Use
22
+ # `format.any(:html, :turbo_stream)` when the action has a turbo stream
23
+ # template. It does not autocorrect for the same reason.
24
+ #
25
+ # @example
26
+ # # bad
27
+ # def show
28
+ # render locals: { venue: }
29
+ # end
30
+ #
31
+ # # good
32
+ # def show
33
+ # respond_to do |format|
34
+ # format.html { render locals: { venue: } }
35
+ # end
36
+ # end
37
+ #
38
+ # # good
39
+ # def update
40
+ # respond_to do |format|
41
+ # format.any(:html, :turbo_stream) { render locals: { collection: } }
42
+ # end
43
+ # end
44
+ #
45
+ # # good
46
+ # render json: venue
47
+ class RenderInsideRespondTo < Base
48
+ MSG = "Render templates inside `respond_to` so unsupported formats answer 406, not a 500."
49
+
50
+ RESTRICT_ON_SEND = %i[render].freeze
51
+
52
+ TEMPLATELESS_KEYS = %i[body file formats html inline js json plain renderable turbo_stream xml].to_set.freeze
53
+
54
+ # @!method component_instance?(node)
55
+ def_node_matcher :component_instance?, <<~PATTERN
56
+ (send const :new ...)
57
+ PATTERN
58
+
59
+ def on_send(node)
60
+ return unless node.receiver.nil?
61
+ return if inside_respond_to?(node)
62
+ return if renders_without_template?(node)
63
+
64
+ add_offense(node)
65
+ end
66
+
67
+ private
68
+
69
+ def inside_respond_to?(node)
70
+ node.each_ancestor(:any_block).any? do |block|
71
+ block.send_node.receiver.nil? && block.method?(:respond_to)
72
+ end
73
+ end
74
+
75
+ def renders_without_template?(node)
76
+ return true if node.arguments? && component_instance?(node.first_argument)
77
+
78
+ node.arguments.select(&:hash_type?).flat_map(&:keys).any? do |key|
79
+ key.sym_type? && TEMPLATELESS_KEYS.include?(key.value)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "katalyst/render_inside_respond_to"
3
4
  require_relative "koi/duplicates_association"
4
5
  require_relative "koi/table_asset"
5
6
  require_relative "koi/table_link_heading"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-katalyst
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.2
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -148,6 +148,7 @@ files:
148
148
  - config/default.yml
149
149
  - config/rubocop-bundler.yml
150
150
  - config/rubocop-gemspec.yml
151
+ - config/rubocop-katalyst.yml
151
152
  - config/rubocop-koi.yml
152
153
  - config/rubocop-layout.yml
153
154
  - config/rubocop-lint.yml
@@ -159,6 +160,7 @@ files:
159
160
  - config/rubocop-rspec.yml
160
161
  - config/rubocop-security.yml
161
162
  - config/rubocop-style.yml
163
+ - lib/rubocop/cop/katalyst/render_inside_respond_to.rb
162
164
  - lib/rubocop/cop/katalyst_cops.rb
163
165
  - lib/rubocop/cop/koi/duplicates_association.rb
164
166
  - lib/rubocop/cop/koi/table_asset.rb