|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -require "simplecov" |
4 |
| -SimpleCov.start do |
5 |
| - add_filter("idempotency_test.rb") unless ENV["CI"] |
6 |
| - add_group("lib", "lib") |
7 |
| - add_group("test", "test") |
| 3 | +unless RUBY_ENGINE == "truffleruby" |
| 4 | + require "simplecov" |
| 5 | + SimpleCov.start do |
| 6 | + add_filter("idempotency_test.rb") unless ENV["CI"] |
| 7 | + add_group("lib", "lib") |
| 8 | + add_group("test", "test") |
| 9 | + end |
8 | 10 | end
|
9 | 11 |
|
10 | 12 | $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
11 | 13 | require "syntax_tree"
|
12 | 14 | require "syntax_tree/cli"
|
13 | 15 |
|
14 |
| -# Here we are going to establish type verification whenever a new node is |
15 |
| -# created. We do this through the reflection module, which in turn parses the |
16 |
| -# source code of the node classes. |
17 |
| -require "syntax_tree/reflection" |
18 |
| -SyntaxTree::Reflection.nodes.each do |name, node| |
19 |
| - next if name == :Statements |
20 |
| - |
21 |
| - clazz = SyntaxTree.const_get(name) |
22 |
| - parameters = clazz.instance_method(:initialize).parameters |
23 |
| - |
24 |
| - # First, verify that all of the parameters listed in the list of attributes. |
25 |
| - # If there are any parameters that aren't listed in the attributes, then |
26 |
| - # something went wrong with the parsing in the reflection module. |
27 |
| - raise unless (parameters.map(&:last) - node.attributes.keys).empty? |
28 |
| - |
29 |
| - # Now we're going to use an alias chain to redefine the initialize method to |
30 |
| - # include type checking. |
31 |
| - clazz.alias_method(:initialize_without_verify, :initialize) |
32 |
| - clazz.define_method(:initialize) do |**kwargs| |
33 |
| - kwargs.each do |kwarg, value| |
34 |
| - attribute = node.attributes.fetch(kwarg) |
35 |
| - |
36 |
| - unless attribute.type === value |
37 |
| - raise TypeError, |
38 |
| - "invalid type for #{name}##{kwarg}, expected " \ |
39 |
| - "#{attribute.type.inspect}, got #{value.inspect}" |
| 16 | +unless RUBY_ENGINE == "truffleruby" |
| 17 | + # Here we are going to establish type verification whenever a new node is |
| 18 | + # created. We do this through the reflection module, which in turn parses the |
| 19 | + # source code of the node classes. |
| 20 | + require "syntax_tree/reflection" |
| 21 | + SyntaxTree::Reflection.nodes.each do |name, node| |
| 22 | + next if name == :Statements |
| 23 | + |
| 24 | + clazz = SyntaxTree.const_get(name) |
| 25 | + parameters = clazz.instance_method(:initialize).parameters |
| 26 | + |
| 27 | + # First, verify that all of the parameters listed in the list of attributes. |
| 28 | + # If there are any parameters that aren't listed in the attributes, then |
| 29 | + # something went wrong with the parsing in the reflection module. |
| 30 | + raise unless (parameters.map(&:last) - node.attributes.keys).empty? |
| 31 | + |
| 32 | + # Now we're going to use an alias chain to redefine the initialize method to |
| 33 | + # include type checking. |
| 34 | + clazz.alias_method(:initialize_without_verify, :initialize) |
| 35 | + clazz.define_method(:initialize) do |**kwargs| |
| 36 | + kwargs.each do |kwarg, value| |
| 37 | + attribute = node.attributes.fetch(kwarg) |
| 38 | + |
| 39 | + unless attribute.type === value |
| 40 | + raise TypeError, |
| 41 | + "invalid type for #{name}##{kwarg}, expected " \ |
| 42 | + "#{attribute.type.inspect}, got #{value.inspect}" |
| 43 | + end |
40 | 44 | end
|
41 |
| - end |
42 | 45 |
|
43 |
| - initialize_without_verify(**kwargs) |
| 46 | + initialize_without_verify(**kwargs) |
| 47 | + end |
44 | 48 | end
|
45 | 49 | end
|
46 | 50 |
|
|
0 commit comments