This commit is contained in:
ConnorSkees 2020-05-24 17:41:24 -04:00
parent 21d830d6ff
commit ec83a9dff7
14 changed files with 81 additions and 96 deletions

View File

@ -121,7 +121,7 @@ impl Function {
self.args(args, scope, super_selector)?;
let stmts = self.eval_body(super_selector)?;
self.call(super_selector, stmts)?
.ok_or(("Function finished without @return.", self.pos).into())
.ok_or_else(|| ("Function finished without @return.", self.pos).into())
}
pub fn call(

View File

@ -80,10 +80,10 @@ fn change_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) ->
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Box::new(Color::from_rgba(
red.unwrap_or(color.red()),
green.unwrap_or(color.green()),
blue.unwrap_or(color.blue()),
alpha.unwrap_or(color.alpha()),
red.unwrap_or_else(|| color.red()),
green.unwrap_or_else(|| color.green()),
blue.unwrap_or_else(|| color.blue()),
alpha.unwrap_or_else(|| color.alpha()),
))));
}
@ -147,10 +147,10 @@ fn adjust_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) ->
if red.is_some() || green.is_some() || blue.is_some() {
return Ok(Value::Color(Box::new(Color::from_rgba(
color.red() + red.unwrap_or(Number::zero()),
color.green() + green.unwrap_or(Number::zero()),
color.blue() + blue.unwrap_or(Number::zero()),
color.alpha() + alpha.unwrap_or(Number::zero()),
color.red() + red.unwrap_or_else(Number::zero),
color.green() + green.unwrap_or_else(Number::zero),
color.blue() + blue.unwrap_or_else(Number::zero),
color.alpha() + alpha.unwrap_or_else(Number::zero),
))));
}
@ -189,10 +189,10 @@ fn adjust_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) ->
// Color::as_hsla() returns more exact values than Color::hue(), etc.
let (this_hue, this_saturation, this_luminance, this_alpha) = color.as_hsla();
return Ok(Value::Color(Box::new(Color::from_hsla(
this_hue + hue.unwrap_or(Number::zero()),
this_saturation + saturation.unwrap_or(Number::zero()),
this_luminance + luminance.unwrap_or(Number::zero()),
this_alpha + alpha.unwrap_or(Number::zero()),
this_hue + hue.unwrap_or_else(Number::zero),
this_saturation + saturation.unwrap_or_else(Number::zero),
this_luminance + luminance.unwrap_or_else(Number::zero),
this_alpha + alpha.unwrap_or_else(Number::zero),
))));
}
@ -266,22 +266,22 @@ fn scale_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) ->
return Ok(Value::Color(Box::new(Color::from_rgba(
scale(
color.red(),
red.unwrap_or(Number::zero()),
red.unwrap_or_else(Number::zero),
Number::from(255),
),
scale(
color.green(),
green.unwrap_or(Number::zero()),
green.unwrap_or_else(Number::zero),
Number::from(255),
),
scale(
color.blue(),
blue.unwrap_or(Number::zero()),
blue.unwrap_or_else(Number::zero),
Number::from(255),
),
scale(
color.alpha(),
alpha.unwrap_or(Number::zero()),
alpha.unwrap_or_else(Number::zero),
Number::one(),
),
))));
@ -313,15 +313,19 @@ fn scale_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) ->
scale(this_hue, Number::zero(), Number::from(360)),
scale(
this_saturation,
saturation.unwrap_or(Number::zero()),
saturation.unwrap_or_else(Number::zero),
Number::one(),
),
scale(
this_luminance,
luminance.unwrap_or(Number::zero()),
luminance.unwrap_or_else(Number::zero),
Number::one(),
),
scale(
this_alpha,
alpha.unwrap_or_else(Number::zero),
Number::one(),
),
scale(this_alpha, alpha.unwrap_or(Number::zero()), Number::one()),
))));
}

View File

@ -27,6 +27,7 @@ fn feature_exists(
) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "feature") {
#[allow(clippy::match_same_arms)]
Value::String(s, _) => Ok(match s.as_str() {
// A local variable will shadow a global variable unless
// `!global` is used.
@ -85,11 +86,11 @@ fn type_of(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> Sass
fn unitless(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(_, Unit::None) => Ok(Value::True),
Value::Dimension(_, _) => Ok(Value::False),
_ => Ok(Value::True),
}
Ok(match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(_, Unit::None) => Value::True,
Value::Dimension(_, _) => Value::False,
_ => Value::True,
})
}
fn inspect(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {

View File

@ -40,11 +40,10 @@ grass input.scss
clippy::implicit_return,
// Self { .. } is less explicit than Foo { .. }
clippy::use_self,
// this is way too pedantic -- some things don't need docs!
// this is too pedantic -- some things don't need docs!
clippy::missing_docs_in_private_items,
// unreachable!() has many valid use cases
clippy::unreachable,
// _ => {} has many valid use cases
// this disallows binding as well
clippy::wildcard_enum_match_arm,
// this is too pedantic -- we are allowed to add numbers!
clippy::integer_arithmetic,
@ -53,12 +52,15 @@ grass input.scss
clippy::missing_errors_doc,
// this incorrectly results in errors for types that derive `Debug`
// https://github.com/rust-lang/rust-clippy/issues/4980
clippy::let_underscore_must_use,
// clippy::let_underscore_must_use,
// this is too pedantic -- it results in some names being less explicit
// than they should
clippy::module_name_repetitions,
// this is too pedantic -- it is sometimes useful to break up `impl`s
clippy::multiple_inherent_impl,
// filter isn't fallible
clippy::filter_map,
clippy::else_if_without_else,
// temporarily allowed while under heavy development.
// eventually these allows should be refactored away
@ -72,8 +74,8 @@ grass input.scss
clippy::cast_possible_truncation,
clippy::single_match_else,
clippy::indexing_slicing,
clippy::match_same_arms,
clippy::or_fun_call,
// clippy::match_same_arms,
// clippy::or_fun_call,
clippy::redundant_pub_crate,
)]
#![cfg_attr(feature = "nightly", feature(track_caller))]
@ -350,18 +352,7 @@ pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
AtRule::Mixin(name, mixin) => Expr::MixinDecl(name, mixin),
AtRule::Function(name, func) => Expr::FunctionDecl(name, func),
AtRule::Charset => todo!("@charset as expr"),
d @ AtRule::Debug(..) => Expr::AtRule(d),
w @ AtRule::Warn(..) => Expr::AtRule(w),
a @ AtRule::Return(_) => Expr::AtRule(a),
c @ AtRule::Content => Expr::AtRule(c),
f @ AtRule::If(..) => Expr::AtRule(f),
f @ AtRule::For(..) => Expr::AtRule(f),
f @ AtRule::While(..) => Expr::AtRule(f),
f @ AtRule::Each(..) => Expr::AtRule(f),
u @ AtRule::Unknown(..) => Expr::AtRule(u),
u @ AtRule::AtRoot(..) => Expr::AtRule(u),
u @ AtRule::Include(..) => Expr::AtRule(u),
u @ AtRule::Media(..) => Expr::AtRule(u),
a => Expr::AtRule(a),
},
span,
}));

View File

@ -90,7 +90,7 @@ impl Scope {
}
pub fn get_var<T: Into<Identifier>>(&self, name: Spanned<T>) -> SassResult<Spanned<Value>> {
let name = name.map_node(|n| n.into());
let name = name.map_node(Into::into);
match self.vars.get(&name.node) {
Some(v) => Ok(v.clone()),
None => get_global_var(name),
@ -112,7 +112,7 @@ impl Scope {
}
pub fn get_mixin<T: Into<Identifier>>(&self, name: Spanned<T>) -> SassResult<Mixin> {
let name = name.map_node(|n| n.into());
let name = name.map_node(Into::into);
match self.mixins.get(&name.node) {
Some(v) => Ok(v.clone()),
None => get_global_mixin(name),
@ -129,7 +129,7 @@ impl Scope {
}
pub fn get_fn<T: Into<Identifier>>(&self, name: Spanned<T>) -> SassResult<Function> {
let name = name.map_node(|n| n.into());
let name = name.map_node(Into::into);
match self.functions.get(&name.node) {
Some(v) => Ok(v.clone()),
None => get_global_fn(name),

View File

@ -419,28 +419,29 @@ impl Selector {
} else {
todo!()
};
if is_selector_name_char(t.kind) {
let name = eat_ident_no_interpolation(toks, false, t.pos)?.node;
Ok(
if toks.peek().is_some() && toks.peek().unwrap().kind == '(' {
toks.next();
let mut inner_toks = read_until_closing_paren(toks)?;
inner_toks.pop();
let inner = Selector::from_tokens(
&mut inner_toks.into_iter().peekmore(),
scope,
super_selector,
)?;
SelectorKind::PseudoParen(name, inner)
} else if is_pseudo_element {
SelectorKind::PseudoElement(name)
} else {
SelectorKind::Pseudo(name)
},
)
} else {
if !is_selector_name_char(t.kind) {
return Err(("Expected identifier.", t.pos).into());
}
let name = eat_ident_no_interpolation(toks, false, t.pos)?.node;
Ok(
if toks.peek().is_some() && toks.peek().unwrap().kind == '(' {
toks.next();
let mut inner_toks = read_until_closing_paren(toks)?;
inner_toks.pop();
let inner = Selector::from_tokens(
&mut inner_toks.into_iter().peekmore(),
scope,
super_selector,
)?;
SelectorKind::PseudoParen(name, inner)
} else if is_pseudo_element {
SelectorKind::PseudoElement(name)
} else {
SelectorKind::Pseudo(name)
},
)
}
pub fn replace(super_selector: &Selector, this: Selector) -> Selector {

View File

@ -165,7 +165,6 @@ impl<'a> StyleParser<'a> {
let value = self.parse_style_value(toks, scope, tok.pos)?;
let t = toks.peek().ok_or(("expected more input.", value.span))?;
match t.kind {
'}' => {}
';' => {
toks.next();
devour_whitespace(toks);

View File

@ -182,6 +182,9 @@ impl<'a> StyleSheetParser<'a> {
| '?'
| '~'
| '|'
| '`'
| '\''
| '"'
| '\u{7f}'..=std::char::MAX => {
rules.extend(self.eat_rules(&Selector::new(), &mut Scope::new())?)
}
@ -336,8 +339,9 @@ impl<'a> StyleSheetParser<'a> {
)?);
}
AtRule::AtRoot(root_rules) => rules.extend(root_rules),
AtRule::Unknown(..) => rules.push(rule.map_node(Stmt::AtRule)),
AtRule::Media(..) => rules.push(rule.map_node(Stmt::AtRule)),
AtRule::Unknown(..) | AtRule::Media(..) => {
rules.push(rule.map_node(Stmt::AtRule))
}
}
}
}
@ -355,9 +359,6 @@ impl<'a> StyleSheetParser<'a> {
'{' | '!' => {
return Err(("expected \"}\".", self.lexer.next().unwrap().pos).into());
}
'`' | '\'' | '"' => {
return Err(("expected selector.", self.lexer.next().unwrap().pos).into());
}
'}' => return Err(("unmatched \"}\".", self.lexer.next().unwrap().pos).into()),
};
}
@ -400,11 +401,7 @@ impl<'a> StyleSheetParser<'a> {
AtRule::Warn(ref message) => self.warn(expr.span, message),
AtRule::Mixin(..) | AtRule::Function(..) => todo!(),
AtRule::Charset => todo!(),
r @ AtRule::Unknown(..) => stmts.push(Spanned {
node: Stmt::AtRule(r),
span,
}),
r @ AtRule::Media(..) => stmts.push(Spanned {
r @ AtRule::Unknown(..) | r @ AtRule::Media(..) => stmts.push(Spanned {
node: Stmt::AtRule(r),
span,
}),

View File

@ -83,6 +83,7 @@ pub(crate) fn eat_number<I: Iterator<Item = Token>>(
let mut times_ten = String::new();
let mut times_ten_is_postive = true;
#[allow(clippy::never_loop)]
loop {
// TODO: https://github.com/rust-lang/rust/issues/54883
if let Some(Token { kind: 'e', .. }) | Some(Token { kind: 'E', .. }) = toks.peek() {

View File

@ -135,14 +135,7 @@ impl Value {
}
_ => Cow::Owned(format!("{}{}", num, unit)),
},
Self::Map(..) => {
return Err((
format!("{} isn't a valid CSS value.", self.inspect(span)?),
span,
)
.into())
}
Self::Function(..) => {
Self::Map(..) | Self::Function(..) => {
return Err((
format!("{} isn't a valid CSS value.", self.inspect(span)?),
span,

View File

@ -76,7 +76,7 @@ impl Add<i32> for Integer {
type Output = Self;
fn add(self, rhs: i32) -> Self::Output {
match self {
Self::Machine(v) => match v.checked_add(rhs as i64) {
Self::Machine(v) => match v.checked_add(i64::from(rhs)) {
Some(v) => Self::Machine(v),
None => Self::Big(BigInt::from(v) + rhs),
},

View File

@ -125,10 +125,9 @@ impl Value {
Op::Mul => lhs.mul(*rhs, span)?,
Op::Div => lhs.div(*rhs, span)?,
Op::Rem => lhs.rem(*rhs, span)?,
Op::GreaterThan => return lhs.cmp(*rhs, op, span),
Op::GreaterThanEqual => return lhs.cmp(*rhs, op, span),
Op::LessThan => return lhs.cmp(*rhs, op, span),
Op::LessThanEqual => return lhs.cmp(*rhs, op, span),
Op::GreaterThan | Op::GreaterThanEqual | Op::LessThan | Op::LessThanEqual => {
return lhs.cmp(*rhs, op, span)
}
Op::Not => unreachable!(),
Op::And => {
if lhs.is_true(span)? {

View File

@ -163,7 +163,7 @@ enum IntermediateValue {
}
impl IntermediateValue {
fn span(self, span: Span) -> Spanned<Self> {
const fn span(self, span: Span) -> Spanned<Self> {
Spanned { node: self, span }
}
}
@ -655,7 +655,7 @@ impl Value {
.span(span));
}
if let Some(c) = NAMED_COLORS.get_by_name(&lower.as_str()) {
if let Some(c) = NAMED_COLORS.get_by_name(lower.as_str()) {
return Ok(IntermediateValue::Value(Value::Color(Box::new(Color::new(
c[0], c[1], c[2], c[3], s,
))))
@ -791,7 +791,7 @@ impl Value {
let mut span = toks.next().unwrap().pos();
let mut inner = match read_until_closing_paren(toks) {
Ok(v) => v,
Err(e) => return Some(Err(e.into())),
Err(e) => return Some(Err(e)),
};
// todo: the above shouldn't eat the closing paren
if let Some(last_tok) = inner.pop() {
@ -833,7 +833,7 @@ impl Value {
let mut span = toks.next().unwrap().pos();
let mut inner = match read_until_closing_square_brace(toks) {
Ok(v) => v,
Err(e) => return Some(Err(e.into())),
Err(e) => return Some(Err(e)),
};
if let Some(last_tok) = inner.pop() {
if last_tok.kind != ']' {

View File

@ -38,8 +38,7 @@ impl SassFunction {
/// Used mainly in debugging and `inspect()`
pub fn name(&self) -> &Identifier {
match self {
Self::Builtin(_, name) => name,
Self::UserDefined(_, name) => name,
Self::Builtin(_, name) | Self::UserDefined(_, name) => name,
}
}