Skip to content

Commit 0c24114

Browse files
authored
Update the order of maps returned by map.deep-merge() (#1680)
Closes sass/sass#3092
1 parent 6eed6eb commit 0c24114

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
## 1.50.2
1+
## 1.51.0
2+
3+
* **Potentially breaking change**: Change the order of maps returned by
4+
`map.deep-merge()` to match those returned by `map.merge()`. All keys that
5+
appeared in the first map will now be listed first in the same order they
6+
appeared in that map, followed by any new keys added from the second map.
27

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

lib/src/functions/map.dart

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,41 +193,28 @@ Value _modify(SassMap map, Iterable<Value> keys, Value modify(Value old),
193193
/// If both [map1] and [map2] have a map value associated with the same key,
194194
/// this recursively merges those maps as well.
195195
SassMap _deepMergeImpl(SassMap map1, SassMap map2) {
196+
if (map1.contents.isEmpty) return map2;
196197
if (map2.contents.isEmpty) return map1;
197198

198-
// Avoid making a mutable copy of `map2` if it would totally overwrite `map1`
199-
// anyway.
200-
var mutable = false;
201-
var result = map2.contents;
202-
void _ensureMutable() {
203-
if (mutable) return;
204-
mutable = true;
205-
result = Map.of(result);
206-
}
199+
var result = Map.of(map1.contents);
207200

208-
// Because values in `map2` take precedence over `map1`, we just check if any
209-
// entries in `map1` don't have corresponding keys in `map2`, or if they're
210-
// maps that need to be merged in their own right.
211-
map1.contents.forEach((key, value) {
212-
var resultValue = result[key];
213-
if (resultValue == null) {
214-
_ensureMutable();
201+
map2.contents.forEach((key, value) {
202+
var resultMap = result[key]?.tryMap();
203+
if (resultMap == null) {
215204
result[key] = value;
216205
} else {
217-
var resultMap = resultValue.tryMap();
218206
var valueMap = value.tryMap();
219-
220-
if (resultMap != null && valueMap != null) {
221-
var merged = _deepMergeImpl(valueMap, resultMap);
207+
if (valueMap != null) {
208+
var merged = _deepMergeImpl(resultMap, valueMap);
222209
if (identical(merged, resultMap)) return;
223-
224-
_ensureMutable();
225210
result[key] = merged;
211+
} else {
212+
result[key] = value;
226213
}
227214
}
228215
});
229216

230-
return mutable ? SassMap(result) : map2;
217+
return SassMap(result);
231218
}
232219

233220
/// Like [new BuiltInCallable.function], but always sets the URL to `sass:map`.

pkg/sass_api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.0-beta.43
2+
3+
* No user-visible changes.
4+
15
## 1.0.0-beta.42
26

37
* No user-visible changes.

pkg/sass_api/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ name: sass_api
22
# Note: Every time we add a new Sass AST node, we need to bump the *major*
33
# version because it's a breaking change for anyone who's implementing the
44
# visitor interface(s).
5-
version: 1.0.0-beta.42
5+
version: 1.0.0-beta.43
66
description: Additional APIs for Dart Sass.
77
homepage: https://github.com/sass/dart-sass
88

99
environment:
1010
sdk: '>=2.12.0 <3.0.0'
1111

1212
dependencies:
13-
sass: 1.50.1
13+
sass: 1.51.0
1414

1515
dependency_overrides:
1616
sass: {path: ../..}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.50.2-dev
2+
version: 1.51.0
33
description: A Sass implementation in Dart.
44
homepage: https://github.com/sass/dart-sass
55

0 commit comments

Comments
 (0)