bellejs 0.1.0 → 0.3.0

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.
@@ -2,7 +2,7 @@ require "rbelly" # javascript parser
2
2
 
3
3
  class BelleTranspiler
4
4
 
5
- BELLEJS_NODE_CLASSES = [RBelly::Nodes::ImportNode, RBelly::Nodes::ClassNode]
5
+ BELLEJS_NODE_CLASSES = [RBelly::Nodes::ImportNode, RBelly::Nodes::ClassNode, RBelly::Nodes::ExtendsNode, RBelly::Nodes::BellejsVarStatementNode, RBelly::Nodes::BellejsFuncStatementNode]
6
6
  LINE_END = RBelly::Nodes::ExpressionStatementNode
7
7
 
8
8
  def initialize(file_contents, local_dir)
@@ -41,6 +41,10 @@ class BelleTranspiler
41
41
  process_import bellejs_statement
42
42
  when RBelly::Nodes::ClassNode
43
43
  process_class bellejs_statement
44
+ when RBelly::Nodes::BellejsVarStatementNode
45
+ process_var_statement bellejs_statement
46
+ when RBelly::Nodes::BellejsFuncStatementNode
47
+ process_function bellejs_statement
44
48
  else
45
49
  bellejs_statement
46
50
  end
@@ -53,7 +57,61 @@ class BelleTranspiler
53
57
  end
54
58
 
55
59
  def process_class(class_statement)
56
- class_as_function = @parser.parse("function #{class_statement.value}() #{class_statement.class_body.to_ecma}")
60
+ compiled_class = "function #{class_statement.value}() #{class_statement.class_body.to_ecma}"
61
+ unless class_statement.parent.nil?
62
+ inheritance_prototype = process_extends(class_statement.value, class_statement.parent)
63
+ compiled_class = compiled_class + "\n" + inheritance_prototype
64
+ end
65
+ @parser.parse compiled_class
66
+ end
67
+
68
+ def process_extends(class_name, parent)
69
+ parent_name = parent.parent
70
+ "#{class_name}.prototype = new #{parent_name}();"
71
+ end
72
+
73
+ def process_var_statement(var_statement)
74
+ visibility = var_statement.value
75
+ js_decl_statement = var_statement.var_statement.value.first
76
+ var_name = js_decl_statement.name
77
+ var_value = (js_decl_statement.value == nil ? nil : js_decl_statement.value.value.value)
78
+ var_value = js_decl_statement.value.value.to_ecma if var_value.class == Array
79
+ var_value_statement = (var_value.nil? ? ";" : " = " + var_value.to_s + ";")
80
+ is_constant = js_decl_statement.constant?
81
+
82
+ if visibility == 'public'
83
+ compiled_string = "this.#{var_name}#{var_value_statement}"
84
+ else
85
+ if visibility == 'private'
86
+ compiled_string = (is_constant ? "const" : "var") + " #{var_name}#{var_value_statement}"
87
+ else
88
+ raise "invalid variable declaration"
89
+ end
90
+ end
91
+ @parser.parse compiled_string
92
+ end
93
+
94
+ def process_function(function_statement)
95
+ visibility = function_statement.value
96
+ func_name = function_statement.func_statement.value
97
+ func_body = function_statement.func_statement.function_body.to_ecma
98
+ func_args = function_statement.func_statement.arguments
99
+ func_args_string = String.new
100
+ unless func_args.nil?
101
+ func_args.each do |arg|
102
+ func_args_string += arg.value.to_s + (arg != func_args.last ? ", " : "")
103
+ end
104
+ end
105
+ if visibility == 'public'
106
+ compiled_string = "this.#{func_name} = function(#{func_args_string})#{func_body}"
107
+ else
108
+ if visibility == 'private'
109
+ compiled_string = "function #{func_name}(#{func_args_string})#{func_body}"
110
+ else
111
+ raise "invalid function declaration"
112
+ end
113
+ end
114
+ @parser.parse compiled_string
57
115
  end
58
116
 
59
117
  # debug helpers:
@@ -1,3 +1,3 @@
1
1
  class Bellejs
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/bellejs.rb CHANGED
@@ -45,7 +45,7 @@ class Bellejs
45
45
  end
46
46
 
47
47
  def uglify_js
48
- @compiled_js = Uglifier.compile(@compiled_js, :mangle => false)
48
+ @compiled_js = Uglifier.compile(@compiled_js, :compress => {:unused => false, })
49
49
  end
50
50
 
51
51
  def write
@@ -12,4 +12,20 @@ class TestBelleTranspilerClass < Test::Unit::TestCase
12
12
  assert_equal File.read("#{TEST_FILE_DIRECTORY}class_test.js"), File.read("#{TEST_FILE_DIRECTORY}non_complied_class_test.js")
13
13
  end
14
14
 
15
+ def test_extends_statement
16
+ file_path = TEST_FILE_DIRECTORY + 'extends_test.bellejs'
17
+ bellejs = Bellejs.new(file_path)
18
+ result = File.read("#{TEST_FILE_DIRECTORY}extends_test.js")
19
+ comparison = File.read("#{TEST_FILE_DIRECTORY}non_compiled_extends_test.js")
20
+ assert_equal result, comparison
21
+ end
22
+
23
+ def test_private_public_statement
24
+ file_path = TEST_FILE_DIRECTORY + 'private_public_test.bellejs'
25
+ bellejs = Bellejs.new(file_path)
26
+ result = File.read("#{TEST_FILE_DIRECTORY}private_public_test.js")
27
+ comparison = File.read("#{TEST_FILE_DIRECTORY}noncompiled_private_public_test.js")
28
+ assert_equal result, comparison
29
+ end
30
+
15
31
  end
@@ -0,0 +1,15 @@
1
+ class Animal {
2
+ this.makeNoise = function () {
3
+ alert("-rustle-");
4
+ }
5
+ }
6
+
7
+ class Wolf extends Animal {
8
+ this.howl = function () {
9
+ alert("Hooooooowlllllll!");
10
+ }
11
+
12
+ this.makeNoise = function () {
13
+ this.howl();
14
+ }
15
+ }
@@ -0,0 +1,14 @@
1
+ function Animal(){
2
+ this.makeNoise = function() {
3
+ alert("-rustle-");
4
+ };
5
+ }
6
+ function Wolf(){
7
+ this.howl = function() {
8
+ alert("Hooooooowlllllll!");
9
+ };
10
+ this.makeNoise = function() {
11
+ this.howl();
12
+ };
13
+ }
14
+ Wolf.prototype = new Animal();
@@ -0,0 +1,12 @@
1
+ function Alligator(){
2
+ var tooth;
3
+ var genitals = "thing";
4
+ this.face = 2345;
5
+ this.tail = ['scaly', 'green', 'long'];
6
+ function pickNose(){
7
+ alert("adsf");
8
+ }
9
+ this.walk = function(arg1, arg2) {
10
+ alert(this.face);
11
+ };
12
+ }
@@ -0,0 +1,16 @@
1
+ class Alligator {
2
+
3
+ private var tooth;
4
+ private var genitals = "thing";
5
+ public var face = 2345;
6
+ public var tail = ['scaly','green','long']
7
+
8
+ private function pickNose() {
9
+ alert("adsf");
10
+ }
11
+
12
+ public function walk(arg1, arg2) {
13
+ alert(this.face);
14
+ }
15
+
16
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bellejs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
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: 2013-04-30 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rbelly
@@ -97,29 +97,19 @@ files:
97
97
  - test/test_belle_transpiler_import.rb
98
98
  - test/test_bellejs.rb
99
99
  - test/test_files/class_test.bellejs
100
+ - test/test_files/extends_test.bellejs
100
101
  - test/test_files/import_test.bellejs
101
102
  - test/test_files/import_test.js
103
+ - test/test_files/non_compiled_extends_test.js
102
104
  - test/test_files/non_complied_class_test.js
105
+ - test/test_files/noncompiled_private_public_test.js
106
+ - test/test_files/private_public_test.bellejs
103
107
  - test/test_files/recursive_double_import.bellejs
104
108
  - test/test_files/recursive_double_import.js
105
109
  - test/test_files/recursive_import_test.bellejs
106
110
  - test/test_files/recursive_import_test.js
107
111
  - test/test_files/simple_javascript.bellejs
108
112
  - test/test_files/simple_javascript.js
109
- - test/test_files/subdir/13617/output_test2.js
110
- - test/test_files/subdir/17210/output_test2.js
111
- - test/test_files/subdir/31452/output_test2.js
112
- - test/test_files/subdir/327/output_test2.js
113
- - test/test_files/subdir/3631/output_test2.js
114
- - test/test_files/subdir/51024/output_test2.js
115
- - test/test_files/subdir/57182/output_test2.js
116
- - test/test_files/subdir/60145/output_test2.js
117
- - test/test_files/subdir/72175/output_test2.js
118
- - test/test_files/subdir/73350/output_test2.js
119
- - test/test_files/subdir/81401/output_test2.js
120
- - test/test_files/subdir/84914/output_test2.js
121
- - test/test_files/subdir/91780/output_test2.js
122
- - test/test_files/subdir/99826/output_test2.js
123
113
  - test/test_files/subdir/output_test.js
124
114
  - test/test_files/subdir/test_file_NaME.js
125
115
  - test/test_files/subdir_import_test.bellejs
@@ -156,29 +146,19 @@ test_files:
156
146
  - test/test_belle_transpiler_import.rb
157
147
  - test/test_bellejs.rb
158
148
  - test/test_files/class_test.bellejs
149
+ - test/test_files/extends_test.bellejs
159
150
  - test/test_files/import_test.bellejs
160
151
  - test/test_files/import_test.js
152
+ - test/test_files/non_compiled_extends_test.js
161
153
  - test/test_files/non_complied_class_test.js
154
+ - test/test_files/noncompiled_private_public_test.js
155
+ - test/test_files/private_public_test.bellejs
162
156
  - test/test_files/recursive_double_import.bellejs
163
157
  - test/test_files/recursive_double_import.js
164
158
  - test/test_files/recursive_import_test.bellejs
165
159
  - test/test_files/recursive_import_test.js
166
160
  - test/test_files/simple_javascript.bellejs
167
161
  - test/test_files/simple_javascript.js
168
- - test/test_files/subdir/13617/output_test2.js
169
- - test/test_files/subdir/17210/output_test2.js
170
- - test/test_files/subdir/31452/output_test2.js
171
- - test/test_files/subdir/327/output_test2.js
172
- - test/test_files/subdir/3631/output_test2.js
173
- - test/test_files/subdir/51024/output_test2.js
174
- - test/test_files/subdir/57182/output_test2.js
175
- - test/test_files/subdir/60145/output_test2.js
176
- - test/test_files/subdir/72175/output_test2.js
177
- - test/test_files/subdir/73350/output_test2.js
178
- - test/test_files/subdir/81401/output_test2.js
179
- - test/test_files/subdir/84914/output_test2.js
180
- - test/test_files/subdir/91780/output_test2.js
181
- - test/test_files/subdir/99826/output_test2.js
182
162
  - test/test_files/subdir/output_test.js
183
163
  - test/test_files/subdir/test_file_NaME.js
184
164
  - test/test_files/subdir_import_test.bellejs
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }
@@ -1,15 +0,0 @@
1
- alert("Hey this is an alert");
2
- $(document).ready(doStuff);
3
- $('a.important_tag').click(function() {
4
- if(doStuff()) {
5
- $(this).attr("href", "www.google.com/");
6
- }
7
- });
8
- function doStuff(){
9
- var number = 9 * 12 - 19 + 2;
10
- if(number == 200) {
11
- return true;
12
- } else {
13
- return false;
14
- }
15
- }