From e300e41cbb5f7bb508c5e38634f64b7a43f0ac1c Mon Sep 17 00:00:00 2001 From: Ed Rivas Date: Thu, 13 Jul 2023 22:49:21 +0000 Subject: [PATCH] Check for Value before and after simplification --- lib/src/node/compile.dart | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/src/node/compile.dart b/lib/src/node/compile.dart index e24b651ca..3eae93ad4 100644 --- a/lib/src/node/compile.dart +++ b/lib/src/node/compile.dart @@ -260,18 +260,24 @@ List _parseFunctions(Object? functions, {bool asynch = false}) { if (!asynch) { late Callable callable; callable = Callable.fromSignature(signature, (arguments) { - var result = - simplify((callback as Function)(toJSArray(arguments)) as Object); - if (result is Value) return result; + var result = (callback as Function)(toJSArray(arguments)); if (isPromise(result)) { throw 'Invalid return value for custom function ' '"${callable.name}":\n' 'Promises may only be returned for sass.compileAsync() and ' 'sass.compileStringAsync().'; - } else { - throw 'Invalid return value for custom function ' - '"${callable.name}": $result is not a sass.Value.'; } + if (result is Value) { + var simplified = simplify(result); + if (simplified is! Value) { + throw 'Custom function "${callable.name}" ' + 'returned an object that cannot be simplified to a ' + 'sass.Value.'; + } + return simplified; + } + throw 'Invalid return value for custom function ' + '"${callable.name}": $result is not a sass.Value.'; }); result.add(callable); } else { @@ -281,9 +287,15 @@ List _parseFunctions(Object? functions, {bool asynch = false}) { if (isPromise(result)) { result = await promiseToFuture(result as Promise); } - - var simplified = simplify(result as Object); - if (simplified is Value) return simplified; + if (result is Value) { + var simplified = simplify(result); + if (simplified is! Value) { + throw 'Custom function "${callable.name}" ' + 'returned an object that cannot be simplified to a ' + 'sass.Value.'; + } + return simplified; + } throw 'Invalid return value for custom function ' '"${callable.name}": $result is not a sass.Value.'; });