From 6a11211e1284c6f8142cdbcc0794dc04323f94a9 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 28 May 2019 13:37:15 -0700 Subject: [PATCH] Add checks to constructors and add missing constructor members (flutter/engine#9106) --- .../web_sdk/test/api_conform_test.dart | 96 +++++++++++++++++-- 1 file changed, 90 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/web_sdk/test/api_conform_test.dart b/engine/src/flutter/web_sdk/test/api_conform_test.dart index 1dd8b850c1..137237bc79 100644 --- a/engine/src/flutter/web_sdk/test/api_conform_test.dart +++ b/engine/src/flutter/web_sdk/test/api_conform_test.dart @@ -39,10 +39,70 @@ void main() { } // Next will check that the public methods exposed in each library are // identical. - final Map uiMethods = {}; - final Map webMethods = {}; + final Map uiMethods = + {}; + final Map webMethods = + {}; + final Map uiConstructors = + {}; + final Map webConstructors = + {}; _collectPublicMethods(uiClass, uiMethods); _collectPublicMethods(webClass, webMethods); + _collectPublicConstructors(uiClass, uiConstructors); + _collectPublicConstructors(webClass, webConstructors); + + for (String name in uiConstructors.keys) { + final ConstructorDeclaration uiConstructor = uiConstructors[name]; + final ConstructorDeclaration webConstructor = webConstructors[name]; + if (webConstructor == null) { + failed = true; + print( + 'Warning: lib/ui/ui.dart $className.$name is missing from lib/stub_ui/ui.dart.', + ); + continue; + } + + if (uiConstructor.parameters.parameters.length != + webConstructor.parameters.parameters.length) { + failed = true; + print( + 'Warning: lib/ui/ui.dart $className.$name has a different parameter ' + 'length than in lib/stub_ui/ui.dart.'); + } + + for (int i = 0; + i < uiConstructor.parameters.parameters.length && + i < uiConstructor.parameters.parameters.length; + i++) { + // Technically you could re-order named parameters and still be valid, + // but we enforce that they are identical. + for (int i = 0; + i < uiConstructor.parameters.parameters.length && + i < webConstructor.parameters.parameters.length; + i++) { + final FormalParameter uiParam = + uiConstructor.parameters.parameters[i]; + final FormalParameter webParam = + webConstructor.parameters.parameters[i]; + if (webParam.identifier.name != uiParam.identifier.name) { + failed = true; + print('Warning: lib/ui/ui.dart $className.$name parameter $i' + ' ${uiParam.identifier.name} has a different name in lib/stub_ui/ui.dart.'); + } + if (uiParam.isPositional != webParam.isPositional) { + failed = true; + print('Warning: lib/ui/ui.dart $className.$name parameter $i' + '${uiParam.identifier.name} is positional, but not in lib/stub_ui/ui.dart.'); + } + if (uiParam.isNamed != webParam.isNamed) { + failed = true; + print('Warning: lib/ui/ui.dart $className.$name parameter $i' + '${uiParam.identifier.name} is named, but not in lib/stub_ui/ui.dart.'); + } + } + } + } for (String methodName in uiMethods.keys) { final MethodDeclaration uiMethod = uiMethods[methodName]; @@ -57,7 +117,8 @@ void main() { if (uiMethod.parameters == null || webMethod.parameters == null) { continue; } - if (uiMethod.parameters.parameters.length != webMethod.parameters.parameters.length) { + if (uiMethod.parameters.parameters.length != + webMethod.parameters.parameters.length) { failed = true; print( 'Warning: lib/ui/ui.dart $className.$methodName has a different parameter ' @@ -65,7 +126,10 @@ void main() { } // Technically you could re-order named parameters and still be valid, // but we enforce that they are identical. - for (int i = 0; i < uiMethod.parameters.parameters.length && i < webMethod.parameters.parameters.length; i++) { + for (int i = 0; + i < uiMethod.parameters.parameters.length && + i < webMethod.parameters.parameters.length; + i++) { final FormalParameter uiParam = uiMethod.parameters.parameters[i]; final FormalParameter webParam = webMethod.parameters.parameters[i]; if (webParam.identifier.name != uiParam.identifier.name) { @@ -73,12 +137,12 @@ void main() { print('Warning: lib/ui/ui.dart $className.$methodName parameter $i' ' ${uiParam.identifier.name} has a different name in lib/stub_ui/ui.dart.'); } - if (uiParam.isPositional && !webParam.isPositional) { + if (uiParam.isPositional != webParam.isPositional) { failed = true; print('Warning: lib/ui/ui.dart $className.$methodName parameter $i' '${uiParam.identifier.name} is positional, but not in lib/stub_ui/ui.dart.'); } - if (uiParam.isNamed && !webParam.isNamed) { + if (uiParam.isNamed != webParam.isNamed) { failed = true; print('Warning: lib/ui/ui.dart $className.$methodName parameter $i' '${uiParam.identifier.name} is named, but not in lib/stub_ui/ui.dart.'); @@ -94,6 +158,8 @@ void main() { exit(0); } +void _checkParameters() {} + // Collects all public classes defined by the part files of [unit]. void _collectPublicClasses(CompilationUnit unit, Map destination, String root) { @@ -121,6 +187,24 @@ void _collectPublicClasses(CompilationUnit unit, } } +void _collectPublicConstructors(ClassDeclaration classDeclaration, + Map destination) { + for (ClassMember member in classDeclaration.members) { + if (member is! ConstructorDeclaration) { + continue; + } + final ConstructorDeclaration method = member; + if (method?.name?.name == null) { + destination['Unnamed Constructor'] = method; + continue; + } + if (method.name.name.startsWith('_')) { + continue; + } + destination[method.name.name] = method; + } +} + void _collectPublicMethods(ClassDeclaration classDeclaration, Map destination) { for (ClassMember member in classDeclaration.members) {