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

Support optional try/catch binding as added in Node 10 & V8 6.6 #1953

Closed
Avuxo opened this issue Oct 19, 2018 · 6 comments · Fixed by #2050
Closed

Support optional try/catch binding as added in Node 10 & V8 6.6 #1953

Avuxo opened this issue Oct 19, 2018 · 6 comments · Fixed by #2050

Comments

@Avuxo
Copy link

Avuxo commented Oct 19, 2018

Support for this ECMAScript Stage 4 proposal. This has already been implemented in v8 6.6 and as a result is already implemented in new versions of Google Chrome/Chromium and Node.js 10. This language feature is used in the Node.js `assert' standard library, so any users who attempt to apply the parser to that file will encounter this error.

Previously, try/catch statements required a binding to be syntactically valid. However, with this change they are no longer a requirement as the binding is now optional.

try {
    // something exceptional
} catch {
    // no longer has a required parameter
}

This issue would be a fairly simple implementation and would likely not add any significant overhead to the parsing process.

Steps to reproduce

// run in any environment using V8 6.6: Google Chrome or Node 10.x
esprima.parse('try { new Error(); } catch {}');

Expected output

// AST
{
  "type": "Program",
  "body": [
    {
      "type": "TryStatement",
      "block": {
        "type": "BlockStatement",
        "body": [
          {
            "type": "ExpressionStatement",
            "expression": {
              "type": "NewExpression",
              "callee": {
                "type": "Identifier",
                "name": "Error"
              },
              "arguments": []
            }
          }
        ]
      },
      "handler": {
        "type": "CatchClause",
        "param": null,
        "body": {
          "type": "BlockStatement",
          "body": []
        }
      },
      "finalizer": null
    }
  ],
  "sourceType": "script"
}

Actual output

/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:1995
	        throw this.unexpectedTokenError(token, message);
	        ^
Error: Line 1: Unexpected token {
    at ErrorHandler.constructError (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:5012:22)
    at ErrorHandler.createError (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:5028:27)
    at Parser.unexpectedTokenError (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:1985:39)
    at Parser.throwUnexpectedToken (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:1995:21)
    at Parser.expect (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:2168:19)
    at Parser.parseCatchClause (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:4049:15)
    at Parser.parseTryStatement (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:4080:58)
    at Parser.parseStatement (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:4157:43)
    at Parser.parseStatementListItem (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:3388:39)
    at Parser.parseScript (/Users/ben/workspace/testing/catch-binding/node_modules/esprima/dist/esprima.js:4723:29)

Relevant references

@ariya
Copy link
Contributor

ariya commented Oct 19, 2018

Hi @Avuxo, thanks for the report.

For a new language feature, we usually need more details, see this guide. For some examples, see e.g. #1550. Please review the issue description to include the necessary information.

@JosephPecoraro
Copy link
Contributor

JosephPecoraro commented Aug 13, 2019

This syntax has been supported in browsers for nearly a year! Lets see what we can do to move this forward.

@JosephPecoraro
Copy link
Contributor

JosephPecoraro commented Aug 13, 2019

ES2019 Feature: Optional catch binding: Tracking Issue

Syntax:

try { } catch { }

New production for Catch that just doesn't require a binding:

 Catch[Yield, Await, Return]:
     catch ( CatchParameter[?Yield, ?Await] ) Block[?Yield, ?Await, ?Return]
+    catch Block[?Yield, ?Await, ?Return]

Spec:

https://github.com/tc39/proposal-optional-catch-binding
https://tc39.es/proposal-optional-catch-binding/

Remaining Tasks:

Additional considerations

  • Supported and shipped in all major Browsers: Safari, Chrome, Firefox
  • Supported in JavaScript environments: jsc, node
  • Proposed AST change is that CatchClause will now have an optional (null) param

@JosephPecoraro
Copy link
Contributor

This was added to ESTree for ES2019:
https://github.com/estree/estree/blob/master/es2019.md


This document specifies the extensions to the core ESTree AST types to support the ES2019 grammar.

Statements

CatchClause

extend interface CatchClause {
    param: Pattern | null;
}

The param is null if the catch binding is omitted. E.g., try { foo() } catch { bar() }

yury-s pushed a commit to yury-s/webkit-http that referenced this issue Aug 16, 2019
…eatures

https://bugs.webkit.org/show_bug.cgi?id=200796

Reviewed by Ross Kirsling.

Source/WebInspectorUI:

Use a fork of Esprima to support modern JavaScript language features
while going through the process to upstream support:

  ES2018 Feature: Async Iteration (for-await-of)
  jquery/esprima#1990

  ES2019 Feature: Numeric Separator
  jquery/esprima#1989

  ES2019 Feature: Optional catch binding
  jquery/esprima#1953

  ES2020 Feature: BigInt
  jquery/esprima#1988

ESTree compatible AST changes are summarized as:

  - CatchClause `param` property is now nullable
  - ForOfStatement now has a boolean `await` property
  - Literal can be a `"bigint"` type (works if the environment has BigInt or not)

The pretty printer only needed to have additional handling for `for-await-of`.

* UserInterface/External/Esprima/esprima.js:
New version. Typescript output expects a modern JavaScript environment
instead of just ES6.

* Tools/Formatting/index.html:
Update the formatting tool for easier use in case of errors.

* UserInterface/Models/ScriptSyntaxTree.js:
(WI.ScriptSyntaxTree.prototype._createInternalSyntaxTree):

* UserInterface/Test/TestHarness.js:
(TestHarness.prototype.passOrFail):
Convenience for pass/fail with the same message based on a condition.

* UserInterface/Workers/Formatter/EsprimaFormatter.js:
(EsprimaFormatter.prototype._handleTokenAtNode):
Ensure a space after `await` in `for await` syntax.

LayoutTests:

* inspector/formatting/resources/javascript-tests/classes-expected.js:
* inspector/formatting/resources/javascript-tests/classes.js:
* inspector/formatting/resources/javascript-tests/for-statements-expected.js:
* inspector/formatting/resources/javascript-tests/for-statements.js:
* inspector/formatting/resources/javascript-tests/generators-expected.js:
* inspector/formatting/resources/javascript-tests/generators.js:
* inspector/formatting/resources/javascript-tests/numbers-expected.js: Added.
* inspector/formatting/resources/javascript-tests/numbers.js: Added.
* inspector/formatting/resources/javascript-tests/try-catch-finally-statements-expected.js:
* inspector/formatting/resources/javascript-tests/try-catch-finally-statements.js:
* inspector/formatting/resources/javascript-tests/unary-binary-expressions-expected.js:
* inspector/formatting/resources/javascript-tests/unary-binary-expressions.js:
Test formatting of new JavaScript language features.

* inspector/formatting/formatting-css-expected.txt:
* inspector/formatting/formatting-javascript-expected.txt:
* inspector/formatting/formatting-javascript.html:
* inspector/formatting/resources/utilities.js:
(TestPage.registerInitializer.async.runFormattingTest):
(TestPage.registerInitializer.window.addFormattingTests):
(TestPage.registerInitializer):
Cleaner output and better handling for debugging failures.

* inspector/model/parse-script-syntax-tree.html:
Test new AST permutations.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@248760 268f45cc-cd09-0410-ab3c-d52691b4dbfc
@ariya
Copy link
Contributor

ariya commented Sep 14, 2019

Thanks for adding some more important details @JosephPecoraro!

@darrnshn
Copy link

darrnshn commented Apr 9, 2020

Any update on this? It still seems to be invalid on the demo page: https://esprima.org/demo/parse.html?code=try%20%7B%7D%20catch%20%7B%7D

We would really appreciate this fix! Thanks!

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

Successfully merging a pull request may close this issue.

4 participants