Skip to content

Class missing metadata field in certain cases #3781

@elcortex

Description

@elcortex

The class is missing the [Symbol.metadata] field if the class itself is not decorated.

Test code:

// Shim the "Symbol.metadata" symbol
Symbol.metadata ??= Symbol('Symbol.metadata');

const track = (_, context) => {
	(context.metadata.names ||= []).push(context.name);
};

class Foo {
	@track foo = 1;
	@track bar = 2;
}

// Should print ["foo", "bar"], but prints undefined
console.log('Foo', Foo[Symbol.metadata]?.names);

const fix = (_, context) => {}

@fix
class Bar {
	@track foo = 1;
	@track bar = 2;
}

// Prints ["foo", "bar"]
console.log('Bar', Bar[Symbol.metadata]?.names);

esbuild output:

Foo undefined
Bar [ 'foo', 'bar' ]

tsc output:

Foo [ 'foo', 'bar' ]
Bar [ 'foo', 'bar' ]

The @fix decorator fixes the code because esbuild stores metadata in the _init2 array and adds it to the Class[Symbol.metadata] field only when the class is replaced:

Bar = __decorateElement(_init2, 0, "Bar", _Bar_decorators, Bar);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions