google / closure-compiler

A JavaScript checker and optimizer.

Home Page:https://developers.google.com/closure/compiler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

regular expression with Unicode flag causes Parse error

josiahcarlson opened this issue · comments

When compiling regular expressions with the unicode flag set as part of /<regex>/gu, the u causes the compiler to choke.

As an example...

root@250c93857c43:/# cat /js/test.js
"hello".search(/\p{gc=Ll}/gu)
root@250c93857c43:/# closure-compiler --js /js/test.js --js_output_file /js/opt/test.js
/js/test.js:1: ERROR - Parse error. invalid flag after regular expression
"hello".search(/\p{gc=Ll}/gu)
               ^

1 error(s), 0 warning(s)

Error persists with the full entity name without the abbreviation:

root@250c93857c43:/# closure-compiler --js /js/test.js --js_output_file /js/opt/test.js
/js/test.js:1: ERROR - Parse error. invalid flag after regular expression
"hello".search(/\p{General_Category=Lowercase_Letter}/gu)
               ^

1 error(s), 0 warning(s)

When omitting the u, the compiler works...

root@250c93857c43:/# cat js/test.js
"hello".search(/\p{gc=Ll}/g)
root@250c93857c43:/# closure-compiler --js /js/test.js --js_output_file /js/opt/test.js

But obviously the regular expression now no longer does what it's supposed to do:

> "hello".search(/\p{gc=Ll}/gu)
> 0
> "hello".search(/\p{gc=Ll}/g)
> -1 

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape#browser_compatibility , this functionality is supposed to exist in all modern javascript / ecmascript runtimes.

Which version of closure-compiler are you running? This works for me:

$ cat test.js
"hello".search(/\p{gc=Ll}/gu)
$ npm i --save google-closure-compiler
$ ./node_modules/.bin/google-closure-compiler --js test.js --js_output_file test_out.js
$ cat test_out.js
"hello".search(/\p{gc=Ll}/gu);
$ ./node_modules/.bin/google-closure-compiler --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20230502

Great question...

Closure Compiler (http://code.google.com/closure/compiler)
Version: 20130227
Built on: 2020/10/28 19:53

I don't even know how I got a version that old. My goodness.

Sorry for the noise!

FYI, even with an updated closure-compiler, you'll need to tell the compiler to generate at least ES_2018 language out (--language_out=ES_2018). If you don't, the compiler will report an error that is basically telling you that it does not transpile regular expressions to work for older versions of the JS language.