Class: RuboCop::Cop::Style::DisableSyntax
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Style::DisableSyntax
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/style/disable_syntax.rb
Overview
Forbid some unfavorite ruby syntax, such as ‘unless`, safe navigation etc.
You can set syntax you want to disable via ‘DisableSyntax`. Available are: ’unless’, ‘ternary’, ‘safe_navigation’, ‘endless_methods’, ‘arguments_forwarding’, ‘numbered_parameters’, ‘pattern_matching’, ‘shorthand_hash_syntax’, ‘and_or_not’, ‘until’, and ‘percent_literals’.
Instance Method Summary collapse
- #on_and(node) ⇒ Object (also: #on_or)
- #on_array(node) ⇒ Object
- #on_case_match(node) ⇒ Object
- #on_csend(node) ⇒ Object
- #on_def(node) ⇒ Object (also: #on_defs)
- #on_hash(node) ⇒ Object
- #on_if(node) ⇒ Object
- #on_numblock(node) ⇒ Object
- #on_regexp(node) ⇒ Object
- #on_send(node) ⇒ Object
- #on_str(node) ⇒ Object
- #on_until(node) ⇒ Object
Instance Method Details
#on_and(node) ⇒ Object Also known as: on_or
96 97 98 99 100 101 102 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 96 def on_and(node) if node.semantic_operator? && !and_or_not_allowed? add_offense(node, message: "Use `#{node.alternate_operator}` instead of `#{node.operator}`.") do |corrector| corrector.replace(node.loc.operator, node.alternate_operator) end end end |
#on_array(node) ⇒ Object
114 115 116 117 118 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 114 def on_array(node) if node.percent_literal? && !percent_literals_allowed? add_offense(node, message: "Do not use `%` literals for arrays.") end end |
#on_case_match(node) ⇒ Object
77 78 79 80 81 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 77 def on_case_match(node) if !pattern_matching_allowed? add_offense(node, message: "Do not use pattern matching.") end end |
#on_csend(node) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 39 def on_csend(node) if ! add_offense(node, message: "Do not use `&.`.") end on_send(node) end |
#on_def(node) ⇒ Object Also known as: on_defs
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 56 def on_def(node) if node.endless? && !endless_methods_allowed? add_offense(node, message: "Do not use endless methods.") do |corrector| arguments = node.arguments.any? ? node.arguments.source : "" corrector.replace(node, <<~RUBY.strip) def #{node.method_name}#{arguments} #{node.body.source} end RUBY end end end |
#on_hash(node) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 83 def on_hash(node) return if shorthand_hash_syntax_allowed? || node.pairs.none?(&:value_omission?) add_offense(node, message: "Do not use shorthand hash syntax.") do |corrector| node.pairs.each do |pair| if pair.value_omission? hash_key_source = pair.key.source corrector.replace(pair, "#{hash_key_source}: #{hash_key_source}") end end end end |
#on_if(node) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 28 def on_if(node) if node.unless? && !unless_allowed? add_offense(node, message: "Do not use `unless`.") do |corrector| corrector.replace(node.loc.keyword, "if") corrector.wrap(node.condition, "!(", ")") end elsif node.ternary? && !ternary_allowed? add_offense(node, message: "Do not use ternary operator.") end end |
#on_numblock(node) ⇒ Object
71 72 73 74 75 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 71 def on_numblock(node) if !numbered_parameters_allowed? add_offense(node, message: "Do not use numbered parameters.") end end |
#on_regexp(node) ⇒ Object
120 121 122 123 124 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 120 def on_regexp(node) if node.percent_r_literal? && !percent_literals_allowed? add_offense(node, message: "Do not use `%` literals for regexes.") end end |
#on_send(node) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 46 def on_send(node) if !arguments_forwarding_allowed? && arguments_forwarding?(node) add_offense(node, message: "Do not use arguments forwarding.") elsif node.prefix_not? && !and_or_not_allowed? add_offense(node, message: "Use `!` instead of `not`.") do |corrector| corrector.replace(node.loc.selector, "!") end end end |
#on_str(node) ⇒ Object
126 127 128 129 130 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 126 def on_str(node) if !percent_literals_allowed? && str_percent_literal?(node) add_offense(node, message: "Do not use `%` literals for strings.") end end |
#on_until(node) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/rubocop/cop/style/disable_syntax.rb', line 105 def on_until(node) return if until_allowed? add_offense(node, message: "Do not use `until`.") do |corrector| corrector.replace(node.loc.keyword, "while") corrector.wrap(node.condition, "!(", ")") end end |