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

AF-3838 Replace python implementation with dart using dart_codemod #12

Merged
merged 11 commits into from
Jan 16, 2019

Conversation

evanweible-wf
Copy link
Contributor

@evanweible-wf evanweible-wf commented Jan 7, 2019

Description

The python version of this tool fell short and was unable to handle all the necessary edge cases. Now that we have dart_codemod, this tool should be ported to dart.

This PR adds the first of 3 codemods:

  • dart1_to_dart1_and_dart2

As well as a readme and tests.

Testing

  • CI passes
  • Codemod can be used to roll out changes to an over_react consumer

Code Review

@corwinsheahan-wf @sebastianmalysa-wf @georgelesica-wf @smaifullerton-wk

@rmconsole-wf
Copy link

rmconsole-wf commented Jan 7, 2019

Merge Requirements Met ✅

Request Rosie to automerge this pull request by including @Workiva/release-management-p in a comment.

General Information

Ticket(s):

Code Review(s): #12
Release Image Tags:

Reviewers: corwinsheahan-wf, smaifullerton-wk, sebastianmalysa-wf

Additional Information

Watchlist Notifications: None

	When this pull is merged I will add it to the following release:
	Version: over_react_codemod 0.1.0
	Release Ticket(s): None


Note: This is a shortened report. Click here to view Rosie's full evaluation.
Last updated on Wednesday, January 16 02:59 PM CST

@aviary2-wf
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

Copy link
Contributor

@corwinsheahan-wf corwinsheahan-wf left a comment

Choose a reason for hiding this comment

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

Almost done, I'll finish up tomorrow. This looks awesome!

return;
}

final targetMetaInitializer = '\$metaFor${node.name.name}';
Copy link
Contributor

Choose a reason for hiding this comment

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

should we pass node.name.name to stripPrivateGeneratedPrefix first?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is for the Dart1 -> Dart1/Dart2 migration, and since this is only visiting props and state mixins, there shouldn't ever be a _$ prefix.. but it wouldn't hurt and it would be a good reminder when writing the dart2-only migration

);
} else {
// Meta field already exists.
if (existingMetaField.initializer.toSource() != targetMetaInitializer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we instead error here? Thinking of the case where the user has defined the meta field already as something else. Then the user gets a heads up that something is not right.

implements Suggestor {
@override
visitWithClause(WithClause node) {
final allMixins = node.mixinTypes.map((n) => n.name.name);
Copy link
Contributor

Choose a reason for hiding this comment

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

In reference to the TODO above, can we look for the metadata off of any mixin node and determine whether it contains an over_react mixin annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Talked about this offline, but this isn't possible with an AstVisitor. We'd need to use an ElementVisitor, which is something I'd like to pursue but is more complicated because we have to setup an AnalysisDriver in order to resolve all of the elements.


static getFactoryInitializerValue(String factoryName) {
if (factoryName.startsWith(privatePrefix)) {
final factoryNameWithoutUnderscore =
Copy link
Contributor

Choose a reason for hiding this comment

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

#nit factoryNameWithoutPrivatePrefix

return;
}

if (factoryNode.initializer != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is another situation that I'm not sure we shouldn't error on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We talked about this offline, and decided that we will error if there is an unexpected value in this initializer (same thing for if the meta field is already defined or initialized with something unexpected. If it's an incorrect value, but one that we would expect from a previous iteration of the codemod, then it will just update it.

// Part library that uses over_react.
final partLib = './lib/part.dart';
final parentLib = './lib/parent.dart';
final partSourceFile = SourceFile.fromString('''part of 'parent.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also run the collector on the parent source file in this test to ensure that there is not a duplicity in the libraries detected.

>>> @Props() private (patches 1)
@Props() class _Foo {}
<<<
@Props() class _$_Foo {}
Copy link
Contributor

@corwinsheahan-wf corwinsheahan-wf Jan 8, 2019

Choose a reason for hiding this comment

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

Private classes should be _$Foo, not _$_Foo. this statement is now incorrect. Private classes should be _$_Foo

class _Foo extends _$_Foo
with
// ignore: mixin_of_non_class, undefined_class
_$_FooAccessorsMixin {
Copy link
Contributor

@corwinsheahan-wf corwinsheahan-wf Jan 8, 2019

Choose a reason for hiding this comment

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

This should also be _$FooAccessorsMixin it should be _$_FooAccessorsMixin

// ignore: mixin_of_non_class, undefined_class
_$_FooAccessorsMixin {
// ignore: const_initialized_with_non_constant_value, undefined_class, undefined_identifier
static const PropsMeta meta = $metaFor_Foo;
Copy link
Contributor

@corwinsheahan-wf corwinsheahan-wf Jan 8, 2019

Choose a reason for hiding this comment

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

This should be $metaForFoo. Should be _$metaFor_Foo

Copy link
Contributor

Choose a reason for hiding this comment

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

@evanweible-wf this comment reflects a needed change^


>>> @State() retains doc comments (patches 1)
/// A doc comment.
/// And a second line!
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get 1 more test which retains both the doc comment and the metadata?

Copy link
Contributor

@corwinsheahan-wf corwinsheahan-wf left a comment

Choose a reason for hiding this comment

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

ok I'm done now :)

});
}

test('returns a false negative when annotation found in a string', () {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just:

Suggested change
test('returns a false negative when annotation found in a string', () {
test('returns true when annotation found in a string', () {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying to make it clear that this is a known limitation in the implementation 🤷‍♂️

- Added a readme
- Updated expected initializer values for static meta fields and UI factories
- Added support for ignoring patch suggestions via single-line comments
- Added support for a --comment-prefix CLI option
@evanweible-wf
Copy link
Contributor Author

@corwinsheahan-wf addressed most of your comments. I'm going to leave out some of the error-throwing that we talked about for now because I think it will be mainly a corner case. We can add it as a follow up if need be.

README.md Outdated
- `pub global run over_react_codemod:dart1_and_dart2_to_dart2`

Use this codemod to migrate over_react code that has previously been
migrated via the `dart1_to_dart1_and_dart1` codemod to the form that is only
Copy link
Contributor

Choose a reason for hiding this comment

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

#nit should this be ...'dart1_to_dart1_and_dart2'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, good catch

class $strippedClassName$typeParamsOnClass extends ${privateGeneratedPrefix}$strippedClassName$typeParamsOnSuper
with
$mixinIgnoreComment
${privateGeneratedPrefix}${strippedClassName}AccessorsMixin {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
${privateGeneratedPrefix}${strippedClassName}AccessorsMixin {
${privateGeneratedPrefix}${strippedClassName}AccessorsMixin$typeParamsOnSuper {

@evanweible-wf evanweible-wf changed the title WIP Replace python implementation with dart using dart_codemod AF-3838 Replace python implementation with dart using dart_codemod Jan 16, 2019
@smaifullerton-wk
Copy link
Contributor

QA +1

  • CI passes
  • Codemod can be used to roll out changes to an over_react consumer

I installed locally from this branch and tested with review_bar, looks good:

image

image

@corwinsheahan-wf
Copy link
Contributor

@Workiva/release-management-p

@rmconsole-wf
Copy link

@corwinsheahan-wf I will not merge this because:

  • untracked dependencies

@evanweible-wf
Copy link
Contributor Author

QA +1

@Workiva/release-management-p

@rmconsole-wf
Copy link

+1 from RM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants