Skip to content

Commit be967fe

Browse files
authored
Check for required fields in utility API mixin (#42127)
* Add basic check on values and property fields * next level of checks
1 parent 194f9a0 commit be967fe

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

scss/mixins/_utilities.scss

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,40 @@
8181
}
8282

8383
@mixin generate-utility($utility, $infix: "") {
84+
// Validate required keys
85+
@if not map.has-key($utility, property) {
86+
@error "Utility is missing required `property` key: #{$utility}";
87+
}
88+
@if not map.has-key($utility, values) {
89+
@error "Utility is missing required `values` key: #{$utility}";
90+
}
91+
92+
// Warn on unknown keys (likely typos)
93+
$valid-keys: property, values, class, selector, responsive, print, important, state, variables;
94+
@each $key in map.keys($utility) {
95+
@if not list.index($valid-keys, $key) {
96+
@warn "Unknown utility key `#{$key}` found. Valid keys are: #{$valid-keys}";
97+
}
98+
}
99+
84100
// Determine if we're generating a class, or an attribute selector
85101
$selectorType: "class";
86102
@if map.has-key($utility, selector) {
87103
$selectorType: map.get($utility, selector);
104+
// Validate selector type
105+
$valid-selectors: "class", "attr-starts", "attr-includes";
106+
@if not list.index($valid-selectors, $selectorType) {
107+
@error "Invalid `selector` value `#{$selectorType}`. Must be one of: #{$valid-selectors}";
108+
}
88109
}
89110
// Then get the class name to use in a class (e.g., .class) or in a attribute selector (e.g., [class^="class"])
90111
$selectorClass: map.get($utility, class);
91112

113+
// Attribute selectors require a `class` key
114+
@if $selectorType != "class" and not map.has-key($utility, class) {
115+
@error "Utility with `selector: #{$selectorType}` requires a `class` key.";
116+
}
117+
92118
// Get the list or map of values and ensure it's a map
93119
$values: map.get($utility, values);
94120
@if meta.type-of($values) != "map" {

0 commit comments

Comments
 (0)