de-duplicate code in analyze.dart (#151279)

This is just a de-duplication PR that should be a no-op.
This commit is contained in:
Christopher Fujino
2024-07-05 13:31:20 -07:00
committed by GitHub
parent 1ebdbeef6c
commit 55520b842c
2 changed files with 10 additions and 38 deletions

View File

@@ -184,8 +184,8 @@ Future<void> run(List<String> arguments) async {
final String ruleNames = rules.map((AnalyzeRule rule) => '\n * $rule').join();
printProgress('Analyzing code in the framework with the following rules:$ruleNames');
await analyzeWithRules(flutterRoot, rules,
includePaths: <String>['packages/flutter/lib'],
excludePaths: <String>['packages/flutter/lib/fix_data'],
includePaths: const <String>['packages/flutter/lib'],
excludePaths: const <String>['packages/flutter/lib/fix_data'],
);
final List<AnalyzeRule> testRules = <AnalyzeRule>[noStopwatches];
final String testRuleNames = testRules.map((AnalyzeRule rule) => '\n * $rule').join();
@@ -196,7 +196,14 @@ Future<void> run(List<String> arguments) async {
final List<AnalyzeRule> toolRules = <AnalyzeRule>[AvoidFutureCatchError()];
final String toolRuleNames = toolRules.map((AnalyzeRule rule) => '\n * $rule').join();
printProgress('Analyzing code in the tool with the following rules:$toolRuleNames');
await analyzeToolWithRules(flutterRoot, toolRules);
await analyzeWithRules(
flutterRoot,
toolRules,
includePaths: const <String>[
'packages/flutter_tools/lib',
'packages/flutter_tools/test',
],
);
} else {
printProgress('Skipped performing further analysis in the framework because "flutter analyze" finished with a non-zero exit code.');
}

View File

@@ -64,41 +64,6 @@ Future<void> analyzeWithRules(String flutterRootDirectory, List<AnalyzeRule> rul
}
}
Future<void> analyzeToolWithRules(String flutterRootDirectory, List<AnalyzeRule> rules) async {
final String libPath = path.canonicalize('$flutterRootDirectory/packages/flutter_tools/lib');
if (!Directory(libPath).existsSync()) {
foundError(<String>['Analyzer error: the specified $libPath does not exist.']);
}
final String testPath = path.canonicalize('$flutterRootDirectory/packages/flutter_tools/test');
final AnalysisContextCollection collection = AnalysisContextCollection(
includedPaths: <String>[libPath, testPath],
);
final List<String> analyzerErrors = <String>[];
for (final AnalysisContext context in collection.contexts) {
final Iterable<String> analyzedFilePaths = context.contextRoot.analyzedFiles();
final AnalysisSession session = context.currentSession;
for (final String filePath in analyzedFilePaths) {
final SomeResolvedUnitResult unit = await session.getResolvedUnit(filePath);
if (unit is ResolvedUnitResult) {
for (final AnalyzeRule rule in rules) {
rule.applyTo(unit);
}
} else {
analyzerErrors.add('Analyzer error: file $unit could not be resolved. Expected "ResolvedUnitResult", got ${unit.runtimeType}.');
}
}
}
if (analyzerErrors.isNotEmpty) {
foundError(analyzerErrors);
}
for (final AnalyzeRule verifier in rules) {
verifier.reportViolations(flutterRootDirectory);
}
}
/// An interface that defines a set of best practices, and collects information
/// about code that violates the best practices in a [ResolvedUnitResult].
///