-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathactive_functions.rb
52 lines (45 loc) · 1.86 KB
/
active_functions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
require 'ruby2js'
module Ruby2JS
module Filter
module ActiveFunctions
include SEXP
def on_send(node)
target, method, *args = node.children
if es2015 and method == :blank?
create_or_update_import("blank$")
process node.updated :send, [nil, "blank$", target]
elsif es2015 and method == :present?
create_or_update_import("present$")
process node.updated :send, [nil, "present$", target]
elsif es2015 and method == :presence
create_or_update_import("presence$")
process node.updated :send, [nil, "presence$", target]
elsif es2015 and method == :chomp
create_or_update_import("chomp$")
process node.updated :send, [nil, "chomp$", target, *args]
elsif es2015 and method == :delete_prefix
create_or_update_import("deletePrefix$")
process node.updated :send, [nil, "deletePrefix$", target, *args]
elsif es2015 and method == :delete_suffix
create_or_update_import("deleteSuffix$")
process node.updated :send, [nil, "deleteSuffix$", target, *args]
else
super
end
end
private
def create_or_update_import(token)
af_import = @options[:import_from_skypack] ? "https://cdn.skypack.dev/@ruby2js/active-functions" : "@ruby2js/active-functions"
if found_node = prepend_list.find {|ast| ast.type == :import && ast.children.first == af_import}
unless found_node.children.last.find {|const| const.children.last == token}
prepend_list.delete found_node
prepend_list << s(:import, found_node.children.first, found_node.children.last.push(s(:const, nil, token)))
end
else
prepend_list << s(:import, af_import, [s(:const, nil, token)])
end
end
end
DEFAULTS.push ActiveFunctions
end
end