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

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

Open
coderaiser opened this issue Feb 11, 2022 · 1 comment

Comments

@coderaiser
Copy link
Contributor

coderaiser commented Feb 11, 2022

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

@gnprice
Copy link
Contributor

gnprice commented Jun 12, 2022

Thanks for the report!

One thing that would help a lot in debugging this would be to have a way to reproduce it without needing to install any additional libraries (like escover/plugin or putout.)

Can you make a version of your test example that replaces the transform call with some explicit modifications to the AST? Like in the style seen here: https://github.com/benjamn/recast#usage . Or if you search through test/printer.ts for the string "ast.program", you'll find a number of examples of test cases that take some AST and then print it.

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

2 participants