Skip to content

Commit

Permalink
Use ?[] instead of .andGet()
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Apr 15, 2021
1 parent 59b9dac commit c2e02a5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lib/src/async_environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class AsyncEnvironment {
}

if (type == "variable") name = "\$$name";
var span = _forwardedModuleNodes.andGet(oldModule)?.span;
var span = _forwardedModuleNodes?[oldModule]?.span;
throw MultiSpanSassScriptException(
'Two forwarded modules both define a $type named $name.',
"new @forward",
Expand Down Expand Up @@ -617,7 +617,7 @@ class AsyncEnvironment {
_lastVariableName = name;
_lastVariableIndex = index;
_variables[index][name] = value;
_variableNodes?.andGet(index)![name] = nodeWithSpan!;
_variableNodes?[index][name] = nodeWithSpan!;
}

/// Sets the variable named [name] to [value], associated with
Expand All @@ -636,7 +636,7 @@ class AsyncEnvironment {
_variableIndices[name] = index;
_variables[index][name] = value;
if (nodeWithSpan != null) {
_variableNodes?.andGet(index)![name] = nodeWithSpan;
_variableNodes?[index][name] = nodeWithSpan;
}
}

Expand Down Expand Up @@ -818,7 +818,7 @@ class AsyncEnvironment {
var configuration = <String, ConfiguredValue>{};
for (var i = 0; i < _variables.length; i++) {
var values = _variables[i];
var nodes = _variableNodes.andGet(i) ?? <String, AstNode>{};
var nodes = _variableNodes?[i] ?? <String, AstNode>{};
for (var entry in values.entries) {
// Implicit configurations are never invalid, making [configurationSpan]
// unnecessary, so we pass null here to avoid having to compute it.
Expand Down
10 changes: 5 additions & 5 deletions lib/src/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_environment.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: 1ef0f32701764f4b160ef045ca162b38653e0504
// Checksum: bb0b47fc04e32f36a0f87dc73bdfe3f89dc51aa4
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -322,7 +322,7 @@ class Environment {
}

if (type == "variable") name = "\$$name";
var span = _forwardedModuleNodes.andGet(oldModule)?.span;
var span = _forwardedModuleNodes?[oldModule]?.span;
throw MultiSpanSassScriptException(
'Two forwarded modules both define a $type named $name.',
"new @forward",
Expand Down Expand Up @@ -625,7 +625,7 @@ class Environment {
_lastVariableName = name;
_lastVariableIndex = index;
_variables[index][name] = value;
_variableNodes?.andGet(index)![name] = nodeWithSpan!;
_variableNodes?[index][name] = nodeWithSpan!;
}

/// Sets the variable named [name] to [value], associated with
Expand All @@ -644,7 +644,7 @@ class Environment {
_variableIndices[name] = index;
_variables[index][name] = value;
if (nodeWithSpan != null) {
_variableNodes?.andGet(index)![name] = nodeWithSpan;
_variableNodes?[index][name] = nodeWithSpan;
}
}

Expand Down Expand Up @@ -824,7 +824,7 @@ class Environment {
var configuration = <String, ConfiguredValue>{};
for (var i = 0; i < _variables.length; i++) {
var values = _variables[i];
var nodes = _variableNodes.andGet(i) ?? <String, AstNode>{};
var nodes = _variableNodes?[i] ?? <String, AstNode>{};
for (var entry in values.entries) {
// Implicit configurations are never invalid, making [configurationSpan]
// unnecessary, so we pass null here to avoid having to compute it.
Expand Down
4 changes: 1 addition & 3 deletions lib/src/util/merged_map_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import 'dart:collection';

import '../utils.dart';
import '../util/nullable.dart';
import 'nullable.dart';

/// An unmodifiable view of multiple maps merged together as though they were a
/// single map.
Expand Down Expand Up @@ -46,7 +44,7 @@ class MergedMapView<K, V> extends MapBase<K, V> {
}
}

V? operator [](Object? key) => _mapsByKey[key as K].andGet(key);
V? operator [](Object? key) => _mapsByKey[key as K]?[key];

operator []=(K key, V value) {
var child = _mapsByKey[key];
Expand Down
20 changes: 0 additions & 20 deletions lib/src/util/nullable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ extension NullableExtension<T> on T? {
}
}

extension NullableListExtension<T> on List<T>? {
/// If [this] is `null`, returns `null`. Otherwise, returns `this[index]`.
///
/// This is the equivalent of `list?.[key]`, if such a thing existed.
T? andGet(int index) {
var self = this;
return self == null ? null : self[index];
}
}

extension NullableMapExtension<K, V> on Map<K, V>? {
/// If [this] is `null`, returns `null`. Otherwise, returns `this[key]`.
///
/// This is the equivalent of `map?.[key]`, if such a thing existed.
V? andGet(Object? key) {
var self = this;
return self == null ? null : self[key];
}
}

extension SetExtension<T> on Set<T?> {
/// Destructively removes the `null` element from this set, if it exists, and
/// returns a view of it casted to a non-nullable type.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/visitor/async_evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ class _EvaluateVisitor
_environment.setLocalVariable(
declaredArguments[i].name,
evaluated.positional[i].withoutSlash(),
evaluated.positionalNodes.andGet(i));
evaluated.positionalNodes?[i]);
}

for (var i = evaluated.positional.length;
Expand All @@ -2231,7 +2231,7 @@ class _EvaluateVisitor
_environment.setLocalVariable(
argument.name,
value.withoutSlash(),
evaluated.namedNodes.andGet(argument.name) ??
evaluated.namedNodes?[argument.name] ??
argument.defaultValue.andThen(_expressionNode));
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/visitor/evaluate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/grind/synchronize.dart for details.
//
// Checksum: 01a7ae41ae622e64443597ea82b27b7aeb73d260
// Checksum: 6b82405fdc448ac69ca703bc6bffbb8d50f3fced
//
// ignore_for_file: unused_import

Expand Down Expand Up @@ -2206,7 +2206,7 @@ class _EvaluateVisitor
_environment.setLocalVariable(
declaredArguments[i].name,
evaluated.positional[i].withoutSlash(),
evaluated.positionalNodes.andGet(i));
evaluated.positionalNodes?[i]);
}

for (var i = evaluated.positional.length;
Expand All @@ -2218,7 +2218,7 @@ class _EvaluateVisitor
_environment.setLocalVariable(
argument.name,
value.withoutSlash(),
evaluated.namedNodes.andGet(argument.name) ??
evaluated.namedNodes?[argument.name] ??
argument.defaultValue.andThen(_expressionNode));
}

Expand Down

0 comments on commit c2e02a5

Please sign in to comment.