Skip to content

transformer: transpiling object with async method to ES2015 can shadow outer scope variables #21445

@JeanSebTr

Description

@JeanSebTr

We're upgrading our project to Vite8 and noticed this transpilation issue.

When transpiling an object with async methods to ES2015, if the object's field is the same name as an outer scope variable, that variable will be lost. Here's a minimal repro example:

interface Element {
  previous(): Promise<Element | null>;
  next(): Promise<Element | null>;
};

function customIteratorMethod(): Element {
  let previous: Element | null = null;
  let next: Element | null  = null;
  return {
    previous: async () =>
      previous || (previous = "previous value"),
    next: async () => next || (next = "next value"),
  };
}

export default customIteratorMethod;

Those async methods get transpiled to:

{
  next: function() {
  	var _ref2 = _asyncToGenerator(function* () {
  		return next || (next = "next value");
  	});
  	return function next() {
  		return _ref2.apply(this, arguments);
  	};
  }()
}

If the names don't match, return function next() { becomes return function() {
But when they do match, the actual reference to the outer let next; get mixed up.

An object fields like { next: async () => { /* ... */ } } should not be in the lexical scope.

Repro on oxc playground.

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / Transpiler

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions