Class: CustomElementsManifestParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_elements_manifest_parser/parser.rb

Overview

Top level interface that users will interact with when reading custom elements JSON.

Examples:

::CustomElementsManifestParser::Parser.new(JSON.parse("custom-elements.json"))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • hash (Hash{String => any})


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/custom_elements_manifest_parser/parser.rb', line 15

def initialize(hash)
  type_check = Types::Strict::Hash
  type_check[hash]

  @visitable_nodes = {}
  @visitable_nodes[Nodes::JavaScriptModule.kind] = Nodes::JavaScriptModule
  @visitable_nodes[Nodes::CustomElementField.kind] = Nodes::CustomElementField
  @visitable_nodes[Nodes::JavaScriptExport.kind] = Nodes::JavaScriptExport
  @visitable_nodes[Nodes::CustomElementExport.kind] = Nodes::CustomElementExport
  @visitable_nodes[Nodes::ClassMethod.kind] = Nodes::ClassMethod

  # Top level declarations
  @visitable_nodes[Nodes::ClassDeclaration.kind] = Nodes::ClassDeclaration
  @visitable_nodes[Nodes::FunctionDeclaration.kind] = Nodes::FunctionDeclaration
  @visitable_nodes[Nodes::VariableDeclaration.kind] = Nodes::VariableDeclaration

  ## This is equivalent to MixinDeclaration | CustomElementDeclaration | CustomElementMixinDeclaration;
  @visitable_nodes[Nodes::MixinDeclaration.kind] = Nodes::MixinDeclaration

  # data_types are different from @visitable_nodes. They serialize data, but do not represent a physical node in the Custom Elements JSON.
  @data_types = {
    attribute: DataTypes::Attribute,
    css_part: DataTypes::CssPart,
    css_custom_property: DataTypes::CssCustomProperty,
    demo: DataTypes::Demo,
    declaration: DataTypes::Reference,
    event: DataTypes::Event,
    function_return_type: DataTypes::FunctionReturnType,
    mixin: DataTypes::Reference,
    parameter: DataTypes::Parameter,
    superclass: DataTypes::Reference,
    source: DataTypes::SourceReference,
    slot: DataTypes::Slot,
    type: DataTypes::Type,
    type_reference: DataTypes::TypeReference,
    inherited_from: DataTypes::Reference,
    resolve_initializer: DataTypes::ResolveInitializer,
    reference: DataTypes::Reference
  }

  # @return [Nodes::Manifest]
  @manifest = Nodes::Manifest.new(hash)
end

Instance Attribute Details

#data_typesObject

Returns the value of attribute data_types.



12
13
14
# File 'lib/custom_elements_manifest_parser/parser.rb', line 12

def data_types
  @data_types
end

#manifestObject

Returns the value of attribute manifest.



12
13
14
# File 'lib/custom_elements_manifest_parser/parser.rb', line 12

def manifest
  @manifest
end

#visitable_nodesObject

Returns the value of attribute visitable_nodes.



12
13
14
# File 'lib/custom_elements_manifest_parser/parser.rb', line 12

def visitable_nodes
  @visitable_nodes
end

Instance Method Details

#find_all_tag_namesHash{String => Nodes::ClassDeclaration}

Returns - Returns a hash keyed off of found tagNames.

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/custom_elements_manifest_parser/parser.rb', line 127

def find_all_tag_names
  custom_elements = {}

  manifest.modules.flatten.each do |mod|
    mod.declarations.flatten.each do |dec|
      # Needs to be != true because == false fails nil checks.
      next if dec.attributes[:customElement] != true

      tag_name = dec.attributes[:tagName]

      custom_elements[tag_name] = dec if tag_name
    end
  end

  custom_elements
end

#find_by_tag_names(*tag_names) ⇒ Hash{String => Nodes::ClassDeclaration}

Returns - Returns a hash keyed off of found tagNames.

Parameters:

  • tag_names (Array<String>)
    • An array of tag names to parse through

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/custom_elements_manifest_parser/parser.rb', line 106

def find_by_tag_names(*tag_names)
  custom_elements = {}

  tag_names = tag_names.flatten

  manifest.modules.flatten.each do |mod|
    mod.declarations.flatten.each do |dec|
      # Needs to be != true because == false fails nil checks.
      next if dec.attributes[:customElement] != true

      tag_name = dec.attributes[:tagName]
      next if tag_names.include?(tag_name) == false

      custom_elements[tag_name] = dec
    end
  end

  custom_elements
end

#find_custom_elementsArray<ClassDeclaration>

Returns - Returns an array of Nodes::ClassDeclarations that describe the customElement.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/custom_elements_manifest_parser/parser.rb', line 90

def find_custom_elements
  custom_elements = []

  manifest.modules.flatten.each do |mod|
    mod.declarations.flatten.each do |dec|
      next if dec.attributes[:customElement] != true

      custom_elements << dec
    end
  end

  custom_elements
end

#parseParser

Builds the fully parsed tree

Returns:



61
62
63
64
# File 'lib/custom_elements_manifest_parser/parser.rb', line 61

def parse
  @manifest = manifest.visit(parser: self)
  self
end

#visit_node(node) ⇒ Object

def array_fields [ "declarations", "exports", "members", "mixins", "attributes", "events", "slots", "cssParts", "cssProperties", "demos", "parameters", "references", "modules", ] end



84
85
86
87
# File 'lib/custom_elements_manifest_parser/parser.rb', line 84

def visit_node(node)
  kind = node[:kind] || node["kind"]
  @visitable_nodes[kind].new(node).visit(parser: self)
end