From 03ddb81c31b8a91f6b4fe8ee7fa017a39ee5a152 Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Thu, 31 May 2018 23:35:36 +0200 Subject: [PATCH 1/4] feat: add `sl-safe-get-function` to use `get-function` with large Sass support --- stylesheets/_SassyLists.scss | 1 + stylesheets/helpers/_safe-get-function.scss | 28 +++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 stylesheets/helpers/_safe-get-function.scss diff --git a/stylesheets/_SassyLists.scss b/stylesheets/_SassyLists.scss index 96569bf..355fb49 100644 --- a/stylesheets/_SassyLists.scss +++ b/stylesheets/_SassyLists.scss @@ -4,6 +4,7 @@ // Repository: https://github.com/at-import/functions/ @import 'helpers/missing-dependencies'; +@import 'helpers/safe-get-function'; @import 'helpers/str-compare'; @import 'helpers/true'; @import 'helpers/is-number'; diff --git a/stylesheets/helpers/_safe-get-function.scss b/stylesheets/helpers/_safe-get-function.scss new file mode 100644 index 0000000..879fc69 --- /dev/null +++ b/stylesheets/helpers/_safe-get-function.scss @@ -0,0 +1,28 @@ +// Copyright (c) 2016, Kaelig Deloumeau-Prigent +// All rights reserved. +// BSD 2-Clause License +// https://tldrlegal.com/license/bsd-2-clause-license-(freebsd) + +/// +/// Polyfill for the `get-function` function +/// @link https://github.com/kaelig/sass-safe-get-function +/// @link http://sass-lang.com/documentation/Sass/Script/Functions.html#get_function-instance_method +/// +/// @access private +/// +/// @param {String} $name - Name of the function +/// +/// @return {Function|String} +/// +@function safe-get-function($name) { + @if (type-of($name) != 'string') { + @error 'The parameter passed to safe-get-function() must be a String. Good: safe-get-function(\'foo\') / Bad: safe-get-function(5).'; + } + + @if function-exists('get-function') { + @return get-function($name); + } + + @return $name; + +} From ed42ed52d686c4f73cf459164495a4d9ea4071e3 Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Thu, 31 May 2018 23:36:30 +0200 Subject: [PATCH 2/4] fix: fix libsass 4.0 call deprecation warning #63 Closes https://github.com/at-import/SassyLists/issues/63 --- stylesheets/functions/_every.scss | 6 +++--- stylesheets/functions/_some.scss | 8 ++++---- stylesheets/functions/_walk.scss | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/stylesheets/functions/_every.scss b/stylesheets/functions/_every.scss index 799cdc8..10db4db 100644 --- a/stylesheets/functions/_every.scss +++ b/stylesheets/functions/_every.scss @@ -20,10 +20,10 @@ @function sl-every($list, $function, $args...) { @each $item in $list { - @if not call($function, $item, $args...) { + @if not call(sl-safe-get-function($function), $item, $args...) { @return false; - } + } } - + @return true; } diff --git a/stylesheets/functions/_some.scss b/stylesheets/functions/_some.scss index d6945b8..123e79e 100644 --- a/stylesheets/functions/_some.scss +++ b/stylesheets/functions/_some.scss @@ -21,13 +21,13 @@ /// /// @return {Bool} /// - + @function sl-some($list, $function, $args...) { @each $item in $list { - @if call($function, $item, $args...) { + @if call(sl-safe-get-function($function), $item, $args...) { @return true; - } + } } - + @return false; } diff --git a/stylesheets/functions/_walk.scss b/stylesheets/functions/_walk.scss index 23f9f17..5d66c6f 100644 --- a/stylesheets/functions/_walk.scss +++ b/stylesheets/functions/_walk.scss @@ -18,18 +18,18 @@ /// /// @return {List | Null} /// - + @function sl-walk($list, $function, $args...) { $_: sl-missing-dependencies('sl-to-map', 'sl-to-list'); - + @if not function-exists($function) { @error 'There is no `#{$function}` function for `sl-walk`.'; } @each $index, $value in sl-to-map($list) { $arguments: join($value, $args); - $list: set-nth($list, $index, call($function, $arguments...)); + $list: set-nth($list, $index, call(sl-safe-get-function($function), $arguments...)); } - + @return sl-to-list($list); } From 54a247effe0071a92a0312960b8b371c7798cb2c Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Fri, 1 Jun 2018 23:57:06 +0200 Subject: [PATCH 3/4] refactor: replace `sl-safe-get-function` by `sl-safe-call-function` Use `sl-safe-call-function` instead and add a warning message to encourage users to switch to first-class functions. sl-safe-call-function($func, ...$args) Polyfill for the `call` function supporting both functions and strings. --- stylesheets/_SassyLists.scss | 2 +- stylesheets/functions/_every.scss | 2 +- stylesheets/functions/_some.scss | 2 +- stylesheets/functions/_walk.scss | 2 +- stylesheets/helpers/_safe-call-function.scss | 28 ++++++++++++++++++++ stylesheets/helpers/_safe-get-function.scss | 28 -------------------- 6 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 stylesheets/helpers/_safe-call-function.scss delete mode 100644 stylesheets/helpers/_safe-get-function.scss diff --git a/stylesheets/_SassyLists.scss b/stylesheets/_SassyLists.scss index 355fb49..f134d87 100644 --- a/stylesheets/_SassyLists.scss +++ b/stylesheets/_SassyLists.scss @@ -4,7 +4,7 @@ // Repository: https://github.com/at-import/functions/ @import 'helpers/missing-dependencies'; -@import 'helpers/safe-get-function'; +@import 'helpers/safe-call-function'; @import 'helpers/str-compare'; @import 'helpers/true'; @import 'helpers/is-number'; diff --git a/stylesheets/functions/_every.scss b/stylesheets/functions/_every.scss index 10db4db..3ba09fb 100644 --- a/stylesheets/functions/_every.scss +++ b/stylesheets/functions/_every.scss @@ -20,7 +20,7 @@ @function sl-every($list, $function, $args...) { @each $item in $list { - @if not call(sl-safe-get-function($function), $item, $args...) { + @if not sl-safe-call-function($function, $item, $args...) { @return false; } } diff --git a/stylesheets/functions/_some.scss b/stylesheets/functions/_some.scss index 123e79e..6e9feb2 100644 --- a/stylesheets/functions/_some.scss +++ b/stylesheets/functions/_some.scss @@ -24,7 +24,7 @@ @function sl-some($list, $function, $args...) { @each $item in $list { - @if call(sl-safe-get-function($function), $item, $args...) { + @if sl-safe-call-function($function, $item, $args...) { @return true; } } diff --git a/stylesheets/functions/_walk.scss b/stylesheets/functions/_walk.scss index 5d66c6f..ea42cb9 100644 --- a/stylesheets/functions/_walk.scss +++ b/stylesheets/functions/_walk.scss @@ -28,7 +28,7 @@ @each $index, $value in sl-to-map($list) { $arguments: join($value, $args); - $list: set-nth($list, $index, call(sl-safe-get-function($function), $arguments...)); + $list: set-nth($list, $index, sl-safe-call-function($function, $arguments...)); } @return sl-to-list($list); diff --git a/stylesheets/helpers/_safe-call-function.scss b/stylesheets/helpers/_safe-call-function.scss new file mode 100644 index 0000000..e3256d8 --- /dev/null +++ b/stylesheets/helpers/_safe-call-function.scss @@ -0,0 +1,28 @@ +/// +/// Polyfill for the `call` function supporting both functions and strings. +/// +/// 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. It is used in this library to keep a maximum compatibility. +/// End developer must be encouraged to use first-class functions. +/// +/// @link http://oddbird.net/2017/03/30/safe-get +/// @link http://sass.logdown.com/posts/809572-sass-35-release-candidate +/// +/// @access private +/// +/// @param {Function|String} $func - Function or name of the function to call +/// @param {*} $args... - Arguments to call the function with +/// +/// @return {*} +/// +@function sl-safe-call-function($func, $args...) { + @if (function-exists('get-function') and type-of($func) == 'string') { + @warn '#{$func} function name was passed for call(). It is recommended to use `get-function()` and first-class functions instead. See http://oddbird.net/2017/03/30/safe-get'; + @return call(get-function($func), $args...); + } + @else { + @return call($func, $args...); + } +} diff --git a/stylesheets/helpers/_safe-get-function.scss b/stylesheets/helpers/_safe-get-function.scss deleted file mode 100644 index 879fc69..0000000 --- a/stylesheets/helpers/_safe-get-function.scss +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2016, Kaelig Deloumeau-Prigent -// All rights reserved. -// BSD 2-Clause License -// https://tldrlegal.com/license/bsd-2-clause-license-(freebsd) - -/// -/// Polyfill for the `get-function` function -/// @link https://github.com/kaelig/sass-safe-get-function -/// @link http://sass-lang.com/documentation/Sass/Script/Functions.html#get_function-instance_method -/// -/// @access private -/// -/// @param {String} $name - Name of the function -/// -/// @return {Function|String} -/// -@function safe-get-function($name) { - @if (type-of($name) != 'string') { - @error 'The parameter passed to safe-get-function() must be a String. Good: safe-get-function(\'foo\') / Bad: safe-get-function(5).'; - } - - @if function-exists('get-function') { - @return get-function($name); - } - - @return $name; - -} From 054c6e4eac0def4fba22898d5815f36bc28d9a63 Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Sat, 2 Jun 2018 00:00:00 +0200 Subject: [PATCH 4/4] docs: update functions doc to add support for first-class functions --- stylesheets/functions/_every.scss | 6 +++--- stylesheets/functions/_some.scss | 6 +++--- stylesheets/functions/_walk.scss | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stylesheets/functions/_every.scss b/stylesheets/functions/_every.scss index 3ba09fb..852d2b6 100644 --- a/stylesheets/functions/_every.scss +++ b/stylesheets/functions/_every.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-every /// -/// @param {List} $list - list to run test against -/// @param {String} $function - function to run against every item from list -/// @param {ArgList} $args - extra arguments to pass to the function +/// @param {List} $list - list to run test against +/// @param {Function|String} $function - function or function name string to run against every item from list +/// @param {ArgList} $args - extra arguments to pass to the function /// /// @example /// sl-every(1 2 3, unitless) diff --git a/stylesheets/functions/_some.scss b/stylesheets/functions/_some.scss index 6e9feb2..43eed23 100644 --- a/stylesheets/functions/_some.scss +++ b/stylesheets/functions/_some.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-some /// -/// @param {List} $list - list to run test against -/// @param {String} $function - function to run against every item from list -/// @param {ArgList} $args - extra arguments to pass to the function +/// @param {List} $list - list to run test against +/// @param {Function|String} $function - function or function name string to run against every item from list +/// @param {ArgList} $args - extra arguments to pass to the function /// /// @example /// sl-some(1 2 3, unitless) diff --git a/stylesheets/functions/_walk.scss b/stylesheets/functions/_walk.scss index ea42cb9..3b78602 100644 --- a/stylesheets/functions/_walk.scss +++ b/stylesheets/functions/_walk.scss @@ -3,9 +3,9 @@ /// /// @ignore Documentation: http://at-import.github.io/SassyLists/documentation/#function-sl-walk /// -/// @param {List} $list - list to update -/// @param {String} $function - function to call on each value -/// @param {ArgList} $args - optional function arguments +/// @param {List} $list - list to update +/// @param {Function|String} $function - function or function name string to call on each value +/// @param {ArgList} $args - optional function arguments /// /// @throws There is no `$function` function for `sl-walk`. ///