Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class missing metadata field in certain cases #3781

Closed
elcortex opened this issue May 25, 2024 · 0 comments
Closed

Class missing metadata field in certain cases #3781

elcortex opened this issue May 25, 2024 · 0 comments
Labels

Comments

@elcortex
Copy link

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);
@evanw evanw added the classes label May 26, 2024
evanw added a commit to evanw/decorator-tests that referenced this issue May 26, 2024
@evanw evanw closed this as completed in b93a2a9 Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants