Skip to content

Commit

Permalink
Update the order of maps returned by map.deep-merge() (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Apr 26, 2022
1 parent 6eed6eb commit 0c24114
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
@@ -1,4 +1,9 @@
## 1.50.2
## 1.51.0

* **Potentially breaking change**: Change the order of maps returned by
`map.deep-merge()` to match those returned by `map.merge()`. All keys that
appeared in the first map will now be listed first in the same order they
appeared in that map, followed by any new keys added from the second map.

* Improve the string output of some AST nodes in error messages.

Expand Down
33 changes: 10 additions & 23 deletions lib/src/functions/map.dart
Expand Up @@ -193,41 +193,28 @@ Value _modify(SassMap map, Iterable<Value> keys, Value modify(Value old),
/// If both [map1] and [map2] have a map value associated with the same key,
/// this recursively merges those maps as well.
SassMap _deepMergeImpl(SassMap map1, SassMap map2) {
if (map1.contents.isEmpty) return map2;
if (map2.contents.isEmpty) return map1;

// Avoid making a mutable copy of `map2` if it would totally overwrite `map1`
// anyway.
var mutable = false;
var result = map2.contents;
void _ensureMutable() {
if (mutable) return;
mutable = true;
result = Map.of(result);
}
var result = Map.of(map1.contents);

// Because values in `map2` take precedence over `map1`, we just check if any
// entries in `map1` don't have corresponding keys in `map2`, or if they're
// maps that need to be merged in their own right.
map1.contents.forEach((key, value) {
var resultValue = result[key];
if (resultValue == null) {
_ensureMutable();
map2.contents.forEach((key, value) {
var resultMap = result[key]?.tryMap();
if (resultMap == null) {
result[key] = value;
} else {
var resultMap = resultValue.tryMap();
var valueMap = value.tryMap();

if (resultMap != null && valueMap != null) {
var merged = _deepMergeImpl(valueMap, resultMap);
if (valueMap != null) {
var merged = _deepMergeImpl(resultMap, valueMap);
if (identical(merged, resultMap)) return;

_ensureMutable();
result[key] = merged;
} else {
result[key] = value;
}
}
});

return mutable ? SassMap(result) : map2;
return SassMap(result);
}

/// Like [new BuiltInCallable.function], but always sets the URL to `sass:map`.
Expand Down
4 changes: 4 additions & 0 deletions pkg/sass_api/CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.0.0-beta.43

* No user-visible changes.

## 1.0.0-beta.42

* No user-visible changes.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass_api/pubspec.yaml
Expand Up @@ -2,15 +2,15 @@ name: sass_api
# Note: Every time we add a new Sass AST node, we need to bump the *major*
# version because it's a breaking change for anyone who's implementing the
# visitor interface(s).
version: 1.0.0-beta.42
version: 1.0.0-beta.43
description: Additional APIs for Dart Sass.
homepage: https://github.com/sass/dart-sass

environment:
sdk: '>=2.12.0 <3.0.0'

dependencies:
sass: 1.50.1
sass: 1.51.0

dependency_overrides:
sass: {path: ../..}
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: sass
version: 1.50.2-dev
version: 1.51.0
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down

0 comments on commit 0c24114

Please sign in to comment.