Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Prev Previous commit
Next Next commit
add test
  • Loading branch information
christopherfujino committed Nov 11, 2022
commit 779e03a9e59d5bba5153581f472db4acbd6288aa
40 changes: 25 additions & 15 deletions tools/const_finder/lib/const_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class _ConstVisitor extends RecursiveVisitor<void> {
this.kernelFilePath,
this.classLibraryUri,
this.className,
this.ignoredClasses,
) : _visitedInstances = <String>{},
constantInstances = <Map<String, dynamic>>[],
skippedConstantInstances = <Map<String, dynamic>>[],
nonConstantLocations = <Map<String, dynamic>>[];

/// The path to the file to open.
Expand All @@ -25,12 +25,15 @@ class _ConstVisitor extends RecursiveVisitor<void> {
/// The name of the class to find.
final String className;

/// A list of two-element tuples corresponding to library URI and class name
/// that should be ignored.
final List<List<String>> ignoredClasses;

final Set<String> _visitedInstances;
final List<Map<String, dynamic>> constantInstances;
final List<Map<String, dynamic>> skippedConstantInstances;
final List<Map<String, dynamic>> nonConstantLocations;

bool inIconClass = false;
bool inIgnoredClass = false;

// A cache of previously evaluated classes.
static final Map<Class, bool> _classHeirarchyCache = <Class, bool>{};
Expand Down Expand Up @@ -78,13 +81,19 @@ class _ConstVisitor extends RecursiveVisitor<void> {

@override
void visitClass(Class node) {
if (node.name == 'Icons' && node.enclosingLibrary.importUri.toString() == 'package:flutter/src/material/icons.dart') {
inIconClass = true;
super.visitClass(node);
inIconClass = false;
} else {
super.visitClass(node);
// check if this is a class that we should ignore
for (final List<String> tuple in ignoredClasses) {
final String libraryUri = tuple[0];
final String className = tuple[1];
if (node.name == className && node.enclosingLibrary.importUri.toString() == libraryUri) {
inIgnoredClass = true;
super.visitClass(node);
inIgnoredClass = false;
return;
}
}
// not an ignored class
super.visitClass(node);
}

@override
Expand All @@ -102,10 +111,11 @@ class _ConstVisitor extends RecursiveVisitor<void> {
final PrimitiveConstant<dynamic> value = kvp.value as PrimitiveConstant<dynamic>;
instance[kvp.key.asField.name.text] = value.value;
}
if (_visitedInstances.add(instance.toString())) {
if (inIconClass) {
skippedConstantInstances.add(instance);
} else {
if (!inIgnoredClass) {
if (_visitedInstances.add(instance.toString())) {
if (instance['stringValue'] == 'unused1') {
throw 'whoops';
}
constantInstances.add(instance);
}
}
Expand All @@ -125,10 +135,12 @@ class ConstFinder {
required String kernelFilePath,
required String classLibraryUri,
required String className,
List<List<String>> ignoredClasses = const <List<String>>[],
}) : _visitor = _ConstVisitor(
kernelFilePath,
classLibraryUri,
className,
ignoredClasses,
);

final _ConstVisitor _visitor;
Expand All @@ -137,11 +149,9 @@ class ConstFinder {
Map<String, dynamic> findInstances() {
_visitor._visitedInstances.clear();
for (final Library library in loadComponentFromBinary(_visitor.kernelFilePath).libraries) {
//io.stderr.writeln('loadComponentFromBinary($library)');
lastLibrary = library;
library.visitChildren(_visitor);
}
//io.stderr.flush();
return <String, dynamic>{
'constantInstances': _visitor.constantInstances,
'nonConstantLocations': _visitor.nonConstantLocations,
Expand Down
2 changes: 1 addition & 1 deletion tools/const_finder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: const_finder
publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"

# Do not add any dependencies that require more than what is provided in
# //third_party/dart/pkg or //third_party/dart/third_party/pkg.
Expand Down
Loading