Class: RuboCop::Cop::Style::Semicolon
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::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/style/semicolon.rb |
Overview
Checks for multiple expressions placed on the same line. It also checks for lines terminated with a semicolon.
This cop has AllowAsExpressionSeparator
configuration option.
It allows ;
to separate several expressions on the same line.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/style/semicolon.rb', line 33'Do not use semicolons to terminate expressions.'
::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 #on_new_investigation and |
Class Method Summary
::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
- #on_begin(node)
- #on_new_investigation
- #check_for_line_terminator_or_opener private
- #each_semicolon private
- #exist_semicolon_after_left_curly_brace?(tokens) ⇒ Boolean private
- #exist_semicolon_after_left_lambda_curly_brace?(tokens) ⇒ Boolean private
- #exist_semicolon_after_left_string_interpolation_brace?(tokens) ⇒ Boolean private
-
#exist_semicolon_before_right_curly_brace?(tokens) ⇒ Boolean
private
Metrics/PerceivedComplexity.
- #exist_semicolon_before_right_string_interpolation_brace?(tokens) ⇒ Boolean private
- #expressions_per_line(exprs) private
- #find_range_node(token_before_semicolon) private
- #find_semicolon_positions(line) private
- #range_nodes private
- #register_semicolon(line, column, after_expression, token_before_semicolon = nil) private
-
#semicolon_position(tokens)
private
Metrics/PerceivedComplexity.
- #tokens_for_lines 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
Class Method Details
.autocorrect_incompatible_with
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 35
def self.autocorrect_incompatible_with [Style::SingleLineMethods] end
Instance Method Details
#check_for_line_terminator_or_opener (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 64
def check_for_line_terminator_or_opener each_semicolon do |line, column, token_before_semicolon| register_semicolon(line, column, false, token_before_semicolon) end end
#each_semicolon (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 70
def each_semicolon tokens_for_lines.each do |line, tokens| semicolon_pos = semicolon_position(tokens) after_expr_pos = semicolon_pos == -1 ? -2 : semicolon_pos yield line, tokens[semicolon_pos].column, tokens[after_expr_pos] if semicolon_pos end end
#exist_semicolon_after_left_curly_brace?(tokens) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/semicolon.rb', line 106
def exist_semicolon_after_left_curly_brace?(tokens) tokens[1]&.left_curly_brace? && tokens[2]&.semicolon? end
#exist_semicolon_after_left_lambda_curly_brace?(tokens) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/semicolon.rb', line 110
def exist_semicolon_after_left_lambda_curly_brace?(tokens) tokens[2]&.type == :tLAMBEG && tokens[3]&.semicolon? end
#exist_semicolon_after_left_string_interpolation_brace?(tokens) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/semicolon.rb', line 118
def exist_semicolon_after_left_string_interpolation_brace?(tokens) tokens[1]&.type == :tSTRING_DBEG && tokens[2]&.semicolon? end
#exist_semicolon_before_right_curly_brace?(tokens) ⇒ Boolean
(private)
Metrics/PerceivedComplexity
# File 'lib/rubocop/cop/style/semicolon.rb', line 102
def exist_semicolon_before_right_curly_brace?(tokens) tokens[-2]&.right_curly_brace? && tokens[-3]&.semicolon? end
#exist_semicolon_before_right_string_interpolation_brace?(tokens) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/semicolon.rb', line 114
def exist_semicolon_before_right_string_interpolation_brace?(tokens) tokens[-3]&.type == :tSTRING_DEND && tokens[-4]&.semicolon? end
#expressions_per_line(exprs) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 142
def expressions_per_line(exprs) # create a map matching lines to the number of expressions on them exprs_lines = exprs.map(&:last_line) exprs_lines.group_by(&:itself) end
#find_range_node(token_before_semicolon) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 156
def find_range_node(token_before_semicolon) range_nodes.detect do |range_node| range_node.source_range.contains?(token_before_semicolon.pos) end end
#find_semicolon_positions(line) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 148
def find_semicolon_positions(line) # Scan for all the semicolons on the line semicolons = processed_source[line - 1].enum_for(:scan, ';') semicolons.each do yield Regexp.last_match.begin(0) end end
#on_begin(node)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 45
def on_begin(node) return if cop_config['AllowAsExpressionSeparator'] return unless node.source.include?(';') exprs = node.children return if exprs.size < 2 expressions_per_line(exprs).each do |line, expr_on_line| # Every line with more than one expression on it is a # potential offense next unless expr_on_line.size > 1 find_semicolon_positions(line) { |pos| register_semicolon(line, pos, true) } end end
#on_new_investigation
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 39
def on_new_investigation return if processed_source.blank? || !processed_source.raw_source.include?(';') check_for_line_terminator_or_opener end
#range_nodes (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 162
def range_nodes return @range_nodes if instance_variable_defined?(:@range_nodes) ast = processed_source.ast @range_nodes = ast.range_type? ? [ast] : [] @range_nodes.concat(ast.each_descendant(:irange, :erange).to_a) end
#register_semicolon(line, column, after_expression, token_before_semicolon = nil) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 122
def register_semicolon(line, column, after_expression, token_before_semicolon = nil) range = source_range(processed_source.buffer, line, column) add_offense(range) do |corrector| if after_expression corrector.replace(range, "\n") else # Prevents becoming one range instance with subsequent line when endless range # without parentheses. # See: https://github.com/rubocop/rubocop/issues/10791 if token_before_semicolon&.regexp_dots? range_node = find_range_node(token_before_semicolon) corrector.wrap(range_node, '(', ')') if range_node end corrector.remove(range) end end end
#semicolon_position(tokens) (private)
Metrics/PerceivedComplexity
# File 'lib/rubocop/cop/style/semicolon.rb', line 84
def semicolon_position(tokens) if tokens.last.semicolon? -1 elsif tokens.first.semicolon? 0 elsif exist_semicolon_before_right_curly_brace?(tokens) -3 elsif exist_semicolon_after_left_curly_brace?(tokens) || exist_semicolon_after_left_string_interpolation_brace?(tokens) 2 elsif exist_semicolon_after_left_lambda_curly_brace?(tokens) 3 elsif exist_semicolon_before_right_string_interpolation_brace?(tokens) -4 end end
#tokens_for_lines (private)
[ GitHub ]# File 'lib/rubocop/cop/style/semicolon.rb', line 79
def tokens_for_lines processed_source.tokens.group_by(&:line) end