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

Missing an early error of ES2016 (ES7) #423

Closed
mysticatea opened this issue Jun 11, 2016 · 6 comments
Closed

Missing an early error of ES2016 (ES7) #423

mysticatea opened this issue Jun 11, 2016 · 6 comments

Comments

@mysticatea
Copy link
Contributor

mysticatea commented Jun 11, 2016

function foo(a = 1) {
  "use strict"
}

In ES2016, this code raises a syntax error.

It is a Syntax Error if ContainsUseStrict of FunctionBody is true and IsSimpleParameterList of FormalParameters is false.
https://tc39.github.io/ecma262/2016/#sec-function-definitions-static-semantics-early-errors

But Acorn (current HEAD: 7323217) succeeded parsing the code.

(Note: this is a valid syntax in ES2015.)


"use strict";

var acorn = require("./dist/acorn");
var ast = acorn.parse(`
function foo(a = 1) {
  "use strict"
}
`, {ecmaVersion: 7, sourceType: "script"});

console.log(JSON.stringify(ast, null, 4));
> node test.js
{
    "type": "Program",
    "start": 0,
    "end": 40,
    "body": [
        {
            "type": "FunctionDeclaration",
            "start": 1,
            "end": 39,
            "id": {
                "type": "Identifier",
                "start": 10,
                "end": 13,
                "name": "foo"
            },
            "generator": false,
            "expression": false,
            "params": [
                {
                    "type": "AssignmentPattern",
                    "start": 14,
                    "end": 19,
                    "left": {
                        "type": "Identifier",
                        "start": 14,
                        "end": 15,
                        "name": "a"
                    },
                    "right": {
                        "type": "Literal",
                        "start": 18,
                        "end": 19,
                        "value": 1,
                        "raw": "1"
                    }
                }
            ],
            "body": {
                "type": "BlockStatement",
                "start": 21,
                "end": 39,
                "body": [
                    {
                        "type": "ExpressionStatement",
                        "start": 25,
                        "end": 37,
                        "expression": {
                            "type": "Literal",
                            "start": 25,
                            "end": 37,
                            "value": "use strict",
                            "raw": "\"use strict\""
                        }
                    }
                ]
            }
        }
    ],
    "sourceType": "script"
}
@marijnh
Copy link
Member

marijnh commented Jun 11, 2016

We're not supporting much of ES2016 yet.

Do you happen to know why this innocent-looking form is marked as an early error?

@mysticatea
Copy link
Contributor Author

mysticatea commented Jun 11, 2016

Do you happen to know why this innocent-looking form is marked as an early error?

From a TC39 Meeting Note: https://github.com/rwaldron/tc39-notes/blob/master/es7/2015-07/july-29.md#611-the-scope-of-use-strict-with-respect-to-destructuring-in-parameter-lists

The main reason seems performance.

@marijnh
Copy link
Member

marijnh commented Jun 11, 2016

Thanks for the link!

@nzakas
Copy link
Contributor

nzakas commented Jun 15, 2016

@marijnh ES2016 is ES7, which Acorn supports (I helped add it,ecmaVersion: 7, it was just the ** operator).

A bit more info from my research: the problem is that strict mode is supposed to apply to function parameters when "use strict" is inside a function. That was okay in ES5 because the only real strict mode check affecting parameters was the double parameter error. However, in ES6, you can have default parameter values which might be functions or use values such as this. So the engine would have to parse through functions with potential nesting to n levels depth only to get to the end and find a "use strict" in the function body and have to rewind and start over again.

@nzakas
Copy link
Contributor

nzakas commented Jun 15, 2016

I'll try to add this.

@RReverser
Copy link
Member

@nzakas Thanks for the thorough explanation!

nzakas added a commit to nzakas/acorn that referenced this issue Jun 15, 2016
In ES2016, you can no longer have "use strict" in the body of a function
that has complex parameters (destructured or using default values).

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

No branches or pull requests

4 participants