Files
sdk/pkg/analyzer_fe_comparison/lib/comparison.dart
T
Paul Berry 0262530e1f Clarify code that checks for analyzer/FE differences.
We take a slight performance hit when differences are actually found
(since we now check for differences first, and then compute the diff
afterwards if differences were found), but this shouldn't be a big
problem since most of the time there are no differences.

Change-Id: I62957bc7ea3ef2e87afdf7ea175ea5e4e75fc1aa
Reviewed-on: https://dart-review.googlesource.com/76703
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2018-09-26 17:05:25 +00:00

70 lines
2.7 KiB
Dart

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer_fe_comparison/src/analyzer.dart' as analyzer;
import 'package:analyzer_fe_comparison/src/comparison_node.dart';
import 'package:analyzer_fe_comparison/src/kernel.dart' as kernel;
import 'package:path/path.dart' as path;
/// Compares the analyzer and kernel representations of a package, and prints
/// the resulting diff.
void comparePackages(
String platformPath, String projectLibPath, String packagesFilePath) async {
ComparisonNode analyzerNode = await analyzer.analyzePackage(projectLibPath);
var packagesFileUri = Uri.file(packagesFilePath);
var inputs = <Uri>[];
for (var library in analyzerNode.children) {
inputs.add(Uri.parse(library.text));
}
var platformUri = Uri.file(platformPath);
ComparisonNode kernelNode =
await kernel.analyzePackage(inputs, packagesFileUri, platformUri);
print(ComparisonNode.diff(kernelNode, analyzerNode));
}
/// Compares the analyzer and kernel representations of a test file, and prints
/// the resulting diff.
///
/// Only libraries reached by a "file:" URI are compared.
void compareTestPrograms(
String sourcePath, String platformPath, String packagesFilePath) async {
var packagesFileUri = Uri.file(packagesFilePath);
var platformUri = Uri.file(platformPath);
ComparisonNode kernelNode = await kernel.analyzeProgram(
path.toUri(sourcePath),
packagesFileUri,
platformUri,
(uri) => uri.scheme == 'file');
if (kernelNode.text == 'Error occurred') {
// TODO(paulberry): really we should verify that the analyzer detects an
// error as well. But that's not easy to do right now because we use the
// front end to chase imports so that we know which files to pass to the
// analyzer, and we can't rely on the front end import chasing when an error
// occurred.
print('No differences found (skipped due to front end compilation error)');
return;
}
String startingPath;
var inputs = <String>[];
for (var library in kernelNode.children) {
var filePath = path.fromUri(Uri.parse(library.text));
if (startingPath == null) {
startingPath = path.dirname(filePath);
} else {
while (!path.isWithin(startingPath, filePath)) {
startingPath = path.dirname(startingPath);
}
}
inputs.add(filePath);
}
ComparisonNode analyzerNode =
await analyzer.analyzeFiles(startingPath, inputs);
if (kernelNode == analyzerNode) {
print('No differences found!');
} else {
print('Differences found:');
print(ComparisonNode.diff(kernelNode, analyzerNode));
}
}