handle self referential default args

This commit is contained in:
ConnorSkees 2020-04-26 13:51:44 -04:00
parent f6fd0e9af5
commit f1b60019a1
3 changed files with 15 additions and 7 deletions

View File

@ -68,26 +68,28 @@ impl Function {
scope: &Scope,
super_selector: &Selector,
) -> SassResult<()> {
let mut scope = scope.clone();
for (idx, arg) in self.args.0.iter().enumerate() {
if arg.is_variadic {
let span = args.span();
self.scope.insert_var(
let arg_list = Value::ArgList(args.get_variadic(&mut scope, super_selector)?);
scope.insert_var(
&arg.name,
Spanned {
node: Value::ArgList(args.get_variadic(scope, super_selector)?),
node: arg_list,
span,
},
)?;
break;
}
let val = match args.get_positional(idx, scope, super_selector) {
let val = match args.get_positional(idx, &mut scope, super_selector) {
Some(v) => v?,
None => match args.get_named(arg.name.clone(), scope, super_selector) {
None => match args.get_named(arg.name.clone(), &mut scope, super_selector) {
Some(v) => v?,
None => match &arg.default {
Some(v) => Value::from_tokens(
&mut v.iter().cloned().peekmore(),
scope,
&mut scope,
super_selector,
)?,
None => {
@ -98,8 +100,9 @@ impl Function {
},
},
};
self.scope.insert_var(&arg.name, val)?;
scope.insert_var(&arg.name, val)?;
}
self.scope.extend(scope);
Ok(())
}

View File

@ -261,7 +261,7 @@ impl StyleSheet {
/// ```
#[inline]
#[cfg(not(feature = "wasm"))]
pub fn from_path<P: AsRef<Path> + Into<String> + Clone>(p: &P) -> SassResult<String> {
pub fn from_path(p: &str) -> SassResult<String> {
let mut map = CodeMap::new();
let file = map.add_file(p.clone().into(), String::from_utf8(fs::read(p.clone())?)?);
Css::from_stylesheet(StyleSheet(

View File

@ -40,3 +40,8 @@ test!(
"@function foo($arg1: bar()) {\n @return true;\n}\n\na {\n color: foo();\n}\n",
"a {\n color: true;\n}\n"
);
test!(
self_referential_default_arg_value,
"@function foo($a, $b: $a) {\n @return $b;\n}\n\na {\n color: foo(2);\n}\n",
"a {\n color: 2;\n}\n"
);