Class: RubyLint::Analysis::LoopKeywords

Inherits:
Base show all
Defined in:
lib/ruby-lint/analysis/loop_keywords.rb

Overview

Analysis class that checks if certain keywords are used inside a block/loop or not. For example, the following is not valid Ruby code:

next

But the following is valid:

[10, 20].each do |n|
  next
end

The following isn’t valid either:

def foo
  next
end

See KEYWORDS for a list of the keywords that can only be used inside a loop.

Constant Summary

KEYWORDS =

List of keywords that can only be used inside a loop.

Returns:

  • (Array)
[:next, :break]
STATEMENTS =

List of statements that do allow the use of the various keywords.

Returns:

  • (Array)
[:while, :until, :for]

Constants inherited from Base

Base::SCOPES

Instance Attribute Summary

Attributes inherited from Base

#config, #report, #vm

Attributes inherited from Iterator

#arity_cache, #arity_cache Hash containing the amount of arguments for

Instance Method Summary collapse

Methods inherited from Base

#add_message, analyze?, #current_scope, #error, #info, #previous_scope, register, #set_current_scope, #set_previous_scope, #warning

Methods included from MethodEvaluation

#unpack_block

Methods inherited from Iterator

#execute_callback, #initialize, #iterate, #skip_child_nodes!

Constructor Details

This class inherits a constructor from RubyLint::Iterator

Instance Method Details

#after_initializeObject



27
28
29
30
# File 'lib/ruby-lint/analysis/loop_keywords.rb', line 27

def after_initialize
  @loop_nesting = 0
  super
end

#verify_keyword(keyword, node) ⇒ Object

Parameters:



64
65
66
67
68
# File 'lib/ruby-lint/analysis/loop_keywords.rb', line 64

def verify_keyword(keyword, node)
  if current_scope.type != :block and @loop_nesting.zero?
    error("#{keyword} can only be used inside a loop/block", node)
  end
end