Class: RuboCop::Cop::Lint::ErbNewArguments
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::RuboCop::Cop::TargetRubyVersion ,
::RuboCop::Cop::AutoCorrector ,
::RuboCop::Cop::Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp ,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/lint/erb_new_arguments.rb |
Overview
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)
.
Constant Summary
-
MESSAGE_EOUTVAR =
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 74'Passing eoutvar with the 4th argument of `ERB.new` is ' \ 'deprecated. Use keyword argument like ' \ '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
-
MESSAGE_SAFE_LEVEL =
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 68'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 =
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 71'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.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 78%i[new].freeze
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp
- Included
Class Attribute Summary
::RuboCop::Cop::AutoCorrector
- Extended
::RuboCop::Cop::Base
- Inherited
.gem_requirements, .lint?, | |
.support_autocorrect? | Returns if class supports autocorrect. |
.support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::TargetRubyVersion
- Extended
maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version? |
::RuboCop::Cop::Base
- Inherited
.autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
.badge | Naming. |
.callbacks_needed, .cop_name, .department, | |
.documentation_url | Returns a url to view this cops documentation online. |
.exclude_from_registry | Call for abstract Cop classes. |
.inherited, | |
.joining_forces | Override and return the Force class(es) you need to join. |
.match? | Returns true if the cop name or the cop namespace matches any of the given names. |
.new, | |
.requires_gem | Register a version requirement for the given gem name. |
.restrict_on_send |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #erb_new_with_non_keyword_arguments(node)
- #on_send(node)
- #arguments_range(node) private
- #autocorrect(corrector, node) private
- #build_kwargs(node) private
- #correct_arguments?(arguments) ⇒ Boolean private
- #message(positional_argument_index, arg_value) private
- #override_by_legacy_args(kwargs, node) private
::RuboCop::Cop::RangeHelp
- Included
#add_range, #column_offset_between, | |
#contents_range | A range containing only the contents of a literal with delimiters (e.g. |
#directions, | |
#effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range |
::RuboCop::Cop::Base
- Inherited
#add_global_offense | Adds an offense that has no particular location. |
#add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
#begin_investigation | Called before any investigation. |
#callbacks_needed, | |
#cop_config | Configuration Helpers. |
#cop_name, #excluded_file?, | |
#external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
#inspect, | |
#message | Gets called if no message is specified when calling |
#name | Alias for Base#cop_name. |
#offenses, | |
#on_investigation_end | Called after all on_… |
#on_new_investigation | Called before all on_… |
#on_other_file | Called instead of all on_… |
#parse | There should be very limited reasons for a Cop to do it’s own parsing. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, | |
#target_gem_version | Returns a gems locked versions (i.e. |
#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
#callback_argument | Reserved for Cop::Cop. |
#complete_investigation | Called to complete an investigation. |
#correct, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #heredoc_range, #max_line_length, #multiline_ranges, #multiline_string?, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation?, #surrounding_heredoc?, #surrounding_percent_array? |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#arguments_range(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 160
def arguments_range(node) arguments = node.arguments range_between(arguments.first.source_range.begin_pos, arguments.last.source_range.end_pos) end
#autocorrect(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 115
def autocorrect(corrector, node) str_arg = node.first_argument.source kwargs = build_kwargs(node) overridden_kwargs = override_by_legacy_args(kwargs, node) good_arguments = [str_arg, overridden_kwargs].flatten.compact.join(', ') corrector.replace(arguments_range(node), good_arguments) end
#build_kwargs(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 130
def build_kwargs(node) return [nil, nil] unless node.last_argument.hash_type? trim_mode_arg, eoutvar_arg = nil node.last_argument.pairs.each do |pair| case pair.key.source when 'trim_mode' trim_mode_arg = "trim_mode: #{pair.value.source}" when 'eoutvar' eoutvar_arg = "eoutvar: #{pair.value.source}" end end [trim_mode_arg, eoutvar_arg] end
#correct_arguments?(arguments) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 126
def correct_arguments?(arguments) arguments.size == 1 || (arguments.size == 2 && arguments[1].hash_type?) end
#erb_new_with_non_keyword_arguments(node)
[ GitHub ]# 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
#message(positional_argument_index, arg_value) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 104
def (positional_argument_index, arg_value) case positional_argument_index when 0 MESSAGE_SAFE_LEVEL when 1 format(MESSAGE_TRIM_MODE, arg_value: arg_value) when 2 format(MESSAGE_EOUTVAR, arg_value: arg_value) end end
#on_send(node)
[ GitHub ]# 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
#override_by_legacy_args(kwargs, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/erb_new_arguments.rb', line 147
def override_by_legacy_args(kwargs, node) arguments = node.arguments overridden_kwargs = kwargs.dup overridden_kwargs[0] = "trim_mode: #{arguments[2].source}" if arguments[2] if arguments[3] && !arguments[3].hash_type? overridden_kwargs[1] = "eoutvar: #{arguments[3].source}" end overridden_kwargs end