Class: Audition::Static::LiteralClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/audition/static/literal_classifier.rb

Overview

Classifies a Prism expression node by Ractor shareability:

:shareable         proven deeply shareable
:mutable_string    unfrozen String literal
:mutable_container Array/Hash literal
:shallow_freeze    frozen container with mutable elements
:sync_primitive    Mutex/Queue/... constructor
:proc              lambda or proc
:unknown           cannot tell statically

Constant Summary collapse

SYNC_PRIMITIVES =
%w[
  Mutex Monitor Queue SizedQueue ConditionVariable
  Thread::Mutex Thread::Queue Thread::SizedQueue
  Thread::ConditionVariable
].freeze
SHAREABLE_FACTORIES =
%w[Struct Class Module].freeze

Instance Method Summary collapse

Constructor Details

#initialize(frozen_string_literal:) ⇒ LiteralClassifier

Returns a new instance of LiteralClassifier.

Parameters:

  • frozen_string_literal (Boolean)

    whether the file has the frozen_string_literal magic comment



25
26
27
# File 'lib/audition/static/literal_classifier.rb', line 25

def initialize(frozen_string_literal:)
  @frozen_string_literal = frozen_string_literal
end

Instance Method Details

#classify(node) ⇒ Symbol

Returns classification, see class docs.

Parameters:

  • node (Prism::Node)

    an expression node

Returns:

  • (Symbol)

    classification, see class docs



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/audition/static/literal_classifier.rb', line 31

def classify(node)
  case node
  when Prism::IntegerNode, Prism::FloatNode,
       Prism::RationalNode, Prism::ImaginaryNode,
       Prism::SymbolNode, Prism::InterpolatedSymbolNode,
       Prism::TrueNode, Prism::FalseNode, Prism::NilNode,
       Prism::RegularExpressionNode,
       Prism::InterpolatedRegularExpressionNode
    :shareable
  when Prism::StringNode
    @frozen_string_literal ? :shareable : :mutable_string
  when Prism::InterpolatedStringNode
    classify_interpolated_string(node)
  when Prism::ArrayNode, Prism::HashNode,
       Prism::KeywordHashNode
    :mutable_container
  when Prism::RangeNode
    ends = [node.left, node.right].compact
    if ends.all? { |n| classify(n) == :shareable }
      :shareable
    else
      :unknown
    end
  when Prism::LambdaNode
    :proc
  when Prism::CallNode
    classify_call(node)
  else
    :unknown
  end
end

#const_name(node) ⇒ Object



63
64
65
66
67
68
# File 'lib/audition/static/literal_classifier.rb', line 63

def const_name(node)
  case node
  when Prism::ConstantReadNode then node.name.to_s
  when Prism::ConstantPathNode then node.location.slice
  end
end