Skip to content

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

@ef4

Description

@ef4

💻

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: traverse

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions