rubocop-dev_doc 0.18.0 → 0.19.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/config/default.yml +161 -2
- data/lib/dev_doc/i18n/pseudo_locale.rb +1 -1
- data/lib/dev_doc/test/lints/cross_tenant_canary_check.rb +1 -1
- data/lib/dev_doc/test/lints/cross_tenant_canary_sweep.rb +6 -3
- data/lib/dev_doc/test/lints/http_driven_controller_tests.rb +2 -0
- data/lib/dev_doc/test/lints/no_file_excludes.rb +1 -1
- data/lib/dev_doc/test/pseudo_i18n_crawler.rb +4 -0
- data/lib/rubocop/cop/dev_doc/auth/adjacent_justification.rb +44 -7
- data/lib/rubocop/cop/dev_doc/auth/load_resource_current_user_guard.rb +2 -2
- data/lib/rubocop/cop/dev_doc/auth/login_only_policy_justification.rb +1 -1
- data/lib/rubocop/cop/dev_doc/auth/unscoped_find_justification.rb +2 -2
- data/lib/rubocop/cop/dev_doc/bundler/conflicting_gem.rb +74 -0
- data/lib/rubocop/cop/dev_doc/exception_class_list.rb +32 -0
- data/lib/rubocop/cop/dev_doc/i18n/translation_key_prefix.rb +1 -1
- data/lib/rubocop/cop/dev_doc/migration/legacy_migration.rb +26 -0
- data/lib/rubocop/cop/dev_doc/migration/redundant_reference_index.rb +5 -15
- data/lib/rubocop/cop/dev_doc/migration/require_reference_index.rb +115 -0
- data/lib/rubocop/cop/dev_doc/rails/avoid_lifecycle_method_override.rb +1 -1
- data/lib/rubocop/cop/dev_doc/rails/no_error_message_comparison.rb +178 -0
- data/lib/rubocop/cop/dev_doc/rails/no_form_param_lookup_in_controller.rb +1 -1
- data/lib/rubocop/cop/dev_doc/style/avoid_kernel_array.rb +103 -0
- data/lib/rubocop/cop/dev_doc/style/no_silent_rescue.rb +36 -26
- data/lib/rubocop/cop/dev_doc/style/no_unscoped_method_definitions.rb +1 -1
- data/lib/rubocop/cop/dev_doc/style/rescue_justification.rb +330 -0
- data/lib/rubocop/cop/dev_doc/test/justification_covers.rb +216 -0
- data/lib/rubocop/cop/dev_doc/test/justification_header.rb +86 -5
- data/lib/rubocop/cop/dev_doc/test/require_job_invocation_in_job_tests.rb +154 -0
- data/lib/rubocop/cop/dev_doc/test/require_unit_test_justification.rb +5 -24
- data/lib/rubocop/cop/dev_doc/test/test_file_placement.rb +30 -2
- data/lib/rubocop/dev_doc/version.rb +1 -1
- metadata +12 -3
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require_relative 'legacy_migration'
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module DevDoc
|
|
6
|
+
module Migration
|
|
7
|
+
# Never opt a reference out of its default index: `index: false` on
|
|
8
|
+
# `belongs_to` / `references` / `add_reference` is banned.
|
|
9
|
+
#
|
|
10
|
+
# ## Rationale
|
|
11
|
+
# Rails has indexed references by default since 5.0, so a reference
|
|
12
|
+
# column carries an index unless the migration explicitly opts out.
|
|
13
|
+
# The usual reason to opt out is a composite index whose leading
|
|
14
|
+
# column is the foreign key — the composite serves FK lookups too —
|
|
15
|
+
# but that dependency is invisible and fragile: a later migration can
|
|
16
|
+
# drop the composite, reorder its columns, or convert it to a partial
|
|
17
|
+
# or non-btree index, and nothing in that migration says a reference
|
|
18
|
+
# depends on it. The FK is silently unindexed from then on.
|
|
19
|
+
#
|
|
20
|
+
# An unindexed foreign key fails far from its cause: PostgreSQL
|
|
21
|
+
# enforces the constraint from the parent side as well, so every
|
|
22
|
+
# DELETE of a parent row runs a lookup against the child table — a
|
|
23
|
+
# sequential scan per deleted row when the column has no index. The
|
|
24
|
+
# cost surfaces in a console prune or a bulk cleanup years later,
|
|
25
|
+
# not at write time. InnoDB creates the index with the constraint;
|
|
26
|
+
# PostgreSQL, like SQL Server, leaves the referencing-side index to
|
|
27
|
+
# the application, so this cop does it.
|
|
28
|
+
#
|
|
29
|
+
# Keeping the default single-column index also frees the composite:
|
|
30
|
+
# with the FK covered on its own, a query-serving composite index may
|
|
31
|
+
# be dropped, reordered, or rebuilt without a hidden victim.
|
|
32
|
+
#
|
|
33
|
+
# ❌ Fragile — the composite is the FK's only index
|
|
34
|
+
# create_table :readings do |t|
|
|
35
|
+
# t.belongs_to :sensor, foreign_key: true, null: false, index: false
|
|
36
|
+
# end
|
|
37
|
+
# add_index :readings, %i[sensor_id recorded_at], unique: true
|
|
38
|
+
#
|
|
39
|
+
# ✔️ The FK keeps its own index; the composite serves queries
|
|
40
|
+
# create_table :readings do |t|
|
|
41
|
+
# t.belongs_to :sensor, foreign_key: true, null: false
|
|
42
|
+
# end
|
|
43
|
+
# add_index :readings, %i[sensor_id recorded_at], unique: true
|
|
44
|
+
#
|
|
45
|
+
# ✔️ A hash customizes the index, it does not remove it
|
|
46
|
+
# t.belongs_to :parent, foreign_key: true, null: false, index: { unique: true }
|
|
47
|
+
#
|
|
48
|
+
# ## Exception
|
|
49
|
+
# None in ordinary code. Where a table's write volume genuinely
|
|
50
|
+
# cannot carry the extra index, keep `index: false` and disable this
|
|
51
|
+
# cop inline with a brief reason — and note in the same comment that
|
|
52
|
+
# the covering composite index is load-bearing for the FK, so later
|
|
53
|
+
# migrations do not reorder or drop it in good faith.
|
|
54
|
+
#
|
|
55
|
+
# NOTE: Migrations declared `< ActiveRecord::Migration[4.2]` are
|
|
56
|
+
# skipped entirely: the 4.2 compatibility layer flips the reference
|
|
57
|
+
# default to `index: false`, so the reference is unindexed whether
|
|
58
|
+
# or not the option is written — and fixing that would require the
|
|
59
|
+
# load-bearing `index: true`, not the removal of `index: false`.
|
|
60
|
+
#
|
|
61
|
+
# NOTE: This cop holds the index; the sibling cops
|
|
62
|
+
# `DevDoc/Migration/RequireReferenceForeignKey` and
|
|
63
|
+
# `DevDoc/Migration/ExplicitNullOnReference` require the constraint
|
|
64
|
+
# and the optionality to be stated, and
|
|
65
|
+
# `DevDoc/Migration/RedundantReferenceIndex` strips the redundant
|
|
66
|
+
# `index: true`. Together: everything written on a reference is a
|
|
67
|
+
# decision, everything omitted is doctrine — and the index is not a
|
|
68
|
+
# decision.
|
|
69
|
+
#
|
|
70
|
+
# @example
|
|
71
|
+
# # bad
|
|
72
|
+
# t.belongs_to :sensor, foreign_key: true, null: false, index: false
|
|
73
|
+
#
|
|
74
|
+
# # good
|
|
75
|
+
# t.belongs_to :sensor, foreign_key: true, null: false
|
|
76
|
+
#
|
|
77
|
+
# # good (customized, not removed)
|
|
78
|
+
# t.belongs_to :parent, foreign_key: true, index: { unique: true }
|
|
79
|
+
class RequireReferenceIndex < Base
|
|
80
|
+
include LegacyMigration
|
|
81
|
+
|
|
82
|
+
MSG = 'Remove `%<source>s` — the reference must keep its own index; a deliberate ' \
|
|
83
|
+
'exception keeps the option plus a reasoned disable of this cop.'.freeze
|
|
84
|
+
|
|
85
|
+
RESTRICT_ON_SEND = %i[belongs_to references add_reference add_belongs_to].freeze
|
|
86
|
+
|
|
87
|
+
def on_send(node)
|
|
88
|
+
return if legacy_migration?(node)
|
|
89
|
+
|
|
90
|
+
options = node.arguments.find(&:hash_type?)
|
|
91
|
+
return unless options
|
|
92
|
+
|
|
93
|
+
options.pairs.each do |pair|
|
|
94
|
+
add_offense(pair, message: format(MSG, source: pair.source)) if index_opt_out?(pair)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
# `index: nil` opts out exactly like `index: false` (Rails checks
|
|
101
|
+
# the option's truthiness), so both literal falsy spellings flag.
|
|
102
|
+
# A hash value (`index: { unique: true }`) customizes the index
|
|
103
|
+
# rather than removing it, and `index: true` is redundant but
|
|
104
|
+
# harmless — RedundantReferenceIndex's domain, not this cop's.
|
|
105
|
+
def index_opt_out?(pair)
|
|
106
|
+
key = pair.key
|
|
107
|
+
return false unless (key.sym_type? || key.str_type?) && key.value.to_s == 'index'
|
|
108
|
+
|
|
109
|
+
pair.value.false_type? || pair.value.nil_type?
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -98,7 +98,7 @@ module RuboCop
|
|
|
98
98
|
private
|
|
99
99
|
|
|
100
100
|
def flagged_methods
|
|
101
|
-
@flagged_methods ||= Array(cop_config['Methods'] || DEFAULT_METHODS).map(&:to_sym)
|
|
101
|
+
@flagged_methods ||= Array(cop_config['Methods'] || DEFAULT_METHODS).map(&:to_sym) # rubocop:disable DevDoc/Style/AvoidKernelArray -- cop config accepts one method name or a list
|
|
102
102
|
end
|
|
103
103
|
end
|
|
104
104
|
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
module RuboCop
|
|
2
|
+
module Cop
|
|
3
|
+
module DevDoc
|
|
4
|
+
module Rails
|
|
5
|
+
# Do not branch on the TEXT of an ActiveModel error message; add the
|
|
6
|
+
# error with a symbol type and branch on the type.
|
|
7
|
+
#
|
|
8
|
+
# ## Rationale
|
|
9
|
+
# The compared text is written in two places — the `errors.add` that
|
|
10
|
+
# produces it and the branch that matches it — and only both copies
|
|
11
|
+
# staying identical keeps the branch alive. Rewording the message,
|
|
12
|
+
# translating it, or a second error joining it on the record silently
|
|
13
|
+
# sends the branch down its generic path; no test notices, because
|
|
14
|
+
# the test asserts the same text. A symbol-typed error has no text to
|
|
15
|
+
# drift: the branch asks the record which KIND of failure it holds.
|
|
16
|
+
#
|
|
17
|
+
# ❌ `errors.add(:base, 'Article is already published')` in the model,
|
|
18
|
+
# `errors.full_messages == ['Article is already published']` in the
|
|
19
|
+
# caller
|
|
20
|
+
# ✔️ `errors.add(:base, :already_published, message: 'Article is
|
|
21
|
+
# already published')` in the model,
|
|
22
|
+
# `errors.of_kind?(:base, :already_published)` in the caller
|
|
23
|
+
#
|
|
24
|
+
# Both legs of the ratchet: observed in a PR under review
|
|
25
|
+
# (2026-11-13, session 27c64a62); measured over the `app/` and
|
|
26
|
+
# `lib/` trees of 13 consuming codebases (9,859 files) — the
|
|
27
|
+
# full_messages family fired zero times, the `errors[]`/`messages`
|
|
28
|
+
# family once, a true positive whose fix is `errors.added?`. The
|
|
29
|
+
# finished cop re-measured over the same apps' `app/` trees: one
|
|
30
|
+
# offense, the same line.
|
|
31
|
+
#
|
|
32
|
+
# ## Exception
|
|
33
|
+
# A genuinely external string the code cannot type — an `errors`
|
|
34
|
+
# object handed over by an API client whose messages are fixed
|
|
35
|
+
# upstream — is not yours to symbol-type. Disable inline, naming the
|
|
36
|
+
# source:
|
|
37
|
+
#
|
|
38
|
+
# # rubocop:disable DevDoc/Rails/NoErrorMessageComparison
|
|
39
|
+
# # -- matches the upstream API's fixed error text
|
|
40
|
+
#
|
|
41
|
+
# NOTE: Blindness, for reviewers: a message stored in a variable
|
|
42
|
+
# before the comparison is not seen; a comparison inside a helper
|
|
43
|
+
# method (the messages arrive as a parameter) is not seen; a
|
|
44
|
+
# comparison inside a block over the messages
|
|
45
|
+
# (`errors.full_messages.any? { |m| m.include?('taken') }`) is not
|
|
46
|
+
# seen — the receiver-chain walk never reaches a block argument; the
|
|
47
|
+
# Rails 6.1 error-object spellings (`errors.first.message`,
|
|
48
|
+
# `errors.where(:email).first.message`, `errors.map(&:message)`,
|
|
49
|
+
# `errors.full_message`) are not seen; an emptiness check
|
|
50
|
+
# (`errors.full_messages == []`) is not flagged — `errors.empty?` is
|
|
51
|
+
# that check's remedy, not this cop; a hash literal on the
|
|
52
|
+
# comparison side (`errors.messages == { email: [...] }`) is not
|
|
53
|
+
# seen — the literal side is a closed list of the observed shapes,
|
|
54
|
+
# and this spelling was never observed; `e.message` and
|
|
55
|
+
# `e.full_message` on exceptions are out of scope (measured mostly
|
|
56
|
+
# legitimate); and paths outside `app/` are not checked.
|
|
57
|
+
#
|
|
58
|
+
# @example
|
|
59
|
+
# # bad
|
|
60
|
+
# if @article.errors.full_messages == ['Article is already published']
|
|
61
|
+
# if @article.errors[:base].include?('Article is already published')
|
|
62
|
+
# case @article.errors.full_messages.first
|
|
63
|
+
# when 'Article is already published' then ...
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# # good
|
|
67
|
+
# @article.errors.add(:base, :already_published) # model
|
|
68
|
+
# if @article.errors.of_kind?(:base, :already_published) # caller
|
|
69
|
+
class NoErrorMessageComparison < Base
|
|
70
|
+
MSG = 'Branching on error message text breaks when the text changes or another error is added. ' \
|
|
71
|
+
'Add the error with a symbol type and check it with `errors.of_kind?` / `errors.added?`.'.freeze
|
|
72
|
+
|
|
73
|
+
RESTRICT_ON_SEND = %i[== != eql? equal? === include? match? match =~ !~
|
|
74
|
+
start_with? end_with? grep grep_v].freeze
|
|
75
|
+
|
|
76
|
+
# ActiveModel-specific names: any receiver chain containing one is
|
|
77
|
+
# an error read. (`full_message` is NOT here: `Exception#full_message`
|
|
78
|
+
# is core Ruby, so the any-receiver rule would flag exception
|
|
79
|
+
# comparisons that are out of scope; the ActiveModel/Error-object
|
|
80
|
+
# spellings are a NOTE blind spot instead.)
|
|
81
|
+
FULL_MESSAGE_METHODS = %i[full_messages full_messages_for].freeze
|
|
82
|
+
# These count only when the chain resolves to an `errors` call, so
|
|
83
|
+
# `conversation.messages` and `params[:errors]` stay silent.
|
|
84
|
+
ERRORS_METHODS = %i[to_a messages messages_for []].freeze
|
|
85
|
+
|
|
86
|
+
def on_send(node)
|
|
87
|
+
flag_comparison(node, [node.receiver, *node.arguments])
|
|
88
|
+
end
|
|
89
|
+
# Safe navigation (`messages&.include?('text')`) parses as csend
|
|
90
|
+
# nodes, which on_send does not receive — alias it so `&.` is
|
|
91
|
+
# caught too.
|
|
92
|
+
alias on_csend on_send
|
|
93
|
+
|
|
94
|
+
# `/text/ =~ errors.full_messages.first` parses as its own node
|
|
95
|
+
# type (the match-assignment idiom), not a send.
|
|
96
|
+
def on_match_with_lvasgn(node)
|
|
97
|
+
flag_comparison(node, node.children)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def on_case(node)
|
|
101
|
+
subject = node.condition
|
|
102
|
+
return unless error_read?(subject)
|
|
103
|
+
return unless node.when_branches.any? do |when_node|
|
|
104
|
+
when_node.conditions.any? { |condition| literalish?(condition) }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
add_offense(subject)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
# Offense when at least one operand is an error read (below) and
|
|
113
|
+
# at least one other is literal-ish (below).
|
|
114
|
+
def flag_comparison(node, operands)
|
|
115
|
+
error_reads, others = operands.compact.partition { |operand| error_read?(operand) }
|
|
116
|
+
return if error_reads.empty?
|
|
117
|
+
return unless others.any? { |operand| literalish?(operand) }
|
|
118
|
+
|
|
119
|
+
add_offense(node)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# An operand reads error message text when a node in its receiver
|
|
123
|
+
# chain is a full-message reader (any receiver — the names are
|
|
124
|
+
# ActiveModel-specific), or one of ERRORS_METHODS whose receiver is
|
|
125
|
+
# an `errors` call.
|
|
126
|
+
def error_read?(node)
|
|
127
|
+
return false unless call_node?(node)
|
|
128
|
+
|
|
129
|
+
if FULL_MESSAGE_METHODS.include?(node.method_name) ||
|
|
130
|
+
(ERRORS_METHODS.include?(node.method_name) && errors_call?(node.receiver))
|
|
131
|
+
true
|
|
132
|
+
else
|
|
133
|
+
error_read?(node.receiver)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# An `errors` call, or a local/ivar assigned from one (`errors =
|
|
138
|
+
# record.errors; errors[:email]...`) — the assignment idiom the
|
|
139
|
+
# observed PR used. A non-ActiveModel object named `errors` trips
|
|
140
|
+
# this too; that is the inline-disable case.
|
|
141
|
+
def errors_call?(node)
|
|
142
|
+
return false unless node
|
|
143
|
+
|
|
144
|
+
if %i[send csend].include?(node.type)
|
|
145
|
+
node.method_name == :errors
|
|
146
|
+
elsif node.type == :lvar
|
|
147
|
+
node.children.first == :errors
|
|
148
|
+
elsif node.type == :ivar
|
|
149
|
+
node.children.first == :@errors
|
|
150
|
+
else
|
|
151
|
+
false
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def call_node?(node)
|
|
156
|
+
%i[send csend].include?(node&.type)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# The literal side of the observed comparisons (inclusion-based
|
|
160
|
+
# detection): string-ish literals, a regexp, a constant (which only
|
|
161
|
+
# moves the text), a `t(...)` / `translate(...)` call (branching on
|
|
162
|
+
# translated text is the same smell plus a locale dependency), and
|
|
163
|
+
# a NON-EMPTY array of those (an empty array is an emptiness check
|
|
164
|
+
# — `errors.full_messages == []` wants `errors.empty?`, not this
|
|
165
|
+
# cop's message). A variable or method call is not flagged.
|
|
166
|
+
def literalish?(node)
|
|
167
|
+
case node&.type
|
|
168
|
+
when :str, :dstr, :regexp, :const then true
|
|
169
|
+
when :send, :csend then %i[t translate].include?(node.method_name)
|
|
170
|
+
when :array then node.children.any? && node.children.all? { |element| literalish?(element) }
|
|
171
|
+
else false
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -223,7 +223,7 @@ module RuboCop
|
|
|
223
223
|
end
|
|
224
224
|
|
|
225
225
|
def finder_methods
|
|
226
|
-
@finder_methods ||= Array(cop_config.fetch('FinderMethods', %w[find find_by find_by!])).map(&:to_sym)
|
|
226
|
+
@finder_methods ||= Array(cop_config.fetch('FinderMethods', %w[find find_by find_by!])).map(&:to_sym) # rubocop:disable DevDoc/Style/AvoidKernelArray -- cop config accepts one method name or a list
|
|
227
227
|
end
|
|
228
228
|
|
|
229
229
|
def justification_marker
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
module RuboCop
|
|
2
|
+
module Cop
|
|
3
|
+
module DevDoc
|
|
4
|
+
module Style
|
|
5
|
+
# Flag `Array(x)` / `Kernel.Array(x)` coercion; when nil is the only
|
|
6
|
+
# alternative to an array, say it with `x || []`.
|
|
7
|
+
#
|
|
8
|
+
# ## Rationale
|
|
9
|
+
# `Kernel#Array` coerces ANY input into an array, differently per
|
|
10
|
+
# shape:
|
|
11
|
+
#
|
|
12
|
+
# | Input | `Array(input)` |
|
|
13
|
+
# |---|---|
|
|
14
|
+
# | `nil` | `[]` |
|
|
15
|
+
# | `[a, b]` | `[a, b]` (unchanged) |
|
|
16
|
+
# | `{ k: v }` | `[[:k, v]]` (pairs) |
|
|
17
|
+
# | any other object | `[obj]` (wrapped) |
|
|
18
|
+
#
|
|
19
|
+
# Most call sites only want the first row: a nil guard. But the call
|
|
20
|
+
# reads as "this could be a scalar or a list" and silently accepts
|
|
21
|
+
# every other shape. When an unexpected shape arrives, the code does
|
|
22
|
+
# not fail — it iterates the wrong thing (Hash pairs, a wrapped
|
|
23
|
+
# scalar) and the bug surfaces somewhere downstream.
|
|
24
|
+
#
|
|
25
|
+
# The quiet one is the jsonb row: a nullable JSON column the code
|
|
26
|
+
# assumes holds an array can legally hold a Hash. `Array()` turns it
|
|
27
|
+
# into pairs and those render as list items with no error anywhere.
|
|
28
|
+
# Note `|| []` does NOT fix that Hash case — `(hash || []).each`
|
|
29
|
+
# yields the same pairs. What `|| []` fixes is nil (the assumption
|
|
30
|
+
# becomes visible) and the scalar (`.each` raises NoMethodError
|
|
31
|
+
# instead of silently wrapping). A column that may hold a Hash needs
|
|
32
|
+
# an explicit shape check: `value.is_a?(Array) ? value : []`.
|
|
33
|
+
#
|
|
34
|
+
# ❌
|
|
35
|
+
# rows = Array(params[:tags]).map { |t| ... } # strong params: array or nil
|
|
36
|
+
# ids = Array(order.line_item_ids) # `array: true` column
|
|
37
|
+
# Array(record.settings).each { ... } # jsonb, could hold a Hash
|
|
38
|
+
#
|
|
39
|
+
# ✔️
|
|
40
|
+
# rows = (params[:tags] || []).map { |t| ... }
|
|
41
|
+
# ids = order.line_item_ids || []
|
|
42
|
+
# settings = record.settings.is_a?(Array) ? record.settings : []
|
|
43
|
+
#
|
|
44
|
+
# `hash.fetch(:key, [])` is NOT an equivalent guard: `{ key: nil }
|
|
45
|
+
# .fetch(:key, [])` returns nil. `|| []` covers both an absent key
|
|
46
|
+
# and a key present with nil.
|
|
47
|
+
#
|
|
48
|
+
# ## Exception
|
|
49
|
+
# Real scalar-or-list input: a mail recipient that may be one string
|
|
50
|
+
# or a list, a filter scope documented as "one status or an array",
|
|
51
|
+
# an ActionMailer `formats:` option. There the coercion is the point
|
|
52
|
+
# — keep the call and disable inline with the reason:
|
|
53
|
+
#
|
|
54
|
+
# # rubocop:disable DevDoc/Style/AvoidKernelArray -- <why the input really is scalar-or-list>
|
|
55
|
+
# recipients = Array(message.to)
|
|
56
|
+
#
|
|
57
|
+
# `Array.wrap` (ActiveSupport: nil → [], Hash → [hash], scalar →
|
|
58
|
+
# [scalar]) is a different, narrower coercion and is not flagged.
|
|
59
|
+
#
|
|
60
|
+
# ## Relationship with Style/ArrayCoercion
|
|
61
|
+
# The stock cop (disabled by default, unsafe) pushes the OTHER way:
|
|
62
|
+
# from `[*x]` and `x = [x] unless x.is_a?(Array)` toward `Array(x)`.
|
|
63
|
+
# Do not enable both — keep this cop and write exceptions down with
|
|
64
|
+
# inline disables.
|
|
65
|
+
#
|
|
66
|
+
# @example
|
|
67
|
+
# # bad
|
|
68
|
+
# Array(params[:tags]).map { |t| t.upcase }
|
|
69
|
+
# Kernel.Array(order.line_item_ids).each { |id| id }
|
|
70
|
+
#
|
|
71
|
+
# # good
|
|
72
|
+
# (params[:tags] || []).map { |t| t.upcase }
|
|
73
|
+
# (order.line_item_ids || []).each { |id| id }
|
|
74
|
+
class AvoidKernelArray < Base
|
|
75
|
+
MSG = '`Array()` coerces any shape (Hash → pairs, scalar → [x]). If the only ' \
|
|
76
|
+
'alternative to an array is nil, use `x || []`; otherwise disable inline ' \
|
|
77
|
+
'with the reason.'.freeze
|
|
78
|
+
|
|
79
|
+
RESTRICT_ON_SEND = %i[Array].freeze
|
|
80
|
+
|
|
81
|
+
# `Array(x)`, `Kernel.Array(x)`, `::Kernel.Array(x)` — the
|
|
82
|
+
# Kernel#Array function by any spelling, with a single argument
|
|
83
|
+
# position (a splat `Array(*args)` included — it is the same call
|
|
84
|
+
# once the splat resolves). Any other receiver named `Array` (a
|
|
85
|
+
# user-defined `foo.Array(x)`) is not the Kernel function and is
|
|
86
|
+
# left alone.
|
|
87
|
+
def_node_matcher :kernel_array_call?, <<~PATTERN
|
|
88
|
+
{
|
|
89
|
+
(send nil? :Array _)
|
|
90
|
+
(send (const {nil? cbase} :Kernel) :Array _)
|
|
91
|
+
}
|
|
92
|
+
PATTERN
|
|
93
|
+
|
|
94
|
+
def on_send(node)
|
|
95
|
+
return unless kernel_array_call?(node)
|
|
96
|
+
|
|
97
|
+
add_offense(node.loc.selector)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -82,6 +82,19 @@ module RuboCop
|
|
|
82
82
|
# even on a branch the run did not take, and a generic payload
|
|
83
83
|
# built WITHOUT the exception ("failed") still flags.
|
|
84
84
|
#
|
|
85
|
+
# ## Expected outcomes
|
|
86
|
+
# A clause rescuing ONLY classes listed in `ExpectedOutcomeClasses`
|
|
87
|
+
# (default: the concurrency verdicts — `RecordNotUnique`,
|
|
88
|
+
# `Deadlocked`, `LockWaitTimeout`, `StaleObjectError`,
|
|
89
|
+
# `SerializationFailure`) may stay silent: losing a race to a unique
|
|
90
|
+
# index is an outcome the code planned for, not a swallowed failure.
|
|
91
|
+
# Every class of the clause must be listed; one other class brings
|
|
92
|
+
# the body checks back. I/O failures are deliberately not in the
|
|
93
|
+
# default: an external failure is still a failure and must be
|
|
94
|
+
# reported, even when the code degrades around it. Whether the
|
|
95
|
+
# clause should exist at all is `DevDoc/Style/RescueJustification`'s
|
|
96
|
+
# question, not this cop's.
|
|
97
|
+
#
|
|
85
98
|
# ## Relationship with `Lint/SuppressedException` (stock)
|
|
86
99
|
# This cop is a strict superset at the stock cop's default settings
|
|
87
100
|
# (`AllowComments: true`, `AllowNil: true`): everything it flags —
|
|
@@ -123,6 +136,11 @@ module RuboCop
|
|
|
123
136
|
# `warning?` are not reporting. Add your project's in-house
|
|
124
137
|
# reporting verbs here.
|
|
125
138
|
#
|
|
139
|
+
# `ExpectedOutcomeClasses` — see "Expected outcomes" above. Matched
|
|
140
|
+
# against the constant as written (leading `::` ignored); an entry
|
|
141
|
+
# ending in `::` matches a namespace. Setting the key replaces the
|
|
142
|
+
# default list.
|
|
143
|
+
#
|
|
126
144
|
# NOTE: The walk judges the body syntactically: any matching call
|
|
127
145
|
# anywhere in the body counts as reporting, even one an if-condition
|
|
128
146
|
# hides, and a failure merely stashed (`@last_error = e`) does not.
|
|
@@ -131,7 +149,11 @@ module RuboCop
|
|
|
131
149
|
# unreported retry loop is the classic forever-silent failure. An
|
|
132
150
|
# empty-bodied clause's below-rescue marker also sits directly above
|
|
133
151
|
# the NEXT clause's `rescue`, so one marker justifies both — a single
|
|
134
|
-
# statement covering adjacent clauses, accepted as intended.
|
|
152
|
+
# statement covering adjacent clauses, accepted as intended. A marker
|
|
153
|
+
# trailing the last code line of one clause's body likewise counts
|
|
154
|
+
# for the next clause's `rescue` (the upward walk accepts trailing
|
|
155
|
+
# comments on code lines); `DevDoc/Style/RescueJustification` reads
|
|
156
|
+
# comment-only lines there instead. Only
|
|
135
157
|
# `app/` and `lib/` are inspected.
|
|
136
158
|
#
|
|
137
159
|
# @example
|
|
@@ -160,6 +182,7 @@ module RuboCop
|
|
|
160
182
|
# end
|
|
161
183
|
class NoSilentRescue < Base
|
|
162
184
|
include Auth::AdjacentJustification
|
|
185
|
+
include ExceptionClassList
|
|
163
186
|
include RescueNode
|
|
164
187
|
|
|
165
188
|
MSG = 'This rescue swallows the failure without a trace — report it (a logger/error-monitor ' \
|
|
@@ -172,8 +195,14 @@ module RuboCop
|
|
|
172
195
|
|
|
173
196
|
PROPAGATING_METHODS = %i[raise fail throw].freeze
|
|
174
197
|
|
|
198
|
+
DEFAULT_EXPECTED_OUTCOME_CLASSES = %w[
|
|
199
|
+
ActiveRecord::RecordNotUnique ActiveRecord::Deadlocked ActiveRecord::LockWaitTimeout
|
|
200
|
+
ActiveRecord::StaleObjectError ActiveRecord::SerializationFailure
|
|
201
|
+
].freeze
|
|
202
|
+
|
|
175
203
|
def on_resbody(node)
|
|
176
204
|
return if rescue_modifier?(node)
|
|
205
|
+
return if all_exceptions_listed?(node, expected_outcome_classes)
|
|
177
206
|
return if reports_or_propagates?(node.body)
|
|
178
207
|
return if surfaces_failure?(node)
|
|
179
208
|
return if adjacent_justification?(node) || leading_body_marker?(node)
|
|
@@ -192,30 +221,6 @@ module RuboCop
|
|
|
192
221
|
body.each_node(:send, :csend).any? { |call| reporting_call?(call) }
|
|
193
222
|
end
|
|
194
223
|
|
|
195
|
-
# The marker also counts in the body's own leading comment block —
|
|
196
|
-
# the contiguous comment-only lines directly below the `rescue`
|
|
197
|
-
# keyword, where adopters naturally state the reason. A comment
|
|
198
|
-
# after the first body statement is not adjacent to the rescue.
|
|
199
|
-
def leading_body_marker?(node)
|
|
200
|
-
marker = normalized_marker
|
|
201
|
-
comments_by_line = processed_source.comments.group_by { |comment| comment.location.line }
|
|
202
|
-
line = node.loc.keyword.line + 1
|
|
203
|
-
while (line_comments = comments_by_line[line]) && comment_only_line?(line)
|
|
204
|
-
return true if contains_marker?(line_comments, marker)
|
|
205
|
-
|
|
206
|
-
line += 1
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
false
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
# The String-downcasing the shared concern applies on its own
|
|
213
|
-
# path; a Regexp marker passes through for `contains_marker?`.
|
|
214
|
-
def normalized_marker
|
|
215
|
-
marker = justification_marker
|
|
216
|
-
marker.is_a?(String) ? marker.downcase : marker
|
|
217
|
-
end
|
|
218
|
-
|
|
219
224
|
# A body whose return value carries the bound exception hands the
|
|
220
225
|
# failure to the caller as data (error payload, rendered 4xx,
|
|
221
226
|
# persisted error column) — surfaced, not swallowed.
|
|
@@ -256,7 +261,12 @@ module RuboCop
|
|
|
256
261
|
|
|
257
262
|
def reporting_methods
|
|
258
263
|
@reporting_methods ||=
|
|
259
|
-
Array(cop_config.fetch('ReportingMethods', DEFAULT_REPORTING_METHODS))
|
|
264
|
+
Array(cop_config.fetch('ReportingMethods', DEFAULT_REPORTING_METHODS)) # rubocop:disable DevDoc/Style/AvoidKernelArray -- cop config accepts one method name or a list
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def expected_outcome_classes
|
|
268
|
+
@expected_outcome_classes ||=
|
|
269
|
+
Array(cop_config.fetch('ExpectedOutcomeClasses', DEFAULT_EXPECTED_OUTCOME_CLASSES)) # rubocop:disable DevDoc/Style/AvoidKernelArray -- cop config accepts one class name or a list
|
|
260
270
|
end
|
|
261
271
|
|
|
262
272
|
def justification_marker
|
|
@@ -143,7 +143,7 @@ module RuboCop
|
|
|
143
143
|
end
|
|
144
144
|
|
|
145
145
|
def safe_receivers
|
|
146
|
-
DEFAULT_SAFE_DSL_RECEIVERS + Array(cop_config.fetch('SafeDSLReceivers', []))
|
|
146
|
+
DEFAULT_SAFE_DSL_RECEIVERS + Array(cop_config.fetch('SafeDSLReceivers', [])) # rubocop:disable DevDoc/Style/AvoidKernelArray -- cop config accepts one receiver name or a list
|
|
147
147
|
end
|
|
148
148
|
end
|
|
149
149
|
end
|