Skip to content

Commit

Permalink
add support for unicode regexes - closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmck committed Oct 9, 2014
1 parent 66b3475 commit 21f039e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,7 @@ It's as easy as:
- [Rest parameters](FEATURES.md#rest-parameters)
- [Spread](FEATURES.md#spread)
- [Template literals](FEATURES.md#template-literals)
- [Unicode Regex](FEATURES.md#unicode-regex)

To be implemented:

Expand Down Expand Up @@ -243,6 +244,7 @@ limitations in ES5 implementations.
| Rest parameters |||||||
| Spread |||||| |
| Template literals |||||||
| Unicode regex ||| ||| |

This comment has been minimized.

Copy link
@mathiasbynens

mathiasbynens Oct 9, 2014

Contributor

Does es6now really support this? It doesn’t seem to:

$ cat x.js
var string = 'foo\uD834\uDF06bar';
console.log(/foo.bar/u.test(string));

$ es6now x.js
/private/tmp/x.js:2
console.log(/foo.bar/u.test(string));
       ^
SyntaxError: Invalid flags supplied to RegExp constructor 'u'

cc @zenparsing

This comment has been minimized.

Copy link
@sebmck

sebmck Oct 9, 2014

Author Contributor

Yeah you're right. Not sure where I got that from.

This comment has been minimized.

Copy link
@zenparsing

zenparsing Oct 9, 2014

Contributor

That's right. Also, @sebmck you should probably remove array comprehensions and generator comprehensions from your feature matrix, since they are not going to be a part of ES6 (and will probably never be a part of ES in that form).


#### Performance

Expand Down
3 changes: 2 additions & 1 deletion lib/6to5/transform.js
Expand Up @@ -107,5 +107,6 @@ transform.transformers = {
blockBinding: require("./transformers/block-binding"),
modules: require("./transformers/modules"),
restParameters: require("./transformers/rest-parameters"),
forOf: require("./transformers/for-of")
forOf: require("./transformers/for-of"),
unicodeRegex: require("./transformers/unicode-regex")
};
15 changes: 15 additions & 0 deletions lib/6to5/transformers/unicode-regex.js
@@ -0,0 +1,15 @@
var regexpu = require("regexpu");
var b = require("ast-types").builders;
var _ = require("lodash");

exports.Literal = function (node) {
var regex = node.regex;
if (!regex) return;

var flags = regex.flags.split("")
if (!_.contains(regex.flags, "u")) return;
_.pull(flags, "u");

var pattern = regexpu.rewritePattern(regex.pattern, regex.flags);
return b.literal(new RegExp(pattern, flags.join("")));
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -46,7 +46,8 @@
"lodash": "2.4.1",
"mkdirp": "0.5.0",
"es6-shim": "^0.18.0",
"es6-symbol": "^0.1.1"
"es6-symbol": "^0.1.1",
"regexpu": "^0.2.2"
},
"devDependencies": {
"es6-transpiler": "0.7.17",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/unicode-regex/basic/actual.js
@@ -0,0 +1,2 @@
var string = 'foo💩bar';
var match = string.match(/foo(.)bar/u);
2 changes: 2 additions & 0 deletions test/fixtures/unicode-regex/basic/expected.js
@@ -0,0 +1,2 @@
var string = 'foo\uD83D\uDCA9bar';
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]))bar/);

0 comments on commit 21f039e

Please sign in to comment.