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

ReturnStatement parse bug when code like "return `\nabc`;" #1829

Closed
fishbar opened this Issue May 3, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@fishbar
Contributor

fishbar commented May 3, 2017

Steps to reproduce

code1:

function a(){
  return `\na`;
}

code2:

function a(){
  return `
a`;
}
let ast1 = esprima.parse(code1);
let ast2 = esprima.parse(code2);

Expected output

both v3.1.3 and v4.0.0-dev have the same output

// ast1, it's fine ,  ReturnStatement with correct argument property
{
    "type": "Program",
    "body": [
        {
            "type": "FunctionDeclaration",
            "id": {
                "type": "Identifier",
                "name": "a"
            },
            "params": [],
            "body": {
                "type": "BlockStatement",
                "body": [
                    {
                        "type": "ReturnStatement",
                        "argument": {
                            "type": "TemplateLiteral",
                            "quasis": [
                                {
                                    "type": "TemplateElement",
                                    "value": {
                                        "raw": "\\na",
                                        "cooked": "\na"
                                    },
                                    "tail": true
                                }
                            ],
                            "expressions": []
                        }
                    }
                ]
            },
            "generator": false,
            "expression": false
        }
    ],
    "sourceType": "script"
}
//  ast2  it is wrong , function body should only contain single node : ReturnStatement
//           but the following ast  have  ExpressionStatement after ReturnStatement,  and ReturnStatement.argument is null
{
    "type": "Program",
    "body": [
        {
            "type": "FunctionDeclaration",
            "id": {
                "type": "Identifier",
                "name": "a"
            },
            "params": [],
            "body": {
                "type": "BlockStatement",
                "body": [
                    {
                        "type": "ReturnStatement",
                        "argument": null
                    },
                    {
                        "type": "ExpressionStatement",
                        "expression": {
                            "type": "TemplateLiteral",
                            "quasis": [
                                {
                                    "type": "TemplateElement",
                                    "value": {
                                        "raw": "\na",
                                        "cooked": "\na"
                                    },
                                    "tail": true
                                }
                            ],
                            "expressions": []
                        }
                    }
                ]
            },
            "generator": false,
            "expression": false
        }
    ],
    "sourceType": "script"
}
@fishbar

This comment has been minimized.

Show comment
Hide comment
@fishbar

fishbar May 3, 2017

Contributor

@ariya try other parsers in https://astexplorer.net/ , seams esprima and uglify both have this bug

Contributor

fishbar commented May 3, 2017

@ariya try other parsers in https://astexplorer.net/ , seams esprima and uglify both have this bug

fishbar added a commit to estools2/esprima that referenced this issue May 12, 2017

fishbar added a commit to estools2/esprima that referenced this issue May 12, 2017

@felixhageloh

This comment has been minimized.

Show comment
Hide comment
@felixhageloh

felixhageloh Jun 13, 2017

I think this might be related. Given code1:

function () {
  return "\
    a\
    b\
  ";
}

doing:

 escodegen.generate(esprima.parse(code1));

produces:

function () {
    return;
     '    a    b  ';
}

expected outcome:

function () {
    return '    a    b  ';
}

v3.x to 4 have this behavior. 2.7.x still produces the expected outcome.

I think this might be related. Given code1:

function () {
  return "\
    a\
    b\
  ";
}

doing:

 escodegen.generate(esprima.parse(code1));

produces:

function () {
    return;
     '    a    b  ';
}

expected outcome:

function () {
    return '    a    b  ';
}

v3.x to 4 have this behavior. 2.7.x still produces the expected outcome.

felixhageloh added a commit to felixhageloh/uebersicht that referenced this issue Oct 8, 2017

downgrades esprima
version 3 and 4 have a bug with returning multiline strings
jquery/esprima#1829

GerHobbelt added a commit to GerHobbelt/esprima that referenced this issue Oct 15, 2017

correct spec file for commit SHA-1: 2617445 :: fix #1829, returnstate…
…ment with multiline template literal string

felixhageloh added a commit to felixhageloh/uebersicht that referenced this issue Nov 14, 2017

downgrades esprima
version 3 and 4 have a bug with returning multiline strings
jquery/esprima#1829

johnjbarton added a commit to johnjbarton/esprima that referenced this issue Nov 17, 2017

Recognize Token.Template as a ReturnStatement argument.
When a Template is broken across multiple lines, the existing hasArgument test fails.
The Template is scanned as a single token so we just need to check for it after the return.

Fixes #1829

johnjbarton added a commit to johnjbarton/esprima that referenced this issue Nov 19, 2017

Recognize Token.Template as a ReturnStatement argument.
When a Template is broken across multiple lines, the existing hasArgument test fails.
The Template is scanned as a single token so we just need to check for it after the return.

Fixes #1829

@ariya ariya closed this in 9973bab Nov 20, 2017

ariya added a commit to ariya/esprima that referenced this issue Jun 15, 2018

Recognize Token.Template as a ReturnStatement argument.
When a Template is broken across multiple lines, the existing hasArgument test fails.
The Template is scanned as a single token so we just need to check for it after the return.

Fixes #1829
Closes gh-1882

ariya added a commit to ariya/esprima that referenced this issue Jun 16, 2018

Recognize Token.Template as a ReturnStatement argument.
When a Template is broken across multiple lines, the existing hasArgument test fails.
The Template is scanned as a single token so we just need to check for it after the return.

Fixes #1829
Closes gh-1882
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment