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

Make attributes and methods protected or private again #16

Open
1 of 4 tasks
Lusito opened this issue Nov 9, 2020 · 2 comments
Open
1 of 4 tasks

Make attributes and methods protected or private again #16

Lusito opened this issue Nov 9, 2020 · 2 comments
Labels
Cleanup good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Lusito
Copy link
Owner

Lusito commented Nov 9, 2020

In flyovers port, almost all members have been made public. Probably due to TypeScript not having a "friend" concept as C++ does.

This should be avoided. Possible alternatives:

  • Using the respective getter/setter methods
  • Introducing actual getter/setter (language feature)
  • Accessing private attributes using the bracket syntax, which bypasses the protection:
class A {
    private x = 0;
}

const a = new A();
a["x"] = 10; // no error

This style should only be used as a last resort internally to imitate the "friend" concept of C++.
I usually only use this in tests, where I want to access/modify data, which is otherwise not allowed to be touched.

Update: After trying this a bit, it causes other issues. Like false detection of unused properties (property is only being written, not being read)

Update 2:
As said in the comments, I've written idtsc to get this working. The core project has been extended with this technique. The other projects might need adjustments as well.

Projects to adjust:

  • controllers
  • core
  • lights
  • particles
@Lusito Lusito added Cleanup good first issue Good for newcomers help wanted Extra attention is needed labels Nov 9, 2020
@Lusito
Copy link
Owner Author

Lusito commented Dec 29, 2020

Another option would be the internal modifier, which has been a suggestion for quite a while:
microsoft/TypeScript#5228

But since it's not implemented yet, an alternative approach would be to add a jsdoc tag, which can then later be used to post-process the generated .d.ts files to turn public into private

@Lusito
Copy link
Owner Author

Lusito commented Dec 29, 2020

Made a tool for the post-processing: https://github.com/Lusito/idtsc

Now all we need to do is add a jsdoc tag @internal to all internal classes, properties and methods and run this tool after the build step

Lusito added a commit that referenced this issue Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cleanup good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant