fix: fix support for function name strings for Sass < 3.5 #117
fix: fix support for function name strings for Sass < 3.5 #117ncoden merged 7 commits intofoundation:developfrom
Conversation
…ass 4 In order to improve modular namespacing, LibSass 4 only accepts first-class functions as argument so functions are called in their own context. In most case, `get-function()` must only be used by the user in its own context. End developer must be encouraged to use first-class functions. In this library, we need to use it to keep support of function name strings in Sass > 3.5. Changes: * Add `-mui-is-function` and `-mui-safe-call` to test and call function in a safe way for all Sass versions. * Use `-mui-is-function` instead of a simple `string` type check as functions can now be first-hand functions with a `function` type * Use `-mui-safe-call` to handle both first-hand functions and function string names the same way. A warning is thrown for function string names with Sass >= 3.5
`function-exists()` can only take strings and cannot be called for first-class functions.
…not found Instead of warning about the usage of function name strings in Sass >= 3.5, check for function arguments and throw the appropriate error if invalid or not found.
|
Poke @gakimball and @DanielRuf for review. 👋 |
src/util/_series.scss
Outdated
| /// @param {Duration} $duration [1s] - Length of the animation. | ||
| /// @param {Duration} $gap [0s] - Amount of time to pause before playing the animation after this one. Use a negative value to make the next effect overlap with the current one. | ||
| /// @param {Function} $keyframes... - One or more effect functions to build the keyframe with. | ||
| /// @param {Function|String} $keyframes... - One or more effect functions to build the keyframe with. |
There was a problem hiding this comment.
This parameter is technically an arglist given you use .... I believe the correct notation would be {Function[]|String[]}?
There was a problem hiding this comment.
SassDoc doesn't mention that notation or anything for .... But you built that tool so I'll believe you 😄
There was a problem hiding this comment.
@hugogiraudel hmmm... Shouldn't it be {Arglist} actually ? I don't find any reference or usage for the [] notation.
src/util/_function.scss
Outdated
| /// @return {*} | ||
| /// | ||
| @function -mui-safe-call($func, $args...) { | ||
| $_: -mui-assert-function($func); |
There was a problem hiding this comment.
If you want to make this code a little less odd, you could write it like this:
@function -mui-safe-call($func, $args...) {
@if (-mui-assert-function($func)) {
@return call(-mui-safe-get-function($func), $args...);
}
}There was a problem hiding this comment.
This is an assert function. We expect it to throw an error if anything is wrong. I don't think this is odd, but I'll make the change if you think it is really needed.
There was a problem hiding this comment.
I understand what it does. The fact is, you use an $_ because Sass doesn’t allow calling functions outside of a variable or an expression. That’s a hack. A necessary one, admittedly.
What I believe your code means is “if the function is callable, call it”. Hence why I would wrap it in an @if block. It produces the same result and appears to be slightly more clean to me.
I don’t feel strongly about it, it’s just a comment.
There was a problem hiding this comment.
Alright, it seems legit. I'll do that 👍
src/util/_function.scss
Outdated
| /// @return {Boolean} | ||
| /// | ||
| @function -mui-assert-function($value) { | ||
| @if -mui-is-callable($value) == true { |
There was a problem hiding this comment.
You could omit the == true here. :)
|
@hugogiraudel Thank you for the review 👍. Do you have an opinion about the approach itself ? Could it be used for SassyList ? |
|
I suppose so? I’m not really invested into SassyLists anymore though. :) |
|
@hugogiraudel I took care of the requested changes. I used |
|
@ncoden SassDoc does not maintain a list of types, so feel free to use the notation you want. |
True, I checked for that. We already always precise the type in the description |
In order to improve modular namespacing, Sass 4 will only accepts first-class functions as argument for
call()so functions will be called in their own context. As a first step, Sass 3.5 addedget-functionand throw a warning if a function name string is passed tocall().In #100, we started to use
get-function()for all calls tocall()to remove the warning and prepare support for Sass 4. This broke Motion UI for all Sass versions < 3.5 as they do not supportget-function().This PR fixes the support for Sass < 3.5 by introducing a set of helpers to safely manage both first-class functions and function name strings.
Note: In most case,
get-function()must only be used by the user in its own context. End developer must be encouraged to use first-class functions.Changes:
-mui-is-functioninstead of a simplestringtype check as functions can now be first-hand functions with afunctiontype-mui-safe-callto handle both first-hand functions and function string names the same way. An error is thrown if the given argument is not callable, with informations aboutget-function()if relevant.Added helpers:
-mui-is-function($value)Return if a given value is a function or a function name string.
-mui-is-callable($value)Return if a given value is callable.
-mui-assert-function($value)Check if a given value is callable and throw the appropriate error otherwise
-mui-safe-get-function($func)Return a reference to the given function or function name string compatible with the current Sass version. For Sass < 3.5, return the passed argument. For Sass >= 3.5, return a first-class function if a function name string was passed.
-mui-safe-call($func, $args...)Polyfill for the
callfunction supporting both functions and strings.References:
Passing a string to call()deprecation warning for Sass 4.0 #100