Skip to content

Commit a9bb706

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: allow the client to specify which files to analyze/migrate.
Change-Id: Ie49f003640342a8c0d26b4f509a9268200955dda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151302 Reviewed-by: Janice Collins <[email protected]>
1 parent 651464c commit a9bb706

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

pkg/nnbd_migration/lib/migration_cli.dart

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,22 @@ class MigrationCliRunner {
498498
return stream.first;
499499
}
500500

501+
/// Computes the set of file paths that should be analyzed by the migration
502+
/// engine. May be overridden by a derived class.
503+
///
504+
/// All files to be migrated must be included in the returned set. It is
505+
/// permissible for the set to contain additional files that could help the
506+
/// migration tool build up a more complete nullability graph (for example
507+
/// generated files, or usages of the code-to-be-migrated by one one of its
508+
/// clients).
509+
///
510+
/// By default returns the set of all `.dart` files contained in the context.
511+
Set<String> computePathsToProcess(DriverBasedAnalysisContext context) =>
512+
context.contextRoot
513+
.analyzedFiles()
514+
.where((s) => s.endsWith('.dart'))
515+
.toSet();
516+
501517
NonNullableFix createNonNullableFix(DartFixListener listener,
502518
ResourceProvider resourceProvider, LineInfo getLineInfo(String path),
503519
{List<String> included = const <String>[],
@@ -609,6 +625,22 @@ Use this interactive web view to review, improve, or apply the results.
609625
}
610626
}
611627

628+
/// Determines whether a migrated version of the file at [path] should be
629+
/// output by the migration too. May be overridden by a derived class.
630+
///
631+
/// This method should return `false` for files that are being considered by
632+
/// the migration tool for information only (for example generated files, or
633+
/// usages of the code-to-be-migrated by one one of its clients).
634+
///
635+
/// By default returns `true` if the file is contained within the context
636+
/// root. This means that if a client overrides [computePathsToProcess] to
637+
/// return additional paths that aren't inside the user's project, but doesn't
638+
/// override this method, then those additional paths will be analyzed but not
639+
/// migrated.
640+
bool shouldBeMigrated(DriverBasedAnalysisContext context, String path) {
641+
return context.contextRoot.isAnalyzed(path);
642+
}
643+
612644
/// Perform the indicated source edits to the given source, returning the
613645
/// resulting transformed text.
614646
String _applyEdits(SourceFileEdit sourceFileEdit, String source) {
@@ -867,7 +899,7 @@ class _FixCodeProcessor extends Object {
867899
NonNullableFix nonNullableFixTask;
868900

869901
_FixCodeProcessor(this.context, this._migrationCli)
870-
: pathsToProcess = _computePathsToProcess(context);
902+
: pathsToProcess = _migrationCli.computePathsToProcess(context);
871903

872904
bool get isPreviewServerRunnning =>
873905
nonNullableFixTask?.isPreviewServerRunning ?? false;
@@ -877,7 +909,7 @@ class _FixCodeProcessor extends Object {
877909

878910
void prepareToRerun() {
879911
var driver = context.driver;
880-
pathsToProcess = _computePathsToProcess(context);
912+
pathsToProcess = _migrationCli.computePathsToProcess(context);
881913
pathsToProcess.forEach(driver.changeFile);
882914
}
883915

@@ -898,8 +930,7 @@ class _FixCodeProcessor extends Object {
898930
var result = await driver.getResolvedLibrary(path);
899931
if (result != null) {
900932
for (var unit in result.units) {
901-
if (pathsToProcess.contains(unit.path) &&
902-
!pathsProcessed.contains(unit.path)) {
933+
if (!pathsProcessed.contains(unit.path)) {
903934
await process(unit);
904935
pathsProcessed.add(unit.path);
905936
}
@@ -960,7 +991,9 @@ class _FixCodeProcessor extends Object {
960991
});
961992
await processResources((ResolvedUnitResult result) async {
962993
_progressBar.tick();
963-
await _task.finalizeUnit(result);
994+
if (_migrationCli.shouldBeMigrated(context, result.path)) {
995+
await _task.finalizeUnit(result);
996+
}
964997
});
965998
var state = await _task.finish();
966999
if (_migrationCli.options.webPreview) {
@@ -970,13 +1003,6 @@ class _FixCodeProcessor extends Object {
9701003

9711004
return nonNullableFixTask.previewUrls;
9721005
}
973-
974-
static Set<String> _computePathsToProcess(
975-
DriverBasedAnalysisContext context) =>
976-
context.contextRoot
977-
.analyzedFiles()
978-
.where((s) => s.endsWith('.dart'))
979-
.toSet();
9801006
}
9811007

9821008
/// Given a Logger and an analysis issue, render the issue to the logger.

pkg/nnbd_migration/test/migration_cli_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/file_system/file_system.dart' show ResourceProvider;
1111
import 'package:analyzer/file_system/memory_file_system.dart';
1212
import 'package:analyzer/file_system/physical_file_system.dart';
1313
import 'package:analyzer/source/line_info.dart';
14+
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1415
import 'package:analyzer/src/test_utilities/mock_sdk.dart' as mock_sdk;
1516
import 'package:args/args.dart';
1617
import 'package:cli_util/cli_logging.dart';
@@ -112,6 +113,10 @@ class _MigrationCliRunner extends MigrationCliRunner {
112113
_runWhilePreviewServerActive = null;
113114
}
114115

116+
@override
117+
Set<String> computePathsToProcess(DriverBasedAnalysisContext context) =>
118+
cli._test.overridePathsToProcess ?? super.computePathsToProcess(context);
119+
115120
@override
116121
NonNullableFix createNonNullableFix(DartFixListener listener,
117122
ResourceProvider resourceProvider, LineInfo getLineInfo(String path),
@@ -139,6 +144,11 @@ class _MigrationCliRunner extends MigrationCliRunner {
139144
fail('Preview server never started');
140145
}
141146
}
147+
148+
@override
149+
bool shouldBeMigrated(DriverBasedAnalysisContext context, String path) =>
150+
cli._test.overrideShouldBeMigrated?.call(path) ??
151+
super.shouldBeMigrated(context, path);
142152
}
143153

144154
abstract class _MigrationCliTestBase {
@@ -148,6 +158,12 @@ abstract class _MigrationCliTestBase {
148158
/// encounters a reference to the `print` function.
149159
bool injectArtificialException = false;
150160

161+
/// If non-null, this is injected as the return value for
162+
/// [_MigrationCliRunner.computePathsToProcess].
163+
Set<String> overridePathsToProcess;
164+
165+
bool Function(String) overrideShouldBeMigrated;
166+
151167
void set logger(_TestLogger logger);
152168

153169
_MockProcessManager get processManager;
@@ -690,6 +706,52 @@ int? f() => null
690706
assertProjectContents(projectDir, projectContents);
691707
}
692708

709+
test_lifecycle_override_paths() async {
710+
Map<String, String> makeProject({bool migrated = false}) {
711+
var projectContents = simpleProject(migrated: migrated);
712+
projectContents['lib/test.dart'] = '''
713+
import 'skip.dart';
714+
import 'analyze_but_do_not_migrate.dart';
715+
void f(int x) {}
716+
void g(int${migrated ? '?' : ''} x) {}
717+
void h(int${migrated ? '?' : ''} x) {}
718+
void call_h() => h(null);
719+
''';
720+
projectContents['lib/skip.dart'] = '''
721+
import 'test.dart';
722+
void call_f() => f(null);
723+
''';
724+
projectContents['lib/analyze_but_do_not_migrate.dart'] = '''
725+
import 'test.dart';
726+
void call_g() => g(null);
727+
''';
728+
return projectContents;
729+
}
730+
731+
var projectContents = makeProject();
732+
var projectDir = await createProjectDir(projectContents);
733+
var testPath =
734+
resourceProvider.pathContext.join(projectDir, 'lib', 'test.dart');
735+
var analyzeButDoNotMigratePath = resourceProvider.pathContext
736+
.join(projectDir, 'lib', 'analyze_but_do_not_migrate.dart');
737+
overridePathsToProcess = {testPath, analyzeButDoNotMigratePath};
738+
overrideShouldBeMigrated = (path) => path == testPath;
739+
var cliRunner = _createCli().decodeCommandLineArgs(
740+
_parseArgs(['--no-web-preview', '--apply-changes', projectDir]));
741+
await cliRunner.run();
742+
assertNormalExit(cliRunner);
743+
// Check that a summary was printed
744+
expect(logger.stdoutBuffer.toString(), contains('Applying changes'));
745+
// And that it refers to test.dart and pubspec.yaml
746+
expect(logger.stdoutBuffer.toString(), contains('test.dart'));
747+
expect(logger.stdoutBuffer.toString(), contains('pubspec.yaml'));
748+
// And that it does not tell the user they can rerun with `--apply-changes`
749+
expect(logger.stdoutBuffer.toString(), isNot(contains('--apply-changes')));
750+
// Changes should have been made only to test.dart, and only accounting for
751+
// the calls coming from analyze_but_do_not_migrate.dart and test.dart
752+
assertProjectContents(projectDir, makeProject(migrated: true));
753+
}
754+
693755
test_lifecycle_preview() async {
694756
var projectContents = simpleProject();
695757
var projectDir = await createProjectDir(projectContents);

0 commit comments

Comments
 (0)