Cleanup toString transformer (flutter/engine#26298)
This change removes uses of toString transformer from Flutter engine. The transformer is already wired up in Dart SDK: front-end server from Dart SDK supports --delete-tostring-package-uri option. After this change only the integration test is left in the Flutter engine to make sure dart:ui is properly transformed. dart-lang/sdk#46022
This commit is contained in:
@@ -15,91 +15,10 @@ import 'package:frontend_server/frontend_server.dart' as frontend
|
||||
CompilerInterface,
|
||||
listenAndCompile,
|
||||
argParser,
|
||||
usage,
|
||||
ProgramTransformer,
|
||||
ToStringTransformer;
|
||||
usage;
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:vm/incremental_compiler.dart';
|
||||
|
||||
/// Wrapper around [FrontendCompiler] that adds [widgetCreatorTracker] kernel
|
||||
/// transformation to the compilation.
|
||||
class _FlutterFrontendCompiler implements frontend.CompilerInterface {
|
||||
final frontend.CompilerInterface _compiler;
|
||||
|
||||
_FlutterFrontendCompiler(StringSink output,
|
||||
{bool unsafePackageSerialization,
|
||||
bool useDebuggerModuleNames,
|
||||
bool emitDebugMetadata,
|
||||
frontend.ProgramTransformer transformer})
|
||||
: _compiler = frontend.FrontendCompiler(output,
|
||||
transformer: transformer,
|
||||
useDebuggerModuleNames: useDebuggerModuleNames,
|
||||
emitDebugMetadata: emitDebugMetadata,
|
||||
unsafePackageSerialization: unsafePackageSerialization);
|
||||
|
||||
@override
|
||||
Future<bool> compile(String filename, ArgResults options,
|
||||
{IncrementalCompiler generator}) async {
|
||||
return _compiler.compile(filename, options, generator: generator);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Null> recompileDelta({String entryPoint}) async { // ignore: prefer_void_to_null
|
||||
return _compiler.recompileDelta(entryPoint: entryPoint);
|
||||
}
|
||||
|
||||
@override
|
||||
void acceptLastDelta() {
|
||||
_compiler.acceptLastDelta();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> rejectLastDelta() async {
|
||||
return _compiler.rejectLastDelta();
|
||||
}
|
||||
|
||||
@override
|
||||
void invalidate(Uri uri) {
|
||||
_compiler.invalidate(uri);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Null> compileExpression( // ignore: prefer_void_to_null
|
||||
String expression,
|
||||
List<String> definitions,
|
||||
List<String> typeDefinitions,
|
||||
String libraryUri,
|
||||
String klass,
|
||||
bool isStatic) {
|
||||
return _compiler.compileExpression(
|
||||
expression, definitions, typeDefinitions, libraryUri, klass, isStatic);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Null> compileExpressionToJs( // ignore: prefer_void_to_null
|
||||
String libraryUri,
|
||||
int line,
|
||||
int column,
|
||||
Map<String, String> jsModules,
|
||||
Map<String, String> jsFrameValues,
|
||||
String moduleName,
|
||||
String expression) {
|
||||
return _compiler.compileExpressionToJs(libraryUri, line, column, jsModules,
|
||||
jsFrameValues, moduleName, expression);
|
||||
}
|
||||
|
||||
@override
|
||||
void reportError(String msg) {
|
||||
_compiler.reportError(msg);
|
||||
}
|
||||
|
||||
@override
|
||||
void resetIncrementalCompiler() {
|
||||
_compiler.resetIncrementalCompiler();
|
||||
}
|
||||
}
|
||||
|
||||
/// Entry point for this module, that creates `_FrontendCompiler` instance and
|
||||
/// Entry point for this module, that creates `FrontendCompiler` instance and
|
||||
/// processes user input.
|
||||
/// `compiler` is an optional parameter so it can be replaced with mocked
|
||||
/// version for testing.
|
||||
@@ -108,24 +27,8 @@ Future<int> starter(
|
||||
frontend.CompilerInterface compiler,
|
||||
Stream<List<int>> input,
|
||||
StringSink output,
|
||||
frontend.ProgramTransformer transformer,
|
||||
}) async {
|
||||
ArgResults options;
|
||||
// TODO(alexmarkov): Remove handling of --delete-tostring-package-uri option
|
||||
// after it is added to Dart SDK,
|
||||
// https://github.com/dart-lang/sdk/issues/46022.
|
||||
final bool handleDeleteToString =
|
||||
!frontend.argParser.options.containsKey('delete-tostring-package-uri');
|
||||
if (handleDeleteToString) {
|
||||
frontend.argParser.addMultiOption(
|
||||
'delete-tostring-package-uri',
|
||||
help:
|
||||
'Replaces implementations of `toString` with `super.toString()` for '
|
||||
'specified package',
|
||||
valueHelp: 'dart:ui',
|
||||
defaultsTo: const <String>[],
|
||||
);
|
||||
}
|
||||
try {
|
||||
options = frontend.argParser.parse(args);
|
||||
} catch (error) {
|
||||
@@ -134,9 +37,6 @@ Future<int> starter(
|
||||
return 1;
|
||||
}
|
||||
|
||||
final Set<String> deleteToStringPackageUris =
|
||||
(options['delete-tostring-package-uri'] as List<String>).toSet();
|
||||
|
||||
if (options['train'] as bool) {
|
||||
if (!options.rest.isNotEmpty) {
|
||||
throw Exception('Must specify input.dart');
|
||||
@@ -157,12 +57,7 @@ Future<int> starter(
|
||||
'--track-widget-creation',
|
||||
'--enable-asserts',
|
||||
]);
|
||||
compiler ??= _FlutterFrontendCompiler(
|
||||
output,
|
||||
transformer: handleDeleteToString
|
||||
? frontend.ToStringTransformer(null, deleteToStringPackageUris)
|
||||
: null,
|
||||
);
|
||||
compiler ??= frontend.FrontendCompiler(output);
|
||||
|
||||
await compiler.compile(input, options);
|
||||
compiler.acceptLastDelta();
|
||||
@@ -180,10 +75,7 @@ Future<int> starter(
|
||||
}
|
||||
}
|
||||
|
||||
compiler ??= _FlutterFrontendCompiler(output,
|
||||
transformer: handleDeleteToString
|
||||
? frontend.ToStringTransformer(transformer, deleteToStringPackageUris)
|
||||
: null,
|
||||
compiler ??= frontend.FrontendCompiler(output,
|
||||
useDebuggerModuleNames: options['debugger-module-names'] as bool,
|
||||
emitDebugMetadata: options['experimental-emit-debug-metadata'] as bool,
|
||||
unsafePackageSerialization:
|
||||
|
||||
@@ -4,297 +4,17 @@
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:frontend_server/frontend_server.dart' as frontend
|
||||
show ProgramTransformer, ToStringTransformer, ToStringVisitor;
|
||||
import 'package:kernel/kernel.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
if (args.length != 2) {
|
||||
stderr.writeln('The first argument must be the path to the forntend server dill.');
|
||||
stderr.writeln('The first argument must be the path to the frontend server dill.');
|
||||
stderr.writeln('The second argument must be the path to the flutter_patched_sdk');
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
const Set<String> uiAndFlutter = <String>{
|
||||
'dart:ui',
|
||||
'package:flutter',
|
||||
};
|
||||
|
||||
test('No packages', () {
|
||||
final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(null, <String>{});
|
||||
final FakeComponent component = FakeComponent();
|
||||
transformer.transform(component);
|
||||
|
||||
expect(
|
||||
!component.visitChildrenCalled,
|
||||
'Expected component.visitChildrenCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('dart:ui package', () {
|
||||
final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(null, uiAndFlutter);
|
||||
final FakeComponent component = FakeComponent();
|
||||
transformer.transform(component);
|
||||
|
||||
expect(
|
||||
component.visitChildrenCalled,
|
||||
'Expected component.visitChildrenCalled to be true',
|
||||
);
|
||||
});
|
||||
|
||||
test('Child transformer', () {
|
||||
final FakeTransformer childTransformer = FakeTransformer();
|
||||
final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(childTransformer, <String>{});
|
||||
final FakeComponent component = FakeComponent();
|
||||
transformer.transform(component);
|
||||
|
||||
expect(
|
||||
!component.visitChildrenCalled,
|
||||
'Expected component.visitChildrenCalled to be false',
|
||||
);
|
||||
expect(
|
||||
childTransformer.transformCalled,
|
||||
'Expected childTransformer.transformCalled to be true',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores non-toString procedures', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('name'),
|
||||
annotations: const <Expression>[],
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!procedure.enclosingLibraryCalled,
|
||||
'Expected procedure.enclosingLibraryCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores top level toString', () {
|
||||
// i.e. a `toString` method specified at the top of a library, like:
|
||||
//
|
||||
// void main() {}
|
||||
// String toString() => 'why?';
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('package:some_package/src/blah.dart');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('tostring'),
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores abstract toString', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final Uri uri = Uri.parse('package:some_package/src/blah.dart');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('toString'),
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: true,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores static toString', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('package:some_package/src/blah.dart');
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('toString'),
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: false,
|
||||
isStatic: true,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled jto be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores enum toString', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('package:some_package/src/blah.dart');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('toString'),
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri)..isEnum = true,
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores non-specified libraries', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('package:some_package/src/blah.dart');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: Name('toString'),
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
test('ToStringVisitor ignores @keepToString', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('dart:ui');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final Name name = Name('toString');
|
||||
final Class annotation = Class(name: '_KeepToString', fileUri: uri)..parent = library;
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: name,
|
||||
function: function,
|
||||
annotations: <Expression>[ConstantExpression(
|
||||
InstanceConstant(
|
||||
Reference()..node = annotation,
|
||||
<DartType>[],
|
||||
<Reference, Constant>{},
|
||||
),
|
||||
)],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
expect(
|
||||
!statement.replaceWithCalled,
|
||||
'Expected statement.replaceWithCalled to be false',
|
||||
);
|
||||
});
|
||||
|
||||
void _validateReplacement(FakeStatement body) {
|
||||
final ReturnStatement replacement = body.replacement as ReturnStatement;
|
||||
expect(
|
||||
replacement.expression is SuperMethodInvocation,
|
||||
'Expected replacement.expression to be a SuperMethodInvocation',
|
||||
);
|
||||
final SuperMethodInvocation superMethodInvocation = replacement.expression as SuperMethodInvocation;
|
||||
expect(
|
||||
superMethodInvocation.name.text == 'toString',
|
||||
'Expected superMethodInvocation.name.text to be "toString"',
|
||||
);
|
||||
}
|
||||
|
||||
test('ToStringVisitor replaces toString in specified libraries (dart:ui)', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('dart:ui');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final Name name = Name('toString');
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: name,
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
_validateReplacement(statement);
|
||||
});
|
||||
|
||||
test('ToStringVisitor replaces toString in specified libraries (package:flutter)', () {
|
||||
final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter);
|
||||
final Uri uri = Uri.parse('package:flutter/src/foundation.dart');
|
||||
final Library library = Library(uri, fileUri: uri);
|
||||
final Name name = Name('toString');
|
||||
final FakeStatement statement = FakeStatement();
|
||||
final FakeFunctionNode function = FakeFunctionNode(
|
||||
body: statement,
|
||||
);
|
||||
final FakeProcedure procedure = FakeProcedure(
|
||||
name: name,
|
||||
function: function,
|
||||
annotations: const <Expression>[],
|
||||
enclosingLibrary: library,
|
||||
enclosingClass: Class(name: 'foo', fileUri: uri),
|
||||
isAbstract: false,
|
||||
isStatic: false,
|
||||
);
|
||||
visitor.visitProcedure(procedure);
|
||||
|
||||
_validateReplacement(statement);
|
||||
});
|
||||
|
||||
group('Integration tests', () {
|
||||
final String dart = Platform.resolvedExecutable;
|
||||
final String frontendServer = args[0];
|
||||
@@ -376,89 +96,3 @@ Future<void> main(List<String> args) async {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Fake {
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) {
|
||||
throw UnimplementedError(invocation.memberName.toString().split('"')[1]);
|
||||
}
|
||||
}
|
||||
|
||||
class FakeComponent extends Fake implements Component {
|
||||
bool visitChildrenCalled = false;
|
||||
|
||||
@override
|
||||
void visitChildren(Visitor<void> v) {
|
||||
visitChildrenCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeTransformer extends Fake implements frontend.ProgramTransformer {
|
||||
bool transformCalled = false;
|
||||
|
||||
@override
|
||||
void transform(Component component) {
|
||||
transformCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeProcedure extends Fake implements Procedure {
|
||||
FakeProcedure({
|
||||
this.name,
|
||||
this.function,
|
||||
this.annotations,
|
||||
Library enclosingLibrary,
|
||||
this.enclosingClass,
|
||||
this.isAbstract,
|
||||
this.isStatic,
|
||||
}) : _enclosingLibrary = enclosingLibrary;
|
||||
|
||||
@override
|
||||
final Name name;
|
||||
|
||||
@override
|
||||
final FunctionNode function;
|
||||
|
||||
@override
|
||||
final List<Expression> annotations;
|
||||
|
||||
@override
|
||||
final bool isAbstract;
|
||||
|
||||
@override
|
||||
final bool isStatic;
|
||||
|
||||
@override
|
||||
final Class enclosingClass;
|
||||
|
||||
final Library _enclosingLibrary;
|
||||
|
||||
bool enclosingLibraryCalled = false;
|
||||
|
||||
@override
|
||||
Library get enclosingLibrary {
|
||||
enclosingLibraryCalled = true;
|
||||
return _enclosingLibrary;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeFunctionNode extends Fake implements FunctionNode {
|
||||
FakeFunctionNode({
|
||||
this.body,
|
||||
});
|
||||
|
||||
@override
|
||||
final Statement body;
|
||||
}
|
||||
|
||||
class FakeStatement extends Fake implements Statement {
|
||||
bool replaceWithCalled = false;
|
||||
|
||||
TreeNode replacement;
|
||||
|
||||
@override
|
||||
void replaceWith(TreeNode r) {
|
||||
replaceWithCalled = true;
|
||||
replacement = r;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,4 @@ part of dart.ui;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
const _KeepToString keepToString = _KeepToString();
|
||||
|
||||
class _KeepToString {
|
||||
const _KeepToString();
|
||||
}
|
||||
const pragma keepToString = pragma('flutter:keep-to-string');
|
||||
|
||||
Reference in New Issue
Block a user