Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

de-duplicate code in analyze.dart #151279

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions dev/bots/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.');
}
Expand Down
35 changes: 0 additions & 35 deletions dev/bots/custom_rules/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
///
Expand Down