Module: Rbs::Merge::NodeTypeNormalizer

Extended by:
Ast::Merge::NodeTyping::Normalizer
Defined in:
lib/rbs/merge/node_type_normalizer.rb

Overview

Normalizes backend-specific node types to canonical RBS types.

Uses Ast::Merge::NodeTyping::Wrapper to wrap nodes with canonical
merge_type, allowing portable merge rules across backends.

Thread Safety

All backend registration and lookup operations are thread-safe via
the shared Ast::Merge::NodeTyping::Normalizer module.

Backends

Currently supports:

  • :rbs - Official RBS gem parser (MRI only, full-featured)
  • :tree_sitter - tree-sitter-rbs grammar (cross-platform)

Extensibility

New backends can be registered at runtime:

Canonical Types

The following canonical types are used for portable merge rules:

Declaration Types

  • :class - Class declaration
  • :module - Module declaration
  • :interface - Interface declaration
  • :type_alias - Type alias declaration
  • :constant - Constant declaration
  • :global - Global variable declaration

Member Types

  • :method - Method definition
  • :alias - Method alias
  • :attr_reader - Attribute reader
  • :attr_writer - Attribute writer
  • :attr_accessor - Attribute accessor
  • :include - Module include
  • :extend - Module extend
  • :prepend - Module prepend
  • :ivar - Instance variable
  • :civar - Class instance variable
  • :cvar - Class variable

Other

  • :comment - Comment lines
  • :document - Root document node

Examples:

Registering a new backend

NodeTypeNormalizer.register_backend(:my_rbs_parser, {
  class_decl: :class,
  module_decl: :module,
})

See Also:

  • Ast::Merge::NodeTyping::Wrapper
  • Ast::Merge::NodeTyping::Normalizer

Constant Summary collapse

DEFAULT_BACKEND =

Default backend for RBS normalization

:rbs

Class Method Summary collapse

Class Method Details

.canonical_type(backend_type, backend = DEFAULT_BACKEND) ⇒ Symbol?

Get the canonical type for a backend-specific type.
Overrides the shared Normalizer to default to :rbs backend.

Parameters:

  • backend_type (Symbol, String, nil)

    The backend’s node type

  • backend (Symbol) (defaults to: DEFAULT_BACKEND)

    The backend identifier (defaults to :rbs)

Returns:

  • (Symbol, nil)

    Canonical type (or original if no mapping)



237
238
239
# File 'lib/rbs/merge/node_type_normalizer.rb', line 237

def canonical_type(backend_type, backend = DEFAULT_BACKEND)
  super(backend_type, backend)
end

.container_type?(type) ⇒ Boolean

Check if a type is a container type (can have children/members)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


274
275
276
277
# File 'lib/rbs/merge/node_type_normalizer.rb', line 274

def container_type?(type)
  canonical = type.to_sym
  %i[document class module interface].include?(canonical)
end

.declaration_type?(type) ⇒ Boolean

Check if a type is a declaration type (class, module, interface, etc.)
Also includes :declaration_wrapper which wraps declarations in tree-sitter-rbs

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


256
257
258
259
# File 'lib/rbs/merge/node_type_normalizer.rb', line 256

def declaration_type?(type)
  canonical = type.to_sym
  %i[class module interface type_alias constant global class_alias module_alias declaration_wrapper].include?(canonical)
end

.member_type?(type) ⇒ Boolean

Check if a type is a member type (method, attr, include, etc.)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


265
266
267
268
# File 'lib/rbs/merge/node_type_normalizer.rb', line 265

def member_type?(type)
  canonical = type.to_sym
  %i[method alias attr_reader attr_writer attr_accessor include extend prepend ivar civar cvar visibility].include?(canonical)
end

.type_definition?(type) ⇒ Boolean

Check if a type is a type definition (type_alias, etc.)

Parameters:

  • type (Symbol, String)

    The type to check

Returns:

  • (Boolean)


283
284
285
286
# File 'lib/rbs/merge/node_type_normalizer.rb', line 283

def type_definition?(type)
  canonical = type.to_sym
  %i[type_alias].include?(canonical)
end

.wrap(node, backend = DEFAULT_BACKEND) ⇒ Ast::Merge::NodeTyping::Wrapper

Wrap a node with its canonical type as merge_type.
Overrides the shared Normalizer to default to :rbs backend.

Parameters:

  • node (Object)

    The backend node to wrap (must respond to #type)

  • backend (Symbol) (defaults to: DEFAULT_BACKEND)

    The backend identifier (defaults to :rbs)

Returns:

  • (Ast::Merge::NodeTyping::Wrapper)

    Wrapped node with canonical merge_type



247
248
249
# File 'lib/rbs/merge/node_type_normalizer.rb', line 247

def wrap(node, backend = DEFAULT_BACKEND)
  super(node, backend)
end