@@ -17,6 +17,30 @@ const { characterClassFixtures } = require("./fixtures/character-class.js");
1717const { unicodeSetFixtures } = require ( "./fixtures/unicode-set.js" ) ;
1818const { modifiersFixtures } = require ( "./fixtures/modifiers.js" ) ;
1919
20+ /** For node 6 compat */
21+ assert . match || ( assert . match = function match ( value , regex ) { assert . ok ( regex . exec ( value ) !== null ) } ) ;
22+ assert . doesNotMatch || ( assert . doesNotMatch = function doesNotMatch ( value , regex ) { assert . ok ( regex . exec ( value ) === null ) } ) ;
23+
24+ /**
25+ * comput output regex flags from input flags and transform options
26+ *
27+ * @param {string } inputFlags
28+ * @param {* } regexpuOptions
29+ */
30+ function getOutputFlags ( inputFlags , options ) {
31+ let result = inputFlags ;
32+ if ( options . unicodeSetsFlag === "transform" ) {
33+ result = result . replace ( "v" , "u" ) ;
34+ }
35+ if ( options . unicodeFlag === "transform" ) {
36+ result = result . replace ( "u" , "" ) ;
37+ }
38+ if ( options . dotAllFlag === "transform" ) {
39+ result = result . replace ( "s" , "" ) ;
40+ }
41+ return result ;
42+ }
43+
2044describe ( 'rewritePattern { unicodeFlag }' , ( ) => {
2145 const options = {
2246 'unicodeFlag' : 'transform'
@@ -95,19 +119,19 @@ describe('unicodePropertyEscapes', () => {
95119 ) ;
96120 assert . equal (
97121 rewritePattern ( '[^\\p{ASCII_Hex_Digit}_]' , 'u' , features ) ,
98- '(?:[\\0-\\/:-@G-\\^`g-\\uD7FF\\uE000-\\ uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^) [\\uDC00-\\uDFFF])'
122+ '(?:[\\0-\\/:-@G-\\^`g-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])'
99123 ) ;
100124 assert . equal (
101125 rewritePattern ( '[\\P{Script_Extensions=Anatolian_Hieroglyphs}]' , 'u' , features ) ,
102- '(?:[\\0-\\uD7FF\\uE000-\\ uFFFF]|[\\uD800-\\uD810\\uD812-\\uDBFF][\\uDC00-\\uDFFF]|\\uD811[\\uDE47-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00 -\\uDFFF])'
126+ '(?:[\\0-\\uFFFF]|[\\uD800-\\uD810\\uD812-\\uDBFF][\\uDC00-\\uDFFF]|\\uD811[\\uDE47-\\uDFFF])'
103127 ) ;
104128 assert . equal (
105129 rewritePattern ( '[\\p{Script_Extensions=Anatolian_Hieroglyphs}_]' , 'u' , features ) ,
106130 '(?:_|\\uD811[\\uDC00-\\uDE46])'
107131 ) ;
108132 assert . equal (
109133 rewritePattern ( '[\\P{Script_Extensions=Anatolian_Hieroglyphs}_]' , 'u' , features ) ,
110- '(?:[\\0-\\uD7FF\\uE000-\\ uFFFF]|[\\uD800-\\uD810\\uD812-\\uDBFF][\\uDC00-\\uDFFF]|\\uD811[\\uDE47-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00 -\\uDFFF])'
134+ '(?:[\\0-\\uFFFF]|[\\uD800-\\uD810\\uD812-\\uDBFF][\\uDC00-\\uDFFF]|\\uD811[\\uDE47-\\uDFFF])'
111135 ) ;
112136 assert . equal (
113137 rewritePattern ( '(?:\\p{ASCII_Hex_Digit})' , 'u' , features ) ,
@@ -219,10 +243,10 @@ describe('unicodePropertyEscapes', () => {
219243 '[\\u{14400}-\\u{14646}]'
220244 ) ;
221245 assert . equal (
222- rewritePattern ( '[\\p {Script_Extensions=Anatolian_Hieroglyphs}]' , 'u' , {
246+ rewritePattern ( '[\\P {Script_Extensions=Anatolian_Hieroglyphs}]' , 'u' , {
223247 'unicodePropertyEscapes' : 'transform' ,
224248 } ) ,
225- '[\\u{14400} -\\u{14646 }]'
249+ '[\\0-\\u{143FF}\\u{14647} -\\u{10FFFF }]'
226250 ) ;
227251 } ) ;
228252 it ( 'should not transpile unicode property when unicodePropertyEscapes is not enabled' , ( ) => {
@@ -391,13 +415,50 @@ describe('character classes', () => {
391415 if ( transpiled != '(?:' + expected + ')' ) {
392416 assert . strictEqual ( transpiled , expected ) ;
393417 }
418+ for ( const match of fixture . matches || [ ] ) {
419+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flags , options ) ) ;
420+ assert . match ( match , transpiledRegex ) ;
421+ }
422+ for ( const nonMatch of fixture . nonMatches || [ ] ) {
423+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flags , options ) ) ;
424+ assert . doesNotMatch ( nonMatch , transpiledRegex ) ;
425+ }
394426 } ) ;
395427 }
396428} ) ;
397429
398430
399431
400432describe ( 'unicodeSets (v) flag' , ( ) => {
433+ // Re-use the unicode fixtures but replacing the input pattern's `u` flag with `v` flag
434+ for ( const fixture of unicodeFixtures ) {
435+ if ( fixture . flags . includes ( "u" ) ) {
436+ for ( let flag of fixture . flags ) {
437+ flag = flag . replace ( "u" , "v" ) ;
438+ const { pattern, transpiled : expected } = fixture ;
439+ const inputRE = `/${ pattern } /${ flag } ` ;
440+ it ( `rewrites \`${ inputRE } \` correctly without using the u flag` , ( ) => {
441+ const options = {
442+ unicodeSetsFlag : "transform" ,
443+ unicodeFlag : "transform" ,
444+ } ;
445+ const transpiled = rewritePattern ( pattern , flag , options ) ;
446+ if ( transpiled != "(?:" + expected + ")" ) {
447+ assert . strictEqual ( transpiled , expected ) ;
448+ }
449+ for ( const match of fixture . matches || [ ] ) {
450+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flag , options ) ) ;
451+ assert . match ( match , transpiledRegex ) ;
452+ }
453+ for ( const nonMatch of fixture . nonMatches || [ ] ) {
454+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flag , options ) ) ;
455+ assert . doesNotMatch ( nonMatch , transpiledRegex ) ;
456+ }
457+ } ) ;
458+ }
459+ }
460+ }
461+
401462 if ( IS_NODE_6 ) return ;
402463
403464 for ( const fixture of unicodeSetFixtures ) {
@@ -421,12 +482,20 @@ describe('unicodeSets (v) flag', () => {
421482 } , throws ) ;
422483 } ) ;
423484 } else {
485+ const transpiled = rewritePattern ( pattern , flags , options ) ;
424486 it ( `rewrites \`${ inputRE } \` correctly ${ transformUnicodeFlag ? 'without ' : '' } using the u flag` , ( ) => {
425- const transpiled = rewritePattern ( pattern , flags , options ) ;
426487 if ( transpiled != '(?:' + expected + ')' ) {
427488 assert . strictEqual ( transpiled , expected ) ;
428489 }
429490 } ) ;
491+ for ( const match of fixture . matches || [ ] ) {
492+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flags , options ) ) ;
493+ assert . match ( match , transpiledRegex ) ;
494+ }
495+ for ( const nonMatch of fixture . nonMatches || [ ] ) {
496+ const transpiledRegex = new RegExp ( `^${ transpiled } $` , getOutputFlags ( flags , options ) ) ;
497+ assert . doesNotMatch ( nonMatch , transpiledRegex ) ;
498+ }
430499 }
431500 }
432501
0 commit comments