Skip to content

Commit

Permalink
Merge pull request #23 from sue445/fix_meaning_less_error
Browse files Browse the repository at this point in the history
Fix `comparison of Integer with nil failed` in `YARD/MeaninglessTag`
  • Loading branch information
ksss authored Dec 9, 2023
2 parents b41fe20 + be0af1e commit df2e68f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace :smoke do
{ name: "tag_type_syntax" }
],
'YARD/MeaninglessTag' => [
{ name: "meaningless_tag" }
{ name: "meaningless_tag" },
{ name: "meaningless_tag_n_plus_one_query" },
],
'YARD/MismatchName' => [
{ name: "mismatch_name", correct: true }
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/yard/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def inline_comment?(comment)

def build_docstring(preceding_lines)
comment_texts = preceding_lines.map { |l| l.text.gsub(/\A#/, '') }
minimum_space = comment_texts.map { |t| t.index(/[^\s]/) }.min
minimum_space = comment_texts.map { |t| t.index(/[^\s]/) }.compact.min
yard_docstring = comment_texts.map { |t| t[minimum_space..-1] }.join("\n")
begin
::YARD::DocstringParser.new.parse(yard_docstring)
Expand Down
20 changes: 20 additions & 0 deletions smoke/generated/meaningless_tag_n_plus_one_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"metadata": {
"rubocop_version": "1.57.2",
"ruby_engine": "ruby",
"ruby_version": "3.2.2",
"ruby_patchlevel": "53",
"ruby_platform": "arm64-darwin22"
},
"files": [
{
"path": "smoke/meaningless_tag_n_plus_one_query.rb",
"offenses": []
}
],
"summary": {
"offense_count": 0,
"target_file_count": 1,
"inspected_file_count": 1
}
}
44 changes: 44 additions & 0 deletions smoke/meaningless_tag_n_plus_one_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Checks that there’s no N+1 query
#
# @note If `Database` isn't configured, auto-correct will not be available. (Only offense detection can be used)
#
# @note For the number of N+1 queries that can be detected by this cop, there are too few that can be corrected automatically
#
# @example
# # bad
# reservations = db.xquery('SELECT * FROM `reservations` WHERE `schedule_id` = ?', schedule_id).map do |reservation|
# reservation[:user] = db.xquery('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', id).first
# reservation
# end
#
# # good
# rows = db.xquery(<<~SQL, schedule_id)
# SELECT
# r.id AS reservation_id,
# r.schedule_id AS reservation_schedule_id,
# r.user_id AS reservation_user_id,
# r.created_at AS reservation_created_at,
# u.id AS user_id,
# u.email AS user_email,
# u.nickname AS user_nickname,
# u.staff AS user_staff,
# u.created_at AS user_created_at
# FROM `reservations` AS r
# INNER JOIN users u ON u.id = r.user_id
# WHERE r.schedule_id = ?
# SQL
#
# # bad
# courses.map do |course|
# teacher = db.xquery('SELECT * FROM `users` WHERE `id` = ?', course[:teacher_id]).first
# end
#
# # good
# # This is similar to ActiveRecord's preload
# # c.f. https://guides.rubyonrails.org/active_record_querying.html#preload
# courses.map do |course|
# @users_by_id ||= db.xquery('SELECT * FROM `users` WHERE `id` IN (?)', courses.map { |course| course[:teacher_id] }).each_with_object({}) { |v, hash| hash[v[:id]] = v }
# teacher = @users_by_id[course[:teacher_id]]
# end
class NPlusOneQuery < Base
end

0 comments on commit df2e68f

Please sign in to comment.