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

Are class decorators always in strict mode? #545

Closed
lpardosixtosMs opened this issue Sep 24, 2024 · 5 comments
Closed

Are class decorators always in strict mode? #545

lpardosixtosMs opened this issue Sep 24, 2024 · 5 comments

Comments

@lpardosixtosMs
Copy link

The decorators spec PR has the following note at the bottom of the Class Definition secition (15.8):

A class definition is always strict mode code.

The existing spec also specifies that "All parts of a ClassDeclaration or a ClassExpression are strict mode code." Given that DecoratorList is part of ClassDeclaration and ClassExpression, I understand that decorators are in strict mode too.

ClassDeclaration[Yield, Await, Default] :
  DecoratorList[?Yield, ?Await]opt class BindingIdentifier[?Yield, ?Await] ClassTail[?Yield, ?Await]

ClassExpression[Yield, Await] :
  DecoratorList[?Yield, ?Await]opt class BindingIdentifier[?Yield, ?Await]opt ClassTail[?Yield, ?Await]

As a consequence expressions like @yield class C{} or @yield() class C{} are not allowed because "yield" as an IdentifierReference is not allowed in strict mode. However, I saw this test262 test that explicitly tests that the latter is a valid expression.

I want to verify if decorators must be evaluated in strict mode.

@ljharb
Copy link
Member

ljharb commented Sep 24, 2024

The decorator's definition (not its invocation) determines whether it's in strict mode, so they absolutely can be in sloppy mode.

@lpardosixtosMs
Copy link
Author

I'm sorry, I think my concrete question was poorly worded. The decorator as a function can be in sloppy mode, but what about the decorator invocation?. Even if function yield() {} is a valid definition in sloppy mode, the following script causes a strict mode syntax error:

function yield() {}
class C {foo() {return yield()}}

And my understanding from the spec draft is that

function yield() {}
@yield() class C{}

causes a syntax error too.

@ljharb
Copy link
Member

ljharb commented Sep 25, 2024

Modes apply to function definitions; the invocation has no effect on that. However, invoking a function named yield inside a class body will certainly error.

I agree that a class decorator is part of the class, and thus its local name must be valid in strict mode.

@JLHwung
Copy link

JLHwung commented Sep 27, 2024

Duplicate of #204.

@lpardosixtosMs
Copy link
Author

Thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants