Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit fec7b42

Browse files
asyncLizcopybara-github
authored andcommitted
feat(theme): add validation option to disallow custom properties
PiperOrigin-RevId: 354349315
1 parent a9ac16b commit fec7b42

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

packages/mdc-theme/_custom-properties.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222
@use 'sass:map';
2323
@use 'sass:meta';
24+
@use 'sass:string';
2425
@use './css';
2526
@use './gss';
2627

@@ -32,6 +33,11 @@
3233
@return meta.type-of($value) == 'map' and map.has-key($value, 'varname');
3334
}
3435

36+
@function is-custom-prop-string($value) {
37+
@return meta.type-of($value) == 'string' and string.slice($value, 1, 4) ==
38+
'var(';
39+
}
40+
3541
/// Returns true if $prop1's varname and fallback values are deeply equal to
3642
/// $prop2's varname and fallback values.
3743
///

packages/mdc-theme/_theme.scss

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@
177177
/// theme configuration.
178178
/// Use this in component's `theme()` mixin implementation to validate if
179179
/// provided theme configuration has supported theme configuration keys.
180-
/// @param {Map} $origin-theme Original theme configuration in Sass map format
180+
/// @param {Map} $origin-theme - Original theme configuration in Sass map format
181181
/// that has all supported keys.
182-
/// @param {Map} $custom-theme Provided theme configuration in Sass map format
182+
/// @param {Map} $custom-theme - Provided theme configuration in Sass map format
183183
/// that should be validated against `$origin-theme`.
184184
/// @examples
185185
/// @mixin theme($theme) {
@@ -189,12 +189,26 @@
189189
/// }
190190
@mixin validate-keys($origin-theme, $custom-theme, $test-only: false) {
191191
$origin-keys: map.keys($origin-theme);
192-
$custom-keys: map.keys($custom-theme);
193192
$unsupported-keys: ();
193+
$unsupported-custom-prop-keys: ();
194194

195-
@each $key in $custom-keys {
195+
@each $key, $value in $custom-theme {
196196
@if (not list.index($origin-keys, $key)) {
197-
$unsupported-keys: list.append($unsupported-keys, $key);
197+
$unsupported-keys: list.append(
198+
$unsupported-keys,
199+
$key,
200+
$separator: comma
201+
);
202+
}
203+
204+
$is-prop: custom-properties.is-custom-prop($value) or
205+
custom-properties.is-custom-prop-string($value);
206+
@if $is-prop {
207+
$unsupported-custom-prop-keys: list.append(
208+
$unsupported-custom-prop-keys,
209+
$key,
210+
$separator: comma
211+
);
198212
}
199213
}
200214

@@ -207,4 +221,14 @@
207221
@error $error-message;
208222
}
209223
}
224+
225+
@if list.length($unsupported-custom-prop-keys) > 0 {
226+
$error-message: 'Custom properties are not supported for theme map keys: #{$unsupported-custom-prop-keys}';
227+
228+
@if $test-only {
229+
content: $error-message;
230+
} @else {
231+
@error $error-message;
232+
}
233+
}
210234
}

packages/mdc-theme/test/theme-validate-keys.test.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ $origin-theme: (
4343
$test-only: true
4444
);
4545
}
46+
47+
// Should throw error when custom properties are provided
48+
.no-custom-props {
49+
@include theme.validate-keys(
50+
$origin-theme,
51+
(
52+
one: var(--one),
53+
two: var(--two, 4px),
54+
three: (
55+
varname: --three,
56+
fallback: teal,
57+
),
58+
),
59+
$test-only: true
60+
);
61+
}

packages/mdc-theme/test/theme.scss.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,11 @@ describe('theme.test.scss', () => {
8787
const css = fs.readFileSync(filePath, 'utf8').trim();
8888
expect(css).toContain('Unsupported keys found: foobar.');
8989
});
90+
91+
it('validate-keys Should throw error when custom properties are provided', () => {
92+
const filePath = path.join(__dirname, 'theme-validate-keys.test.css');
93+
const css = fs.readFileSync(filePath, 'utf8').trim();
94+
expect(css).toContain(
95+
'Custom properties are not supported for theme map keys: one, two, three');
96+
});
9097
});

0 commit comments

Comments
 (0)