Skip to content
This repository
Browse code

Update to js1.8 regexps from @mozsquib

  • Loading branch information...
commit d5ec8da4f1b8517780e0c41bf419aa93139993d6 1 parent 6ff6c7b
James Burke authored
40 build/tests/lib/js18/build.js
@@ -5,22 +5,36 @@
5 5 insertRequire: ['main'],
6 6 out: 'main-built.js',
7 7 onBuildRead: function (id, path, contents) {
8   - //Remove destructuring so parsing works. Do this until esprima has
9   - //a fix for: http://code.google.com/p/esprima/issues/detail?id=241
10   - var destructRegExp = /(var|let|const)\s*\{[^\}]+\}\s*=/g,
11   - yieldRegExp = /\byield\b/g;
  8 + // Regexps courtesy of @mozsquib on github.
  9 + // Remove destructuring, minimal functions, and yields so parsing works.
  10 + // Do this until esprima has a fix for this, among other things:
  11 + // http://code.google.com/p/esprima/issues/detail?id=241
12 12
13   - return contents
14   - .replace(destructRegExp, '/*DESTRUCT$&DESTRUCT*/')
15   - .replace(yieldRegExp, '/*YIELD*/');
  13 + var destructRe = /((?:var|let|const)\s*)(\{[^\}]+\})(\s*(?:=|in\b))/g;
  14 + var destructRe2 = /((?:var|let|const)\s*)(\[[^\]]+\])(\s*(?:=|in\b))/g;
  15 + var destructRe3 = /\n(\s*)(\[[^\]]+\])(\s*(?:=|in\b))/g;
  16 + var getterRe = /get\s+\w+\s*\(\).*,/g;
  17 + var minifuncRe = /(function\s*\(.*?\))(?=\s*\w)/g;
  18 + var yieldRe = /yield/g;
  19 + var temp = contents.replace(destructRe, '$1/*DESTRUCT$2DESTRUCT*/_$3')
  20 + .replace(destructRe2, '$1/*DESTRUCT$2DESTRUCT*/_$3')
  21 + .replace(destructRe3, '$1/*DESTRUCT$2DESTRUCT*/_$3')
  22 + .replace(getterRe, '/*GETTER$&GETTER*/')
  23 + .replace(minifuncRe, '/*MINIFUNC$1MINIFUNC*/')
  24 + .replace(yieldRe, 'return YIELD+');
  25 + //console.log(temp);
  26 + return temp;
16 27 },
17 28 onBuildWrite: function (id, path, contents) {
18   - //Restore destructuring
19   - var destructRegExp = /\/\*DESTRUCT(.*)DESTRUCT\*\//g,
20   - yieldRegExp = /\/\*YIELD\*\//g;
  29 + // Restore destructuring, minimal functions, and yields.
21 30
22   - return contents
23   - .replace(destructRegExp, '$1')
24   - .replace(yieldRegExp, 'yield');
  31 + var restructRe = /\/\*DESTRUCT(.*?)DESTRUCT\*\/_/g;
  32 + var getterRe = /\/\*GETTER(.*?)GETTER\*\//g;
  33 + var minifuncRe = /\/\*MINIFUNC(.*?)MINIFUNC\*\//g;
  34 + var yieldRe = /return YIELD\+/g;
  35 + return contents.replace(restructRe, '$1')
  36 + .replace(getterRe, '$1')
  37 + .replace(minifuncRe, '$1')
  38 + .replace(yieldRe, 'yield');
25 39 }
26 40 })
15 build/tests/lib/js18/expected.js
@@ -11,6 +11,21 @@ define('a',['require'],function (require) {
11 11 define('main',['require','a'],function (require) {
12 12 let a = require('a');
13 13 var {name, other} = a;
  14 +
  15 + var c = 1;
  16 + var d = 3;
  17 + [c, d] = [d, c];
  18 +
  19 + function generator() {
  20 + var i = 0;
  21 + while (i < 10) {
  22 + ++i;
  23 + if ((i % 2) == 0) {
  24 + yield true;
  25 + }
  26 + }
  27 + yield false;
  28 + }
14 29 });
15 30
16 31 require(["main"]);
15 build/tests/lib/js18/main-built.js
@@ -11,6 +11,21 @@ define('a',['require'],function (require) {
11 11 define('main',['require','a'],function (require) {
12 12 let a = require('a');
13 13 var {name, other} = a;
  14 +
  15 + var c = 1;
  16 + var d = 3;
  17 + [c, d] = [d, c];
  18 +
  19 + function generator() {
  20 + var i = 0;
  21 + while (i < 10) {
  22 + ++i;
  23 + if ((i % 2) == 0) {
  24 + yield true;
  25 + }
  26 + }
  27 + yield false;
  28 + }
14 29 });
15 30
16 31 require(["main"]);
16 build/tests/lib/js18/main.js
... ... @@ -1,4 +1,20 @@
1 1 define(function (require) {
2 2 let a = require('a');
3 3 var {name, other} = a;
  4 +
  5 + var c = 1;
  6 + var d = 3;
  7 +
  8 + [c, d] = [d, c];
  9 +
  10 + function generator() {
  11 + var i = 0;
  12 + while (i < 10) {
  13 + ++i;
  14 + if ((i % 2) == 0) {
  15 + yield true;
  16 + }
  17 + }
  18 + yield false;
  19 + }
4 20 });

0 comments on commit d5ec8da

Please sign in to comment.
Something went wrong with that request. Please try again.