escope icon indicating copy to clipboard operation
escope copied to clipboard

Add ES6 Support

Open Constellation opened this issue 10 years ago • 77 comments

Supporting ES6 block scopes and some special scopes (such as arraw function and method)

Constellation avatar Feb 16 '14 17:02 Constellation

Are you still going to do this? ESLint has a bug related to let scoping that could use this: https://github.com/eslint/eslint/issues/917

nzakas avatar Jun 06 '14 17:06 nzakas

Oh, OK. I'll work on it on weekend :)

Constellation avatar Jun 06 '14 17:06 Constellation

semantics in ES6 spec may be mature enough :)

Constellation avatar Jun 06 '14 17:06 Constellation

They are. I've documented it in my book: https://github.com/nzakas/understandinges6/blob/master/manuscript/01-The-Basics.md#block-bindings

nzakas avatar Jun 06 '14 17:06 nzakas

This would be awesome to have!

jlongster avatar Jul 10 '14 15:07 jlongster

Just checking in - any chance you're working on this? :)

nzakas avatar Sep 14 '14 21:09 nzakas

Hi @nzakas, Sorry for my further late reply.

I'm now gradually supporting ES6 in the tools, now escodegen supports ES6 Syntax completely. So, next, I'm planning to support ES6 in estraverse since it is used in the escope. Then, support scoping in escope.

Since there's significant difference between ES5 scopes and ES6 scopes, maybe I'll introduce ecmaVersion option.

Constellation avatar Sep 15 '14 19:09 Constellation

That sounds awesome!

nzakas avatar Sep 15 '14 20:09 nzakas

@Constellation what's the status on this? Is there anything we can do to help out? I'm working on trying to get an ES6-supported version of ESlint using the harmony branch of esprima, so my next step is to approach ES6 support in escope and estraverse. :)

elwayman02 avatar Oct 15 '14 19:10 elwayman02

OK, so, let's start to support ES6. I'll start it with estraverse.

Constellation avatar Oct 18 '14 15:10 Constellation

Support ES6 traversing in estraverse. https://github.com/Constellation/estraverse/commit/19f7a5c5dbb7bcaca3226d3cd608e59dbea1a275

Constellation avatar Oct 18 '14 17:10 Constellation

This would really help for getting eslint running on jsx

StoneCypher avatar Nov 11 '14 08:11 StoneCypher

Any update on this? Definently keen to use this for 6to5. Reference 6to5/6to5#175

sebmck avatar Nov 16 '14 06:11 sebmck

Currently, we're investigating the ES6 spec and Esprima AST nodes. And We're now updating escodegen and estraverse. Does anyone knows the issue about them? I think estraverse now completely support ES6 syntax. If it's correct, my mental model of ES6 AST may be stable.

Constellation avatar Nov 16 '14 13:11 Constellation

Main issues you'll run into is implementing temporal dead zones. Default parameters have their own scope and you can't access let/const variables before they're initialised.

sebmck avatar Nov 16 '14 13:11 sebmck

Main issues you'll run into is implementing temporal dead zones. Default parameters have their own scope and you can't access let/const variables before they're initialised.

Nice, it's very useful information. And we need to consider destructuring assigned variables.

var { a, b, c } = obj;  // it materialize a, b, c variables.

Constellation avatar Nov 16 '14 13:11 Constellation

And we need to ensure,

for (let i = 0; i < 10; ++i) { console.log(i); }

scope behavior.

Constellation avatar Nov 16 '14 14:11 Constellation

We're now implementing scopes for ES6. But be careful, it's experimental and the behavior might be changed

Constellation avatar Nov 16 '14 14:11 Constellation

@sebmck:

Do you know the actual section in the ES6 draft (rev28)? I'm now investigating it.

Constellation avatar Nov 16 '14 14:11 Constellation

@Constellation For what specifically?

sebmck avatar Nov 16 '14 14:11 sebmck

@sebmck I think sticking to the formal spec is very important ;) It provides a clear reason to "why it is so implemented". And makes the tool stable.

Constellation avatar Nov 16 '14 15:11 Constellation

@Constellation Yeah I understand that. I was referring to what section specifically that you were looking for.

sebmck avatar Nov 16 '14 15:11 sebmck

@sebmck Ah, I've misunderstood.

Default parameters have their own scope.

I'm looking for it. Now seeing 9.2.13, http://people.mozilla.org/~jorendorff/es6-draft.html#sec-functiondeclarationinstantiation

Constellation avatar Nov 16 '14 15:11 Constellation

Added class declaration / expression scope support.

Constellation avatar Nov 16 '14 19:11 Constellation

Remaining TODO is,

  • Destructuring assignments
    • [x] VariableDeclaration
    • [x] Assignment
    • [x] Parameters
  • Initializers
    • [ ] Default parameters
    • [ ] Initializers in Destructuring Assignments
  • [x] MethodDefinition's strictness
  • [x] import declaration
  • [x] export declaration
  • [x] For/ForIn/ForOf special block scope
  • [x] Class
  • [x] TemplateLiteral

Please feel free to modify this list. Now, 4:00 JST, so I'll sleep today...

Constellation avatar Nov 16 '14 19:11 Constellation

There's also the import statement.

nzakas avatar Nov 17 '14 00:11 nzakas

@nzakas Nice. I've added it to TODO list.

Constellation avatar Nov 17 '14 01:11 Constellation

Destructuring assignments support is partially done. Initializers / Default parameters are not supported yet. Maybe need to change Esprima's AST architecture.

Constellation avatar Nov 19 '14 19:11 Constellation

Seeing the ClassDefinitionEvaluation1, we attempt to create class body scope only when the className is provided. However, in this case, computed property names are evaluated in the upper scope, so sometimes it is not strict scope. But spec says, ClassBody is strict code. I think it may be a spec bug. In this time, I always create the scope for strictness of scope. And I'll open the issue to TC39 bugzilla.

edit: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-strict-mode-code I'll investigate more.

Constellation avatar Nov 23 '14 21:11 Constellation

@ariya @michaelficarra Found Esprima's bug,

esprima.parse('try{}catch({a,b,c}){}').body[0].handlers[0].param

Should return ObjectPattern, but returns ObjectExpression.

Constellation avatar Nov 23 '14 21:11 Constellation