Created
March 23, 2021 23:52
-
-
Save jonahwilliams/5c548e8243617b2167cd501d1455673d to your computer and use it in GitHub Desktop.
Confusing null promotion failure with while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:file/file.dart'; | |
String? findProjectRoot(FileSystem fileSystem, [ String? directory ]) { | |
const String kProjectRootSentinel = 'pubspec.yaml'; | |
directory ??= fileSystem.currentDirectory.path; | |
while (true) { | |
if (fileSystem.isFileSync(fileSystem.path.join(directory, kProjectRootSentinel))) { | |
return directory; | |
} | |
final String parent = fileSystem.path.dirname(directory); | |
if (directory == parent) { | |
return null; | |
} | |
directory = parent; | |
} | |
} | |
String? findProjectRoot3(FileSystem fileSystem, [ String? directory ]) { | |
const String kProjectRootSentinel = 'pubspec.yaml'; | |
var localDirectory = directory ?? fileSystem.currentDirectory.path; | |
while (true) { | |
if (fileSystem.isFileSync(fileSystem.path.join(localDirectory, kProjectRootSentinel))) { | |
return localDirectory; | |
} | |
final String parent = fileSystem.path.dirname(localDirectory); | |
if (localDirectory == parent) { | |
return null; | |
} | |
localDirectory = parent; | |
} | |
} | |
String? findProjectRoot2(FileSystem fileSystem, [ String? directory ]) { | |
const String kProjectRootSentinel = 'pubspec.yaml'; | |
directory ??= fileSystem.currentDirectory.path; | |
if (fileSystem.isFileSync(fileSystem.path.join(directory, kProjectRootSentinel))) { | |
return directory; | |
} | |
final String parent = fileSystem.path.dirname(directory); | |
if (directory == parent) { | |
return null; | |
} | |
directory = parent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment