Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught SyntaxError: invalid escape sequence in regular expression #71

Open
nickwesselman opened this issue Jan 26, 2022 · 1 comment

Comments

@nickwesselman
Copy link
Contributor

While experimenting with a solution that uses javy, I happened to include the camelcase package. When attempting to build, I get the error below.

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Uncaught SyntaxError: invalid escape sequence in regular expression
    at RegExp (native)
    at <eval> (<input>:10)
    at ./node_modules/camelcase/index.js (script.js:39)
    at __webpack_require__ (script.js:63)
    at <eval> (<input>:2)
    at ./src/index.js (script.js:29)
    at __webpack_require__ (script.js:63)
    at <anonymous> (script.js:115)
    at <anonymous> (script.js:118)
    at webpackUniversalModuleDefinition (script.js:15)
    at <eval> (script.js:120)
', crates/core/src/lib.rs:35:61
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: the `wizer.initialize` function trapped

Caused by:
    wasm trap: wasm `unreachable` instruction executed
    wasm backtrace:
        0: 0xcb9d9 - <unknown>!__rust_start_panic
    note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable to may show more debugging information

Error: Couldn't create wasm from input

Reproduction here, I was able to isolate the issue to the following lines in camelcase:

const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
const SEPARATORS = /[_.\- ]+/;
const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);

// THIS LINE APPEARS TO BE THE ISSUE
const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');

Not sure what the exact issue is, or how common it might be in other code.

@saulecabrera
Copy link
Member

Good catch. I spent some time investigating this and I was able to the get to the bottom of the issue: passing the unicode flag to a regular expression puts the regular expression engine in "strict mode", in plain words this means that some escape sequences, such as \-, \: will be considered erroneous (ref); in that sense the error can be considered expected, what's unexpected here is the behaviour across engines, in V8 for example, using the RegExp constructor and passing the u flag, doesn't cause an error, but when constructing the regular expression through a literal with an invalid escape sequence, the error appears:

var r = new RegExp("\-", 'u');
console.log(r) //  /-/u => the escape was removed

// vs 

/\:/u // Results in Invalid regular expression: /\:/: Invalid escape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants