-
Notifications
You must be signed in to change notification settings - Fork 15
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
Added support for static private fields accessors and methods. #50
Added support for static private fields accessors and methods. #50
Conversation
61ead3a
to
d65136f
Compare
…rs. There is no spec to go with this behavior as of yet.
…initialization in esnext. Fixed issue where nested privates produce incorrect brand check.
…d-accessors' into es-private-static-fields-methods-and-accessors
…c-fields-methods-and-accessors
427cce4
to
23f6c67
Compare
b564ef6
to
374e6f3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of initial thoughts. Will try to look over again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @joeywatts also mentioned this in one of his comments but the following example doesn't work correctly right now:
class C {
static #x = 42;
static #y = this.#z();
static #z() {
return this.#x;
}
static m() {
return C.#y;
}
}
console.log(C.m()); // should log 42
We should fix the checker (produces TS2334
) and the emit (should probably replace this
in the field initializers with the class constructor?).
…seDefineForClassFields is not specified and target is esnext.
…h `target:esnext`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm halfway through the fixture changes but reviewed everything else. Leaving a few comments but overall it looks good.
src/compiler/checker.ts
Outdated
@@ -32296,6 +32309,9 @@ namespace ts { | |||
getNodeLinks(lexicalScope).flags |= NodeCheckFlags.ContainsClassWithPrivateIdentifiers; | |||
} | |||
} | |||
if (isPrivateIdentifier(node.name) && hasStaticModifier(node) && node.initializer && languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will need to be updated to use getUseDefineForClassFields()
when microsoft#42663 gets in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. After we get that in we should do a merge and do a sweep for useDefineForClassFields
again. I'll leave this open so we don't forget
tests/cases/conformance/classes/members/privateNames/privateNameDuplicateField.ts(47,9): error TS2300: Duplicate identifier '#foo'. | ||
tests/cases/conformance/classes/members/privateNames/privateNameDuplicateField.ts(48,9): error TS2300: Duplicate identifier '#foo'. | ||
tests/cases/conformance/classes/members/privateNames/privateNameDuplicateField.ts(49,9): error TS2300: Duplicate identifier '#foo'. | ||
tests/cases/conformance/classes/members/privateNames/privateNameDuplicateField.ts(6,9): error TS2300: Duplicate identifier '#foo'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha what happened here? I this because we replaced the early return
with continue
in the checker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Since duplication detection was so unreliable (combinations of type and order matter) I decided to add all possible combinations between instance and static private class elements.
~~~~~~ | ||
!!! error TS18019: 'static' modifier cannot be used with a private identifier. | ||
~~~~~~~ | ||
!!! error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess technically private static emit can be implemented in earlier versions because it doesn't use WeakSet
or WeakMap
but we wan't to have a consistent minimum target?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. I think it would cause more confusion than benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! One small issue in a test.
__classStaticPrivateFieldSet(_a = C, C, _C_test, +__classStaticPrivateFieldGet(_a, C, _C_test) + 1); | ||
__classStaticPrivateFieldSet(_b = C, C, _C_test, +__classStaticPrivateFieldGet(_b, C, _C_test) - 1); | ||
__classStaticPrivateFieldSet(_c = C, C, _C_test, +__classStaticPrivateFieldGet(_c, C, _C_test) + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not specific to private statics but it's a bit weird that C.#test++
and ++C.#test
evaluates to the same thing as standalone expressions. They are correctly handled if the expression is used, though.
// Error | ||
class A_StaticGetter_StaticField { | ||
static get #foo() { return ""} | ||
static #foo() { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static #foo() { } | |
static #foo = "foo"; | |
* Update editorServices.ts * Fix baselines Co-authored-by: Orta <[email protected]>
* Fixed three typo errors * Reverted * Reverted * Fixed typo
* Initial implementation+first big transitions * about 10 more * Change baseline filename + more baselines 1. Use containing file name instead of first @filename. 2. Switch the rest of the tests I need for @link over to baselines. * fix lint * Remove unused/incorrectly named fourslash baselines * fix incorrectly updated baselines * dedupe non-unique filenames * Add names to marker baselines Also rename another duped test filename. * Fix semicolon lint
…rver (microsoft#42542) * Move fixed chunk size polling as a watch option and move it out of server Fixes microsoft#41549 * Feedback
…c-fields-methods-and-accessors
…5877) * fix receiver of imported and exported functions fixes: microsoft#35420 * Rebase against master and clean up substitution flow * Add evaluator tests * Fix evaluator tests Co-authored-by: Ron Buckton <[email protected]>
…ields Filter transient flags to fix useDefineForClassFields
* Do a shallow clone for docker tests. * Remove the 'pull' step from the Docker files.
* Update the DOM: March 03, 2021 Mostly Typescript-DOM-lib-generator#915 * update baselines
… JavaScript files (microsoft#42392)
…ains a string index signature to 'any' (microsoft#43065) * Added test. * Accepted baselines. * Allow other index signatures to 'any' if there is a string index signature to 'any'. * Accepted baselines.
…soft#42747) * fix combined type mapper in getConditionalType * Add regression tests
* feat(lib): improve parseInt type definition and docstring * Accepted baselines * update tests
…natures (microsoft#43086) * Remove undefined from optional properties when inferring to index signatures * Add tests
…osoft#42472) * Exclude old number/enum literal compatibility rule from comparable relation * Add tests * Accept new baselines
…static-fields-methods-and-accessors
Draft PR for private static fields methods and accessors.