use Identifier for FuncArg

This commit is contained in:
ConnorSkees 2020-05-22 22:43:26 -04:00
parent 7b8b5c233c
commit 33c5acc35f
4 changed files with 25 additions and 16 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::mem;
use codemap::{Span, Spanned}; use codemap::{Span, Spanned};
@ -20,7 +21,7 @@ pub(crate) struct FuncArgs(pub Vec<FuncArg>);
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct FuncArg { pub(crate) struct FuncArg {
pub name: String, pub name: Identifier,
pub default: Option<Vec<Token>>, pub default: Option<Vec<Token>>,
pub is_variadic: bool, pub is_variadic: bool,
} }
@ -128,10 +129,10 @@ impl CallArgs {
} }
} }
pub fn get( pub fn get<T: Into<Identifier>>(
&mut self, &mut self,
position: usize, position: usize,
name: String, name: T,
scope: &Scope, scope: &Scope,
super_selector: &Selector, super_selector: &Selector,
) -> Option<SassResult<Spanned<Value>>> { ) -> Option<SassResult<Spanned<Value>>> {
@ -244,7 +245,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
',' => { ',' => {
toks.next(); toks.next();
args.push(FuncArg { args.push(FuncArg {
name: name.replace('_', "-"), name: name.node.into(),
default: Some(default), default: Some(default),
is_variadic, is_variadic,
}); });
@ -252,7 +253,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
} }
')' => { ')' => {
args.push(FuncArg { args.push(FuncArg {
name: name.replace('_', "-"), name: name.node.into(),
default: Some(default), default: Some(default),
is_variadic, is_variadic,
}); });
@ -285,7 +286,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
is_variadic = true; is_variadic = true;
args.push(FuncArg { args.push(FuncArg {
name: name.replace('_', "-"), name: name.node.into(),
default: Some(default), default: Some(default),
is_variadic, is_variadic,
}); });
@ -294,7 +295,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
')' => { ')' => {
close_paren_span = span; close_paren_span = span;
args.push(FuncArg { args.push(FuncArg {
name: name.replace('_', "-"), name: name.node.into(),
default: if default.is_empty() { default: if default.is_empty() {
None None
} else { } else {
@ -305,7 +306,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
break; break;
} }
',' => args.push(FuncArg { ',' => args.push(FuncArg {
name: name.replace('_', "-"), name: name.node.into(),
default: None, default: None,
is_variadic, is_variadic,
}), }),
@ -399,9 +400,8 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
} else { } else {
CallArg::Named(name.as_str().into()) CallArg::Named(name.as_str().into())
}, },
val.clone(), mem::take(&mut val),
); );
val.clear();
devour_whitespace(toks); devour_whitespace(toks);
if toks.peek().is_none() { if toks.peek().is_none() {

View File

@ -1,3 +1,5 @@
use std::mem;
use super::eat_stmts; use super::eat_stmts;
use codemap::{Span, Spanned}; use codemap::{Span, Spanned};
@ -75,7 +77,7 @@ impl Function {
let span = args.span(); let span = args.span();
let arg_list = Value::ArgList(args.get_variadic(&scope, super_selector)?); let arg_list = Value::ArgList(args.get_variadic(&scope, super_selector)?);
self.scope.insert_var( self.scope.insert_var(
&arg.name, arg.name.clone(),
Spanned { Spanned {
node: arg_list, node: arg_list,
span, span,
@ -87,7 +89,7 @@ impl Function {
Some(v) => v?, Some(v) => v?,
None => match arg.default.as_mut() { None => match arg.default.as_mut() {
Some(v) => Value::from_tokens( Some(v) => Value::from_tokens(
&mut std::mem::take(v).into_iter().peekmore(), &mut mem::take(v).into_iter().peekmore(),
&scope, &scope,
super_selector, super_selector,
)?, )?,
@ -98,8 +100,8 @@ impl Function {
} }
}, },
}; };
scope.insert_var(&arg.name, val.clone())?; scope.insert_var(arg.name.clone(), val.clone())?;
self.scope.insert_var(&arg.name, val)?; self.scope.insert_var(mem::take(&mut arg.name), val)?;
} }
Ok(()) Ok(())
} }

View File

@ -1,3 +1,4 @@
use std::mem;
use std::vec::IntoIter; use std::vec::IntoIter;
use codemap::{Span, Spanned}; use codemap::{Span, Spanned};
@ -67,7 +68,7 @@ impl Mixin {
if arg.is_variadic { if arg.is_variadic {
let span = args.span(); let span = args.span();
self.scope.insert_var( self.scope.insert_var(
&arg.name, mem::take(&mut arg.name),
Spanned { Spanned {
node: Value::ArgList(args.get_variadic(scope, super_selector)?), node: Value::ArgList(args.get_variadic(scope, super_selector)?),
span, span,
@ -90,7 +91,7 @@ impl Mixin {
} }
}, },
}; };
self.scope.insert_var(&arg.name, val)?; self.scope.insert_var(mem::take(&mut arg.name), val)?;
} }
Ok(self) Ok(self)
} }

View File

@ -146,3 +146,9 @@ impl Display for Identifier {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
impl Default for Identifier {
fn default() -> Self {
Self(String::new())
}
}