oxc 0.1.0-arm-linux-gnu → 0.149.0-arm-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7003799f248bc17853e84b4573f6666e47ff9a26e705617e2ea0dce66f0e372f
4
- data.tar.gz: 89a4facbe706ded06469348779f2996e9edfe0cb6bf63121ede39524b169307a
3
+ metadata.gz: bbcb94b81fe82d086d425ceaca0c7fc0c54ba0177b22537be764fec951410bb0
4
+ data.tar.gz: 8ff558d7b1d18b91324de7acfdd45c6907ee61cb0b1e00a424fab743394b8e60
5
5
  SHA512:
6
- metadata.gz: cd218b1db0f4bb52bc213a9a21751e460f825615ad30e1a1b245ffd2f0fa31eb020cfc4216e162f5bc07c733f22a3507342fb9a96f8be1cea67c072c6114cfd4
7
- data.tar.gz: f83c40bb390e93baf83d5169528bd460b94f58aca9ec94be42f4f5c03cc30d6647fc0793e780973572f5a8f7b1b0adfcd0418ececda7044e4c2f339de0c4d42d
6
+ metadata.gz: fe0b453313f7a14b60fa4697ac1503ef3c1ea6ec15d12cd92e5a9ee2a5f4554ccf224d52c90a81839c14e98cd0fd96ff12f38c1f840670ff55aca578fe58eec2
7
+ data.tar.gz: 2378b04f48856ec7e25e6ebc09c906a73e9a57ed5a5fa1095e79a0228737fe756a328d628bd14d72737d34b7040251fe1166cb5cecea1667bd3c21064b67d821
data/README.md CHANGED
@@ -27,6 +27,14 @@ bundle add oxc
27
27
 
28
28
  Anywhere a precompiled gem is not published, the gem builds from source and needs the [Rust toolchain](https://rustup.rs) at 1.96 or newer.
29
29
 
30
+ ### Versioning
31
+
32
+ The gem carries the version of oxc it packages. Gem v0.149.0 bundles oxc v0.149.0, and `Oxc.oxc_version` reports what a given build was compiled against.
33
+
34
+ So the version tracks oxc, not the Ruby API here. A minor bump is a minor oxc release, and says nothing about whether this gem's own API moved. Pin on the oxc version you want.
35
+
36
+ If the gem needs releasing again for the same oxc version, that release adds a fourth digit, as in v0.149.0.1.
37
+
30
38
  ### Usage
31
39
 
32
40
  #### Minifying
@@ -205,6 +213,171 @@ Oxc.parse("let a; let a;", semantic_errors: true).errors.map(&:message)
205
213
  #=> ["Identifier `a` has already been declared"]
206
214
  ```
207
215
 
216
+ #### Walking the AST
217
+
218
+ `root` answers the program as an `Oxc::Node`, which walks, reads its fields by name, and knows what it sits inside.
219
+
220
+ ```ruby
221
+ root = Oxc.parse(source).root
222
+
223
+ root.type #=> "Program"
224
+ root.keys #=> the ESTree fields this node carries
225
+ root.fields #=> those fields and their values, without the span
226
+ root.child_nodes #=> the nodes directly under it
227
+ root.every("Identifier") #=> every identifier in the file
228
+ root.at(offset) #=> the innermost node covering a byte offset
229
+ root.each #=> an Enumerator over every node
230
+ ```
231
+
232
+ Inspecting a node shows every field it carries, so there is always something to reach for next.
233
+
234
+ ```
235
+ #<Oxc::Node VariableDeclaration range=[0, 13] kind="let" declarations=[... 1 item]>
236
+ #<Oxc::Node VariableDeclarator range=[4, 13] id=#<Oxc::Node Identifier> init=#<Oxc::Node Literal>>
237
+ #<Oxc::Node Identifier range=[4, 9] name="count">
238
+ ```
239
+
240
+ Every field is there, so what `inspect` prints and what `keys` answers never disagree. A field holding a node prints as that node's type, one holding a list prints how many it holds, and one holding nothing prints the `nil`, the `false` or the `[]` it holds.
241
+
242
+ An ESTree field always wins over a method of the gem's own, since `name`, `attributes` and `children` are all real fields. `Identifier#name` is the identifier's name, `JSXElement#children` is what the element wraps, and `ImportDeclaration#attributes` is the import's `with` clause. The walker spells its own versions `underscored_type`, `to_h` and `child_nodes`, which no ESTree field can be called.
243
+
244
+ A field comes back as a node when it holds one, so reads chain.
245
+
246
+ ```ruby
247
+ declaration = root.child_nodes.first
248
+
249
+ declaration.kind
250
+ #=> "let"
251
+
252
+ declaration.declarations.first.id.name
253
+ #=> "count"
254
+ ```
255
+
256
+ ESTree names its fields in camelCase, and a field answers to its snake_case name too, so reading an AST does not mean writing JavaScript casing in Ruby.
257
+
258
+ ```ruby
259
+ node.type_annotation # the same field as node.typeAnnotation
260
+ node.super_class # superClass
261
+ root.source_type # sourceType
262
+ ```
263
+
264
+ Patterns take either name as well, binding what you asked for.
265
+
266
+ ```ruby
267
+ node => { type_annotation: { type: }, readonly: }
268
+ ```
269
+
270
+ `ancestors` is what a rewrite needs, since a reference sits inside the expression that has to be replaced.
271
+
272
+ ```ruby
273
+ reference = root.at(source.index("count +="))
274
+ reference.ancestors.find { |node| node.type == "AssignmentExpression" }.slice
275
+ #=> "count += 1"
276
+ ```
277
+
278
+ A parse result keeps the source it read and hands it down to every node it builds, so `slice` answers with no argument at all.
279
+
280
+ ```ruby
281
+ parsed = Oxc.parse(source)
282
+
283
+ parsed.source
284
+ #=> "let count = 0\nfunction bump() { count += 1; render(count) }"
285
+
286
+ parsed.root.every("FunctionDeclaration").first.slice
287
+ #=> "function bump() { count += 1; render(count) }"
288
+ ```
289
+
290
+ It still takes one, for a node assembled by hand or read against a different string.
291
+
292
+ ```ruby
293
+ node.slice(other_source)
294
+ ```
295
+
296
+ Nodes pattern match, and nest, since a field holding a node comes back as one.
297
+
298
+ ```ruby
299
+ node => { type: "VariableDeclarator", id: { name: }, init: { value: } }
300
+ name #=> "count"
301
+ value #=> 0
302
+
303
+ root.select { |node| node in { type: "FunctionDeclaration", id: { name: /^handle/ } } }
304
+ ```
305
+
306
+ `deconstruct_keys` is the whole protocol here. There is no `deconstruct`, since `each` yields every descendant and an array pattern over direct children would disagree with `to_a`.
307
+
308
+ `to_h` and `to_json` answer the ESTree the node wraps, which is what a snapshot test or a dump to another tool wants.
309
+
310
+ ```ruby
311
+ node.to_h
312
+ #=> { "type" => "Identifier", "name" => "count", "start" => 4, "end" => 9 }
313
+
314
+ node.to_json
315
+ #=> "{\"type\":\"Identifier\",\"name\":\"count\",\"start\":4,\"end\":9}"
316
+ ```
317
+
318
+ `Oxc::Visitor` answers a node with the method named after its type, and walks through anything nothing answers.
319
+
320
+ ```ruby
321
+ class Reads < Oxc::Visitor
322
+ def visit_assignment_expression(node)
323
+ puts "#{node.left["name"]} #{node.operator}"
324
+
325
+ visit_children(node)
326
+ end
327
+ end
328
+
329
+ Reads.new.visit(Oxc.parse(source))
330
+ ```
331
+
332
+ It takes a parse result or a node, so the common case needs no `root`. A result with no AST is nothing to walk and visits nothing.
333
+
334
+ There is one node class, not one per type, so a type the gem has never seen still walks and still answers. The types and their fields are [ESTree](https://github.com/estree/estree). For the TypeScript and JSX nodes, which ESTree does not cover, oxc publishes the exact shapes it emits as [`@oxc-project/types`](https://www.npmjs.com/package/@oxc-project/types).
335
+
336
+ #### Rewriting
337
+
338
+ `Oxc::MutationVisitor` records what to do to a node and splices the original text at the end, so everything it did not touch survives byte for byte, comments and indentation included.
339
+
340
+ ```ruby
341
+ class Renamer < Oxc::MutationVisitor
342
+ def visit_identifier(node)
343
+ replace(node, "renamed") if node["name"] == "count"
344
+ end
345
+ end
346
+
347
+ Renamer.new.rewrite("let count = 1 // keep me")
348
+ #=> "let renamed = 1 // keep me"
349
+ ```
350
+
351
+ `replace`, `remove`, `insert_before`, `insert_after` and `wrap` are the operations, and each takes a node. Spans are exact, so removing `debugger;` removes what the node covered and leaves the newline after it alone.
352
+
353
+ Walking into a node that was replaced would edit text that is no longer there, so it stops. Two edits over the same span raise `Oxc::MutationVisitor::Overlap` instead of quietly producing something broken.
354
+
355
+ What goes in is text, so a node can become anything, including several statements or nothing at all. Nothing checks it on the way, so the result is read back afterwards and refused if it stopped being JavaScript.
356
+
357
+ ```ruby
358
+ Breaker.new.rewrite("foo(data)")
359
+ #=> Oxc::MutationVisitor::Invalid: what was rewritten no longer reads as JavaScript: Unexpected token
360
+ ```
361
+
362
+ Pass `verify: false` for a fragment that was never going to parse on its own.
363
+
364
+ `parsed` reaches what the source parsed to, so a rewrite can ask for symbols and drive from them. That is the difference between rewriting a name and rewriting the right one.
365
+
366
+ ```ruby
367
+ class ToState < Oxc::MutationVisitor
368
+ def rewrite(source) = super(source, symbols: true)
369
+
370
+ def visit_identifier(node)
371
+ reference = parsed.symbols.fetch("declared").flat_map { |symbol| symbol["references"] }
372
+ .find { |found| found["start"] == node.start }
373
+
374
+ replace(node, %(state.get("#{node["name"]}"))) if reference && !reference["write"]
375
+ end
376
+ end
377
+ ```
378
+
379
+ Drive from references, not from every `Identifier`. That is what keeps a declaration, a shadowed local, and a same-named property out of the rewrite.
380
+
208
381
  #### What a file declared, and what it only used
209
382
 
210
383
  `symbols: true` answers every binding with the span it was declared at and the spans of every reference to it, plus the names the file used without declaring.
data/ext/oxc/extconf.rb CHANGED
@@ -65,12 +65,12 @@ if target_platform
65
65
 
66
66
  system("rustup target add #{target_platform}") || warn("oxc: Failed to add Rust target #{target_platform}")
67
67
 
68
- cargo_args = "--release --target #{target_platform}"
68
+ cargo_args = "--release --locked --target #{target_platform}"
69
69
  lib_dir = File.join(target_dir, target_platform, "release")
70
70
  else
71
71
  puts "oxc: Compiling Rust library for native platform..."
72
72
 
73
- cargo_args = "--release"
73
+ cargo_args = "--release --locked"
74
74
  lib_dir = File.join(target_dir, "release")
75
75
  end
76
76
 
data/lib/oxc/3.2/oxc.so CHANGED
Binary file
data/lib/oxc/3.3/oxc.so CHANGED
Binary file
data/lib/oxc/3.4/oxc.so CHANGED
Binary file
data/lib/oxc/4.0/oxc.so CHANGED
Binary file
@@ -80,7 +80,7 @@ module Oxc
80
80
 
81
81
  #: () -> String
82
82
  def inspect
83
- "#<#{self.class.name} #{start}..#{finish}#{" #{message.inspect}" if message}>"
83
+ "#<#{self.class.name} range=[#{start}, #{finish}]#{" #{message.inspect}" if message}>"
84
84
  end
85
85
  end
86
86
  end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oxc
4
+ # A visitor that rewrites the source it walked, by recording what to do to a node and splicing the
5
+ # original text at the end. Everything nothing touched survives byte for byte.
6
+ #
7
+ # class Renamer < Oxc::MutationVisitor
8
+ # def visit_identifier(node)
9
+ # replace(node, "renamed") if node["name"] == "count"
10
+ # end
11
+ # end
12
+ #
13
+ # Renamer.new.rewrite("let count = 1") #=> "let renamed = 1"
14
+ #
15
+ class MutationVisitor < Visitor
16
+ class Overlap < StandardError; end
17
+ class Invalid < Error; end
18
+
19
+ Edit = Data.define(
20
+ :start, #: Integer
21
+ :finish, #: Integer
22
+ :text, #: String
23
+ :order #: Integer
24
+ )
25
+
26
+ attr_reader :source #: String
27
+ attr_reader :parsed #: Oxc::ParseResult
28
+
29
+ #: (String, ?verify: bool, **untyped) -> String
30
+ def rewrite(source, verify: true, **options)
31
+ @source = source
32
+ @edits = [] #: Array[Edit]
33
+ @replaced = [] #: Array[[Integer, Integer]]
34
+ @parsed = Oxc.parse(source, **options).validate!
35
+
36
+ visit(parsed)
37
+
38
+ rewritten = apply
39
+
40
+ verify ? verified(rewritten, options) : rewritten
41
+ end
42
+
43
+ #: (Oxc::Node, String) -> void
44
+ def replace(node, text)
45
+ @replaced << [node.start, node.finish]
46
+
47
+ edit(node.start, node.finish, text)
48
+ end
49
+
50
+ #: (Oxc::Node) -> void
51
+ def remove(node)
52
+ replace(node, "")
53
+ end
54
+
55
+ #: (Oxc::Node, String) -> void
56
+ def insert_before(node, text)
57
+ edit(node.start, node.start, text)
58
+ end
59
+
60
+ #: (Oxc::Node, String) -> void
61
+ def insert_after(node, text)
62
+ edit(node.finish, node.finish, text)
63
+ end
64
+
65
+ #: (Oxc::Node, String, String) -> void
66
+ def wrap(node, before, after)
67
+ insert_before(node, before)
68
+ insert_after(node, after)
69
+ end
70
+
71
+ #: (Oxc::Node) -> void
72
+ def visit_children(node)
73
+ return nil if replaced?(node)
74
+
75
+ super
76
+ end
77
+
78
+ private
79
+
80
+ #: (String, Hash[Symbol, untyped]) -> String
81
+ def verified(rewritten, options)
82
+ answer = Oxc.parse(rewritten, **options, ast: false)
83
+
84
+ return rewritten unless answer.errors?
85
+
86
+ raise Invalid, "what was rewritten no longer reads as JavaScript: #{answer.errors.first&.message}"
87
+ end
88
+
89
+ #: (Oxc::Node) -> bool
90
+ def replaced?(node)
91
+ @replaced.any? { |start, finish| node.start >= start && node.finish <= finish }
92
+ end
93
+
94
+ #: (Integer, Integer, String) -> void
95
+ def edit(start, finish, text)
96
+ @edits.each do |existing|
97
+ next unless overlaps?(existing, start, finish)
98
+
99
+ raise Overlap, "an edit at #{start}..#{finish} overlaps one at #{existing.start}..#{existing.finish}"
100
+ end
101
+
102
+ @edits << Edit.new(start: start, finish: finish, text: text, order: @edits.length)
103
+
104
+ nil
105
+ end
106
+
107
+ #: (Edit, Integer, Integer) -> bool
108
+ def overlaps?(existing, start, finish)
109
+ return false if existing.start == existing.finish && start == finish
110
+
111
+ start < existing.finish && existing.start < finish
112
+ end
113
+
114
+ #: () -> String
115
+ def apply
116
+ taken = 0
117
+ result = +""
118
+
119
+ @edits.sort_by { |edit| [edit.start, edit.order] }.each do |edit|
120
+ result << source.byteslice(taken, edit.start - taken).to_s << edit.text
121
+
122
+ taken = edit.finish
123
+ end
124
+
125
+ result << source.byteslice(taken..).to_s
126
+ end
127
+ end
128
+ end
data/lib/oxc/node.rb ADDED
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oxc
4
+ class Node
5
+ include Enumerable #[Oxc::Node]
6
+
7
+ ACRONYM = /([A-Z]+)([A-Z][a-z])/ #: Regexp
8
+ BOUNDARY = /([a-z\d])([A-Z])/ #: Regexp
9
+ SPAN = ["type", "start", "end"].freeze #: Array[String]
10
+
11
+ attr_reader :parent #: Oxc::Node?
12
+
13
+ protected
14
+
15
+ attr_reader :attributes #: Hash[String, untyped]
16
+ attr_reader :text #: String?
17
+
18
+ public
19
+
20
+ #: (Hash[String, untyped], ?Oxc::Node?, ?String?) -> void
21
+ def initialize(attributes, parent = nil, text = nil)
22
+ @attributes = attributes
23
+ @parent = parent
24
+ @text = text || parent&.text
25
+ end
26
+
27
+ #: () -> String
28
+ def type
29
+ attributes.fetch("type")
30
+ end
31
+
32
+ #: () -> Integer
33
+ def start
34
+ attributes.fetch("start")
35
+ end
36
+
37
+ #: () -> Integer
38
+ def finish
39
+ attributes.fetch("end")
40
+ end
41
+
42
+ #: () -> String
43
+ def underscored_type
44
+ @underscored_type ||= type.gsub(ACRONYM, '\1_\2').gsub(BOUNDARY, '\1_\2').downcase
45
+ end
46
+
47
+ #: (String) -> untyped
48
+ def [](key)
49
+ attributes[key]
50
+ end
51
+
52
+ #: (?String?) -> String?
53
+ def slice(from = text)
54
+ raise ArgumentError, "this node does not know its source, so #slice needs it" unless from
55
+
56
+ from.byteslice(start, finish - start)
57
+ end
58
+
59
+ #: () -> Array[String]
60
+ def keys
61
+ attributes.keys
62
+ end
63
+
64
+ #: () -> Hash[String, untyped]
65
+ def to_h
66
+ attributes
67
+ end
68
+
69
+ #: (?untyped) -> String
70
+ def to_json(state = nil)
71
+ state ? attributes.to_json(state) : attributes.to_json
72
+ end
73
+
74
+ #: (Array[Symbol]?) -> Hash[Symbol, untyped]
75
+ def deconstruct_keys(keys)
76
+ found = {} #: Hash[Symbol, untyped]
77
+
78
+ if keys
79
+ keys.each do |name|
80
+ key = field_for(name.to_s)
81
+
82
+ found[name] = wrap(attributes[key]) if key
83
+ end
84
+ else
85
+ attributes.each_key { |key| found[key.to_sym] = wrap(attributes[key]) }
86
+ end
87
+
88
+ found
89
+ end
90
+
91
+ #: () -> Array[Oxc::Node]
92
+ def child_nodes
93
+ @child_nodes ||= attributes.each_value
94
+ .flat_map { |value| value.is_a?(Array) ? value : [value] }
95
+ .select { |value| value.is_a?(Hash) && value.key?("type") }
96
+ .map { |value| Node.new(value, self) }
97
+ .freeze
98
+ end
99
+
100
+ #: () { (Oxc::Node) -> void } -> void
101
+ #: () -> Enumerator[Oxc::Node, void]
102
+ def each(&)
103
+ return enum_for(:each) unless block_given?
104
+
105
+ yield self
106
+
107
+ child_nodes.each { |child| child.each(&) }
108
+ end
109
+
110
+ #: () -> Array[Oxc::Node]
111
+ def ancestors
112
+ parent ? [parent, *parent.ancestors] : []
113
+ end
114
+
115
+ #: (Integer) -> Oxc::Node?
116
+ def at(offset)
117
+ covering = each.select { |node| offset >= node.start && offset < node.finish }
118
+
119
+ covering.min_by { |node| node.finish - node.start }
120
+ end
121
+
122
+ #: (String) -> Array[Oxc::Node]
123
+ def every(type)
124
+ each.select { |node| node.type == type }
125
+ end
126
+
127
+ #: () -> Hash[String, untyped]
128
+ def fields
129
+ attributes.except(*SPAN)
130
+ end
131
+
132
+ #: () -> String
133
+ def inspect
134
+ described = fields.map { |key, value| described_field(key, value) }
135
+
136
+ "#<#{self.class.name} #{type} range=[#{start}, #{finish}]#{" #{described.join(" ")}" unless described.empty?}>"
137
+ end
138
+
139
+ #: (Symbol, *untyped) -> untyped
140
+ def method_missing(name, *arguments)
141
+ key = field_for(name.to_s)
142
+
143
+ return super unless key
144
+
145
+ wrap(attributes[key])
146
+ end
147
+
148
+ #: (Symbol, ?bool) -> bool
149
+ def respond_to_missing?(name, include_private = false)
150
+ !field_for(name.to_s).nil? || super
151
+ end
152
+
153
+ private
154
+
155
+ #: (String, untyped) -> String
156
+ def described_field(key, value)
157
+ case value
158
+ when Array then "#{key}=#{if value.empty?
159
+ "[]"
160
+ else
161
+ "[... #{value.length} #{value.length == 1 ? "item" : "items"}]"
162
+ end}"
163
+ when Hash then "#{key}=#<#{self.class.name} #{value["type"]}>"
164
+ else "#{key}=#{value.inspect}"
165
+ end
166
+ end
167
+
168
+ #: (String) -> String?
169
+ def field_for(name)
170
+ return name if attributes.key?(name)
171
+
172
+ camelized = name.gsub(/_([a-z\d])/) { Regexp.last_match(1).to_s.upcase }
173
+
174
+ camelized if attributes.key?(camelized)
175
+ end
176
+
177
+ #: (untyped) -> untyped
178
+ def wrap(value)
179
+ case value
180
+ when Hash then value.key?("type") ? Node.new(value, self, text) : value
181
+ when Array then value.map { |item| wrap(item) }
182
+ else value
183
+ end
184
+ end
185
+ end
186
+ end
data/lib/oxc/options.rb CHANGED
@@ -72,9 +72,9 @@ module Oxc
72
72
  freeze
73
73
  end
74
74
 
75
- #: (?untyped) -> String
75
+ #: (?JSON::options?) -> String
76
76
  def to_json(state = nil)
77
- JSON.generate(to_h, state)
77
+ state ? JSON.generate(to_h, state) : JSON.generate(to_h)
78
78
  end
79
79
 
80
80
  #: () -> String
@@ -4,17 +4,19 @@ module Oxc
4
4
  class ParseResult
5
5
  include Diagnosed
6
6
 
7
+ attr_reader :source #: String?
7
8
  attr_reader :program #: Hash[String, untyped]?
8
9
  attr_reader :module_record #: Hash[String, untyped]?
9
10
  attr_reader :symbols #: Hash[String, untyped]?
10
11
  attr_reader :comments #: Array[Oxc::Comment]
11
12
  attr_reader :diagnostics #: Array[Oxc::Diagnostic]
12
13
 
13
- #: (String) -> Oxc::ParseResult
14
- def self.from_json(payload)
14
+ #: (String, ?String?) -> Oxc::ParseResult
15
+ def self.from_json(payload, source = nil)
15
16
  parsed = JSON.parse(payload)
16
17
 
17
18
  new(
19
+ source: source,
18
20
  program: parsed["program"],
19
21
  module_record: parsed["module_record"],
20
22
  symbols: parsed["symbols"],
@@ -24,8 +26,9 @@ module Oxc
24
26
  )
25
27
  end
26
28
 
27
- #: (comments: Array[Oxc::Comment], diagnostics: Array[Oxc::Diagnostic], ?program: Hash[String, untyped]?, ?module_record: Hash[String, untyped]?, ?symbols: Hash[String, untyped]?, ?panicked: bool) -> void
28
- def initialize(comments:, diagnostics:, program: nil, module_record: nil, symbols: nil, panicked: false)
29
+ #: (comments: Array[Oxc::Comment], diagnostics: Array[Oxc::Diagnostic], ?source: String?, ?program: Hash[String, untyped]?, ?module_record: Hash[String, untyped]?, ?symbols: Hash[String, untyped]?, ?panicked: bool) -> void
30
+ def initialize(comments:, diagnostics:, source: nil, program: nil, module_record: nil, symbols: nil, panicked: false)
31
+ @source = source
29
32
  @program = program.freeze
30
33
  @module_record = module_record.freeze
31
34
  @symbols = symbols.freeze
@@ -36,6 +39,11 @@ module Oxc
36
39
  freeze
37
40
  end
38
41
 
42
+ #: () -> Oxc::Node?
43
+ def root
44
+ program ? Node.new(program, nil, source) : nil
45
+ end
46
+
39
47
  #: () -> Oxc::ParseResult
40
48
  def validate!
41
49
  return self unless errors? || panicked?
@@ -46,7 +54,9 @@ module Oxc
46
54
  #: () -> String
47
55
  def inspect
48
56
  parts = [] #: Array[String]
49
- parts << "program=#{program["type"]}" if program
57
+ parts << "#{root&.type} range=[#{root&.start}, #{root&.finish}]" if program
58
+ parts << "module_record" if module_record
59
+ parts << "symbols=#{symbols.fetch("declared").length}" if symbols
50
60
  parts << "comments=#{comments.length}" unless comments.empty?
51
61
  parts << "diagnostics=#{diagnostics.length}" unless diagnostics.empty?
52
62
 
data/lib/oxc/result.rb CHANGED
@@ -18,7 +18,7 @@ module Oxc
18
18
  @panicked = panicked
19
19
  end
20
20
 
21
- #: (?strict: untyped) -> self
21
+ #: (?strict: bool?) -> self
22
22
  def validate!(strict: false)
23
23
  return self unless errors? || panicked?
24
24
  return self unless strict || panicked? || code.empty?
data/lib/oxc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Oxc
4
- VERSION = "0.1.0"
4
+ VERSION = "0.149.0"
5
5
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oxc
4
+ class Visitor
5
+ #: (Oxc::Node | Oxc::ParseResult) -> void
6
+ def visit(node)
7
+ node = node.root if node.is_a?(ParseResult)
8
+
9
+ return nil unless node
10
+
11
+ answer = "visit_#{node.underscored_type}"
12
+
13
+ respond_to?(answer) ? public_send(answer, node) : visit_children(node)
14
+
15
+ nil
16
+ end
17
+
18
+ #: (Oxc::Node) -> void
19
+ def visit_children(node)
20
+ node.child_nodes.each { |child| visit(child) }
21
+
22
+ nil
23
+ end
24
+ end
25
+ end