-
Notifications
You must be signed in to change notification settings - Fork 336
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
_warning
mixin cannot be used within functions
#3427
Comments
Good catch. Really this should be a function, I'm not sure why it's not 🤔 |
Produce a Sass compile warning if a defunct organisation is being referenced, with information on the new/replacement organisation. This currently uses the `@warn` Sass built-in as the `_warning` mixin produces an error in this context. See #3427.
Probably because being a mixin is the only way that does it 'right'. It seems like Sass only likes function calls to be used in certain ways. Changing the @function get-colour {
@if true {
_warning("doo-wop", "woo-bop");
}
@return #786999;
}
body {
color: get-colour();
} Changing it to use the @function get-colour {
@if true {
@return _warning("doo-wop", "woo-bop");
}
@return #786999;
}
body {
color: get-colour();
} Trying to use it within a mixin will also only work if it's attached to a property (real or not). @mixin nice-colours {
warning: _warning("doo-wop", "woo-bop");
color: white;
background-color: #786999;
}
body {
@include nice-colours;
} Using functions to manipulate global variables is also frowned upon in the Sass docs (see first "Head's up!"), which says to use mixins instead, though it's technically possible. Bootstrap also uses a mixin for deprecation warnings and (appears) to claim that it can be used with functions, however it doesn't seem to be used in the same way. It looks like they use it only for deprecating entire functions, and do so by including the mixin in the same file—outside of the function definition—rather than as an include within the function definition. |
Thanks for the writeup here. Boostrap's approach is really interesting (ref). As you say, their practice is to put the definition outside the thing being deprecated, I presume because it hits at the point of import rather than at the point of use. I'm personally not sure this is a great approach for us as we don't split our stuff out so explicitly at the moment and teams are generally more likely to just import the whole project rather than segment their stuff, especially for prototypes. A solution to this could be to be to just do that, split everything out, but a quicker win for now might simply be to update the docs on deprecation to specify that the warning mixin won't work with functions and you'll need to interact with the API manually 😞 It's not fantastic but it's a start in the absence of a better solution. |
Produce a Sass compile warning if a defunct organisation is being referenced, with information on the new/replacement organisation. This currently uses the `@warn` Sass built-in as the `_warning` mixin produces an error in this context. See #3427.
Produce a Sass compile warning if a defunct organisation is being referenced, with information on the new/replacement organisation. This currently uses the `@warn` Sass built-in as the `_warning` mixin produces an error in this context. See #3427.
We had a chat about this on slack during the review phase of #3576 and a few ideas emerged. Generally, we think that making the different pieces of
This could look something like this: @mixin _warning($key, $message) {
@if _should-warn($key) {
@warn _warning-text($key, $message);
$govuk-suppressed-warnings: append($govuk-suppressed-warnings, $key) !global;
}
}
@function _should-warn($key) {
@return index($govuk-suppressed-warnings, $key) == null;
}
@function _warning-text($key, $message) {
@return $message + " To silence this warning, update $govuk-suppressed-warnings " +
"with key: \"#{$key}\"";
} ... and would mean instances where the mixin couldn't be used could instead be replicated like so: @if _should-warn("my-warning-key") {
@warn _warning-text("my-warning-key", "This is a warning message");
} There's additionally a question around if the line in |
Produce a Sass compile warning if a defunct organisation is being referenced, with information on the new/replacement organisation. This currently uses the `@warn` Sass built-in as the `_warning` mixin produces an error in this context. See #3427.
Splits the _warning mixin into separate functions that can be used in places where mixins are not allowed by the SCSS syntax, as suggested in #3427. Co-authored-by: owenatgov <owen.jones@digital.cabinet-office.gov.uk>
I've opened a PR that implements the above suggestions and updates the offending documentation. Feel free to pick it up in my absence. |
Splits the _warning mixin into separate functions that can be used in places where mixins are not allowed by the SCSS syntax, as suggested in #3427. Co-authored-by: owenatgov <owen.jones@digital.cabinet-office.gov.uk>
Description of the issue
We use a
_warning
mixin in Sass to standardise our deprecation warnings and provide a mechanism for service teams to disable intrusive and repetative warnings.Our documentation for 'managing change' includes a section about using the mixin within a function.
However, it isn't actually possible to call a Sass mixin from within a function. Doing so returns an error in all Sass compilers that we currently support in Frontend:
This at-rule is not allowed here.
Functions can only contain variable declarations and control directives.
Functions can only contain variable declarations and control directives.
Steps to reproduce the issue
Try to include the
_warning
mixin (or any mixin) from inside of a function.Actual vs expected behaviour
Presumably the documentation is incorrect and needs to be amended.
We may need to devise a method of producing (and suppressing) warnings that are spawned by functions.
Environment
The text was updated successfully, but these errors were encountered: