Skip to content

Commit 8f02909

Browse files
authored
custom-properties : fix how preserve false interacts with duplicate code (#662)
* custom-properties : add failing test for #661 * fix * closing tags * clarify changelog
1 parent f5af3cc commit 8f02909

9 files changed

+117
-25
lines changed

plugins/postcss-custom-properties/CHANGELOG.md

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
# Changes to PostCSS Custom Properties
22

3+
### Unreleased
4+
5+
- Fix how `preserve: false` interacts with logic around duplicate code (see `12.1.9`).
6+
7+
```diff
8+
:root {
9+
--my-order: 1;
10+
}
11+
12+
.foo {
13+
order: 1;
14+
order: var(--my-order);
15+
}
16+
17+
/* With `preserve: false` : */
18+
19+
.foo {
20+
order: 1;
21+
}
22+
```
23+
324
### 12.1.9 (September 14, 2022)
425

526
- Prevent duplicate code generation.

plugins/postcss-custom-properties/src/lib/transform-properties.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default (decl, customProperties, opts) => {
2222
// conditionally transform values that have changed
2323
if (value !== originalValue) {
2424
if (parentHasExactFallback(decl, value)) {
25+
if (!opts.preserve) {
26+
decl.remove();
27+
}
28+
2529
return;
2630
}
2731

plugins/postcss-custom-properties/test/basic.css

+14-4
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,22 @@ html {
139139
font-family: var(--font, "Helvetica Neue", Arial, sans-serif);
140140
}
141141

142-
.ignores-declarations-that-have-an-exact-fallback {
142+
.ignores-declarations-that-have-an-exact-fallback-a {
143143
left: 1rem;
144-
left: var(--size, 1rem);
144+
left: var(--does-not-exist, 1rem);
145145
}
146146

147-
.does-not-ignore-declarations-that-have-an-exact-override {
148-
left: var(--size, 1rem);
147+
.ignores-declarations-that-have-an-exact-fallback-b {
148+
right: 2em;
149+
right: var(--✅-size);
150+
}
151+
152+
.does-not-ignore-declarations-that-have-an-exact-override-a {
153+
left: var(--does-not-exist, 1rem);
149154
left: 1rem;
150155
}
156+
157+
.does-not-ignore-declarations-that-have-an-exact-override-b {
158+
right: var(--✅-size);
159+
right: 2em;
160+
}

plugins/postcss-custom-properties/test/basic.expect.css

+15-4
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,24 @@ html {
160160
font-family: var(--font, "Helvetica Neue", Arial, sans-serif);
161161
}
162162

163-
.ignores-declarations-that-have-an-exact-fallback {
163+
.ignores-declarations-that-have-an-exact-fallback-a {
164164
left: 1rem;
165-
left: var(--size, 1rem);
165+
left: var(--does-not-exist, 1rem);
166166
}
167167

168-
.does-not-ignore-declarations-that-have-an-exact-override {
168+
.ignores-declarations-that-have-an-exact-fallback-b {
169+
right: 2em;
170+
right: var(--✅-size);
171+
}
172+
173+
.does-not-ignore-declarations-that-have-an-exact-override-a {
169174
left: 1rem;
170-
left: var(--size, 1rem);
175+
left: var(--does-not-exist, 1rem);
171176
left: 1rem;
172177
}
178+
179+
.does-not-ignore-declarations-that-have-an-exact-override-b {
180+
right: 2em;
181+
right: var(--✅-size);
182+
right: 2em;
183+
}

plugins/postcss-custom-properties/test/basic.import-is-empty.expect.css

+15-4
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,24 @@ html {
160160
font-family: var(--font, "Helvetica Neue", Arial, sans-serif);
161161
}
162162

163-
.ignores-declarations-that-have-an-exact-fallback {
163+
.ignores-declarations-that-have-an-exact-fallback-a {
164164
left: 1rem;
165-
left: var(--size, 1rem);
165+
left: var(--does-not-exist, 1rem);
166166
}
167167

168-
.does-not-ignore-declarations-that-have-an-exact-override {
168+
.ignores-declarations-that-have-an-exact-fallback-b {
169+
right: 2em;
170+
right: var(--✅-size);
171+
}
172+
173+
.does-not-ignore-declarations-that-have-an-exact-override-a {
169174
left: 1rem;
170-
left: var(--size, 1rem);
175+
left: var(--does-not-exist, 1rem);
171176
left: 1rem;
172177
}
178+
179+
.does-not-ignore-declarations-that-have-an-exact-override-b {
180+
right: 2em;
181+
right: var(--✅-size);
182+
right: 2em;
183+
}

plugins/postcss-custom-properties/test/basic.import-override.expect.css

+11-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@
110110
font-family: "Helvetica Neue", Arial, sans-serif;
111111
}
112112

113-
.ignores-declarations-that-have-an-exact-fallback {
113+
.ignores-declarations-that-have-an-exact-fallback-a {
114114
left: 1rem;
115-
left: var(--size, 1rem);
116115
}
117116

118-
.does-not-ignore-declarations-that-have-an-exact-override {
117+
.ignores-declarations-that-have-an-exact-fallback-b {
118+
right: 2em;
119+
}
120+
121+
.does-not-ignore-declarations-that-have-an-exact-override-a {
119122
left: 1rem;
120123
left: 1rem;
121124
}
125+
126+
.does-not-ignore-declarations-that-have-an-exact-override-b {
127+
right: 2em;
128+
right: 2em;
129+
}

plugins/postcss-custom-properties/test/basic.import-override.inverse.expect.css

+11-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@
110110
font-family: "Helvetica Neue", Arial, sans-serif;
111111
}
112112

113-
.ignores-declarations-that-have-an-exact-fallback {
113+
.ignores-declarations-that-have-an-exact-fallback-a {
114114
left: 1rem;
115-
left: var(--size, 1rem);
116115
}
117116

118-
.does-not-ignore-declarations-that-have-an-exact-override {
117+
.ignores-declarations-that-have-an-exact-fallback-b {
118+
right: 2em;
119+
}
120+
121+
.does-not-ignore-declarations-that-have-an-exact-override-a {
119122
left: 1rem;
120123
left: 1rem;
121124
}
125+
126+
.does-not-ignore-declarations-that-have-an-exact-override-b {
127+
right: 2em;
128+
right: 2em;
129+
}

plugins/postcss-custom-properties/test/basic.import.expect.css

+15-4
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,24 @@ html {
161161
font-family: var(--font, "Helvetica Neue", Arial, sans-serif);
162162
}
163163

164-
.ignores-declarations-that-have-an-exact-fallback {
164+
.ignores-declarations-that-have-an-exact-fallback-a {
165165
left: 1rem;
166-
left: var(--size, 1rem);
166+
left: var(--does-not-exist, 1rem);
167167
}
168168

169-
.does-not-ignore-declarations-that-have-an-exact-override {
169+
.ignores-declarations-that-have-an-exact-fallback-b {
170+
right: 2em;
171+
right: var(--✅-size);
172+
}
173+
174+
.does-not-ignore-declarations-that-have-an-exact-override-a {
170175
left: 1rem;
171-
left: var(--size, 1rem);
176+
left: var(--does-not-exist, 1rem);
172177
left: 1rem;
173178
}
179+
180+
.does-not-ignore-declarations-that-have-an-exact-override-b {
181+
right: 2em;
182+
right: var(--✅-size);
183+
right: 2em;
184+
}

plugins/postcss-custom-properties/test/basic.preserve.expect.css

+11-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@
110110
font-family: "Helvetica Neue", Arial, sans-serif;
111111
}
112112

113-
.ignores-declarations-that-have-an-exact-fallback {
113+
.ignores-declarations-that-have-an-exact-fallback-a {
114114
left: 1rem;
115-
left: var(--size, 1rem);
116115
}
117116

118-
.does-not-ignore-declarations-that-have-an-exact-override {
117+
.ignores-declarations-that-have-an-exact-fallback-b {
118+
right: 2em;
119+
}
120+
121+
.does-not-ignore-declarations-that-have-an-exact-override-a {
119122
left: 1rem;
120123
left: 1rem;
121124
}
125+
126+
.does-not-ignore-declarations-that-have-an-exact-override-b {
127+
right: 2em;
128+
right: 2em;
129+
}

0 commit comments

Comments
 (0)