unjs/ufo

SyntaxError: "missing ( before catch"

Kolobok12309 opened this issue · 2 comments

Environment

chrome 75.0.3770 and firefox 67 and other

Reproduction

Run ufo code from browser like chrome 75.0.3770

Describe the bug

SyntaxError: "missing ( before catch"

Error from this fragment

ufo/src/encoding.ts

Lines 117 to 123 in ab3d4eb

export function decode (text: string | number = ""): string {
try {
return decodeURIComponent("" + text);
} catch {
return "" + text;
}
}

Additional context

Simple replace catch { -> catch (err) { fix it

Logs

No response

Optional catch binding seems to have been supported since Chrome 66 and Firefox 58 (link).

Actually it is working as far as I tested in the following way.


  1. Create web directory in the root of this repository

  2. Create index.html in the directory with the content below:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Reproduction</title>
    </head>
    <body>
      <script type="module">
        import * as ufo from './index.js'
    
        console.log(ufo.decode('%3F'))
        console.log(ufo.decode('%malformed'))
      </script>
    </body>
    </html>
  3. Build ufo package (i.e. pnpm run build) and copy dist/index.mjs to web/index.mjs

  4. Run npx serve ./web and access to localhost:3000

  5. See the console

  6. '%3F' is successfully decoded to '?'

  7. '%malformed' throws an error internally (i.e. decodeURIComponent('%malformed')) and fell back to '%malformed'


We could investigate this issue in more depth with a reproduction. 🙌

@nozomuikuta In modern browsers it works as expected yes, my fail, need update babel for catching "optional catch"