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
Support for auto-accessor fields from the Stage 3 Decorators proposal #49705
Conversation
|
@typescript-bot perf test |
|
@typescript-bot perf test |
|
Heya @rbuckton, I've started to run the perf test suite on this PR at a37f09a. You can monitor the build here. Update: The results are in! |
|
@rbuckton Here they are:Comparison Report - main..49705
System
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@typescript-bot run dt |
|
Heya @rbuckton, I've started to run the diff-based user code test suite on this PR at 54024cf. You can monitor the build here. Update: The results are in! |
|
@rbuckton |
|
Why is this a thing? Like, what problem does it actually solve (genuine question, not snark)? The generated accessors don't do anything and unlike, e.g., C#, changing a plain property to an accessor isn't generally an API break... edit: Oh, I see, it's mostly because decorators will apparently make a distinction between field and accessor. |
This was a requirement from VM implementers to avoid the potential hidden shape changes that could occur if you were able to convert a field declaration into an accessor dynamically rather than statically. It also helps to avoid issues with public field shadowing in cases like the following, such as with class C {
// observe attaches an accessor pair to C.prototype, but the field `x` is defined
// on the instance, shadowing the accessor pair.
@observe x = 1;
}
// vs
class C {
// observe can now instead intercept the `{ get, set }` provided to attach its behavior.
@observe accessor x = 1;
} |
9a9624b
to
ba6e369
Compare
|
What are the assignability rules for auto-accessor fields? I guess that they're the same as a normal class field, but might be missing something. |
|
They should be treated the same as we would a get/set pair. |
|
Ah, that explains why the code in the checker is with the existing accessor code, not the property declaration code. |
tests/cases/conformance/decorators/class/property/decoratorOnClassProperty13.ts
Show resolved
Hide resolved
tests/baselines/reference/accessorFieldDisallowedModifiers.errors.txt
Outdated
Show resolved
Hide resolved
c18ec1f
to
8f12856
Compare
|
@sandersn can you take another look? |
40dfa6a
to
30edcc0
Compare
tests/baselines/reference/autoAccessor6(target=esnext,usedefineforclassfields=false).js
Show resolved
Hide resolved
tests/baselines/reference/autoAccessor7(target=es2022,usedefineforclassfields=false).js
Show resolved
Hide resolved
tests/baselines/reference/autoAccessorExperimentalDecorators(target=esnext).errors.txt
Show resolved
Hide resolved
|
@sandersn I've updated based on your feedback as well as some additional cleanup of the classFields transform. Can you take another look? |
|
@typescript-bot perf test |
|
Heya @rbuckton, I've started to run the perf test suite on this PR at 02405ec. You can monitor the build here. Update: The results are in! |
|
@rbuckton Here they are:
CompilerComparison Report - main..49705
System
Hosts
Scenarios
TSServerComparison Report - main..49705
System
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This PR adds support for
accessorfield declarations described in the Stage 3 Decorators proposal.An Auto-Accessor is a field declaration that will be transformed by the runtime into a pair of
getandsetaccessors that access a private backing field:When you use
--target ESNext,accessorfields will be left as is to be transformed by the runtime. Any earlier--targetwill result in TypeScript downleveling theaccessorfield to a compatible runtime implementation.Auto-Accessor fields have several capabilities:
get/setwithout a superclass field potentially shadowing the property during initialization.getandsetaccessor pair and can replace them without changing the runtime shape of the class.In addition, there are several rules around the use of the
accessorkeyword:accessorfields require a minimum of--target ES2015, similar to our support for private identifiers (i.e.,#x) and for the same reasons (a dependency onWeakMap/WeakSet).accessormay only appear in front of field declarations on aclass. It is not supported ininterfaceor object type literals.accessorcannot be used withreadonlyordeclareon the same field declaration.accessorcan be used in an ambient class declaration.accessorfield declarations can be decorated with TypeScript's legacy decorators (i.e., under--experimentalDecorators). They will behave as if you decorated agetorsetdeclaration (i.e., you will receive aPropertyDescriptorat runtime with bothgetandsetfunctions).accessorwill also be marked withaccessorin the output declaration file.accessorfield does not make a class nominal, despite the synthetic private backing field.Symbolfor anaccessorfield is not aSymbolFlags.Property, but rather aSymbolFlags.GetAccessor | SymbolFlags.SetAccessor.NOTE: This is not an implementation of the full Stage 3 Decorators proposal as that effort is still in progress.