Repository: DmitrySoshnikov/regexp-tree (js)

Issue:
Broken optimization `/^[\p{Mc}\p{Me}\p{Mn}]$/` -> `/^[Mcen\p{}]$/`
```
invalid Unicode escape \p
```

Or

```js
SyntaxError: Invalid regular expression: /^[Mcen\p{}]$/: Invalid property name in character class
```

Pull request:
Return sort value for UnicodeProperty
closes #239

Patch:
diff --git a/src/optimizer/transforms/char-class-classranges-merge-transform.js b/src/optimizer/transforms/char-class-classranges-merge-transform.js
index ae531cd..c766558 100644
--- a/src/optimizer/transforms/char-class-classranges-merge-transform.js
+++ b/src/optimizer/transforms/char-class-classranges-merge-transform.js
@@ -24,7 +24,7 @@ module.exports = {
 
     const metas = [];
     // Extract metas
-    expressions.forEach(expression => {
+    expressions.forEach((expression) => {
       if (isMeta(expression)) {
         metas.push(expression.value);
       }
@@ -89,7 +89,7 @@ function sortCharClass(a, b) {
 }
 
 /**
- * @param {Object} expression - Char or ClassRange node
+ * @param {Object} expression - Char, ClassRange or UnicodeProperty node
  * @returns {number}
  */
 function getSortValue(expression) {
@@ -105,6 +105,11 @@ function getSortValue(expression) {
     }
     return expression.codePoint;
   }
+
+  if (expression.type === 'UnicodeProperty') {
+    return -1;
+  }
+
   // ClassRange
   return expression.from.codePoint;
 }


Test patch:
diff --git a/src/optimizer/transforms/__tests__/char-class-classranges-merge-transform-test.js b/src/optimizer/transforms/__tests__/char-class-classranges-merge-transform-test.js
index 520ffb4..9a81148 100644
--- a/src/optimizer/transforms/__tests__/char-class-classranges-merge-transform-test.js
+++ b/src/optimizer/transforms/__tests__/char-class-classranges-merge-transform-test.js
@@ -159,6 +159,13 @@ describe('char-class-classranges-merge', () => {
     expect(re.toString()).toBe('/[\\ud83d\\ude88-\\ud83d\\ude8a]/u');
   });
 
+  it('does not combine unicode property chars with \\w chars', () => {
+    const re = transform('/[\\w\\p{L}]/u', [
+      charClassClassrangesMerge,
+    ]);
+    expect(re.toString()).toBe('/[\\w\\p{L}]/u');
+  });
+
   it('does not combine sequential chars that are nor \\w chars nor char codes', () => {
     const re = transform(/[<=>?]/, [
       charClassClassrangesMerge,
@@ -166,4 +173,4 @@ describe('char-class-classranges-merge', () => {
     expect(re.toString()).toBe('/[<=>?]/');
   });
 
-});
\ No newline at end of file
+});