-
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
FED-2094 Add suggestor that replaces useRef with useRefInit #273
Merged
rmconsole3-wf
merged 6 commits into
master
from
batch/fedx/FED-2094_useRefInit_migration
Mar 6, 2024
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61272bf
Add suggestor that replaces useRef with useRefInit
aaronlademann-wf c4fa9c3
Remove standalone executable
aaronlademann-wf 6c5f9d2
Add null_safety_prep executable
aaronlademann-wf 439e321
Fix test name
aaronlademann-wf 6fff852
No need for resolved AST
aaronlademann-wf 9031bd7
Format
aaronlademann-wf 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
64 changes: 64 additions & 0 deletions
64
lib/src/dart3_suggestors/null_safety_prep/use_ref_init_migration.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,64 @@ | ||
// Copyright 2024 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/analysis/results.dart'; | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
import 'package:over_react_codemod/src/util/class_suggestor.dart'; | ||
|
||
/// Suggestor that finds instances of `useRef` function invocations that | ||
/// pass an argument, and replaces them with `useRefInit` to prep for | ||
/// null safety. | ||
/// | ||
/// Example: | ||
/// | ||
/// ```dart | ||
/// // Before | ||
/// final ref = useRef(someNonNulLValue); | ||
/// // After | ||
/// final ref = useRefInit(someNonNulLValue); | ||
/// ``` | ||
class UseRefInitMigration extends RecursiveAstVisitor with ClassSuggestor { | ||
ResolvedUnitResult? _result; | ||
|
||
@override | ||
visitArgumentList(ArgumentList node) { | ||
super.visitArgumentList(node); | ||
|
||
if (node.arguments.isEmpty) return; | ||
|
||
dynamic possibleInvocation = node.parent; | ||
if (possibleInvocation is MethodInvocation) { | ||
String fnName = ''; | ||
if (possibleInvocation.function is SimpleIdentifier) { | ||
fnName = (possibleInvocation.function as SimpleIdentifier).name; | ||
} | ||
|
||
if (fnName == 'useRef') { | ||
yieldPatch('useRefInit', possibleInvocation.function.offset, | ||
possibleInvocation.function.end); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<void> generatePatches() async { | ||
_result = await context.getResolvedUnit(); | ||
if (_result == null) { | ||
throw Exception( | ||
'Could not get resolved result for "${context.relativePath}"'); | ||
} | ||
_result!.unit.accept(this); | ||
} | ||
} |
aaronlademann-wf marked this conversation as resolved.
Show resolved
Hide resolved
|
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 2024 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:args/args.dart'; | ||
import 'package:codemod/codemod.dart'; | ||
import 'package:over_react_codemod/src/dart3_suggestors/null_safety_prep/use_ref_init_migration.dart'; | ||
import 'package:over_react_codemod/src/ignoreable.dart'; | ||
import 'package:over_react_codemod/src/util.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:dom_callback_null_args | ||
"""; | ||
|
||
void main(List<String> args) async { | ||
final parser = ArgParser.allowAnything(); | ||
|
||
final parsedArgs = parser.parse(args); | ||
final dartPaths = allDartPathsExceptHiddenAndGenerated(); | ||
|
||
exitCode = await runInteractiveCodemod( | ||
dartPaths, | ||
ignoreable(UseRefInitMigration()), | ||
defaultYes: true, | ||
args: parsedArgs.rest, | ||
additionalHelpOutput: parser.usage, | ||
changesRequiredOutput: _changesRequiredOutput, | ||
); | ||
} |
108 changes: 108 additions & 0 deletions
108
test/dart3_suggestors/null_safety_prep/use_ref_init_migration_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,108 @@ | ||
// Copyright 2024 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/dart3_suggestors/null_safety_prep/use_ref_init_migration.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../resolved_file_context.dart'; | ||
import '../../util.dart'; | ||
import '../../util/component_usage_migrator_test.dart'; | ||
|
||
void main() { | ||
final resolvedContext = SharedAnalysisContext.overReact; | ||
|
||
// Warm up analysis in a setUpAll so that if getting the resolved AST times out | ||
// (which is more common for the WSD context), it fails here instead of failing the first test. | ||
setUpAll(resolvedContext.warmUpAnalysis); | ||
|
||
group('DomCallbackNullArgs', () { | ||
aaronlademann-wf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
late SuggestorTester testSuggestor; | ||
|
||
setUp(() { | ||
testSuggestor = getSuggestorTester( | ||
UseRefInitMigration(), | ||
resolvedContext: resolvedContext, | ||
); | ||
}); | ||
|
||
test( | ||
'leaves useRef function invocations alone when the argument list is empty', | ||
() async { | ||
await testSuggestor( | ||
expectedPatchCount: 0, | ||
input: withOverReactImport(''' | ||
final Foo = uiFunction<UiProps>( | ||
(props) { | ||
final foo = useRef(); | ||
print(foo); | ||
return null; | ||
}, | ||
UiFactoryConfig(displayName: 'Foo'), | ||
); | ||
'''), | ||
); | ||
}); | ||
|
||
test('replaces useRef usages with useRefInit when an argument is passed', | ||
() async { | ||
await testSuggestor( | ||
expectedPatchCount: 1, | ||
input: withOverReactImport(''' | ||
final Foo = uiFunction<UiProps>( | ||
(props) { | ||
final foo = useRef('bar'); | ||
return (Dom.div()..id = foo.current)(); | ||
}, | ||
UiFactoryConfig(displayName: 'Foo'), | ||
); | ||
'''), | ||
expectedOutput: withOverReactImport(''' | ||
final Foo = uiFunction<UiProps>( | ||
(props) { | ||
final foo = useRefInit('bar'); | ||
return (Dom.div()..id = foo.current)(); | ||
}, | ||
UiFactoryConfig(displayName: 'Foo'), | ||
); | ||
'''), | ||
); | ||
}); | ||
|
||
test( | ||
'replaces useRef<Generic> usages with useRefInit<Generic> when an argument is passed', | ||
() async { | ||
await testSuggestor( | ||
expectedPatchCount: 1, | ||
input: withOverReactImport(''' | ||
final Foo = uiFunction<UiProps>( | ||
(props) { | ||
final foo = useRef<String>('bar'); | ||
return (Dom.div()..id = foo.current)(); | ||
}, | ||
UiFactoryConfig(displayName: 'Foo'), | ||
); | ||
'''), | ||
expectedOutput: withOverReactImport(''' | ||
final Foo = uiFunction<UiProps>( | ||
(props) { | ||
final foo = useRefInit<String>('bar'); | ||
return (Dom.div()..id = foo.current)(); | ||
}, | ||
UiFactoryConfig(displayName: 'Foo'), | ||
); | ||
'''), | ||
); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
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.
I was waiting forever for this to run on DPC and brought it up to Greg and he realized - does this need to be running on resolved AST? It doesn't seem like anything is checking for the package source so it would probably run a lot faster to do this not resolved
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.
So do I just remove the
generatePatches
override altogether then? Seems like its an empty method in the base class?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.
nvm... I figured it out