Skip to content

Syntax error thrown for correct syntax within node shell #36513

Description

@jadutter
  • Version: v14.15.1
  • Platform: Darwin LM-BWI-40009439 19.6.0 Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64 x86_64
  • Subsystem:

The node shell does not correctly parse a multiline function that uses optional chaining, ternary operator, and an identity operator.

These commands work as expected

ser@HOST ~ % nvm use 14
Now using node v14.15.1 (npm v6.14.8)

user@HOST ~ % node -v
v14.15.1

user@HOST ~ % node < <( echo '
    const test1 = (value) => { return (value?.constructor); }
    const test2 = (value) => { return (value.constructor === Array) ? value : [value]; }
    const test3 = (value) => { return (value?.constructor) ? value : [value]; }
    const test4 = (value) => { return ((value?.constructor) === Array) ? value : [value]; }
    const test5 = (value) => { 
        return ((value?.constructor) === Array) ? value : [value]; 
    }
    const arr = new Array();
    console.log(test5(arr))
    ')
[]

However, attempting to define function test5 from within a node shell fails


user@HOST ~ % node
> const test1 = (value) => { return (value?.constructor); }
undefined
> const test2 = (value) => { return (value.constructor === Array) ? value : [value]; }
undefined
> const test3 = (value) => { return (value?.constructor) ? value : [value]; }
undefined
> const test4 = (value) => { return ((value?.constructor) === Array) ? value : [value]; }
undefined
> const test5 = (value) => { 
...     return ((value?.constructor) === Array) ? value : [value]; 



Uncaught SyntaxError: Unexpected end of input
> }
}
^

Activity

  1. richardlau commented on Dec 14, 2020

    @richardlau
    Member

    Are you able to test the proposed v14.15.2 release (#36476)? I suspect this may be addressed by the acorn parser related changes.

  2. jadutter commented on Dec 15, 2020

    @jadutter
    Author

    It appears to be fixed in 14.15.2
    To make sure I followed the correct steps, this is how I tested:

    git clone https://github.com/nodejs/node.git
    cd node
    git fetch origin v14.15.2-proposal
    git checkout v14.15.2-proposal
    ./configure && make -j4 test
    

    Once it was finished building, I found the path to the 14.15.2 version I just built

    node_file_type="$(file -b "$(which node)" )"
    new_node_path="$(find ./out/Release \
        -type f \
        -name node \
        -exec file {} + | \
        grep "$node_file_type" | \
        cut -d: -f1)"
    $new_node_path -v 
    

    Then I used it to open a node shell and pasted the lines that failed in 14.15.1

    $new_node_path
    const test1 = (value) => { return (value?.constructor); }
    const test2 = (value) => { return (value.constructor === Array) ? value : [value]; }
    const test3 = (value) => { return (value?.constructor) ? value : [value]; }
    const test4 = (value) => { return ((value?.constructor) === Array) ? value : [value]; }
    const test5 = (value) => { 
        return ((value?.constructor) === Array) ? value : [value]; 
    }
    const arr = new Array();
    console.log(test5(arr))
    

    It did not throw an error.

  3. richardlau commented on Dec 16, 2020

    @richardlau
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions