-
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
AF-1917: Make over_react_codemod script idempotent #5
Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on HipChat: InfoSec Forum. |
Merge Requirements Met ✅Request Rosie to automerge this pull request by including @Workiva/release-management-p in a comment.
General InformationTicket(s): Code Review(s): #5 Reviewers: corwinsheahan-wf, evanweible-wf Additional InformationWatchlist Notifications: None
Note: This is a shortened report. Click here to view Rosie's full evaluation. |
migrater.py
Outdated
@@ -240,9 +248,8 @@ def props_metas_suggest(lines, path): | |||
|
|||
last_class_def_line = lines[line_number + offset] | |||
if class_body_is_empty: | |||
last_class_def_line = last_class_def_line.replace('{}\n', '{\n') | |||
last_class_def_line = last_class_def_line.replace('{}\n', '{\n') |
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.
one too many indents here
test/migrater_test.dart
Outdated
|
||
/// Codemod the temporary directory contents twice. | ||
await codemodTempDirectory(pathToMigrater, pathToTempDirectory); | ||
await codemodTempDirectory(pathToMigrater, pathToTempDirectory); |
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 is a lot of extra setup for each of these idempotent tests. Instead, I think we could accomplish this via an optional boolean param on the setupAndMakeComparison
function like bool assertIdempotence
. If set, then the function can run the script once, capture the result, run it again, capture the second result, and ensure that they're identical.
migrater.py
Outdated
@@ -32,7 +32,7 @@ def get_factory_name(line): | |||
>>> get_factory_name('UiFactory<DemoProps> Demo;') | |||
'Demo' | |||
""" | |||
match = re.search(r' (\w+);', line) | |||
match = re.search(r'(\w+);', line) |
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.
Prior to this boilerplate change, factory definitions would always be on one line. But with this change, dartfmt may break the right-hand-side of the factory assignment onto its own line like so:
UiFactory<NotificationManagerPrimitiveProps> NotificationManagerPrimitive =
$NotificationManagerPrimitive;
We'll need to account for this; currently the script throws when it encounters this.
I think we should add a new method that determines whether or not the current line needs a factory update (which also accounts for lines that have already been transformed). Something like this:
diff --git a/migrater.py b/migrater.py
index c850d6d..4d25e7c 100644
--- a/migrater.py
+++ b/migrater.py
@@ -9,6 +9,7 @@ IGNORE_GENERATED_URI_COMMENT_LINE = '// ignore: uri_does_not_exist\n'
GENERATED_PART_EXTENSION = '.generated.dart'
DOLLAR_PROPS_USAGE_REGEX = r'(?:const|new)\s+\$Props\s*\(\s*([$A-Za-z0-9_.]+)\s*\)'
DOLLAR_PROP_KEYS_USAGE_REGEX = r'(?:const|new)\s+\$PropKeys\s*\(\s*([$A-Za-z0-9_.]+)\s*\)'
+FACTORY_REGEX = r'^UiFactory(<\w+>) (\w+);'
def get_class_name(line):
@@ -32,10 +33,10 @@ def get_factory_name(line):
>>> get_factory_name('UiFactory<DemoProps> Demo;')
'Demo'
"""
- match = re.search(r'(\w+);', line)
+ match = re.search(FACTORY_REGEX, line)
if not match:
raise Exception('Could not parse factory name from:\n%s' % line)
- return match.group(1)
+ return match.group(2)
def get_part_name(path):
@@ -88,6 +89,10 @@ def has_dollar_prop_keys_usages(lines):
return re.search(DOLLAR_PROP_KEYS_USAGE_REGEX, s, flags=re.MULTILINE) is not None
+def needs_factory_update(line):
+ return re.search(FACTORY_REGEX, line) is not None
+
+
def update_dollar_props_usages(lines):
s = ''.join(lines)
match = re.search(DOLLAR_PROPS_USAGE_REGEX, s, flags=re.MULTILINE)
@@ -139,16 +144,12 @@ def factories_suggest(lines, path):
need_part = False
for line_number, line in enumerate(lines):
- if line.startswith('UiFactory'):
+ if needs_factory_update(line):
factory_name = get_factory_name(line)
-
- if '$' + factory_name in line:
- continue
-
need_part = True
ignore_line = '// ignore: undefined_identifier\n'
- new_line = line.replace(';\n', ' = $%s;\n' % (factory_name))
+ new_line = line.replace(';\n', ' = $%s;\n' % factory_name)
patches.append(codemod.Patch(line_number, new_lines=[
ignore_line,
…F-1917 # Conflicts: # test/migrater_test.dart
QA +1
@Workiva/release-management-p |
@evanweible-wf I will not merge this because:
|
+1 from RM |
DO NOT MERGE UNTIL #4 IS MERGED
Changes
migrater.py
idempotent@corwinsheahan-wf @evanweible-wf @maxwellpeterson-wf