Skip to content

Commit 2fbe5c1

Browse files
committed
Migration: Make the three migration phases explicit in NonNullableFix API.
Previously the API was general, exposing a `numPhases` integer, and providing a generic processing function with a `phase` argument. This was necessary back when migration was built into DartFix (because different kinds of fixes had different numbers of phases), but now that migration is a separate tool, it gets in the way because it makes it difficult for the caller to have different behaviors in different phases. This is a prerequisite for making it possible to have some files analyzed but not finalized (which is in turn necessary for handling generated files). Change-Id: I5993808eb3dace21d8b6cb9c5308f94a3b1f898e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151081 Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 9f8776f commit 2fbe5c1

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

pkg/nnbd_migration/lib/migration_cli.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ class _BadArgException implements Exception {
848848
}
849849

850850
class _FixCodeProcessor extends Object {
851+
static const numPhases = 3;
852+
851853
final DriverBasedAnalysisContext context;
852854

853855
NonNullableFix _task;
@@ -922,7 +924,7 @@ class _FixCodeProcessor extends Object {
922924
Future<void> runFirstPhase({bool singlePhaseProgress = false}) async {
923925
// All tasks should be registered; [numPhases] should be finalized.
924926
_progressBar = _ProgressBar(
925-
pathsToProcess.length * (singlePhaseProgress ? 1 : _task.numPhases));
927+
pathsToProcess.length * (singlePhaseProgress ? 1 : numPhases));
926928

927929
// Process package
928930
_task.processPackage(context.contextRoot.root);
@@ -939,23 +941,24 @@ class _FixCodeProcessor extends Object {
939941
}
940942
if (_migrationCli.options.ignoreErrors ||
941943
_migrationCli.fileErrors.isEmpty) {
942-
await _task.processUnit(0, result);
944+
await _task.prepareUnit(result);
943945
}
944946
});
945947
}
946948

947949
Future<List<String>> runLaterPhases({bool resetProgress = false}) async {
948950
if (resetProgress) {
949-
_progressBar =
950-
_ProgressBar(pathsToProcess.length * (_task.numPhases - 1));
951+
_progressBar = _ProgressBar(pathsToProcess.length * (numPhases - 1));
951952
}
952953

953-
for (var phase = 1; phase < _task.numPhases; phase++) {
954-
await processResources((ResolvedUnitResult result) async {
955-
_progressBar.tick();
956-
await _task.processUnit(phase, result);
957-
});
958-
}
954+
await processResources((ResolvedUnitResult result) async {
955+
_progressBar.tick();
956+
await _task.processUnit(result);
957+
});
958+
await processResources((ResolvedUnitResult result) async {
959+
_progressBar.tick();
960+
await _task.finalizeUnit(result);
961+
});
959962
var state = await _task.finish();
960963
if (_migrationCli.options.webPreview) {
961964
await _task.startPreviewServer(state);

pkg/nnbd_migration/lib/src/front_end/non_nullable_fix.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,17 @@ class NonNullableFix {
9191

9292
bool get isPreviewServerRunning => _server != null;
9393

94-
int get numPhases => 3;
95-
9694
InstrumentationListener createInstrumentationListener(
9795
{MigrationSummary migrationSummary}) =>
9896
InstrumentationListener(migrationSummary: migrationSummary);
9997

98+
Future<void> finalizeUnit(ResolvedUnitResult result) async {
99+
if (!_packageIsNNBD) {
100+
return;
101+
}
102+
migration.finalizeInput(result);
103+
}
104+
100105
Future<MigrationState> finish() async {
101106
migration.finish();
102107
final state = MigrationState(
@@ -105,6 +110,13 @@ class NonNullableFix {
105110
return state;
106111
}
107112

113+
Future<void> prepareUnit(ResolvedUnitResult result) async {
114+
if (!_packageIsNNBD) {
115+
return;
116+
}
117+
migration.prepareInput(result);
118+
}
119+
108120
/// Processes the non-source files of the package rooted at [pkgFolder].
109121
///
110122
/// This means updating the pubspec.yaml file and the package_config.json
@@ -138,23 +150,11 @@ class NonNullableFix {
138150
}
139151
}
140152

141-
Future<void> processUnit(int phase, ResolvedUnitResult result) async {
153+
Future<void> processUnit(ResolvedUnitResult result) async {
142154
if (!_packageIsNNBD) {
143155
return;
144156
}
145-
switch (phase) {
146-
case 0:
147-
migration.prepareInput(result);
148-
break;
149-
case 1:
150-
migration.processInput(result);
151-
break;
152-
case 2:
153-
migration.finalizeInput(result);
154-
break;
155-
default:
156-
throw ArgumentError('Unsupported phase $phase');
157-
}
157+
migration.processInput(result);
158158
}
159159

160160
Future<MigrationState> rerun() async {

0 commit comments

Comments
 (0)