Skip to content

Commit 467a1b0

Browse files
pqCommit Bot
authored and
Commit Bot
committed
dart fix for single files
Fixes: #43892 Change-Id: If1dca6bdef59e9de6dfd74e9774f61072651166d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246160 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 3290d7a commit 467a1b0

File tree

2 files changed

+245
-166
lines changed

2 files changed

+245
-166
lines changed

pkg/dartdev/lib/src/commands/fix.dart

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,28 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
7474
return 0;
7575
}
7676

77-
var arguments = args.rest;
78-
var argumentCount = arguments.length;
79-
if (argumentCount > 1) {
80-
usageException('Only one file or directory is expected.');
77+
var target = _getTarget(args.rest);
78+
if (!target.existsSync()) {
79+
var entity = target.isDirectory ? 'Directory' : 'File';
80+
usageException("$entity doesn't exist: ${target.path}");
8181
}
8282

83-
var dir =
84-
argumentCount == 0 ? io.Directory.current : io.Directory(arguments[0]);
85-
if (!dir.existsSync()) {
86-
usageException("Directory doesn't exist: ${dir.path}");
83+
if (inTestMode && !target.isDirectory) {
84+
usageException('Golden comparison requires a directory argument.');
8785
}
88-
dir = io.Directory(path.canonicalize(path.normalize(dir.absolute.path)));
89-
var dirPath = dir.path;
86+
87+
var fixPath = target.path;
9088

9189
var modeText = dryRun ? ' (dry run)' : '';
9290

93-
final projectName = path.basename(dirPath);
91+
final targetName = path.basename(fixPath);
9492
Progress? computeFixesProgress = log.progress(
95-
'Computing fixes in ${log.ansi.emphasized(projectName)}$modeText');
93+
'Computing fixes in ${log.ansi.emphasized(targetName)}$modeText');
9694

9795
var server = AnalysisServer(
9896
null,
9997
io.Directory(sdk.sdkPath),
100-
[dir],
98+
[target],
10199
commandName: 'fix',
102100
argResults: argResults,
103101
);
@@ -123,7 +121,7 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
123121
List<SourceFileEdit> edits;
124122
var pass = 0;
125123
do {
126-
var fixes = await server.requestBulkFixes(dirPath, inTestMode);
124+
var fixes = await server.requestBulkFixes(fixPath, inTestMode);
127125
_mergeDetails(detailsMap, fixes.details);
128126
edits = fixes.edits;
129127
_applyEdits(server, edits);
@@ -142,6 +140,7 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
142140
computeFixesProgress = null;
143141
}
144142

143+
var dir = target.isDirectory ? target as io.Directory : target.parent;
145144
if (inTestMode) {
146145
var result = _compareFixesInDirectory(dir);
147146
log.stdout('Passed: ${result.passCount}, Failed: ${result.failCount}');
@@ -267,6 +266,18 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
267266
String _compressWhitespace(String code) =>
268267
code.replaceAll(RegExp(r'\s+'), ' ');
269268

269+
io.FileSystemEntity _getTarget(List<String> arguments) {
270+
var argumentCount = arguments.length;
271+
if (argumentCount == 0) return io.Directory.current.absolute;
272+
if (argumentCount > 1) {
273+
usageException('Only one file or directory is expected.');
274+
}
275+
var normalizedPath = path.canonicalize(path.normalize(arguments[0]));
276+
return io.FileSystemEntity.isDirectorySync(normalizedPath)
277+
? io.Directory(normalizedPath)
278+
: io.File(normalizedPath);
279+
}
280+
270281
/// Merge the fixes from the current round's [details] into the [detailsMap].
271282
void _mergeDetails(Map<String, BulkFix> detailsMap, List<BulkFix> details) {
272283
for (var detail in details) {
@@ -361,3 +372,7 @@ class _TestResult {
361372
/// Initialize a newly created result object.
362373
_TestResult();
363374
}
375+
376+
extension on io.FileSystemEntity {
377+
bool get isDirectory => this is io.Directory;
378+
}

0 commit comments

Comments
 (0)