Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the order of maps returned by map.deep-merge() #1680

Merged
merged 2 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## 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.

## 1.50.1

### Embedded Sass
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.1
version: 1.51.0
description: A Sass implementation in Dart.
homepage: https://github.com/sass/dart-sass

Expand Down