Skip to content

Vite generated incorrect JavaScript from TSX #690

@fpsqdb

Description

@fpsqdb

Related plugins

Describe the bug

type A = {
  register: (callback: unknown) => void;
};
const a = {
  register: (callback: unknown) => {
    console.log(callback);
  },
};

export function Test() {
  const funA = async <T,>(arg1: T) => {
    console.log(arg1)
  }
  (a as A).register(funA);
  return (
    <div>
      <h1>Test</h1>
    </div>
  );
}

generated code:

const a = {
  register: (callback) => {
    console.log(callback);
  }
};
export function Test() {
  const funA = (async (arg1) => {
    console.log(arg1);
  })(
    a
  ).register(funA);
  return /* @__PURE__ */ jsxDEV("div", { children: /* @__PURE__ */ jsxDEV("h1", { children: "Test" }, void 0, false, {
    fileName: "/project/workspace/src/Test.tsx",
    lineNumber: 36,
    columnNumber: 7
  }, this) }, void 0, false, {
    fileName: "/project/workspace/src/Test.tsx",
    lineNumber: 35,
    columnNumber: 5
  }, this);
}

Error: Uncaught ReferenceError: Cannot access 'funA' before initialization
at Test (Test.tsx:14:21)

Corrected code should be

const a = {
  register: (callback) => {
    console.log(callback);
  }
};
export function Test() {
  const funA = (async (arg1) => {
    console.log(arg1);
  });
  a.register(funA);
  return /* @__PURE__ */ jsxDEV("div", { children: /* @__PURE__ */ jsxDEV("h1", { children: "Test" }, void 0, false, {
    fileName: "/project/workspace/src/Test.tsx",
    lineNumber: 36,
    columnNumber: 7
  }, this) }, void 0, false, {
    fileName: "/project/workspace/src/Test.tsx",
    lineNumber: 35,
    columnNumber: 5
  }, this);
}

If you add a semicolon at the end of the funA declaration, everything works fine.

  const funA = async <T,>(arg1: T) => {
    console.log(arg1)
  };
  (a as A).register(funA);

Reproduction

https://codesandbox.io/p/devbox/9hq6fr

Steps to reproduce

No response

System Info

codsandbox

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions