Comments (7)
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.
@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.
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.
(who's thought a lot about destructuring stuff while building transpilation support for it over in jstransform
and recast
)
from esprima.
(oops...this issue is more about default params than destructuring...by bad, let's track destructuring stuff in the destructuring issue :p )
from esprima.
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.
This has already been completed.
from esprima.
Related Issues (20)
- npm ERR! 404 Not Found - GET https://codeload.github.com/ariya/esprima/legacy.tar.gz/master HOT 1
- Merge fixes from Contrast-Security-Inc's Fork
- npm audit report 29 vulnerabilities HOT 1
- esprima-next -> fork for development and npm package HOT 1
- High Surrogate Unicode value is wrong. HOT 1
- Replace JSF CLA with OpenJS CLA HOT 1
- Unused packages detected in the esprima project on Tag: 4.0.1 HOT 1
- [feature] esprima cannot parse bigint HOT 1
- JSX Identify the problem HOT 3
- Fails to parse access to import.meta HOT 1
- Esprima fails to parse classes that have public fields HOT 2
- Static property fails to parse HOT 1
- Feature request: Optional chaining operator HOT 9
- syntax error by rest parameter of spread syntax HOT 4
- AST_CALL is not applied on dictionary argument in a function
- Invalid UpdateExpression's arguments
- Parsing `-999999984306749440;` turns into `999999984306749400` HOT 1
- Broken link in file `docs/syntax-tree-format.md`
- Unexpected Syntax Error
- Is this project abandoned? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esprima.