diff --git a/lib/src/transformer/impl_generation.dart b/lib/src/transformer/impl_generation.dart index 034a6abae..d4f6fb930 100644 --- a/lib/src/transformer/impl_generation.dart +++ b/lib/src/transformer/impl_generation.dart @@ -104,9 +104,10 @@ class ImplGenerator { } declarations.factory.node.variables.variables.forEach((variable) { - if (variable.initializer != null) { + if (variable.initializer != null && !(variable.initializer.toString() == '\$$factoryName')) { logger.error( - 'Factory variables are stubs for the generated factories, and should not have initializers.', + 'Factory variables are stubs for the generated factories, and should not have initializers ' + 'unless initialized with \$$factoryName for Dart 2 builder compatibility.', span: getSpan(sourceFile, variable.initializer) ); } diff --git a/test/vm_tests/transformer/impl_generation_test.dart b/test/vm_tests/transformer/impl_generation_test.dart index 9947b0087..6881fcd33 100644 --- a/test/vm_tests/transformer/impl_generation_test.dart +++ b/test/vm_tests/transformer/impl_generation_test.dart @@ -256,6 +256,28 @@ main() { reason: 'should preserve existing inheritance'); }); + test('with an initialized UiFactory with \$', () { + final originalUiFactoryLine = 'UiFactory Foo = \$Foo;'; + final transformedUiFactoryLine = 'UiFactory Foo = ([Map backingProps]) => new _\$FooPropsImpl(backingProps);'; + + preservedLineNumbersTest(''' + @Factory() + UiFactory Foo = \$Foo; + + @Props() + class FooProps {} + + @Component() + class FooComponent { + render() => null; + } + '''); + + var transformedSource = transformedFile.getTransformedText(); + expect(transformedSource, isNot(contains(originalUiFactoryLine))); + expect(transformedSource, contains(transformedUiFactoryLine)); + }); + test('with static PropsMeta and StateMeta declaration', () { final originalPropsMetaLine = 'static const PropsMeta meta = \$metaForFooProps;'; final originalStateMetaLine = 'static const StateMeta meta = \$metaForFooState;'; @@ -661,7 +683,20 @@ main() { $restOfComponent '''); - verify(logger.error('Factory variables are stubs for the generated factories, and should not have initializers.', span: any)); + verify(logger.error('Factory variables are stubs for the generated factories, and should not have initializers' + ' unless initialized with \$Foo for Dart 2 builder compatibility.', span: any)); + }); + + test('declared with an \$ prefixed initializer matching the factory name', () { + setUpAndGenerate(''' + @Factory() + UiFactory Foo = \$Foo; + + $restOfComponent + '''); + + verifyNever(logger.error('Factory variables are stubs for the generated factories, and should not have initializers' + ' unless initialized with \$Foo for Dart 2 builder compatibility.', span: any)); }); });