Skip to content

transformer: lowering accessor + useDefineForClassFields: false outputs code with incorrect execution order #21365

Description

@sapphi-red
class Hello {
    private input = { foo };
    accessor util = this.input.foo();
}

var hello = new Hello()
console.log(hello);

is transformed to

class Hello {
	constructor() {
		this.input = { foo };
	}
	#_util_accessor_storage = this.input.foo();
	get util() {
		return this.#_util_accessor_storage;
	}
	set util(value) {
		this.#_util_accessor_storage = value;
	}
}
var hello = new Hello();
console.log(hello);

(playground).
But this executes this.input.foo() before this.input = and will cause an error.

esbuild avoids this by moving the private class field definition inside the constructor (esbuild try). TypeScript has a similar output:

class Hello {
    constructor() {
        this.input = { foo };
        this.#util_accessor_storage = this.input.foo();
    }
    #util_accessor_storage;
    get util() { return this.#util_accessor_storage; }
    set util(value) { this.#util_accessor_storage = value; }
}
var hello = new Hello();
console.log(hello);

Originally reported at vitejs/vite#22212

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / Transpiler

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions