-
Notifications
You must be signed in to change notification settings - Fork 57
AF-3749: Loosen validation on Factory and meta initializers #222
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import 'dart:mirrors'; | |
| import 'package:analyzer/analyzer.dart'; | ||
| import 'package:barback/barback.dart' show TransformLogger; | ||
| import 'package:over_react/src/component_declaration/annotations.dart' as annotations; | ||
| import 'package:over_react/src/transformer/util.dart' show getMetaField; | ||
| import 'package:source_span/source_span.dart'; | ||
| import 'package:transformer_utils/src/transformed_source_file.dart' show getSpan; | ||
| import 'package:transformer_utils/transformer_utils.dart'; | ||
|
|
@@ -211,15 +212,7 @@ class ParsedDeclarations { | |
| }); | ||
| } else { | ||
| void validateMetaField(ClassDeclaration cd, String expectedType) { | ||
| bool isPropsOrStateMeta(ClassMember member) { | ||
| if (member is! FieldDeclaration) return false; | ||
| final FieldDeclaration fd = member; | ||
| if (!fd.isStatic) return false; | ||
| if (fd.fields.variables.length > 1) return false; | ||
| if (fd.fields.variables.single.name.name != 'meta') return false; | ||
| return true; | ||
| } | ||
| final FieldDeclaration metaField = cd.members.firstWhere(isPropsOrStateMeta, orElse: () => null); | ||
| final metaField = getMetaField(cd); | ||
| if (metaField == null) return; | ||
|
|
||
| if (metaField.fields.type?.toSource() != expectedType) { | ||
|
|
@@ -229,15 +222,17 @@ class ParsedDeclarations { | |
| ); | ||
| } | ||
| final isClassPrivate = cd.name.name.startsWith('_'); | ||
| final expectedInitializer = isClassPrivate | ||
| ? '_\$metaFor${cd.name.name.substring(1)}' | ||
| : '\$metaFor${cd.name.name}'; | ||
| var expectedInitializers = ['\$metaFor${cd.name.name}', '_\$metaFor${cd.name.name}']; | ||
| if (isClassPrivate) { | ||
| expectedInitializers.add('_\$metaFor${cd.name.name.substring(1)}'); | ||
| } | ||
|
|
||
| final initializer = metaField.fields.variables.single.initializer?.toSource(); | ||
| if (initializer != expectedInitializer) { | ||
| if (!expectedInitializers.contains(initializer)) { | ||
| error( | ||
| 'Static $expectedType field in accessor class must be initialized to ' | ||
| '`$expectedInitializer`', | ||
| 'Static $expectedType field in accessor class must be initialized to:' | ||
| // The second in the list of expected initializers is the one it will need to be once on Dart 2 | ||
| '`${expectedInitializers[1]}`', | ||
| getSpan(sourceFile, metaField), | ||
| ); | ||
| } | ||
|
|
@@ -280,6 +275,36 @@ class ParsedDeclarations { | |
| } | ||
| } | ||
|
|
||
| // validate that the factory is initialized correctly | ||
| final factory = declarationMap[key_factory].length <= 1 ? singleOrNull(declarationMap[key_factory]) : null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #nit should use a different variable name since
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, TIL! Thanks Greg :) |
||
| if (factory != null && factory is TopLevelVariableDeclaration) { | ||
| final String factoryName = factory.variables.variables.first.name.name; | ||
|
|
||
| if (factory.variables.variables.length != 1) { | ||
| error('Factory declarations must be a single variable.', | ||
| getSpan(sourceFile, factory.variables)); | ||
| } | ||
|
|
||
| final variable = factory.variables.variables.first; | ||
| final isPrivate = factoryName.startsWith('_'); | ||
| var expectedInitializers = ['\$$factoryName', '_\$$factoryName']; | ||
|
|
||
| if (isPrivate) { | ||
| expectedInitializers.add('_\$${factoryName.substring(1)}'); | ||
| } | ||
|
|
||
| if (variable.initializer != null && | ||
| !expectedInitializers.contains(variable.initializer.toString())) { | ||
| 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. ' | ||
| // The second in the list of expected initializers is the one it will need to be once on Dart 2 | ||
| 'Should be:\n ${expectedInitializers[1]}', | ||
| getSpan(sourceFile, variable.initializer) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (hasErrors) { | ||
| for (final key in declarationMap.keys) { | ||
| declarationMap[key] = []; | ||
|
|
||
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.
#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think addressing #222 (comment) handles that effectively.