mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Make const_finder work with implemented and extended classes (#20614)
This commit is contained in:
parent
bf6d7b4e48
commit
3dd1151e3b
@ -13,11 +13,11 @@ class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
this.classLibraryUri,
|
||||
this.className,
|
||||
) : assert(kernelFilePath != null),
|
||||
assert(classLibraryUri != null),
|
||||
assert(className != null),
|
||||
_visitedInstances = <String>{},
|
||||
constantInstances = <Map<String, dynamic>>[],
|
||||
nonConstantLocations = <Map<String, dynamic>>[];
|
||||
assert(classLibraryUri != null),
|
||||
assert(className != null),
|
||||
_visitedInstances = <String>{},
|
||||
constantInstances = <Map<String, dynamic>>[],
|
||||
nonConstantLocations = <Map<String, dynamic>>[];
|
||||
|
||||
/// The path to the file to open.
|
||||
final String kernelFilePath;
|
||||
@ -32,9 +32,19 @@ class _ConstVisitor extends RecursiveVisitor<void> {
|
||||
final List<Map<String, dynamic>> constantInstances;
|
||||
final List<Map<String, dynamic>> nonConstantLocations;
|
||||
|
||||
// A cache of previously evaluated classes.
|
||||
static Map<Class, bool> _classHeirarchyCache = <Class, bool>{};
|
||||
bool _matches(Class node) {
|
||||
return node.enclosingLibrary.importUri.toString() == classLibraryUri &&
|
||||
node.name == className;
|
||||
assert(node != null);
|
||||
final bool result = _classHeirarchyCache[node];
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
final bool exactMatch = node.name == className
|
||||
&& node.enclosingLibrary.importUri.toString() == classLibraryUri;
|
||||
_classHeirarchyCache[node] = exactMatch
|
||||
|| node.supers.any((Supertype supertype) => _matches(supertype.classNode));
|
||||
return _classHeirarchyCache[node];
|
||||
}
|
||||
|
||||
// Avoid visiting the same constant more than once.
|
||||
|
||||
@ -71,11 +71,28 @@ void _checkConsts() {
|
||||
<String, dynamic>{'stringValue': '10', 'intValue': 10, 'targetValue': null},
|
||||
<String, dynamic>{'stringValue': '9', 'intValue': 9},
|
||||
<String, dynamic>{'stringValue': '7', 'intValue': 7, 'targetValue': null},
|
||||
<String, dynamic>{'stringValue': '11', 'intValue': 11, 'targetValue': null},
|
||||
<String, dynamic>{'stringValue': '12', 'intValue': 12, 'targetValue': null},
|
||||
<String, dynamic>{'stringValue': 'package', 'intValue':-1, 'targetValue': null},
|
||||
],
|
||||
'nonConstantLocations': <dynamic>[],
|
||||
}),
|
||||
);
|
||||
|
||||
final ConstFinder finder2 = ConstFinder(
|
||||
kernelFilePath: constsDill,
|
||||
classLibraryUri: 'package:const_finder_fixtures/target.dart',
|
||||
className: 'MixedInTarget',
|
||||
);
|
||||
expect<String>(
|
||||
jsonEncode(finder2.findInstances()),
|
||||
jsonEncode(<String, dynamic>{
|
||||
'constantInstances': <Map<String, dynamic>>[
|
||||
<String, dynamic>{'val': '13'},
|
||||
],
|
||||
'nonConstantLocations': <dynamic>[],
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
void _checkNonConsts() {
|
||||
|
||||
@ -31,6 +31,14 @@ void main() {
|
||||
|
||||
final StaticConstInitializer staticConstMap = StaticConstInitializer();
|
||||
staticConstMap.useOne(1);
|
||||
|
||||
const ExtendsTarget extendsTarget = ExtendsTarget('11', 11, null);
|
||||
extendsTarget.hit();
|
||||
const ImplementsTarget implementsTarget = ImplementsTarget('12', 12, null);
|
||||
implementsTarget.hit();
|
||||
|
||||
const MixedInTarget mixedInTraget = MixedInTarget('13');
|
||||
mixedInTraget.hit();
|
||||
}
|
||||
|
||||
class IgnoreMe {
|
||||
|
||||
36
tools/const_finder/test/fixtures/lib/target.dart
vendored
36
tools/const_finder/test/fixtures/lib/target.dart
vendored
@ -13,3 +13,39 @@ class Target {
|
||||
print('$stringValue $intValue');
|
||||
}
|
||||
}
|
||||
|
||||
class ExtendsTarget extends Target {
|
||||
const ExtendsTarget(String stringValue, int intValue, Target targetValue)
|
||||
: super(stringValue, intValue, targetValue);
|
||||
}
|
||||
|
||||
class ImplementsTarget implements Target {
|
||||
const ImplementsTarget(this.stringValue, this.intValue, this.targetValue);
|
||||
|
||||
@override
|
||||
final String stringValue;
|
||||
@override
|
||||
final int intValue;
|
||||
@override
|
||||
final Target targetValue;
|
||||
|
||||
@override
|
||||
void hit() {
|
||||
print('ImplementsTarget - $stringValue $intValue');
|
||||
}
|
||||
}
|
||||
|
||||
mixin MixableTarget {
|
||||
String get val;
|
||||
|
||||
void hit() {
|
||||
print(val);
|
||||
}
|
||||
}
|
||||
|
||||
class MixedInTarget with MixableTarget {
|
||||
const MixedInTarget(this.val);
|
||||
|
||||
@override
|
||||
final String val;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user