AF-3749: Loosen validation on Factory and meta initializers#222
AF-3749: Loosen validation on Factory and meta initializers#222rmconsole3-wf merged 3 commits intomasterfrom
Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on Slack: #support-infosec. |
Codecov Report
@@ Coverage Diff @@
## master #222 +/- ##
========================================
+ Coverage 90.1% 90.3% +0.2%
========================================
Files 35 35
Lines 1767 1772 +5
========================================
+ Hits 1592 1600 +8
+ Misses 175 172 -3 |
| import 'package:transformer_utils/src/transformed_source_file.dart' show getSpan; | ||
| import 'package:transformer_utils/transformer_utils.dart'; | ||
|
|
||
|
|
| }); | ||
| }); | ||
|
|
||
|
|
sebastianmalysa-wf
left a comment
There was a problem hiding this comment.
Nice!! Looks good to me.
| } | ||
|
|
||
| // validate that the factory is initialized correctly | ||
| final factory = declarationMap[key_factory].length <= 1 ? singleOrNull(declarationMap[key_factory]) : null; |
There was a problem hiding this comment.
#nit should use a different variable name since factory is a language keyword
There was a problem hiding this comment.
FYI factory just shows up weirdly formatted in GitHub, and isn't officially discouraged for use a variable name since it's a built-in identifier.
There was a problem hiding this comment.
Oh, TIL! Thanks Greg :)
| final factory = declarationMap[key_factory].length <= 1 ? singleOrNull(declarationMap[key_factory]) : null; | ||
| if (factory != null && factory is TopLevelVariableDeclaration) { | ||
| final String factoryName = factory.variables.variables.first.name.toString(); | ||
| factory.variables.variables.forEach((variable) { |
There was a problem hiding this comment.
Should be sufficient to just look at the first variable in this declaration instead of iterating over the list
There was a problem hiding this comment.
We should also probably validate that there's only one variable in the declaration, but that's unrelated to these changes PR. (this was cut-pasted)
There was a problem hiding this comment.
That is already done in impl_generation, but I moved it to here, since we might as well fail earlier in that case.
| // validate that the factory is initialized correctly | ||
| final factory = declarationMap[key_factory].length <= 1 ? singleOrNull(declarationMap[key_factory]) : null; | ||
| if (factory != null && factory is TopLevelVariableDeclaration) { | ||
| final String factoryName = factory.variables.variables.first.name.toString(); |
There was a problem hiding this comment.
#nit my preference is to use a field if available rather than relying on a toString() method (it's not always clear whether or not toString() has been overridden to provide something meaningful), and in this case there should be a name field available:
| final String factoryName = factory.variables.variables.first.name.toString(); | |
| final String factoryName = factory.variables.variables.first.name.name; |
| error( | ||
| 'Factory variables are stubs for the generated factories, and should not have initializers ' | ||
| 'unless initialized with a valid variable name for Dart 2 builder compatibility. ' | ||
| 'Should be one of:\n $expectedInitializers', |
There was a problem hiding this comment.
Instead of listing all of them (which sort of formalizes the support when really we want it to be temporary), let's provide the initializer value that we will eventually expect it to be
| test('_\$ prefixed variable name with private prefix stripped', () { | ||
| setUpAndParse(''' | ||
| @Factory() | ||
| UiFactory<FooProps> _Foo = _\$_Foo; |
There was a problem hiding this comment.
private prefix isn't stripped here
| UiFactory<FooProps> _Foo = _\$_Foo; | |
| UiFactory<FooProps> _Foo = _\$Foo; |
| var expectedInitializers = ['\$metaFor${cd.name.name}', '_\$metaFor${cd.name.name}']; | ||
| if (isClassPrivate) { | ||
| expectedInitializers.add('_\$metaFor${cd.name.name.substring(1)}'); | ||
| } |
There was a problem hiding this comment.
#nit would be nice to have aTODO or a comment saying which versions need to eventually be removed?
There was a problem hiding this comment.
I think addressing #222 (comment) handles that effectively.
|
QA +1
@Workiva/release-management-p |
Ultimate problem:
The new version of codemod will update the initializer for private factories to
_$_Fooif the factory name is_Foo. We need to relax the validation on this initializer to allow for this.It will do the same with the
metaForconstant.All of the following variations should be supported:
Factories:
$Foo_$Foo$_Foo_$_Foo_$FooMeta fields:
$metaForFoo_$metaForFoo$metaFor_Foo_$metaFor_Foo_$metaForFooNote: once the Dart 2 transition is complete, these restrictions will be tightened down to:
Factories:
_$Foo_$_FooMeta fields:
_$metaForFoo_$metaFor_FooHow it was fixed:
Testing suggestions:
Potential areas of regression:
False positive validation warnings on the already codemodded WSD. Hitting the testing suggestions will cover this possible regression.