Module: JsxRosetta::AST::Inflector

Defined in:
lib/jsx_rosetta/ast/inflector.rb

Overview

Internal helpers for converting between Babel’s camelCase field names and Ruby’s snake_case conventions.

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object



18
19
20
21
# File 'lib/jsx_rosetta/ast/inflector.rb', line 18

def camelize(string)
  parts = string.split("_")
  parts[0] + parts[1..].map(&:capitalize).join
end

.ruby_string_literal(value) ⇒ Object

Emit a Ruby string literal in the rubocop-default single-quoted form when safe. Falls back to ‘String#inspect` (double-quoted with escapes) when the source contains characters that prevent the single-quoted form: single quotes themselves, backslashes (Ruby single-quoted strings only escape `\` and `'`), or control characters (`n`, `t`, etc — single-quoted strings render those literally). Non-ASCII characters (emojis, unicode) are fine in single-quoted strings, so they don’t force the fallback.



31
32
33
34
35
36
# File 'lib/jsx_rosetta/ast/inflector.rb', line 31

def ruby_string_literal(value)
  str = value.to_s
  return str.inspect if str.include?("'") || str.include?("\\") || str.match?(/[\x00-\x1f\x7f]/)

  "'#{str}'"
end

.underscore(string) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/jsx_rosetta/ast/inflector.rb', line 10

def underscore(string)
  string
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end