Git Product home page Git Product logo

Comments (7)

xjamundx avatar xjamundx commented on May 18, 2024

I'm working on adding destructuring and default parameters of this stuff over into espree and am running into some trouble when using esprima as a reference.

This simple example that uses the shorthand version of destructuring defaults fails:

> esprima.parse("function a({b=10}){}")
Error: Line 1: Unexpected token {
    at createError (/Users/jamuferguson/dev/esprima/esprima.js:2004:21)
    at unexpectedTokenError (/Users/jamuferguson/dev/esprima/esprima.js:2072:13)
    at throwUnexpectedToken (/Users/jamuferguson/dev/esprima/esprima.js:2077:15)
    at parseVariableIdentifier (/Users/jamuferguson/dev/esprima/esprima.js:2981:17)
    at parseParam (/Users/jamuferguson/dev/esprima/esprima.js:3693:17)
    at parseParams (/Users/jamuferguson/dev/esprima/esprima.js:3722:22)
    at parseFunctionDeclaration (/Users/jamuferguson/dev/esprima/esprima.js:3764:15)
    at parseSourceElement (/Users/jamuferguson/dev/esprima/esprima.js:3841:24)
    at parseSourceElements (/Users/jamuferguson/dev/esprima/esprima.js:3877:29)
    at parseProgram (/Users/jamuferguson/dev/esprima/esprima.js:3894:16)

This nested destructuring with defaults example also fails in both master and harmony:

> esprima.parse("function a({b=10, a: {x=10}}){}")
Error: Line 1: Unexpected token {
    at createError (/Users/jamuferguson/dev/esprima/esprima.js:2004:21)
    at unexpectedTokenError (/Users/jamuferguson/dev/esprima/esprima.js:2072:13)
    at throwUnexpectedToken (/Users/jamuferguson/dev/esprima/esprima.js:2077:15)
    at parseVariableIdentifier (/Users/jamuferguson/dev/esprima/esprima.js:2981:17)
    at parseParam (/Users/jamuferguson/dev/esprima/esprima.js:3693:17)
    at parseParams (/Users/jamuferguson/dev/esprima/esprima.js:3722:22)
    at parseFunctionDeclaration (/Users/jamuferguson/dev/esprima/esprima.js:3764:15)
    at parseSourceElement (/Users/jamuferguson/dev/esprima/esprima.js:3841:24)
    at parseSourceElements (/Users/jamuferguson/dev/esprima/esprima.js:3877:29)
    at parseProgram (/Users/jamuferguson/dev/esprima/esprima.js:3894:16)

There's also some questions regarding what the correct filler is for the defaults array when you have less only some defaults applied. While the test shows [null, "value"] the actual parser appears to generate [undefined, "value"]

For a bit more context: eslint/espree#55

from esprima.

ariya avatar ariya commented on May 18, 2024

@xjamundx I can relate to that: destructuring is a beast and handling correctly it requires several attempts. Unless it is strongly related the issue of default parameter only, let's hold off the complication until the default parameter is implemented (which does not mean it is final, depending on how it goes with destructuring).

from esprima.

xjamundx avatar xjamundx commented on May 18, 2024

Sure, so the only point to add here is to ask for clarity around the filler for the defaults array when some are missing defaults. This test indicates that it should be null, but the actual parser shows:

> esprima.parse("function a(a=10, b){}").body[0].defaults
[ { type: 'Literal',
    value: 10,
    raw: '10' },
  undefined ]

from esprima.

jeffmo avatar jeffmo commented on May 18, 2024

cc @DmitrySoshnikov

(who's thought a lot about destructuring stuff while building transpilation support for it over in jstransform and recast)

from esprima.

jeffmo avatar jeffmo commented on May 18, 2024

(oops...this issue is more about default params than destructuring...by bad, let's track destructuring stuff in the destructuring issue :p )

from esprima.

michaelficarra avatar michaelficarra commented on May 18, 2024

destructuring is a beast

I disagree. Destructuring and defaults and rest parameters are not very hard at all. I added these to the Shift parser two weeks ago: shapesecurity/shift-parser-js#45

from esprima.

mikesherov avatar mikesherov commented on May 18, 2024

This has already been completed.

from esprima.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.