sindresorhus / normalize-url

Normalize a URL
MIT License
837 stars 123 forks source link

Add `preferJsRegexpLookbehind` option (#105) #145

Closed loynoir closed 3 years ago

loynoir commented 3 years ago

As SyntaxError can not be try-catch, use new RegExp to avoid js parser error.

sindresorhus commented 3 years ago

I'm unfortunately not interested in an option like this.

loynoir commented 3 years ago

Hi, @sindresorhus I remove that in option, what do you think if I change that to detect-fallback?

I think at least one line should be changed https://github.com/sindresorhus/normalize-url/blob/3fb24bb03e340162b2e81d6145de8e4e5b3d6262/index.js#L124

As I don't have an environment lack of js-regexp-lookbehind, I use unbalance /(/ for damonstration

In node.js is OK, ok fn is not held down by SyntaxError.

> console.log('ok fn'); try { /(/ } catch(e) {console.log('err fn') }
ok fn
err fn

But in browser, ok fn is not even ran, which is held down by SyntaxError.

// Uncaught SyntaxError: unterminated parenthetical


- Expected
```js
var s2=document.createElement('script')
s2.innerText=`console.log('ok fn'); try { new RegExp('(') } catch(e) {console.log('err fn') }`
document.head.appendChild(s2)

// ok fn
// err fn