Class: RuboCop::Cop::Lint::ErbNewArguments

Inherits:
Cop
  • Object
show all
Extended by:
TargetRubyVersion
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/lint/erb_new_arguments.rb

Overview

This cop emulates the following Ruby warnings in Ruby 2.6.

% 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)`.

Examples:

# Target codes supports Ruby 2.6 and higher only
# bad
ERB.new(str, nil, '-', '@output_buffer')

# good
ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')

# Target codes supports Ruby 2.5 and lower only
# good
ERB.new(str, nil, '-', '@output_buffer')

# Target codes supports Ruby 2.6, 2.5 and lower
# bad
ERB.new(str, nil, '-', '@output_buffer')

# good
# Ruby standard library style
# https://github.com/ruby/ruby/commit/3406c5d
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
  ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
else
  ERB.new(str, nil, '-', '@output_buffer')
end

# good
# Use `RUBY_VERSION` style
if RUBY_VERSION >= '2.6'
  ERB.new(str, trim_mode: '-', eoutvar: '@output_buffer')
else
  ERB.new(str, nil, '-', '@output_buffer')
end

Constant Summary collapse

MESSAGES =
[
  'Passing safe_level with the 2nd argument of `ERB.new` is ' \
  'deprecated. Do not use it, and specify other arguments as ' \
  'keyword arguments.',
  '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.',
  'Passing eoutvar with the 4th argument of `ERB.new` is ' \
  'deprecated. Use keyword argument like ' \
  '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
].freeze

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods included from TargetRubyVersion

minimum_target_ruby_version, support_target_ruby_version?

Methods inherited from Cop

#add_offense, all, autocorrect_incompatible_with, badge, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, #correct, department, #disable_uncorrectable, #duplicate_location?, #excluded_file?, #external_dependency_checksum, #find_location, #highlights, inherited, #initialize, #join_force?, lint?, match?, #message, #messages, #parse, qualified_cop_name, #reason_to_not_correct, #relevant_file?, #target_rails_version, #target_ruby_version

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first

Methods included from AST::Sexp

#s

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_offense, #disable_uncorrectable?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, comment_line?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, tokens, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, chdir, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path

Constructor Details

This class inherits a constructor from RuboCop::Cop::Cop

Instance Method Details

#autocorrect(node) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 101

def autocorrect(node)
  str_arg = node.arguments[0].source

  kwargs = build_kwargs(node)
  overridden_kwargs = override_by_legacy_args(kwargs, node)

  good_arguments = [
    str_arg, overridden_kwargs
  ].flatten.compact.join(', ')

  lambda do |corrector|
    corrector.replace(arguments_range(node), good_arguments)
  end
end

#on_send(node) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 85

def on_send(node)
  erb_new_with_non_keyword_arguments(node) do |arguments|
    return if correct_arguments?(arguments)

    arguments[1..3].each_with_index do |argument, i|
      next if !argument || argument.hash_type?

      message = format(MESSAGES[i], arg_value: argument.source)

      add_offense(
        node, location: argument.source_range, message: message
      )
    end
  end
end