Class: TypeScript::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/typescript/merge/node_wrapper.rb,
sig/typescript/merge.rbs

Overview

Structural identity and source-range adapter for one top-level TypeScript owner. rubocop:disable Metrics/AbcSize, Metrics/ClassLength, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity -- TypeScript declaration forms share one structural identity boundary

Constant Summary collapse

DECLARATION_TYPES =

Returns:

  • (Array[String])
%w[
  ambient_declaration
  class_declaration
  enum_declaration
  expression_statement
  export_statement
  function_declaration
  function_signature
  import_statement
  interface_declaration
  internal_module
  lexical_declaration
  type_alias_declaration
  variable_declaration
].freeze
NAMED_DECLARATIONS =

Returns:

  • (Hash[String, [Symbol, Array[String]]])
{
  'class_declaration' => [:class, %w[type_identifier identifier]],
  'enum_declaration' => [:enum, %w[identifier type_identifier]],
  'interface_declaration' => [:interface, %w[type_identifier identifier]],
  'type_alias_declaration' => [:type, %w[type_identifier identifier]],
  'internal_module' => [:namespace, %w[identifier string nested_identifier]],
  'function_declaration' => [:function, %w[identifier]],
  'function_signature' => [:function, %w[identifier]]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, source:, leading_comments: []) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Object)
  • source: (String)
  • leading_comments: (Array[untyped]) (defaults to: [])


35
36
37
38
39
40
# File 'lib/typescript/merge/node_wrapper.rb', line 35

def initialize(node, source:, leading_comments: [])
  @node = node
  @source = source
  @leading_comments = leading_comments.freeze
  @trailing_comments = []
end

Instance Attribute Details

#leading_commentsArray[untyped] (readonly)

Returns the value of attribute leading_comments.

Returns:

  • (Array[untyped])


33
34
35
# File 'lib/typescript/merge/node_wrapper.rb', line 33

def leading_comments
  @leading_comments
end

#nodeObject (readonly)

Returns the value of attribute node.

Returns:

  • (Object)


33
34
35
# File 'lib/typescript/merge/node_wrapper.rb', line 33

def node
  @node
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


33
34
35
# File 'lib/typescript/merge/node_wrapper.rb', line 33

def source
  @source
end

#trailing_commentsArray[untyped] (readonly)

Returns the value of attribute trailing_comments.

Returns:

  • (Array[untyped])


33
34
35
# File 'lib/typescript/merge/node_wrapper.rb', line 33

def trailing_comments
  @trailing_comments
end

Instance Method Details

#attach_trailing_comment!(comment) ⇒ Object

Parameters:

  • comment (Object)

Returns:

  • (Object)


63
# File 'lib/typescript/merge/node_wrapper.rb', line 63

def attach_trailing_comment!(comment) = trailing_comments << comment

#end_byteInteger

Returns:

  • (Integer)


59
# File 'lib/typescript/merge/node_wrapper.rb', line 59

def end_byte = trailing_comments.last&.end_byte || node.end_byte

#end_lineInteger

Returns:

  • (Integer)


61
# File 'lib/typescript/merge/node_wrapper.rb', line 61

def end_line = line_for_byte(end_byte.zero? ? 0 : end_byte - 1)

#grouped?Boolean

Returns:

  • (Boolean)


56
# File 'lib/typescript/merge/node_wrapper.rb', line 56

def grouped? = variable_declaration?(node)

#membership_signatureArray[untyped]

Returns:

  • (Array[untyped])


52
53
54
# File 'lib/typescript/merge/node_wrapper.rb', line 52

def membership_signature
  signature
end

#semantic_treeArray[untyped]

Returns:

  • (Array[untyped])


64
# File 'lib/typescript/merge/node_wrapper.rb', line 64

def semantic_tree = semantic_node(node)

#signatureArray[untyped]

Returns:

  • (Array[untyped])


42
43
44
45
46
47
48
49
50
# File 'lib/typescript/merge/node_wrapper.rb', line 42

def signature
  return expression_statement_signature if node.type == 'expression_statement'
  return import_signature if node.type == 'import_statement'
  return export_signature if node.type == 'export_statement'
  return variable_signature(node) if variable_declaration?(node)
  return ambient_signature if node.type == 'ambient_declaration'

  named_signature(node)
end

#source_textString

Returns:

  • (String)


62
# File 'lib/typescript/merge/node_wrapper.rb', line 62

def source_text = source.byteslice(start_byte...end_byte).to_s

#start_byteInteger

Returns:

  • (Integer)


58
# File 'lib/typescript/merge/node_wrapper.rb', line 58

def start_byte = leading_comments.first&.start_byte || node.start_byte

#start_lineInteger

Returns:

  • (Integer)


60
# File 'lib/typescript/merge/node_wrapper.rb', line 60

def start_line = line_for_byte(start_byte)