Skip to content

Commit

Permalink
[web] migrate web_sdk to null safety (flutter#26720)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjbanov authored Jun 12, 2021
1 parent 04347f5 commit d19fb6e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/web_ui/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: ui
publish_to: none

# Keep the SDK version range in sync with pubspecs under web_sdk
environment:
sdk: ">=2.12.0-0 <3.0.0"

Expand All @@ -9,7 +10,6 @@ dependencies:
meta: 1.3.0

dev_dependencies:
analyzer: 1.1.0
archive: 3.1.2
html: 0.15.0
http: 0.13.0
Expand Down
8 changes: 5 additions & 3 deletions web_sdk/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: web_sdk_tests
author: Flutter Authors <[email protected]>

# Keep the SDK version range in sync with lib/web_ui/pubspec.yaml
environment:
sdk: '>=2.11.0 <3.0.0'
sdk: ">=2.12.0-0 <3.0.0"

dev_dependencies:
analyzer: ^0.40.6
test: any
analyzer: 1.7.1
test: 1.17.7
4 changes: 1 addition & 3 deletions web_sdk/sdk_rewriter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6

import 'dart:io';

import 'package:args/args.dart';
Expand Down Expand Up @@ -106,7 +104,7 @@ void main(List<String> arguments) {
}
}

String rewriteFile(String source, {String filePath, bool isUi, bool isEngine}) {
String rewriteFile(String source, {required String filePath, required bool isUi, required bool isEngine}) {
final List<Replacer> replacementPatterns = <Replacer>[];
replacementPatterns.addAll(sharedPatterns);
if (isUi) {
Expand Down
83 changes: 42 additions & 41 deletions web_sdk/test/api_conform_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6

import 'dart:io';

import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:pub_semver/pub_semver.dart';

// Ignore members defined on Object.
const Set<String> _kObjectMembers = <String>{
Expand All @@ -19,7 +18,10 @@ const Set<String> _kObjectMembers = <String>{
};

CompilationUnit _parseAndCheckDart(String path) {
final FeatureSet analyzerFeatures = FeatureSet.fromEnableFlags(<String>['non-nullable']);
final FeatureSet analyzerFeatures = FeatureSet.fromEnableFlags2(
sdkLanguageVersion: Version.parse('2.12.0'),
flags: <String>['non-nullable'],
);
if (!analyzerFeatures.isEnabled(Feature.non_nullable)) {
throw Exception('non-nullable feature is disabled.');
}
Expand Down Expand Up @@ -50,9 +52,9 @@ void main() {

bool failed = false;
print('Checking ${uiClasses.length} public classes.');
for (String className in uiClasses.keys) {
final ClassDeclaration uiClass = uiClasses[className];
final ClassDeclaration webClass = webClasses[className];
for (final String className in uiClasses.keys) {
final ClassDeclaration uiClass = uiClasses[className]!;
final ClassDeclaration webClass = webClasses[className]!;
// If the web class is missing there isn't much left to do here. Print a
// warning and move along.
if (webClass == null) {
Expand All @@ -76,9 +78,9 @@ void main() {
_collectPublicConstructors(uiClass, uiConstructors);
_collectPublicConstructors(webClass, webConstructors);

for (String name in uiConstructors.keys) {
final ConstructorDeclaration uiConstructor = uiConstructors[name];
final ConstructorDeclaration webConstructor = webConstructors[name];
for (final String name in uiConstructors.keys) {
final ConstructorDeclaration uiConstructor = uiConstructors[name]!;
final ConstructorDeclaration? webConstructor = webConstructors[name];
if (webConstructor == null) {
failed = true;
print(
Expand Down Expand Up @@ -109,31 +111,31 @@ void main() {
uiConstructor.parameters.parameters[i];
final FormalParameter webParam =
webConstructor.parameters.parameters[i];
if (webParam.identifier.name != uiParam.identifier.name) {
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/web_ui/ui.dart.');
' ${uiParam.identifier!.name} has a different name in lib/web_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/web_ui/ui.dart.');
'${uiParam.identifier!.name} is positional, but not in lib/web_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/web_ui/ui.dart.');
'${uiParam.identifier!.name} is named, but not in lib/web_ui/ui.dart.');
}
}
}
}

for (String methodName in uiMethods.keys) {
for (final String methodName in uiMethods.keys) {
if (_kObjectMembers.contains(methodName)) {
continue;
}
final MethodDeclaration uiMethod = uiMethods[methodName];
final MethodDeclaration webMethod = webMethods[methodName];
final MethodDeclaration uiMethod = uiMethods[methodName]!;
final MethodDeclaration? webMethod = webMethods[methodName];
if (webMethod == null) {
failed = true;
print(
Expand All @@ -144,8 +146,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 '
Expand All @@ -154,25 +156,25 @@ 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 < 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) {
final FormalParameter uiParam = uiMethod.parameters!.parameters[i];
final FormalParameter webParam = webMethod.parameters!.parameters[i];
if (webParam.identifier!.name != uiParam.identifier!.name) {
failed = true;
print('Warning: lib/ui/ui.dart $className.$methodName parameter $i'
' ${uiParam.identifier.name} has a different name in lib/web_ui/ui.dart.');
' ${uiParam.identifier!.name} has a different name in lib/web_ui/ui.dart.');
}
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/web_ui/ui.dart.');
'${uiParam.identifier!.name} is positional, but not in lib/web_ui/ui.dart.');
}
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/web_ui/ui.dart.');
'${uiParam.identifier!.name} is named, but not in lib/web_ui/ui.dart.');
}
}
// check return type.
Expand All @@ -199,18 +201,18 @@ void main() {
// Collects all public classes defined by the part files of [unit].
void _collectPublicClasses(CompilationUnit unit,
Map<String, ClassDeclaration> destination, String root) {
for (Directive directive in unit.directives) {
for (final Directive directive in unit.directives) {
if (directive is! PartDirective) {
continue;
}
final PartDirective partDirective = directive as PartDirective;
final PartDirective partDirective = directive;
final String literalUri = partDirective.uri.toString();
final CompilationUnit subUnit = _parseAndCheckDart('$root${literalUri.substring(1, literalUri.length - 1)}');
for (CompilationUnitMember member in subUnit.declarations) {
for (final CompilationUnitMember member in subUnit.declarations) {
if (member is! ClassDeclaration) {
continue;
}
final ClassDeclaration classDeclaration = member as ClassDeclaration;
final ClassDeclaration classDeclaration = member;
if (classDeclaration.name.name.startsWith('_')) {
continue;
}
Expand All @@ -221,32 +223,31 @@ void _collectPublicClasses(CompilationUnit unit,

void _collectPublicConstructors(ClassDeclaration classDeclaration,
Map<String, ConstructorDeclaration> destination) {
for (ClassMember member in classDeclaration.members) {
for (final ClassMember member in classDeclaration.members) {
if (member is! ConstructorDeclaration) {
continue;
}
final ConstructorDeclaration method = member as ConstructorDeclaration;
if (method?.name?.name == null) {
destination['Unnamed Constructor'] = method;
final String? methodName = member.name?.name;
if (methodName == null) {
destination['Unnamed Constructor'] = member;
continue;
}
if (method.name.name.startsWith('_')) {
if (methodName.startsWith('_')) {
continue;
}
destination[method.name.name] = method;
destination[methodName] = member;
}
}

void _collectPublicMethods(ClassDeclaration classDeclaration,
Map<String, MethodDeclaration> destination) {
for (ClassMember member in classDeclaration.members) {
for (final ClassMember member in classDeclaration.members) {
if (member is! MethodDeclaration) {
continue;
}
final MethodDeclaration method = member as MethodDeclaration;
if (method.name.name.startsWith('_')) {
if (member.name.name.startsWith('_')) {
continue;
}
destination[method.name.name] = method;
destination[member.name.name] = member;
}
}
2 changes: 0 additions & 2 deletions web_sdk/web_engine_tester/lib/golden_tester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.12

import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
Expand Down
1 change: 1 addition & 0 deletions web_sdk/web_engine_tester/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: web_engine_tester

# Keep the SDK version range in sync with lib/web_ui/pubspec.yaml
environment:
sdk: ">=2.12.0-0 <3.0.0"

Expand Down
1 change: 1 addition & 0 deletions web_sdk/web_test_utils/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: web_test_utils

# Keep the SDK version range in sync with lib/web_ui/pubspec.yaml
environment:
sdk: ">=2.12.0-0 <3.0.0"

Expand Down

0 comments on commit d19fb6e

Please sign in to comment.