Skip to content

minifier: await assignments should not be merged to an assignment to this.* #21296

@sapphi-red

Description

@sapphi-red
class Base {
  constructor(obj) {
    obj.start()
  }
}

class Wrapped extends Base {
  constructor() {
    super({
      start: async () => {
        const result = await new Promise(resolve => { setTimeout(() => { resolve(0) }, 0) })
        this.value = result
      }
    })
  }
}

const wrapped = new Wrapped()
setTimeout(() => {
  console.log('value', wrapped.value)
}, 10)

is currently minified to

class Base {
	constructor(obj) {
		obj.start();
	}
}
class Wrapped extends Base {
	constructor() {
		super({ start: async () => {
			this.value = await new Promise((resolve) => {
				setTimeout(() => {
					resolve(0);
				}, 0);
			});
		} });
	}
}
const wrapped = new Wrapped();
setTimeout(() => {
	console.log("value", wrapped.value);
}, 10);

(playground).

But we shouldn't do this because evaluating this throws an error until super is called.

Originally reported at vitejs/vite#22186

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions