Skip to content

🎩 SequenceExpression printed incorrectly when used as part of MemberExpression #1061

Description

@coderaiser

I got case that may be relevant to #1057, since it's about SequenceExpressions. For such code:

const name = (FIRST || SECOND).VALUE || HELLO;

After instrumenting by coverage tool 🎩ESCover I receive next results.

@babel/generate produces correct result:

const name = (__c4['🧨'](1, 13), ((__c4['🧨'](1, 14), FIRST) || (__c4['🧨'](1, 23), SECOND)).VALUE) || (__c4['🧨'](1, 40), HELLO);
//                               ^ opening round bracket            closing round bracket ^

recast produces incorrect result:

 const name = (__c4['🧨'](1, 13), (__c4['🧨'](1, 14), FIRST) || (__c4['🧨'](1, 23), SECOND).VALUE) || (__c4['🧨'](1, 40), HELLO);
//                               ^ missing                                       missing ^                                                                 

Because if __c4['🧨'] calls are removed, code looks like this:

 const name = (FIRST || SECOND.VALUE) || HELLO;

Which is absolutely wrong. Here is code (+ what inslide a fix function) of 🐊Putout plugin that does transforms with Babel API.

Here is test example:

it("sequence", async function() {
    const source = 'const name = (a || b).value || value';
    const expected = `const name = (__c4['🧨'](1, 13), ((__c4['🧨'](1, 14), a) || (__c4['🧨'](1, 19), b).value)) || (__c4['🧨'](1, 31), value)`;

    // @ts-ignore
    const plugin = await eval('import("escover/plugin")') as any;
    // @ts-ignore
    const {transform, parse, print} = await eval('import("putout")') as any;

    const ast = parse(source);

    transform(ast, source, {
        plugins: [
            ['escover', plugin],
        ],
    });

    assert.strictEqual(
      print(ast),
      expected,
    );
});

Only transformed made by 🐊Putout, parse and print it's recast.

Looks like it is related to

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions