-
Notifications
You must be signed in to change notification settings - Fork 6
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
Guard against shadowing for mixins and traits #54
Comments
I also found #29, which is very related. Though I think discussion on the 2/11/19 sofware design patterns meeting was more concerned with accidental function overriding than accidentally replacing properties. |
Accidental shadowing can also occur for |
Dev Meeting 02/14/19 AP: Return to dev meeting with a recommendation to handle accidental overrides. SR: Consider using ES6 proxy to catch shadowing. May not be compatible with all browsers. JO: A better pattern for checking for existence before the mixin may avoid proxy usage. |
Discussed during core hours 2/25/19, we would like to guard against shadowing for methods and fields when using the mixin/trait pattern. If the solution to this will work with |
Part of this might include creating a more concrete |
This came up again in the context of phetsims/sun#257. We are using mixin/trait heavily for scenery and a11y. Now we're adding it to UI components in sun and scenery-phet. We need to guard against accidentally shadowing properties (lowercase 'p') of the host class. Something as simple as this might do the job: function assertVerifyMixinProperties( object, properties ) {
properties.forEach( property =>
assert( !object.hasOwnProperty( property ), `mixin will shadow host property: ${property}` )
}
// In sun.EnabledComponent
assert && assertPropertyOverrides( this, [ 'enabledProperty', 'setEnabled', 'getEnabled', 'disposeEnabledComponent', '_disposeEnabledComponent' ] ); Labeling for developer meeting. |
I think @zepumph pointed out that |
Right, hasOwnProperty is not sufficient. We need to look all the way up the class hierarchy. Or I should say "up the prototype chain", since JavaScript really doesn't have classes. |
|
|
I considered that when I suggested |
So we're looking for a library function that scans a specific object's prototype to determine if it has a definition? Then ideally a helper function that will scan a list of property names and error out nicely if they exist? I'm happy to write that and provide it, let me know. |
@jonathanolson I think part of the appeal is to check non prototype elements to. For example making sure that Are we interested in doing this for the prototype methods too? Perhaps this could be a replacement for the @jonathanolson I would say go for it and we can take it for a test drive. |
Visited this issue 5/21/20 developer meeting and still think this would be useful, particularly since a11y related code is leveraging trait/mixin pattern more. |
I'd like to understand how this problem would appear when using the class-factory pattern identified in phetsims/phet-info#143 |
This would be similar to the problem of "how do you know if you're overwriting a method/property with normal inheritance", which we currently don't auto-detect. |
In dev meeting, we're going to put this on hold until phetsims/phet-info#143 is completed. |
TypeScript now catches shadowing and will error unless you annotate as |
This came out of discussion in phetsims/phet-info#92 and phetsims/phet-info#88. When using mixin/trait pattern there is no guard against accidentally overriding functions in the mixin/trait.
It was recommended that someone examine our mixin/trait pattern and recommend a solution for this. Adding to developer meeting to discuss with @ariel-phet priority and who should investigate.
The text was updated successfully, but these errors were encountered: