forked from rubocop/rubocop
/
numblock_handler.rb
69 lines (63 loc) · 1.81 KB
/
numblock_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
module RuboCop
module Cop
module InternalAffairs
# Checks for missing `numblock handlers. The blocks with numbered
# arguments introduced in Ruby 2.7 are parsed with a node type of
# `numblock` instead of block. Cops that define `block` handlers
# need to define `numblock` handlers or disable this cope for them.
#
# @example
#
# # bad
# class BlockRelatedCop < Base
# def on_block(node)
# end
# end
#
# # good
# class BlockRelatedCop < Base
# def on_block(node)
# end
#
# alias on_numblock on_block
# end
#
# class BlockRelatedCop < Base
# def on_block(node)
# end
#
# alias_method :on_numblock, :on_block
# end
#
# class BlockRelatedCop < Base
# def on_block(node)
# end
#
# def on_numblock(node)
# end
# end
class NumblockHandler < Base
MSG = 'Define on_numblock to handle blocks with numbered arguments.'
def on_def(node)
return unless block_handler?(node)
return unless node.parent
add_offense(node) unless numblock_handler?(node.parent)
end
private
# @!method block_handler?(node)
def_node_matcher :block_handler?, <<~PATTERN
(def :on_block (args (arg :node)) ...)
PATTERN
# @!method numblock_handler?(node)
def_node_matcher :numblock_handler?, <<~PATTERN
{
`(def :on_numblock (args (arg :node)) ...)
`(alias (sym :on_numblock) (sym :on_block))
`(send nil? :alias_method (sym :on_numblock) (sym :on_block))
}
PATTERN
end
end
end
end