Class: RuboCop::Cop::Lint::ErbNewArguments
- Extended by:
- AutoCorrector, TargetRubyVersion
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/lint/erb_new_arguments.rb
Overview
Emulates the following Ruby warnings in Ruby 2.6.
- source,console
$ cat example.rb ERB.new(‘hi’, nil, ‘-’, ‘@output_buffer’) $ ruby -rerb example.rb example.rb:1: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. example.rb:1: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode:…) instead. example.rb:1: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: …) instead.
Now non-keyword arguments other than first one are softly deprecated and will be removed when Ruby 2.5 becomes EOL. ‘ERB.new` with non-keyword arguments is deprecated since ERB 2.2.0. Use `:trim_mode` and `:eoutvar` keyword arguments to `ERB.new`. This cop identifies places where `ERB.new(str, trim_mode, eoutvar)` can be replaced by `ERB.new(str, :trim_mode: trim_mode, eoutvar: eoutvar)`.
Constant Summary collapse
- MESSAGE_SAFE_LEVEL =
'Passing safe_level with the 2nd argument of `ERB.new` is ' \ 'deprecated. Do not use it, and specify other arguments as ' \ 'keyword arguments.'
- MESSAGE_TRIM_MODE =
'Passing trim_mode with the 3rd argument of `ERB.new` is ' \ 'deprecated. Use keyword argument like ' \ '`ERB.new(str, trim_mode: %<arg_value>s)` instead.'
- MESSAGE_EOUTVAR =
'Passing eoutvar with the 4th argument of `ERB.new` is ' \ 'deprecated. Use keyword argument like ' \ '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
- RESTRICT_ON_SEND =
%i[new].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods included from AutoCorrector
Methods included from TargetRubyVersion
minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#erb_new_with_non_keyword_arguments(node) ⇒ Object
81 82 83 84 |
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 81 def_node_matcher :erb_new_with_non_keyword_arguments, <<~PATTERN (send (const {nil? cbase} :ERB) :new $...) PATTERN |
#on_send(node) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 86 def on_send(node) erb_new_with_non_keyword_arguments(node) do |arguments| return if arguments.empty? || correct_arguments?(arguments) arguments[1..3].each_with_index do |argument, i| next if !argument || argument.hash_type? add_offense( argument, message: (i, argument.source) ) do |corrector| autocorrect(corrector, node) end end end end |