rubocop-katalyst 3.3.1 → 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 +4 -4
- data/CHANGELOG.md +7 -0
- data/config/default.yml +1 -0
- data/config/rubocop-katalyst.yml +15 -0
- data/lib/rubocop/cop/katalyst/render_inside_respond_to.rb +85 -0
- data/lib/rubocop/cop/katalyst_cops.rb +1 -0
- data/lib/rubocop/katalyst/prettier_task.rb +14 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f9732097a9a8c67e05871b2af9d42fb9ff3f5e8078e2312b2553ce0492465e37
|
|
4
|
+
data.tar.gz: 5df1a7c025c139e3949f5f1d642c1442c436f23e26fdbe505a15e30278c75554
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
@@ -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
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "rake"
|
|
4
4
|
require "rake/tasklib"
|
|
5
|
+
require "json"
|
|
5
6
|
|
|
6
7
|
module RuboCop
|
|
7
8
|
module Katalyst
|
|
@@ -40,8 +41,20 @@ module RuboCop
|
|
|
40
41
|
File.join(root, ".prettierrc.json")
|
|
41
42
|
end
|
|
42
43
|
|
|
44
|
+
# True when the nearest package.json (walking up from the working
|
|
45
|
+
# directory) already declares prettier. `yarn add` would rewrite that
|
|
46
|
+
# file and its lockfile, which leaves a dirty tree when a gem builds its
|
|
47
|
+
# dummy app in CI before running `rake release`.
|
|
48
|
+
def prettier_declared?
|
|
49
|
+
package_json = Pathname.new(root).ascend.map { |dir| dir.join("package.json") }.find(&:exist?)
|
|
50
|
+
return false unless package_json
|
|
51
|
+
|
|
52
|
+
json = JSON.parse(package_json.read)
|
|
53
|
+
%w[dependencies devDependencies].any? { |key| json[key].is_a?(Hash) && json[key].key?("prettier") }
|
|
54
|
+
end
|
|
55
|
+
|
|
43
56
|
def install_prettier
|
|
44
|
-
yarn("add", "--dev", "prettier")
|
|
57
|
+
yarn("add", "--dev", "prettier") unless prettier_declared?
|
|
45
58
|
File.write(config, <<~JSON)
|
|
46
59
|
{}
|
|
47
60
|
JSON
|
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.
|
|
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
|
|
@@ -189,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
189
191
|
- !ruby/object:Gem::Version
|
|
190
192
|
version: '0'
|
|
191
193
|
requirements: []
|
|
192
|
-
rubygems_version: 4.0.
|
|
194
|
+
rubygems_version: 4.0.20
|
|
193
195
|
specification_version: 4
|
|
194
196
|
summary: Code standards for Katalyst
|
|
195
197
|
test_files: []
|