Class: Haml::RubyExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/haml/ruby_expression.rb

Class Method Summary collapse

Class Method Details

.string_literal?(code) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/haml/ruby_expression.rb', line 18

def self.string_literal?(code)
  !string_literal_node(code).nil?
end

.string_literal_node(code) ⇒ Prism::Node?

Returns - the node of a string literal StringSplitter can split, if any. Its locations are byte offsets into code as given, so code must not be stripped here.

Returns:

  • (Prism::Node, nil)
    • the node of a string literal StringSplitter can split, if any. Its locations are byte offsets into code as given, so code must not be stripped here.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/haml/ruby_expression.rb', line 24

def self.string_literal_node(code)
  result = Prism.parse(code, **PARSE_OPTIONS)
  return if result.failure?

  statements = result.value.statements.body
  return if statements.size > 1

  case (node = statements.first)
  when Prism::StringNode, Prism::InterpolatedStringNode
    # Adjacent concatenation (`"a" "b"`) is one node without an opening
    # delimiter of its own, and each of its parts keeps its own quotes.
    node if !node.opening.nil? && node.opening != CHAR_LITERAL_OPENING
  end
end

.syntax_error?(code) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/haml/ruby_expression.rb', line 14

def self.syntax_error?(code)
  Prism.parse_failure?(code, **PARSE_OPTIONS)
end