Skip to content

Commit

Permalink
Use lint prefer_final_locals (#4247)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm authored Apr 29, 2024
1 parent 8d312a2 commit e1fbda7
Show file tree
Hide file tree
Showing 213 changed files with 956 additions and 947 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ linter:
- package_api_docs
- prefer_asserts_in_initializer_lists
- prefer_const_declarations
- prefer_final_locals
- prefer_relative_imports
- prefer_single_quotes
- require_trailing_commas
Expand Down
8 changes: 4 additions & 4 deletions lib/src/ascii_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ String fromFiles(
required bool showFileSizes,
}) {
// Parse out the files into a tree of nested maps.
var root = <String, Map>{};
final root = <String, Map>{};
for (var file in files) {
final relativeFile =
baseDir == null ? file : p.relative(file, from: baseDir);
Expand Down Expand Up @@ -117,7 +117,7 @@ String fromFiles(
/// ' '---logging
/// '---barback
String fromMap(Map<String, Map> map, {bool startingAtTop = true}) {
var buffer = StringBuffer();
final buffer = StringBuffer();
_draw(buffer, '', null, map, depth: startingAtTop ? 0 : 1);
return buffer.toString();
}
Expand Down Expand Up @@ -162,10 +162,10 @@ void _draw(
if (name != null) _drawLine(buffer, prefix, isLast, name, depth <= 1);

// Recurse to the children.
var childNames = ordered(children.keys);
final childNames = ordered(children.keys);

void drawChild(bool isLastChild, String child) {
var childPrefix = _getPrefix(depth <= 1, isLast);
final childPrefix = _getPrefix(depth <= 1, isLast);
_draw(
buffer,
'$prefix$childPrefix',
Expand Down
2 changes: 1 addition & 1 deletion lib/src/authentication/token_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class TokenStore {
///
/// `null` if no config directory could be found.
String? get tokensFile {
var dir = configDir;
final dir = configDir;
return dir == null ? null : p.join(dir, 'pub-tokens.json');
}
}
8 changes: 4 additions & 4 deletions lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ abstract class PubCommand extends Command<int> {
Command get _topCommand {
Command current = this;
while (true) {
var parent = current.parent;
final parent = current.parent;
if (parent == null) return current;
current = parent;
}
Expand All @@ -145,7 +145,7 @@ abstract class PubCommand extends Command<int> {
@override
String get invocation {
PubCommand? command = this;
var names = <String?>[];
final names = <String?>[];
do {
names.add(command?.name);
command = command?.parent as PubCommand?;
Expand Down Expand Up @@ -254,7 +254,7 @@ and attaching the relevant parts of that log file.
/// appropriate exit code could be found.
int _chooseExitCode(Object exception) {
if (exception is SolveFailure) {
var packageNotFound = exception.packageNotFound;
final packageNotFound = exception.packageNotFound;
if (packageNotFound != null) exception = packageNotFound;
}
while (exception is WrappedException && exception.innerError is Exception) {
Expand Down Expand Up @@ -322,7 +322,7 @@ and attaching the relevant parts of that log file.
}

static void _computeCommand(ArgResults argResults) {
var list = <String?>[];
final list = <String?>[];
for (var command = argResults.command;
command != null;
command = command.command) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ Specify multiple sdk packages with descriptors.''');
) async {
final name = package.ref.name;
final dependencies = [...original.dependencies.values];
var devDependencies = [...original.devDependencies.values];
var dependencyOverrides = [...original.dependencyOverrides.values];
final devDependencies = [...original.devDependencies.values];
final dependencyOverrides = [...original.dependencyOverrides.values];

final dependencyNames = dependencies.map((dependency) => dependency.name);
final devDependencyNames =
Expand Down
10 changes: 5 additions & 5 deletions lib/src/command/cache_add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class CacheAddCommand extends PubCommand {

// Don't allow extra arguments.
if (argResults.rest.length > 1) {
var unexpected = argResults.rest.skip(1).map((arg) => '"$arg"');
var arguments = pluralize('argument', unexpected.length);
final unexpected = argResults.rest.skip(1).map((arg) => '"$arg"');
final arguments = pluralize('argument', unexpected.length);
usageException('Unexpected $arguments ${toSentence(unexpected)}.');
}

var package = argResults.rest.single;
final package = argResults.rest.single;

// Parse the version constraint, if there is one.
var constraint = VersionConstraint.any;
Expand All @@ -61,10 +61,10 @@ class CacheAddCommand extends PubCommand {
}

// TODO(rnystrom): Support installing from git too.
var source = cache.hosted;
final source = cache.hosted;

// TODO(rnystrom): Allow specifying the server.
var ids = (await cache.getVersions(source.refFor(package)))
final ids = (await cache.getVersions(source.refFor(package)))
.where((id) => constraint.allows(id.version))
.toList();

Expand Down
6 changes: 3 additions & 3 deletions lib/src/command/cache_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class CacheListCommand extends PubCommand {
@override
Future<void> runProtected() async {
// TODO(keertip): Add flag to list packages from non default sources.
var packagesObj = <String, Map>{};
final packagesObj = <String, Map>{};

var source = cache.defaultSource as CachedSource;
final source = cache.defaultSource as CachedSource;
for (var package in source.getCachedPackages(cache)) {
var packageInfo = packagesObj.putIfAbsent(package.name, () => {});
final packageInfo = packagesObj.putIfAbsent(package.name, () => {});
packageInfo[package.version.toString()] = {'location': package.dir};
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/command/cache_repair.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class CacheRepairCommand extends PubCommand {
final failures = repairResults.where((result) => !result.success);

if (successes.isNotEmpty) {
var packages = pluralize('package', successes.length);
final packages = pluralize('package', successes.length);
log.message(
'Reinstalled ${log.green(successes.length.toString())} $packages.',
);
}

if (failures.isNotEmpty) {
var packages = pluralize('package', failures.length);
var buffer = StringBuffer(
final packages = pluralize('package', failures.length);
final buffer = StringBuffer(
'Failed to reinstall ${log.red(failures.length.toString())} $packages:\n',
);

Expand All @@ -60,17 +60,17 @@ class CacheRepairCommand extends PubCommand {
log.message(buffer.toString());
}

var (repairSuccesses, repairFailures) =
final (repairSuccesses, repairFailures) =
await globals.repairActivatedPackages();
if (repairSuccesses.isNotEmpty) {
var packages = pluralize('package', repairSuccesses.length);
final packages = pluralize('package', repairSuccesses.length);
log.message(
'Reactivated ${log.green(repairSuccesses.length.toString())} $packages.',
);
}

if (repairFailures.isNotEmpty) {
var packages = pluralize('package', repairFailures.length);
final packages = pluralize('package', repairFailures.length);
log.message(
'Failed to reactivate ${log.red(repairFailures.length.toString())} $packages:',
);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/command/dependency_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ class DependencyServicesApplyCommand extends PubCommand {
);
// Remove the now outdated content-hash - it will be restored below
// after resolution.
var packageMap = lockFileEditor
final packageMap = lockFileEditor
.parseAt(['packages', targetPackage, 'description']).value as Map;
var hasSha = packageMap.containsKey('sha256');
final hasSha = packageMap.containsKey('sha256');
if (hasSha) {
lockFileEditor.remove(
['packages', targetPackage, 'description', 'sha256'],
Expand Down
32 changes: 16 additions & 16 deletions lib/src/command/deps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class DepsCommand extends PubCommand {
});
toVisit.addAll(next);
}
var executables = [
final executables = [
for (final package in [
entrypoint.workspaceRoot,
...entrypoint.workspaceRoot.immediateDependencies.keys
Expand Down Expand Up @@ -216,7 +216,7 @@ class DepsCommand extends PubCommand {
);
}

var transitive = await _getTransitiveDependencies();
final transitive = await _getTransitiveDependencies();
await _outputCompactPackages('transitive dependencies', transitive, buffer);
}

Expand All @@ -231,14 +231,14 @@ class DepsCommand extends PubCommand {
buffer.writeln();
buffer.writeln('$section:');
for (var name in ordered(names)) {
var package = await _getPackage(name);
final package = await _getPackage(name);

buffer.write('- ${_labelPackage(package)}');
if (package.dependencies.isEmpty) {
buffer.writeln();
} else {
var depNames = package.dependencies.keys;
var depsList = "[${depNames.join(' ')}]";
final depNames = package.dependencies.keys;
final depsList = "[${depNames.join(' ')}]";
buffer.writeln(' ${log.gray(depsList)}');
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ class DepsCommand extends PubCommand {
);
}

var transitive = await _getTransitiveDependencies();
final transitive = await _getTransitiveDependencies();
if (transitive.isEmpty) return;

await _outputListSection(
Expand All @@ -295,7 +295,7 @@ class DepsCommand extends PubCommand {
buffer.writeln('$name:');

for (var name in deps) {
var package = await _getPackage(name);
final package = await _getPackage(name);
buffer.writeln('- ${_labelPackage(package)}');

for (var dep in package.dependencies.values) {
Expand All @@ -318,15 +318,15 @@ class DepsCommand extends PubCommand {
// The work list for the breadth-first traversal. It contains the package
// being added to the tree, and the parent map that will receive that
// package.
var toWalk = Queue<(Package, Map<String, Map>)>();
var visited = <String>{};
final toWalk = Queue<(Package, Map<String, Map>)>();
final visited = <String>{};

// Start with the root dependencies.
var packageTree = <String, Map>{};
final packageTree = <String, Map>{};
final workspacePackageNames = [
...entrypoint.workspaceRoot.transitiveWorkspace.map((p) => p.name),
];
var immediateDependencies =
final immediateDependencies =
entrypoint.workspaceRoot.immediateDependencies.keys.toSet();
if (!_includeDev) {
immediateDependencies
Expand All @@ -346,7 +346,7 @@ class DepsCommand extends PubCommand {
}

// Populate the map with this package's dependencies.
var childMap = <String, Map>{};
final childMap = <String, Map>{};
map[_labelPackage(package)] = childMap;

final isRoot = workspacePackageNames.contains(package.name);
Expand All @@ -370,7 +370,7 @@ class DepsCommand extends PubCommand {

/// Gets the names of the non-immediate dependencies of the workspace packages.
Future<Set<String>> _getTransitiveDependencies() async {
var transitive = await _getAllDependencies();
final transitive = await _getAllDependencies();
for (final root in entrypoint.workspaceRoot.transitiveWorkspace) {
transitive.remove(root.name);
transitive.removeAll(root.dependencies.keys);
Expand All @@ -388,7 +388,7 @@ class DepsCommand extends PubCommand {
return graph.packages.keys.toSet();
}

var nonDevDependencies = [
final nonDevDependencies = [
for (final package in entrypoint.workspaceRoot.transitiveWorkspace) ...[
...package.dependencies.keys,
...package.dependencyOverrides.keys,
Expand All @@ -407,7 +407,7 @@ class DepsCommand extends PubCommand {
/// but it's possible, since [Entrypoint.assertUpToDate]'s modification time
/// check can return a false negative. This fails gracefully if that happens.
Future<Package> _getPackage(String name) async {
var package = (await entrypoint.packageGraph).packages[name];
final package = (await entrypoint.packageGraph).packages[name];
if (package != null) return package;
dataError('The pubspec.yaml file has changed since the pubspec.lock file '
'was generated, please run "$topLevelProgram pub get" again.');
Expand All @@ -426,7 +426,7 @@ class DepsCommand extends PubCommand {
};

for (var package in packages) {
var executables = package.executableNames;
final executables = package.executableNames;
if (executables.isNotEmpty) {
buffer.writeln(_formatExecutables(package.name, executables.toList()));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/command/downgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DowngradeCommand extends PubCommand {
unlock: argResults.rest,
dryRun: _dryRun,
);
var example = entrypoint.example;
final example = entrypoint.example;
if (argResults.flag('example') && example != null) {
await example.acquireDependencies(
SolveType.get,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/command/get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class GetCommand extends PubCommand {
enforceLockfile: argResults.flag('enforce-lockfile'),
);

var example = entrypoint.example;
final example = entrypoint.example;
if ((argResults.flag('example')) && example != null) {
await example.acquireDependencies(
SolveType.get,
Expand Down
12 changes: 6 additions & 6 deletions lib/src/command/global_activate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ class GlobalActivateCommand extends PubCommand {

String readArg([String error = '']) {
if (args.isEmpty) usageException(error);
var arg = args.first;
final arg = args.first;
args = args.skip(1);
return arg;
}

void validateNoExtraArgs() {
if (args.isEmpty) return;
var unexpected = args.map((arg) => '"$arg"');
var arguments = pluralize('argument', unexpected.length);
final unexpected = args.map((arg) => '"$arg"');
final arguments = pluralize('argument', unexpected.length);
usageException('Unexpected $arguments ${toSentence(unexpected)}.');
}

Expand All @@ -121,7 +121,7 @@ class GlobalActivateCommand extends PubCommand {

switch (argResults.optionWithDefault('source')) {
case 'git':
var repo = readArg('No Git repository given.');
final repo = readArg('No Git repository given.');
validateNoExtraArgs();
return globals.activateGit(
repo,
Expand All @@ -132,7 +132,7 @@ class GlobalActivateCommand extends PubCommand {
);

case 'hosted':
var package = readArg('No package to activate given.');
final package = readArg('No package to activate given.');

PackageRef ref;
try {
Expand Down Expand Up @@ -168,7 +168,7 @@ class GlobalActivateCommand extends PubCommand {
);

case 'path':
var path = readArg('No package to activate given.');
final path = readArg('No package to activate given.');
validateNoExtraArgs();
return globals.activatePath(
path,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/command/global_deactivate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class GlobalDeactivateCommand extends PubCommand {

// Don't allow extra arguments.
if (argResults.rest.length > 1) {
var unexpected = argResults.rest.skip(1).map((arg) => '"$arg"');
var arguments = pluralize('argument', unexpected.length);
final unexpected = argResults.rest.skip(1).map((arg) => '"$arg"');
final arguments = pluralize('argument', unexpected.length);
usageException('Unexpected $arguments ${toSentence(unexpected)}.');
}

Expand Down
Loading

0 comments on commit e1fbda7

Please sign in to comment.