-
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-6563 Codemod to migrate consumer overlays to new APIs, fixing deprecations #58
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79b8ed8
Add consumer overlay update executable
sydneyjodon-wk 289f5ad
Add migrator
sydneyjodon-wk 9d19526
Add null check
sydneyjodon-wk 90930a4
Update spacing
sydneyjodon-wk f48877f
Update pubspec ordering
sydneyjodon-wk e70b098
Add check for in component usage
sydneyjodon-wk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2019 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
export 'package:over_react_codemod/src/executables/react16_consumer_overlay_update.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:codemod/codemod.dart'; | ||
import 'package:over_react_codemod/src/react16_suggestors/consumer_overlay_migrator.dart'; | ||
|
||
const _changesRequiredOutput = """ | ||
To update your code, run the following commands in your repository: | ||
pub global activate over_react_codemod | ||
pub global run over_react_codemod:react16_consumer_overlay_update | ||
pub run dart_dev format (If you format this repository). | ||
Then, review the the changes, address any FIXMEs, and commit. | ||
"""; | ||
|
||
void main(List<String> args) { | ||
final query = FileQuery.dir( | ||
pathFilter: isDartFile, | ||
recursive: true, | ||
); | ||
|
||
exitCode = runInteractiveCodemodSequence( | ||
query, | ||
[ | ||
ConsumerOverlayMigrator(), | ||
], | ||
args: args, | ||
defaultYes: true, | ||
changesRequiredOutput: _changesRequiredOutput, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2019 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:codemod/codemod.dart'; | ||
import 'package:over_react_codemod/src/component2_suggestors/component2_utilities.dart'; | ||
|
||
import '../constants.dart'; | ||
|
||
/// Suggestor that migrates consumer overlay prop names in component usages. | ||
class ConsumerOverlayMigrator extends GeneralizingAstVisitor | ||
with AstVisitingSuggestorMixin | ||
implements Suggestor { | ||
@override | ||
visitCascadeExpression(CascadeExpression node) { | ||
super.visitCascadeExpression(node); | ||
|
||
final containingClass = node.thisOrAncestorOfType<ClassDeclaration>(); | ||
|
||
var extendsName = containingClass?.extendsClause?.superclass?.name; | ||
if (extendsName == null) { | ||
return; | ||
} | ||
|
||
String reactImportName = | ||
getImportNamespace(containingClass, 'package:react/react.dart'); | ||
|
||
// Check if usage is in a component class. | ||
if (extendsName.name != '$reactImportName.Component' && | ||
extendsName.name != '$reactImportName.Component2' && | ||
!containingClass.metadata.any((m) => | ||
overReact16ComponentAnnotationNamesToMigrate | ||
.contains(m.name.name) || | ||
overReact16Component2AnnotationNames.contains(m.name.name))) { | ||
return; | ||
} | ||
|
||
// Update consumer overlay props. | ||
for (AssignmentExpression expression | ||
in node.cascadeSections.whereType<AssignmentExpression>()) { | ||
final leftHandSide = expression.leftHandSide; | ||
|
||
if (leftHandSide.toSource() == '..overlay' || | ||
leftHandSide.toSource() == '..isOverlay') { | ||
yieldPatch(leftHandSide.end, leftHandSide.end, '2'); | ||
} else if (leftHandSide.toSource() == '..useLegacyPositioning') { | ||
yieldPatch(expression.offset, expression.end, ''); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
test/react16_suggestors/consumer_overlay_migrator_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// Copyright 2019 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import 'package:over_react_codemod/src/react16_suggestors/consumer_overlay_migrator.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../util.dart'; | ||
|
||
main() { | ||
group('ConsumerOverlayMigrator', () { | ||
final testSuggestor = getSuggestorTester(ConsumerOverlayMigrator()); | ||
|
||
test('empty file', () { | ||
testSuggestor(expectedPatchCount: 0, input: ''); | ||
}); | ||
|
||
test('no matches', () { | ||
testSuggestor( | ||
expectedPatchCount: 0, | ||
input: ''' | ||
library foo; | ||
var a = 'b'; | ||
class Foo {} | ||
''', | ||
); | ||
}); | ||
|
||
test('`overlay` prop', () { | ||
testSuggestor( | ||
expectedPatchCount: 1, | ||
input: ''' | ||
@Component() | ||
class FooComponent extends UiComponent<FooProps> { | ||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..overlay = _renderIndicator() | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
expectedOutput: ''' | ||
@Component() | ||
class FooComponent extends UiComponent<FooProps> { | ||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..overlay2 = _renderIndicator() | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
); | ||
}); | ||
|
||
test('`isOverlay` prop', () { | ||
testSuggestor( | ||
expectedPatchCount: 1, | ||
input: ''' | ||
import 'package:react/react.dart' as react; | ||
|
||
class FooComponent extends react.Component<FooProps> { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay = false | ||
..initiallyOpen = false | ||
); | ||
} | ||
''', | ||
expectedOutput: ''' | ||
import 'package:react/react.dart' as react; | ||
|
||
class FooComponent extends react.Component<FooProps> { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay2 = false | ||
..initiallyOpen = false | ||
); | ||
} | ||
''', | ||
); | ||
}); | ||
|
||
test('`useLegacyPositioning` prop', () { | ||
testSuggestor( | ||
expectedPatchCount: 1, | ||
input: ''' | ||
import 'package:react/react.dart' as react; | ||
|
||
class FooComponent extends react.Component2<FooProps> { | ||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..useLegacyPositioning = false | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
expectedOutput: ''' | ||
import 'package:react/react.dart' as react; | ||
|
||
class FooComponent extends react.Component2<FooProps> { | ||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
); | ||
}); | ||
|
||
test('`overlay`, `isOverlay`, and `useLegacyPositioning` props', () { | ||
testSuggestor( | ||
expectedPatchCount: 3, | ||
input: ''' | ||
@Component2() | ||
class FooComponent extends SomeOtherClass<FooProps> { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay = false | ||
..initiallyOpen = false | ||
); | ||
|
||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..trigger = OverlayTriggerType.MANUAL | ||
..useLegacyPositioning = false | ||
..overlay = _renderIndicator() | ||
..repositionOverlay = repositionOverlayOverTrigger | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
expectedOutput: ''' | ||
@Component2() | ||
class FooComponent extends SomeOtherClass<FooProps> { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay2 = false | ||
..initiallyOpen = false | ||
); | ||
|
||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..trigger = OverlayTriggerType.MANUAL | ||
..overlay2 = _renderIndicator() | ||
..repositionOverlay = repositionOverlayOverTrigger | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
); | ||
}); | ||
|
||
test('already updated props', () { | ||
testSuggestor( | ||
expectedPatchCount: 0, | ||
input: ''' | ||
@Component2() | ||
class FooComponent extends UiComponent2<FooProps> { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay2 = false | ||
..initiallyOpen = false | ||
); | ||
|
||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..trigger = OverlayTriggerType.MANUAL | ||
..overlay2 = _renderIndicator() | ||
..repositionOverlay = repositionOverlayOverTrigger | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
); | ||
}); | ||
|
||
test('not in component class', () { | ||
testSuggestor( | ||
expectedPatchCount: 0, | ||
input: ''' | ||
class FooClass extends SomeOtherClass { | ||
@override | ||
Map getDefaultProps() => (newProps() | ||
..isFontSizeControl = false | ||
..isOverlay = false | ||
..initiallyOpen = false | ||
); | ||
|
||
@override | ||
render() { | ||
return (OverlayTrigger() | ||
..addProps(props.overlayTriggerProps) | ||
..trigger = OverlayTriggerType.MANUAL | ||
..useLegacyPositioning = false | ||
..overlay = _renderIndicator() | ||
..repositionOverlay = repositionOverlayOverTrigger | ||
..ref = ((ref) => overlayTriggerRef = ref) | ||
)(_renderMainContent()); | ||
} | ||
} | ||
''', | ||
); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Girlscoutin like a champ! 👍