Ameba::AST::Util - ameba 1.7.0

# module Ameba::AST::Util

##
Overview

Utility module for Ameba's rules.

##
Extended Modules

- Ameba::AST::Util

##
Direct including types

- Ameba::AST::ElseIfAwareNodeVisitor
- Ameba::AST::FlowExpression
- Ameba::AST::FlowExpressionVisitor
- Ameba::AST::ReachingDefinitionAnalyzer
- Ameba::Reportable
- Ameba::Rule::Documentation::Admonition
- Ameba::Rule::Lint::AmbiguousAssignment
- Ameba::Rule::Lint::BadDirective
- Ameba::Rule::Lint::ComparisonToBoolean
- Ameba::Rule::Lint::DebuggerStatement
- Ameba::Rule::Lint::DeprecatedRule
- Ameba::Rule::Lint::DuplicateBranch
- Ameba::Rule::Lint::EmptyLoop
- Ameba::Rule::Lint::LiteralAssignmentsInExpressions
- Ameba::Rule::Lint::LiteralInCondition
- Ameba::Rule::Lint::LiteralInInterpolation
- Ameba::Rule::Lint::LiteralsComparison
- Ameba::Rule::Lint::NonExistentRule
- Ameba::Rule::Lint::NotNil
- Ameba::Rule::Lint::NotNilAfterNoBang
- Ameba::Rule::Lint::RedundantStringCoercion
- Ameba::Rule::Lint::RequireParentheses
- Ameba::Rule::Lint::SharedVarInFiber
- Ameba::Rule::Lint::SignalTrap
- Ameba::Rule::Lint::SpecEqWithBoolOrNilLiteral
- Ameba::Rule::Lint::SpecFocus
- Ameba::Rule::Lint::TopLevelOperatorDefinition
- Ameba::Rule::Lint::UnneededDisableDirective
- Ameba::Rule::Lint::UnusedBlockArgument
- Ameba::Rule::Lint::UnusedRescueVariable
- Ameba::Rule::Lint::VoidOutsideLib
- Ameba::Rule::Lint::WhitespaceAroundMacroExpression
- Ameba::Rule::Naming::AccessorMethodName
- Ameba::Rule::Naming::BinaryOperatorParameterName
- Ameba::Rule::Naming::QueryBoolMethods
- Ameba::Rule::Naming::RescuedExceptionsVariableName
- Ameba::Rule::Performance::AnyAfterFilter
- Ameba::Rule::Performance::AnyInsteadOfEmpty
- Ameba::Rule::Performance::AnyInsteadOfPresent
- Ameba::Rule::Performance::ChainedCallWithNoBang
- Ameba::Rule::Performance::CompactAfterMap
- Ameba::Rule::Performance::ExcessiveAllocations
- Ameba::Rule::Performance::FirstLastAfterFilter
- Ameba::Rule::Performance::FlattenAfterMap
- Ameba::Rule::Performance::MapInsteadOfBlock
- Ameba::Rule::Performance::MinMaxAfterMap
- Ameba::Rule::Performance::SizeAfterFilter
- Ameba::Rule::Performance::TimesMap
- Ameba::Rule::Style::CallParentheses
- Ameba::Rule::Style::GuardClause
- Ameba::Rule::Style::HeredocEscape
- Ameba::Rule::Style::HeredocIndent
- Ameba::Rule::Style::IsAFilter
- Ameba::Rule::Style::IsANil
- Ameba::Rule::Style::LargeNumbers
- Ameba::Rule::Style::MultilineCurlyBlock
- Ameba::Rule::Style::ParenthesesAroundCondition
- Ameba::Rule::Style::RedundantBegin
- Ameba::Rule::Style::RedundantNext
- Ameba::Rule::Style::RedundantNilInControlExpression
- Ameba::Rule::Style::RedundantReturn
- Ameba::Rule::Style::RedundantSelf
- Ameba::Rule::Style::VerboseBlock
- Ameba::Rule::Style::VerboseNilType
- Ameba::Rule::Typing::MethodParameterTypeRestriction
- Ameba::Rule::Typing::MethodReturnTypeRestriction

##
Defined in:

ameba/ast/util.cr

##
Instance Method Summary

- #abort?(node)

  Returns `true` if node represents `abort` method call.

- #control_exp_code(node : Crystal::ControlExpression, code_lines)

  Returns the exp code of a control expression.

- #dynamic_literal?(node) : Bool

  Returns `true` if current `node` is a dynamic literal, `false` otherwise.

- #each_inline_directive(source, &block : Crystal::Token, String, Array(String) -> _)

  Yields each inline directive found in the source.

- #exit?(node)

  Returns `true` if node represents `exit` method call.

- #flow_command?(node, in_loop)

  Returns `true` if node is a flow command, `false` otherwise.

- #flow_expression?(node, in_loop = false)

  Returns `true` if node is a flow expression, `false` if not.

- #has_arguments?(node) : Bool

  Returns `true` if the node is a `Crystal::Call` with either `node.block` or `node.block_arg` set, `false` otherwise.

- #has_block?(node) : Bool

  Returns `true` if the node is a `Crystal::Call` with either `node.block` or `node.block_arg` set, `false` otherwise.

- #has_short_block?(node, code_lines)

  Returns `true` if node is a call with a short block version (`&.foo?`).

- #heredoc?(node : Crystal::ASTNode, source : Source)

  Returns `true` if node is a heredoc, `false` otherwise.

- #literal?(node) : Bool

  Returns `true` if current `node` is a literal, `false` otherwise.

- #loop?(node)

  Returns `true` if node represents a loop.

- #name_end_location(node)

  Returns `nil` if node does not contain a name.

- #name_location(node)

  Returns `nil` if node does not contain a name.

- #name_location_or(token : Crystal::Token, name, *, adjust_location_column_number = nil)
- #name_location_or(node : Crystal::ASTNode, *, adjust_location_column_number = nil)
- #name_size(node)

  Returns zero if node does not contain a name.

- #node_source(node, code_lines)

  Returns a source code for the current node.

- #nodoc?(node)

  Returns `true` if node has a `:nodoc:` annotation as the first line.

- #operator_method?(node)

  Returns `true` if node represents operator method.

- #operator_method_name?(name : String)

  Returns `true` if name represents operator method.

- #path_name(node : Crystal::Path, *, include_global = true) : String

  Returns the path name of the path.

- #path_named?(node, *names : String) : Bool

  Returns `true` if current `node` is a `Crystal::Path` matching given name, `false` otherwise.

- #raise?(node)

  Returns `true` if node represents `raise` method call.

- #setter_method?(node)

  Returns `true` if node represents setter method.

- #setter_method_name?(name : String)

  Returns `true` if name represents setter method.

- #short_block?(node, code_lines)

  Returns `true` if node represents a short block version (`&.foo?`).

- #source_between(loc, end_loc, code_lines) : String | Nil

  Returns the source code from loc to end_loc (inclusive).

- #static_literal?(node) : Bool

  Returns `true` if current `node` is a static literal, `false` otherwise.

- #suffix?(node)

  Returns `true` if node is a suffix node (`if` / `unless` / `rescue` / `ensure`).

- #takes_arguments?(node) : Bool

  Returns `true` if the node is a `Crystal::Def` with either `args`, `splat_index`, or `double_splat` set, `false` otherwise.

##
Instance Method Detail

def abort?(node) #

Returns `true` if node represents `abort` method call.

def control_exp_code(node : Crystal::ControlExpression, code_lines) #

Returns the exp code of a control expression. Wraps implicit tuple literal with curly brackets (e.g. multi-return).

def dynamic_literal?(node) : Bool #

Returns `true` if current `node` is a dynamic literal, `false` otherwise.

def each_inline_directive(source, &block : Crystal::Token, String, Array(String) -> _) #

Yields each inline directive found in the source.

def exit?(node) #

Returns `true` if node represents `exit` method call.

def flow_command?(node, in_loop) #

Returns `true` if node is a flow command, `false` otherwise. Node represents a flow command if it is a control expression, or special call node that interrupts execution (i.e. raise, exit, abort).

def flow_expression?(node, in_loop = false) #

Returns `true` if node is a flow expression, `false` if not. Node represents a flow expression if it is full-filled by a flow command.

For example, this node is a flow expression, because each branch contains a flow command `return`:

    if a > 0
      return :positive
    elsif a < 0
      return :negative
    else
      return :zero
    end

This node is a not a flow expression:

    if a > 0
      return :positive
    end

That's because not all branches return(i.e. `else` is missing).

def has_arguments?(node) : Bool #

Returns `true` if the node is a `Crystal::Call` with either `node.block` or `node.block_arg` set, `false` otherwise.

def has_block?(node) : Bool #

Returns `true` if the node is a `Crystal::Call` with either `node.block` or `node.block_arg` set, `false` otherwise.

def has_short_block?(node, code_lines) #

Returns `true` if node is a call with a short block version (`&.foo?`).

def heredoc?(node : Crystal::ASTNode, source : Source) #

Returns `true` if node is a heredoc, `false` otherwise.

def literal?(node) : Bool #

Returns `true` if current `node` is a literal, `false` otherwise.

def loop?(node) #

Returns `true` if node represents a loop.

def name_end_location(node) #

Returns `nil` if node does not contain a name.

NOTE Use this instead of `Crystal::Call#name_end_location` to avoid an off-by-one error.

def name_location(node) #

Returns `nil` if node does not contain a name.

def name_location_or(token : Crystal::Token, name, *, adjust_location_column_number = nil) #
def name_location_or(node : Crystal::ASTNode, *, adjust_location_column_number = nil) #
def name_size(node) #

Returns zero if node does not contain a name.

def node_source(node, code_lines) #

Returns a source code for the current node. This method uses `node.location` and `node.end_location` to determine and cut a piece of source of the node.

def nodoc?(node) #

Returns `true` if node has a `:nodoc:` annotation as the first line.

def operator_method?(node) #

Returns `true` if node represents operator method.

def operator_method_name?(name : String) #

Returns `true` if name represents operator method.

def path_name(node : Crystal::Path, *, include_global = true) : String #

Returns the path name of the path.

If include_global is `true` (default), the global prefix `::` is included.

def path_named?(node, *names : String) : Bool #

Returns `true` if current `node` is a `Crystal::Path` matching given name, `false` otherwise.

def raise?(node) #

Returns `true` if node represents `raise` method call.

def setter_method?(node) #

Returns `true` if node represents setter method.

def setter_method_name?(name : String) #

Returns `true` if name represents setter method.

def short_block?(node, code_lines) #

Returns `true` if node represents a short block version (`&.foo?`).

def source_between(loc, end_loc, code_lines) : String | Nil #

Returns the source code from loc to end_loc (inclusive).

def static_literal?(node) : Bool #

Returns `true` if current `node` is a static literal, `false` otherwise.

def suffix?(node) #

Returns `true` if node is a suffix node (`if` / `unless` / `rescue` / `ensure`).

def takes_arguments?(node) : Bool #

Returns `true` if the node is a `Crystal::Def` with either `args`, `splat_index`, or `double_splat` set, `false` otherwise.