Skip to content

fix(ts): raise TSError for class accessors with discarding accessibility#13250

Closed
fedeci wants to merge 2 commits into
babel:mainfrom
fedeci:accessibilty-accessors
Closed

fix(ts): raise TSError for class accessors with discarding accessibility#13250
fedeci wants to merge 2 commits into
babel:mainfrom
fedeci:accessibilty-accessors

Conversation

@fedeci

@fedeci fedeci commented May 2, 2021

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues? Closes #13125
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

We override ClassScopeHandler in the TS plugin so that it is possible to check for class accessors with different accessibility levels.
In this PR we do not take into account MemberExpression and CallExpression keys because TS does not return any error for those

Comment on lines +2 to +3
public get bar() {}
set bar(v) {}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is a TS bug 🤔

@fedeci fedeci added area: typescript pkg: parser PR: Spec Compliance 👓 A type of pull request used for our changelog categories labels May 2, 2021
@babel-bot

babel-bot commented May 2, 2021

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47131/

@codesandbox-ci

codesandbox-ci Bot commented May 2, 2021

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d97eda5:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration


enter() {
this.stack.push(new ClassScope());
this.stack.push(this.createScope());

@fedeci fedeci May 2, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Cannot call 'this.stack.push' because 'ClassScope' [1] is incompatible with 'IClassScope' [2] in array element.
Why this? Shouldn't it be allowed since IClassScope: ClassScope?

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems that the behaviour of different accessibility for accessors has changed in TS 4.3:

class Foo {
  public get f() { return 1 }
  private set f(x) {}
}

is now allowed. https://www.typescriptlang.org/play?target=8&ts=4.3.0-dev.20210503#code/MYGwhgzhAEBiD29oG8BQ1oAcCuAjEAlsNAOYCmALtAGYAUAlCtAE6XbMB20AjNAL7oszAgDcwFMtAiUatAB6NkAvkA

Based on that we may want to hold off this PR and close #13125 since it does not throw on TS 4.3

@fedeci

fedeci commented May 4, 2021

Copy link
Copy Markdown
Member Author
class Foo {
    protected get foo() { return 1 }
    set foo(v: any) { }
}

In 4.3.0 accessors are allowed to have a different accessibility level, but getters must have at least the same level of the setters. https://www.typescriptlang.org/play?ts=4.3.0-beta#code/MYGwhgzhAEBiD29oG8BQ0PQA4Cd4BcBTYIgE2gHND9oAzRACgEoVodqBXHAO2gEZoAX3SYI1OowBuALmhhuATxbIhqQUA
Ref PR: microsoft/TypeScript#42425

@fedeci fedeci force-pushed the accessibilty-accessors branch from 706c907 to d97eda5 Compare June 28, 2021 13:46
@fedeci fedeci marked this pull request as draft June 28, 2021 15:14
@fedeci

fedeci commented Sep 26, 2021

Copy link
Copy Markdown
Member Author

Closing this, we may want to check the correct behaviour when implementing auto accessors

@fedeci fedeci closed this Sep 26, 2021
@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 27, 2021
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Spec Compliance 👓 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Babel parser typescript should throw when accessors do not agree in visibility

3 participants