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

explicit-member-accessibility typescript-eslint rule #1258

Closed
Tracked by #37
pixelzoom opened this issue Jun 2, 2022 · 35 comments
Closed
Tracked by #37

explicit-member-accessibility typescript-eslint rule #1258

pixelzoom opened this issue Jun 2, 2022 · 35 comments

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Jun 2, 2022

Here's an example of why I'd like to revisit the decision to make public optional (at developer discretion).

In bending-light GridCanvasNode.ts:

class GridCanvasNode extends CanvasNode {
  modelViewTransformProperty: Property<ModelViewTransform2>;
  strokeDash: number[];
  gridLines: ObservableArray<{ x1: number; y1: number; x2: number; y2: number; lineDashOffset: number }>;
...
  constructor( gridLines: ObservableArray<{ x1: number; y1: number; x2: number; y2: number; lineDashOffset: number }>,
               modelViewTransformProperty: Property<ModelViewTransform2>, strokeDash: number[], providedOptions?: NodeOptions ) {

    super( providedOptions );
    this.gridLines = gridLines; // @private
    this.modelViewTransformProperty = modelViewTransformProperty; // @private
    this.strokeDash = strokeDash; // @private
  }

The fields here were probably auto-created using WebStorm, with default public visibility. And then they were never given the proper visibility, which should be private (as noted in the comments).

And this is not an isolated case. While working on #1224 to remove redundant @private annotations, I'm finding this problem throughout the fields and methods of bending-light. And I've seen it elsewhere.

My feeling is that requiring developers to explicilty add an annotation is more likely to result in a correct annotation. And (so far) some developers have abused default visibility.

@pixelzoom
Copy link
Contributor Author

I should mention that the readonly modifier is also missing from the field declarations in the example above. This is another common problem that I'm seeing throughout PhET code.

@pixelzoom
Copy link
Contributor Author

And again in SeriesCanvasNode.ts:

class SeriesCanvasNode extends CanvasNode {
  seriesProperty: Property<DataPoint[]>;
  modelViewTransformProperty: Property<ModelViewTransform2>;
  color: string;
...
  constructor( seriesProperty: Property<DataPoint[]>, modelViewTransformProperty: Property<ModelViewTransform2>,
               color: string, providedOptions?: NodeOptions ) {

    super( providedOptions );
    this.seriesProperty = seriesProperty; // @private
    this.modelViewTransformProperty = modelViewTransformProperty; // @private
    this.color = color; // @private
  }

@samreid
Copy link
Member

samreid commented Jun 2, 2022

We discussed this at today's dev meeting. @zepumph recommend we separate this into 3 problems:

@samreid
Copy link
Member

samreid commented Jun 2, 2022

Have you been mindful of public/private?
AV - Forgot it existed, using the default
CM - yes, and enforced in package.json
CK - yes, and i opted to use public explicitly in NP
JB - I generally try to specify public explicitly
JG -
JO - Don’t mind either way, yes mindful of that
KP
LM
LV - yes, I try to use public explicitly
MK- I’m sorry I haven’t been a good participant.
MP
MS: I’m being mindful.
SR: I’m being mindful of public vs private during new code development. Making things private where possible

Recommendation for going forward, for voting for requiring public annotation: #1258
AV - Sounds like explicit is better
CM - explicit
CK - prefer explicit
JB - yes for explicit use of the “public” specifier
JG - Prefer explicit (especially for methods).
JO - Don't mind either way
KP - Yes, explicit
LM
LV
MK - Require some sort of this rule, turning it on for properties (public too) sounds really great.
MP
MS: Generally leaning towards dev choice, but am open either way.
SR: Can we try turning on this rule for a week and see how annoying it is?
Vote tally:
Explicit: 7
Dev Choice or Don’t feel strongly: 3

@samreid
Copy link
Member

samreid commented Jun 2, 2022

Several team members recommended that we should be consistent, even requiring public|private|protected on constructors, methods, getters, etc.

@samreid
Copy link
Member

samreid commented Jun 2, 2022

@samreid will also check why --fix isn't working.

UPDATE: According to https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/explicit-member-accessibility.ts, it appears the only support for autofix is removing an unwanted "public" modifier.

@samreid
Copy link
Member

samreid commented Jun 3, 2022

I added a (horribly inefficient) script that adds accessibility modifiers.

samreid added a commit to phetsims/axon that referenced this issue Jun 3, 2022
@samreid
Copy link
Member

samreid commented Jun 3, 2022

I used the script in axon to make all missing modifiers private, then I used the type checker to visit each site and make them public where appropriate.

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jun 3, 2022

I'd be interested in a script like this for general use. For some collection of .ts files, the script would change all fields to private readonly and methods to private (which should be the default, if a language is really serious about creating a good API) regardless of how you had set them. Then let tsc identify where modifiers need to be changed. I'm not sure how much effort this would be, but it would certainly catch incorrect modifiers.

marlitas added a commit to phetsims/mean-share-and-balance that referenced this issue Jun 6, 2022
@marlitas marlitas removed their assignment Jun 6, 2022
marlitas added a commit to phetsims/mean-share-and-balance that referenced this issue Jun 6, 2022
@samreid
Copy link
Member

samreid commented Jun 9, 2022

Here is the result of grunt lint-everything --chip-away for this rule:
5827 problems (5827 errors, 0 warnings)

@pixelzoom pixelzoom changed the title Revisit decision to make public modifier optional. Revisit decision to make public modifier optional (explicit-member-accessibility) Jun 9, 2022
zepumph added a commit to phetsims/ratio-and-proportion that referenced this issue Jun 9, 2022
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jun 9, 2022

vegas, sun, scenery-phet, and simula-rasa are all cleaned up, and are checking explicit-member-accessibility via their package.json.

pixelzoom added a commit to phetsims/simula-rasa that referenced this issue Jun 9, 2022
jonathanolson added a commit to phetsims/dot that referenced this issue Jul 15, 2022
jonathanolson added a commit to phetsims/kite that referenced this issue Jul 15, 2022
zepumph added a commit to phetsims/phet-core that referenced this issue Jul 15, 2022
jonathanolson added a commit to phetsims/scenery that referenced this issue Jul 15, 2022
jonathanolson added a commit to phetsims/joist that referenced this issue Jul 15, 2022
@jonathanolson jonathanolson removed their assignment Jul 15, 2022
@jonathanolson jonathanolson moved this from General to On Hold in Jonathan Olson's Board Jul 15, 2022
samreid added a commit to phetsims/mean-share-and-balance that referenced this issue Jul 15, 2022
samreid added a commit to phetsims/center-and-variability that referenced this issue Jul 15, 2022
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jul 15, 2022

This rule makes most of the Access Modifiers section irrelevant in PhET's TypeScript Conventions doc. Should we remove the entire section? Keep part of it?

@zepumph
Copy link
Member

zepumph commented Jul 15, 2022

I would remove it! It is out of date anyways

zepumph added a commit to phetsims/build-a-nucleus that referenced this issue Jul 15, 2022
zepumph added a commit to phetsims/mean-share-and-balance that referenced this issue Jul 15, 2022
@zepumph
Copy link
Member

zepumph commented Jul 15, 2022

After that, feel free to close, I cleaned up all package.jsons that were using rule.

@zepumph zepumph assigned pixelzoom and unassigned zepumph Jul 15, 2022
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jul 15, 2022

@samreid do you have any feelings about blowing away the entire Access Modifiers section? I think that you may have been the author of the final 2 paragraphs.

@pixelzoom pixelzoom assigned samreid and unassigned pixelzoom Jul 15, 2022
@samreid
Copy link
Member

samreid commented Jul 15, 2022

Looks good to be deleted, thanks!

@pixelzoom pixelzoom self-assigned this Jul 15, 2022
@samreid samreid removed their assignment Jul 18, 2022
@samreid
Copy link
Member

samreid commented Jul 18, 2022

In today's meeting, we agreed this issue can be closed when:

  • The section mentioned above is deleted

pixelzoom added a commit to phetsims/phet-info that referenced this issue Jul 19, 2022
@pixelzoom
Copy link
Contributor Author

Access Modifiers section deleted in the above commit. Closing.

marlitas pushed a commit to phetsims/soccer-common that referenced this issue Aug 23, 2023
marlitas pushed a commit to phetsims/soccer-common that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

10 participants