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

Added support for static private fields accessors and methods. #50

Conversation

dragomirtitian
Copy link

Draft PR for private static fields methods and accessors.

@dragomirtitian dragomirtitian force-pushed the es-private-static-fields-methods-and-accessors branch 2 times, most recently from 61ead3a to d65136f Compare February 16, 2021 16:26
@dragomirtitian dragomirtitian force-pushed the es-private-static-fields-methods-and-accessors branch from 427cce4 to 23f6c67 Compare February 19, 2021 08:03
@dragomirtitian dragomirtitian force-pushed the es-private-static-fields-methods-and-accessors branch from b564ef6 to 374e6f3 Compare February 19, 2021 18:36
Copy link

@joeywatts joeywatts left a 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

Copy link

@mkubilayk mkubilayk left a 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?).

src/compiler/transformers/classFields.ts Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Show resolved Hide resolved
Copy link

@mkubilayk mkubilayk left a 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 Show resolved Hide resolved
@@ -32296,6 +32309,9 @@ namespace ts {
getNodeLinks(lexicalScope).flags |= NodeCheckFlags.ContainsClassWithPrivateIdentifiers;
}
}
if (isPrivateIdentifier(node.name) && hasStaticModifier(node) && node.initializer && languageVersion === ScriptTarget.ESNext && !compilerOptions.useDefineForClassFields) {

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.

Copy link
Author

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

src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Outdated Show resolved Hide resolved
src/compiler/transformers/classFields.ts Show resolved Hide resolved
tests/baselines/reference/privateNameAndAny.js Outdated Show resolved Hide resolved
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'.

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?

Copy link
Author

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.

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?

Copy link
Author

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.

Copy link

@mkubilayk mkubilayk left a 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.

Comment on lines 56 to 58
__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);

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() { }
Copy link

@mkubilayk mkubilayk Feb 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static #foo() { }
static #foo = "foo";

pushkine and others added 3 commits March 2, 2021 06:35
* Update editorServices.ts

* Fix baselines

Co-authored-by: Orta <[email protected]>
* Fixed three typo errors

* Reverted

* Reverted

* Fixed typo
a-tarasyuk and others added 28 commits March 2, 2021 16:32
* 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
…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
…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
@dragomirtitian dragomirtitian merged commit fbd749c into es-private-methods-and-accessors Mar 17, 2021
@dragomirtitian dragomirtitian deleted the es-private-static-fields-methods-and-accessors branch November 17, 2023 15:56
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

Successfully merging this pull request may close these issues.