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

[Bug]: path.evaluate should not be confident about array literal variable initializer #14197

Open
1 task
ef4 opened this issue Jan 25, 2022 · 6 comments
Open
1 task

Comments

@ef4
Copy link

ef4 commented Jan 25, 2022

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

This is a complete standalone reproduction:

const babel = require("@babel/core");
const assert = require("assert");

let src = `
  function example() {
    let value = [];
    value.push(Math.random());
    return value;
  }
`;

babel.transform(src, {
  plugins: [
    function () {
      return {
        visitor: {
          ReturnStatement(path) {
            let result = path.get("argument").evaluate();
            assert.equal(result.confident, false);
          },
        },
      };
    },
  ],
});

Configuration file name

No response

Configuration

No response

Current and expected behavior

The assertion in my repro program above fails, because Babel believes it can statically evaluate the value of the return statement. path.evaluate() returns { confident: true, depot: null, value: [] }.

Instead, it should return { confident: false }, because it's not possible to statically determine the return value of this code snippet.

Environment

Tested against @babel/core 7.16.12 in Node 16.

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @ef4! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@ef4
Copy link
Author

ef4 commented Jan 25, 2022

This is broken for object literals too. If you substitute:

function example() {
    let value = { a: 1 };
    value.a = 2;
    return value;
  }

path.evaluate() says it is confident that the return value is {a: 1}.

@The-x-Theorist
Copy link
Contributor

If the initial length of the array is 0 then it should return { confident: false }, what can be the condition for return to be { confident: true }?

@ef4
Copy link
Author

ef4 commented Feb 8, 2022

You can be confident only if there are no other references to the binding in between initialization and return:

function() {
  let value = { a: 1 };
  return value; // confident
}

function() {
  let value = { a: 2 };
  anythingThatUses(value);
  return value; // not confident
}

@The-x-Theorist
Copy link
Contributor

I'd like to work on this issue?

@The-x-Theorist
Copy link
Contributor

Can I work on this Issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants