js2 0.3.0.pre5 → 0.3.0.pre6

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.
Files changed (2) hide show
  1. data/lib/js2/js2.js +84 -65
  2. metadata +7 -20
data/lib/js2/js2.js CHANGED
@@ -841,7 +841,11 @@ function mainFunction (arg) {
841
841
 
842
842
  toString: function() {
843
843
  var v = this.validate(/(curry)(\s*)(Braces)?(\s*)(with)?(\s*)(Braces)?(\s*)(Block)/);
844
- var ret = [ '(function(){return function' ];
844
+
845
+ var scopeOuter = (v[5] ? v[7].toString() : "()");
846
+ var scopeInner = scopeOuter.replace(/\bthis\b/, '$this');
847
+
848
+ var ret = [ '(function' + scopeInner + '{return function' ];
845
849
 
846
850
  // args
847
851
  ret.push(v[3] ? v[3].toString() : '($1,$2,$3)');
@@ -853,7 +857,7 @@ function mainFunction (arg) {
853
857
  ret.push("})");
854
858
 
855
859
  // scope
856
- ret.push(v[5] ? v[7].toString() : "()");
860
+ ret.push(scopeOuter);
857
861
 
858
862
  if (this.addSemiColon) ret.push(';');
859
863
 
@@ -1102,6 +1106,68 @@ JS2.Class.extend('Updater', {
1102
1106
  });
1103
1107
 
1104
1108
 
1109
+ JS2.Class.extend('Config', {
1110
+ "CLI_REGEX":/^-(r|i|f)(:?=(\w+))$/,
1111
+ "optsLookup":{
1112
+ 'n': 'non-recursive',
1113
+ 'i': 'interval',
1114
+ 'f': 'format'
1115
+ },
1116
+
1117
+ initialize:function (fs, argv) {
1118
+ this.format = 'browser';
1119
+ this.recursive = true;
1120
+ this.interval = 2;
1121
+ this.sourceDir = './app/js2';
1122
+ this.outDir = './public/javascripts';
1123
+ this.args = [];
1124
+
1125
+ this.fs = fs;
1126
+
1127
+ if (! this.loadConfigFile('./config/js2.json')) {
1128
+ this.loadConfigFile('./js2.json');
1129
+ }
1130
+
1131
+ if (argv) {
1132
+ while (argv.length) {
1133
+ var opt = argv.shift();
1134
+ var m = opt.match(this.CLI_REGEX);
1135
+ if (m) {
1136
+ this[this.optsLookup[m[0]]] = m[1] || true;
1137
+ } else if (! this.command) {
1138
+ this.command = opt;
1139
+ } else {
1140
+ this.args.push(opt);
1141
+ }
1142
+ }
1143
+ }
1144
+
1145
+ this.interval = parseInt(this.interval);
1146
+
1147
+ },
1148
+
1149
+ loadConfigFile:function (file) {
1150
+ if (this.fs.isFile(file)) {
1151
+ try {
1152
+ var config = JSON.parse(this.fs.read(file).replace(/\n/g, ''));
1153
+
1154
+ this.format = config.format || this.format;
1155
+ this.recursive = config['non-recursive'] ? false : this.recursive;
1156
+ this.interval = config['interval'] ? config['interval'] : this.interval;
1157
+ this.sourceDir = config['source-dir'] || this.sourceDir;
1158
+ this.outDir = config['out-dir'] || this.outDir;
1159
+
1160
+ return true;
1161
+ } catch(e) {
1162
+ console.log(e.toString());
1163
+ }
1164
+ }
1165
+ return false;
1166
+ }
1167
+
1168
+ });
1169
+
1170
+
1105
1171
  JS2.Class.extend('Commander', {
1106
1172
  "BANNER":"js2 <command> [options] <arguments>\n" +
1107
1173
  "Commands:\n" +
@@ -1109,12 +1175,12 @@ JS2.Class.extend('Commander', {
1109
1175
  " * render <file> -- Shows JS2 compiled output\n" +
1110
1176
  " * compile <inDir> [outDir] -- Compiles a directory and puts js files into outDir. If outDir is not specified, inDir will be used\n" +
1111
1177
  " Options:\n" +
1112
- " -r -- Traverse directories recursively\n" +
1113
- " -m=<mode> -- Compile for different modes: node, ringo, or browser\n" +
1178
+ " -n -- Do NOT traverse directories recursively\n" +
1179
+ " -f=<format> -- Compile for different formats: node, ringo, or browser\n" +
1114
1180
  " * compile <file> -- Compiles a single js2 file into js\n" +
1115
1181
  " * watch <inDir> <outDir> -- Similar to compile, but update will keep looping while watching for modifications\n" +
1116
1182
  " Options:\n" +
1117
- " -r -- Traverse directories recursively\n" +
1183
+ " -n -- Do NOT traverse directories recursively\n" +
1118
1184
  " -i=<seconds> -- Interval time in seconds between loops\n",
1119
1185
 
1120
1186
  "DEFAULT_CONFIG":{
@@ -1123,99 +1189,52 @@ JS2.Class.extend('Commander', {
1123
1189
  },
1124
1190
 
1125
1191
  initialize:function (argv) {
1126
- this.argv = argv;
1127
- this.command = this.argv.shift();
1128
1192
  this.fs = JS2.fs;
1129
- this.parseOpts(argv);
1193
+ this.config = new JS2.Config(this.fs, argv);
1194
+ this.command = this.config.command;
1130
1195
  },
1131
1196
 
1132
1197
  cli:function () {
1133
1198
  if (this[this.command]) {
1134
- if (this.argv.length == 0) {
1135
- this.loadArgvFromConfig();
1136
- }
1137
-
1138
- this[this.command](this.argv);
1199
+ this[this.command]();
1139
1200
  } else {
1140
1201
  this.showBanner();
1141
1202
  }
1142
1203
  },
1143
1204
 
1144
- loadArgvFromConfig:function () {
1145
- },
1146
-
1147
- render:function (argv) {
1148
- console.log(js2.render(this.fs.read(argv[0])));
1205
+ render:function () {
1206
+ console.log(js2.render(this.fs.read(this.config.args[0])));
1149
1207
  },
1150
1208
 
1151
- run:function (argv) {
1209
+ run:function () {
1152
1210
  var file;
1153
1211
  var i = 0;
1154
- while (file = argv[i++]) {
1212
+ while (file = this.config.args[i++]) {
1155
1213
  eval(js2.render(this.fs.read(file)));
1156
1214
  }
1157
1215
  },
1158
1216
 
1159
- "options":{
1160
- 'r': 'recursive',
1161
- 'i': 'interval',
1162
- 'm': 'mode'
1163
- },
1164
-
1165
- parseOpts:function (argv) {
1166
- this.opts = { main: [] };
1167
- var opts = this.opts;
1168
-
1169
- for (var i=0; i<argv.length; i++) {
1170
- var arg = argv[i];
1171
- var m = arg.match(/^-(\w)(=(\w+))?$/);
1172
- if (m) {
1173
- var key = this.options[m[1]];
1174
- if (! key) console.log('Invalid option: ' + m[1]);
1175
- opts[key] = m[3] || true;
1176
- } else {
1177
- opts.main.push(arg);
1178
- }
1179
- }
1180
-
1181
- switch(opts['mode']) {
1182
- case 'ringo': JS2.DECORATOR = new JS2.Decorator.Ringo(); break;
1183
- case 'node': JS2.DECORATOR = new JS2.Decorator.Node(); break;
1184
- default: JS2.DECORATOR = new JS2.Decorator.Browser(); break;
1185
- }
1186
- },
1187
-
1188
1217
  compile:function () {
1189
- var inDir = this.opts.main[0];
1190
1218
  var self = this;
1191
-
1192
- this.getUpdater().update(true, function($1,$2,$3){ return JS2.DECORATOR.file((self.handleSource($1))); });
1193
- },
1194
-
1195
- handleSource:function (code) {
1196
- if (this.opts.browsers) {
1197
- return code;
1198
- } else {
1199
- return code;
1200
- }
1219
+ this.getUpdater().update(true, function($1,$2,$3){ return JS2.DECORATOR.file($1); });
1201
1220
  },
1202
1221
 
1203
1222
  getUpdater:function () {
1204
- var inDir = this.opts.main[0] || '.';
1205
- var outDir = this.opts.main[1] || inDir;
1206
- return new JS2.Updater(this.fs, inDir, outDir, this.opts.recursive);
1223
+ var inDir = this.config.args[0] || '.';
1224
+ var outDir = this.config.args[1] || inDir;
1225
+ return new JS2.Updater(this.fs, inDir, outDir, this.config.recursive);
1207
1226
  },
1208
1227
 
1209
1228
  watch:function () {
1210
1229
  var updater = this.getUpdater();
1211
1230
  var self = this;
1212
- var interval = this.opts.interval || 2;
1231
+ var interval = this.config.interval || 2;
1213
1232
  console.log('Input Directory:' + updater.inDir + ' -> Output Directory:' + updater.outDir);
1214
1233
  if (updater.recursive) console.log('RECURSIVE');
1215
1234
 
1216
1235
  // HACK to get this integrated with ruby
1217
1236
  updater.update();
1218
- setInterval(function($1,$2,$3){ console.log('updating'); updater.update() }, interval * 1000);
1237
+ setInterval(function($1,$2,$3){ console.log('updating'); updater.update(true, function($1,$2,$3){ return JS2.DECORATOR.file($1); }); }, interval * 1000);
1219
1238
  },
1220
1239
 
1221
1240
  showBanner:function () {
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js2
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
5
- segments:
6
- - 0
7
- - 3
8
- - 0
9
- - pre5
10
- version: 0.3.0.pre5
4
+ prerelease: 6
5
+ version: 0.3.0.pre6
11
6
  platform: ruby
12
7
  authors:
13
8
  - Jeff Su
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-03-05 00:00:00 -08:00
13
+ date: 2011-03-08 00:00:00 +08:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,8 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- segments:
30
- - 0
31
24
  version: "0"
32
25
  type: :runtime
33
26
  version_requirements: *id001
@@ -43,11 +36,11 @@ extra_rdoc_files: []
43
36
  files:
44
37
  - bin/js2
45
38
  - bin/js2-ruby
46
- - lib/js2/command.rb
39
+ - lib/js2.rb
47
40
  - lib/js2/context.rb
48
- - lib/js2/fs.rb
49
41
  - lib/js2/rack.rb
50
- - lib/js2.rb
42
+ - lib/js2/command.rb
43
+ - lib/js2/fs.rb
51
44
  - lib/js2/js2.js
52
45
  has_rdoc: true
53
46
  homepage: http://jeffsu.github.com
@@ -63,23 +56,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
56
  requirements:
64
57
  - - ">="
65
58
  - !ruby/object:Gem::Version
66
- segments:
67
- - 0
68
59
  version: "0"
69
60
  required_rubygems_version: !ruby/object:Gem::Requirement
70
61
  none: false
71
62
  requirements:
72
63
  - - ">"
73
64
  - !ruby/object:Gem::Version
74
- segments:
75
- - 1
76
- - 3
77
- - 1
78
65
  version: 1.3.1
79
66
  requirements: []
80
67
 
81
68
  rubyforge_project:
82
- rubygems_version: 1.3.7
69
+ rubygems_version: 1.5.2
83
70
  signing_key:
84
71
  specification_version: 3
85
72
  summary: Javascript Syntactic Sugar