ruby2js 1.13.0 → 1.13.1
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.
- data/README.md +5 -0
- data/lib/ruby2js.rb +12 -1
- data/lib/ruby2js/filter/react.rb +12 -11
- data/lib/ruby2js/filter/require.rb +70 -0
- data/lib/ruby2js/version.rb +1 -1
- data/ruby2js.gemspec +3 -3
- metadata +3 -2
data/README.md
CHANGED
@@ -144,6 +144,11 @@ the script.
|
|
144
144
|
* [return](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/return.rb)
|
145
145
|
adds `return` to the last expression in functions.
|
146
146
|
|
147
|
+
* [require](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/require.rb)
|
148
|
+
supports `require` and `require_relative` statements. Contents of files
|
149
|
+
that are required are converted to JavaScript and expanded inline.
|
150
|
+
`require` function calls in expressions are left alone.
|
151
|
+
|
147
152
|
* [camelCase](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/camelCase.rb)
|
148
153
|
converts `underscore_case` to `camelCase`. See
|
149
154
|
[camelCase_spec](https://github.com/rubys/ruby2js/blob/master/spec/camelCase_spec.rb)
|
data/lib/ruby2js.rb
CHANGED
@@ -15,6 +15,10 @@ module Ruby2JS
|
|
15
15
|
class Processor < Parser::AST::Processor
|
16
16
|
BINARY_OPERATORS = Converter::OPERATORS[2..-1].flatten
|
17
17
|
|
18
|
+
def options=(options)
|
19
|
+
@options = options
|
20
|
+
end
|
21
|
+
|
18
22
|
# handle all of the 'invented' ast types
|
19
23
|
def on_attr(node); on_send(node); end
|
20
24
|
def on_autoreturn(node); on_return(node); end
|
@@ -54,6 +58,7 @@ module Ruby2JS
|
|
54
58
|
file,line = source.source_location
|
55
59
|
source = File.read(file.dup.untaint).untaint
|
56
60
|
ast = find_block( parse(source), line )
|
61
|
+
options[:file] = file
|
57
62
|
elsif Parser::AST::Node === source
|
58
63
|
ast = source
|
59
64
|
source = ast.loc.expression.source_buffer.source
|
@@ -68,7 +73,9 @@ module Ruby2JS
|
|
68
73
|
filters.reverse.each do |mod|
|
69
74
|
filter = Class.new(filter) {include mod}
|
70
75
|
end
|
71
|
-
|
76
|
+
filter = filter.new
|
77
|
+
filter.options = options
|
78
|
+
ast = filter.process(ast)
|
72
79
|
end
|
73
80
|
|
74
81
|
ruby2js = Ruby2JS::Converter.new( ast )
|
@@ -78,6 +85,10 @@ module Ruby2JS
|
|
78
85
|
if ruby2js.binding and not ruby2js.ivars
|
79
86
|
ruby2js.ivars = ruby2js.binding.eval \
|
80
87
|
'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
|
88
|
+
elsif options[:scope] and not ruby2js.ivars
|
89
|
+
scope = options.delete(:scope)
|
90
|
+
ruby2js.ivars = Hash[scope.instance_variables.map {|var|
|
91
|
+
[var, scope.instance_variable_get(var)]}]
|
81
92
|
end
|
82
93
|
|
83
94
|
ruby2js.width = options[:width] if options[:width]
|
data/lib/ruby2js/filter/react.rb
CHANGED
@@ -46,6 +46,7 @@ module Ruby2JS
|
|
46
46
|
|
47
47
|
begin
|
48
48
|
react, @react = @react, true
|
49
|
+
reactClass, @reactClass = @reactClass, true
|
49
50
|
|
50
51
|
# automatically capture the displayName for the class
|
51
52
|
pairs = [s(:pair, s(:sym, :displayName),
|
@@ -60,6 +61,7 @@ module Ruby2JS
|
|
60
61
|
end
|
61
62
|
ensure
|
62
63
|
@react = react
|
64
|
+
@reactClass = reactClass
|
63
65
|
end
|
64
66
|
|
65
67
|
# emit a createClass statement
|
@@ -370,11 +372,17 @@ module Ruby2JS
|
|
370
372
|
|
371
373
|
return super unless @react
|
372
374
|
|
375
|
+
# traverse through potential "css proxy" style method calls
|
376
|
+
child = node.children.first
|
377
|
+
test = child.children.first
|
378
|
+
while test and test.type == :send and not test.is_method?
|
379
|
+
child, test = test, test.children.first
|
380
|
+
end
|
381
|
+
|
373
382
|
# iterate over Enumerable arguments if (a) node is a createElement
|
374
383
|
# type of call, and (b) there are args present
|
375
384
|
if
|
376
|
-
|
377
|
-
node.children.first.children[1] =~ /^_/ and
|
385
|
+
child.children[0] == nil and child.children[1] =~ /^_/ and
|
378
386
|
not node.children[1].children.empty?
|
379
387
|
then
|
380
388
|
send = node.children.first.children
|
@@ -384,13 +392,6 @@ module Ruby2JS
|
|
384
392
|
*node.children[1..-1]))
|
385
393
|
end
|
386
394
|
|
387
|
-
# traverse through potential "css proxy" style method calls
|
388
|
-
child = node.children.first
|
389
|
-
test = child.children.first
|
390
|
-
while test and test.type == :send and not test.is_method?
|
391
|
-
child, test = test, test.children.first
|
392
|
-
end
|
393
|
-
|
394
395
|
# append block as a standalone proc to wunderbar style method call
|
395
396
|
if child.children[0] == nil and child.children[1] =~ /^_\w/
|
396
397
|
block = s(:block, s(:send, nil, :proc), s(:args),
|
@@ -403,13 +404,13 @@ module Ruby2JS
|
|
403
404
|
|
404
405
|
# convert global variables to refs
|
405
406
|
def on_gvar(node)
|
406
|
-
return super unless @
|
407
|
+
return super unless @reactClass
|
407
408
|
s(:attr, s(:attr, s(:self), :refs), node.children.first.to_s[1..-1])
|
408
409
|
end
|
409
410
|
|
410
411
|
# convert instance variables to state
|
411
412
|
def on_ivar(node)
|
412
|
-
return super unless @
|
413
|
+
return super unless @reactClass
|
413
414
|
s(:attr, s(:attr, s(:self), :state), node.children.first.to_s[1..-1])
|
414
415
|
end
|
415
416
|
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'ruby2js'
|
2
|
+
|
3
|
+
module Ruby2JS
|
4
|
+
module Filter
|
5
|
+
module Require
|
6
|
+
include SEXP
|
7
|
+
|
8
|
+
@@valid_path = /\A[-\w_.]+\Z/
|
9
|
+
|
10
|
+
def self.valid_path=(valid_path)
|
11
|
+
@@valid_path = valid_path
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_send(node)
|
15
|
+
if
|
16
|
+
not @require_expr and # only statements
|
17
|
+
node.children.length == 3 and
|
18
|
+
node.children[0] == nil and
|
19
|
+
[:require, :require_relative].include? node.children[1] and
|
20
|
+
node.children[2].type == :str and
|
21
|
+
@options[:file]
|
22
|
+
then
|
23
|
+
|
24
|
+
begin
|
25
|
+
file2 = @options[:file2]
|
26
|
+
|
27
|
+
basename = node.children[2].children.first
|
28
|
+
dirname = File.dirname(File.expand_path(@options[:file]))
|
29
|
+
|
30
|
+
if file2 and node.children[1] == :require_relative
|
31
|
+
dirname = File.dirname(File.expand_path(file2))
|
32
|
+
end
|
33
|
+
|
34
|
+
filename = File.join(dirname, basename)
|
35
|
+
|
36
|
+
segments = basename.split(/[\/\\]/)
|
37
|
+
if segments.all? {|path| path =~ @@valid_path and path != '..'}
|
38
|
+
filename.untaint
|
39
|
+
end
|
40
|
+
|
41
|
+
@options[:file2] = filename
|
42
|
+
process Ruby2JS.parse(File.read(filename))
|
43
|
+
ensure
|
44
|
+
if file2
|
45
|
+
@options[:file2] = file2
|
46
|
+
else
|
47
|
+
@options.delete(:file2)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
else
|
51
|
+
begin
|
52
|
+
require_expr, @require_expr = @require_expr, true
|
53
|
+
super
|
54
|
+
ensure
|
55
|
+
@require_expr = require_expr
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def on_lvasgn(node)
|
61
|
+
require_expr, @require_expr = @require_expr, true
|
62
|
+
super
|
63
|
+
ensure
|
64
|
+
@require_expr = require_expr
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
DEFAULTS.push Require
|
69
|
+
end
|
70
|
+
end
|
data/lib/ruby2js/version.rb
CHANGED
data/ruby2js.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ruby2js"
|
5
|
-
s.version = "1.13.
|
5
|
+
s.version = "1.13.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Sam Ruby"]
|
9
|
-
s.date = "2015-02-
|
9
|
+
s.date = "2015-02-11"
|
10
10
|
s.description = " The base package maps Ruby syntax to JavaScript semantics.\n Filters may be provided to add Ruby-specific or framework specific\n behavior.\n"
|
11
11
|
s.email = "rubys@intertwingly.net"
|
12
|
-
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/execjs.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/version.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/camelCase.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/react.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/nthref.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js.rb"]
|
12
|
+
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/execjs.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/version.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/require.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/camelCase.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/react.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/nthref.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js.rb"]
|
13
13
|
s.homepage = "http://github.com/rubys/ruby2js"
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
s.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.13.
|
4
|
+
version: 1.13.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-02-
|
12
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/ruby2js/filter/jquery.rb
|
43
43
|
- lib/ruby2js/filter/return.rb
|
44
44
|
- lib/ruby2js/filter/underscore.rb
|
45
|
+
- lib/ruby2js/filter/require.rb
|
45
46
|
- lib/ruby2js/filter/angularrb.rb
|
46
47
|
- lib/ruby2js/filter/minitest-jasmine.rb
|
47
48
|
- lib/ruby2js/filter/camelCase.rb
|