opal-zeitwerk 0.4.5 → 0.4.6

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.
@@ -1,117 +1,117 @@
1
- # backtick_javascript: true
2
-
3
- module Zeitwerk::Loader::Helpers
4
- private
5
-
6
- # --- Files and directories ---------------------------------------------------------------------
7
-
8
- # @sig (String) { (String, String) -> void } -> void
9
- def ls(dir)
10
- outer_ls = false
11
- # cache the Opal.modules keys array for subsequent ls calls during setup
12
- %x{
13
- if (#@module_paths === nil) {
14
- #@module_paths = Object.keys(Opal.modules);
15
- outer_ls = true;
16
- }
17
- }
18
- visited_abspaths = `{}`
19
- dir_first_char = dir[0]
20
- path_start = dir.size + 1
21
- path_parts = `[]`
22
- basename = ''
23
- @module_paths.each do |abspath|
24
- %x{
25
- if (abspath[0] === dir_first_char) {
26
- if (!abspath.startsWith(dir)) { #{next} }
27
- path_parts = abspath.slice(path_start).split('/');
28
- basename = path_parts[0];
29
- abspath = dir + '/' + basename;
30
- if (visited_abspaths.hasOwnProperty(abspath)) { #{next} }
31
- visited_abspaths[abspath] = true;
32
- #{yield basename, abspath unless ignored_paths.member?(abspath)}
33
- }
34
- }
35
- end
36
- # remove cache, because Opal.modules may change after setup
37
- %x{
38
- if (outer_ls) { #@module_paths = nil }
39
- }
40
- end
41
-
42
- # @sig (String) -> bool
43
- def ruby?(abspath)
44
- `Opal.modules.hasOwnProperty(abspath)`
45
- end
46
-
47
- # @sig (String) -> bool
48
- def dir?(path)
49
- dir_path = path + '/'
50
- module_paths = if @module_paths # possibly set by ls
51
- @module_paths
52
- else
53
- `Object.keys(Opal.modules)`
54
- end
55
- path_first = `path[0]`
56
- module_paths.each do |m_path|
57
- %x{
58
- if (m_path[0] !== path_first) { #{ next } }
59
- if (m_path.startsWith(dir_path)) { #{return true} }
60
- }
61
- end
62
- false
63
- end
64
-
65
- # --- Constants ---------------------------------------------------------------------------------
66
-
67
- # The autoload? predicate takes into account the ancestor chain of the
68
- # receiver, like const_defined? and other methods in the constants API do.
69
- #
70
- # For example, given
71
- #
72
- # class A
73
- # autoload :X, "x.rb"
74
- # end
75
- #
76
- # class B < A
77
- # end
78
- #
79
- # B.autoload?(:X) returns "x.rb".
80
- #
81
- # We need a way to strictly check in parent ignoring ancestors.
82
- #
83
- # @sig (Module, Symbol) -> String?
84
- if method(:autoload?).arity == 1
85
- def strict_autoload_path(parent, cname)
86
- parent.autoload?(cname) if cdef?(parent, cname)
87
- end
88
- else
89
- def strict_autoload_path(parent, cname)
90
- parent.autoload?(cname, false)
91
- end
92
- end
93
-
94
- # @sig (Module, Symbol) -> String
95
- if Symbol.method_defined?(:name)
96
- # Symbol#name was introduced in Ruby 3.0. It returns always the same
97
- # frozen object, so we may save a few string allocations.
98
- def cpath(parent, cname)
99
- Object == parent ? cname.name : "#{real_mod_name(parent)}::#{cname.name}"
100
- end
101
- else
102
- def cpath(parent, cname)
103
- Object == parent ? cname.to_s : "#{real_mod_name(parent)}::#{cname}"
104
- end
105
- end
106
-
107
- # @sig (Module, Symbol) -> bool
108
- def cdef?(parent, cname)
109
- parent.const_defined?(cname, false)
110
- end
111
-
112
- # @raise [NameError]
113
- # @sig (Module, Symbol) -> Object
114
- def cget(parent, cname)
115
- parent.const_get(cname, false)
116
- end
117
- end
1
+ # backtick_javascript: true
2
+
3
+ module Zeitwerk::Loader::Helpers
4
+ private
5
+
6
+ # --- Files and directories ---------------------------------------------------------------------
7
+
8
+ # @sig (String) { (String, String) -> void } -> void
9
+ def ls(dir)
10
+ outer_ls = false
11
+ # cache the Opal.modules keys array for subsequent ls calls during setup
12
+ %x{
13
+ if (#@module_paths === nil) {
14
+ #@module_paths = Object.keys(Opal.modules);
15
+ outer_ls = true;
16
+ }
17
+ }
18
+ visited_abspaths = `{}`
19
+ dir_first_char = dir[0]
20
+ path_start = dir.size + 1
21
+ path_parts = `[]`
22
+ basename = ''
23
+ @module_paths.each do |abspath|
24
+ %x{
25
+ if (abspath[0] === dir_first_char) {
26
+ if (!abspath.startsWith(dir)) { #{next} }
27
+ path_parts = abspath.slice(path_start).split('/');
28
+ basename = path_parts[0];
29
+ abspath = dir + '/' + basename;
30
+ if (visited_abspaths.hasOwnProperty(abspath)) { #{next} }
31
+ visited_abspaths[abspath] = true;
32
+ #{yield basename, abspath unless ignored_paths.member?(abspath)}
33
+ }
34
+ }
35
+ end
36
+ # remove cache, because Opal.modules may change after setup
37
+ %x{
38
+ if (outer_ls) { #@module_paths = nil }
39
+ }
40
+ end
41
+
42
+ # @sig (String) -> bool
43
+ def ruby?(abspath)
44
+ `Opal.modules.hasOwnProperty(abspath)`
45
+ end
46
+
47
+ # @sig (String) -> bool
48
+ def dir?(path)
49
+ dir_path = path + '/'
50
+ module_paths = if @module_paths # possibly set by ls
51
+ @module_paths
52
+ else
53
+ `Object.keys(Opal.modules)`
54
+ end
55
+ path_first = `path[0]`
56
+ module_paths.each do |m_path|
57
+ %x{
58
+ if (m_path[0] !== path_first) { #{ next } }
59
+ if (m_path.startsWith(dir_path)) { #{return true} }
60
+ }
61
+ end
62
+ false
63
+ end
64
+
65
+ # --- Constants ---------------------------------------------------------------------------------
66
+
67
+ # The autoload? predicate takes into account the ancestor chain of the
68
+ # receiver, like const_defined? and other methods in the constants API do.
69
+ #
70
+ # For example, given
71
+ #
72
+ # class A
73
+ # autoload :X, "x.rb"
74
+ # end
75
+ #
76
+ # class B < A
77
+ # end
78
+ #
79
+ # B.autoload?(:X) returns "x.rb".
80
+ #
81
+ # We need a way to strictly check in parent ignoring ancestors.
82
+ #
83
+ # @sig (Module, Symbol) -> String?
84
+ if method(:autoload?).arity == 1
85
+ def strict_autoload_path(parent, cname)
86
+ parent.autoload?(cname) if cdef?(parent, cname)
87
+ end
88
+ else
89
+ def strict_autoload_path(parent, cname)
90
+ parent.autoload?(cname, false)
91
+ end
92
+ end
93
+
94
+ # @sig (Module, Symbol) -> String
95
+ if Symbol.method_defined?(:name)
96
+ # Symbol#name was introduced in Ruby 3.0. It returns always the same
97
+ # frozen object, so we may save a few string allocations.
98
+ def cpath(parent, cname)
99
+ Object == parent ? cname.name : "#{real_mod_name(parent)}::#{cname.name}"
100
+ end
101
+ else
102
+ def cpath(parent, cname)
103
+ Object == parent ? cname.to_s : "#{real_mod_name(parent)}::#{cname}"
104
+ end
105
+ end
106
+
107
+ # @sig (Module, Symbol) -> bool
108
+ def cdef?(parent, cname)
109
+ parent.const_defined?(cname, false)
110
+ end
111
+
112
+ # @raise [NameError]
113
+ # @sig (Module, Symbol) -> Object
114
+ def cget(parent, cname)
115
+ parent.const_get(cname, false)
116
+ end
117
+ end