-
Notifications
You must be signed in to change notification settings - Fork 7
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
CPLAT-9308 Codemod Utility to tell if Something is Public API #74
Changes from 10 commits
53d0862
4900548
070c5f9
723a406
cadf6ae
d806385
c5e7e74
7027deb
900a557
7c1f44e
457deb6
f9d81d9
8abec86
c61a5c4
d53c197
82e39f7
c5430ab
7d844b0
5783dcb
23dc002
8775714
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,12 @@ import 'package:over_react_codemod/src/util.dart'; | |
typedef void YieldPatch( | ||
int startingOffset, int endingOffset, String replacement); | ||
|
||
// Stub while <https://jira.atl.workiva.net/browse/CPLAT-9308> is in progress | ||
bool _isPublic(ClassDeclaration node) => false; | ||
|
||
/// Whether a props or state class class [node] should be migrated as part of the boilerplate codemod. | ||
bool shouldMigratePropsAndStateClass(ClassDeclaration node) { | ||
bool shouldMigratePropsAndStateClass( | ||
ClassDeclaration node, SemverHelper helper) { | ||
return isAssociatedWithComponent2(node) && | ||
isAPropsOrStateClass(node) && | ||
// Stub while <https://jira.atl.workiva.net/browse/CPLAT-9308> is in progress | ||
!_isPublic(node); | ||
helper.getPublicExportLocations(node) == null; | ||
} | ||
|
||
/// A simple RegExp against the parent of the class to verify it is `UiProps` | ||
|
@@ -119,3 +116,29 @@ void migrateClassToMixin(ClassDeclaration node, YieldPatch yieldPatch, | |
propsAndStateClassNamesConvertedToNewBoilerplate[originalPublicClassName] = | ||
newMixinName; | ||
} | ||
|
||
class SemverHelper { | ||
Map _exportList; | ||
|
||
SemverHelper(Map jsonReport) { | ||
_exportList = jsonReport['exports']; | ||
} | ||
|
||
/// Returns semver report information for [node] if it is publicly exported. | ||
/// | ||
/// If [node] is not publicly exported, returns `null`. | ||
Map<String, dynamic> getPublicExportLocations(ClassDeclaration node) { | ||
final className = node?.name?.name; | ||
String classKey; | ||
|
||
if (className == null || _exportList == null) return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we may want this to be a louder failure. Have we seen the class name be If these can be |
||
|
||
_exportList.forEach((key, value) { | ||
if (value['type'] == 'class' && value['grammar']['name'] == className) { | ||
classKey = key; | ||
} | ||
}); | ||
sydneyjodon-wk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return _exportList[classKey]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"exports": { | ||
"lib/web_skin_dart.dart/ButtonProps": { | ||
"type": "class", | ||
"grammar": { | ||
"name": "ButtonProps", | ||
"meta": ["@Props()"] | ||
} | ||
}, | ||
"lib/web_skin_dart.dart/BarProps": { | ||
"type": "class", | ||
"grammar": { | ||
"name": "BarProps", | ||
"meta": ["@Props()"] | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion builds on code that isn't here yet (it would come from Aaron's branches), but how would you feel about integrating the helper into an
isPublic
function? I think the implementation could be something like:boilerplate_utilities.dart
boilerplate_upgrade.dart
The main thing I'm trying to prevent is passing around the same helper instance through all the suggestors and utilities, but if that seems like a good goal then I'm open to whatever implementation works best!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing around the instance is actually what I'd prefer, as opposed to a global variable.
Advantages:
-Make it easier to reason about where this is used, and force us to be intentional about where we pass it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could definitely see that, and normally I wouldn't advocate for a global variable, but my main thought is that the function which answers the question of whether a node is public or not is at the utility level, and it doesn't seem like the suggestor should need to be concerned with how it answers that question (outside of passing in the node).
Also, because this helper should receive the report for the relevant library it's running on, it seems like it makes sense for it to operate independently since any instantiation for an executable should be passing it the same report. For tests that does break down, but then the testing environment could have its own instance when needed. If you feel strongly, Greg, I think that generally global variables are bad, this is just a weird case to me 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel pretty strongly here that moving away from a top-level variable would improve the modularity of this code, as well as the predictability of how data is passed around and used.
That's true, but may not always be true. For example, what if we wanted this utility to behave slightly differently for different suggestors? Or if we wanted to run multiple sets of suggestors on different directories concurrently? Having global variables would make it much more difficult to implement these changes. To be fair, this is the weakest argument of the ones I've presented, but it is something to keep in mind.
I'm happy to make a PR with those changes, especially since some work was already done to move it more toward top-level variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving away from a global variable sounds good to me. Thanks for explaining your logic, Greg! And sorry for making ya change it in the first place Sydney 😅