sebastienros / esprima-dotnet

Esprima .NET (BSD license) is a .NET port of the esprima.org project. It is a standard-compliant ECMAScript parser (also popularly known as JavaScript).
BSD 3-Clause "New" or "Revised" License
430 stars 75 forks source link

Issues generating nested logical expressions #415

Closed CharlieEriksen closed 1 year ago

CharlieEriksen commented 1 year ago

Version used

3.0.1

Describe the bug The ToJavascriptString produces some odd code. It turns:

return n || o === "back" ? (n ?? "") || "back" : "";

into:

return n || o === "back" ? n ?? "" || "back" : "";

To Reproduce

    [Fact]
    public void TestCatch()
    {
        string javascriptCode = @"let Button$1 = class  {

  get iconSource() {
    var { icon: n, type: o } = this.props;
    return n || o === ""back"" ? (n ?? """") || ""back"" : """";
  }
};
";

        var parsed = new JavaScriptParser(new ParserOptions(){Tolerant = true});
        var node = parsed.ParseModule(javascriptCode);
        node.Should().NotBeNull();
        var prettied = node.ToJavaScriptString(true);
         node = parsed.ParseModule(prettied);
        node.Should().NotBeNull();
    }

Expected behavior It should generate valid JS after prettifying.

CharlieEriksen commented 1 year ago

Might be same root cause as https://github.com/sebastienros/esprima-dotnet/pull/413, but wanted to just file it anyway in case it's not :)