未验证 提交 3dd1151e 编写于 作者: D Dan Field 提交者: GitHub

Make const_finder work with implemented and extended classes (#20614)

上级 bf6d7b4e
......@@ -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 {
......
......@@ -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;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册