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]: Property left of BinaryExpression expected node to be of a type ["Expression"] but instead got "StringLiteral" #14034

Closed
1 task
loynoir opened this issue Dec 8, 2021 · 3 comments
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@loynoir
Copy link

loynoir commented Dec 8, 2021

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

https://github.com/loynoir/reproduce-esbuild-1837.mjs

import * as t from "@babel/types";
console.log(
  t.binaryExpression("+", t.stringLiteral("x"), t.stringLiteral("y"))
);
esbuild --bundle ./index.mjs --inject:./shims.mjs --format=iife --outfile=./dist/output.js

Configuration file name

No response

Configuration

No response

Current and expected behavior

Expected

ESM context output

{type: "BinaryExpression", operator: "+", left: {…}, right: {…}}

Unexpected

iife context output

Uncaught TypeError: Property left of BinaryExpression expected node to be of a type ["Expression"] but instead got "StringLiteral"

Environment

Browser

deps:

    "@babel/types": "^7.16.0",
    "esbuild": "^0.14.2",
    "process": "^0.11.10"

Possible solution

evanw/esbuild#1837 (comment)

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @loynoir! 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.

@lightmare
Copy link
Contributor

Thanks for the link to possible solution, the explanation given there was very helpful.

I am partially responsible for this — as in, I wrote the PR #13843 that uses non-object this, which inadvertently plays into a bug in esbuild. I cloned your repro. repo and it produces this code:

  // node_modules/.pnpm/@babel+types@7.16.0/node_modules/@babel/types/lib/builders/builder.js
  var require_builder = __commonJS({
    "node_modules/.pnpm/@babel+types@7.16.0/node_modules/@babel/types/lib/builders/builder.js"(exports) {
      init_shims();
      "use strict"; 
      Object.defineProperty(exports, "__esModule", {
        value: true
      });
      exports.default = builder;
      var _definitions = require_definitions();
      var _validate = require_validate();
      function builder() {
        const type = this;
        ...

The builder function has to be strict mode, because this is supposed to be a string primitive. But there is init_shims(); inserted before "use strict"; which means it's no longer treated as strict mode directive. Consequently the builder function ends up being non-strict, and when called converts this (string primitive) to String object.

@nicolo-ribaudo
Copy link
Member

Closing since this is an esbuild bug (which injects init_shims in the wrong place).

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 12, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants