Skip to content

Commit

Permalink
Handle @use module as *.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Dec 1, 2020
1 parent a676113 commit 7b3f34f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 49 deletions.
35 changes: 27 additions & 8 deletions src/output/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,21 +800,40 @@ impl Format {
}
}

use crate::sass::UseAs;
fn do_use(
scope: &mut dyn Scope,
name: &SassString,
as_name: &Option<SassString>,
as_name: &UseAs,
) -> Result<(), Error> {
let (name, _q) = name.evaluate(scope)?;
let as_name = if let Some(as_name) = as_name {
Some(as_name.evaluate(scope)?.0)
use crate::functions::get_global_module;
let name = name.evaluate(scope)?.0;
if let Some(module) = get_global_module(&name) {
match as_name {
UseAs::KeepName => {
let name = name
.rfind(':')
.map(|i| &name[i + 1..])
.unwrap_or(&name);
scope.define_module(name, module);
}
UseAs::Star => {
for (name, function) in module {
scope.define_function(name, function.clone());
}
}
UseAs::Name(name) => {
let name = name.evaluate(scope)?.0;
scope.define_module(&name, module);
}
}
Ok(())
} else {
None
};
scope.do_use(&name, as_name.as_ref().map(|n| n.as_ref()));
Ok(())
Err(Error::S(format!("Module {:?} not found", name).into()))
}
}


struct CssWriter {
imports: Vec<u8>,
contents: Vec<u8>,
Expand Down
9 changes: 6 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use self::value::{
use crate::functions::SassFunction;
#[cfg(test)]
use crate::sass::{CallArgs, FormalArgs};
use crate::sass::{Item, Value};
use crate::sass::{Item, UseAs, Value};
use crate::selectors::Selectors;
use crate::value::ListSeparator;
#[cfg(test)]
Expand Down Expand Up @@ -306,13 +306,16 @@ fn use2(input: Span) -> IResult<Span, Item> {
),
opt(delimited(
terminated(tag("as"), opt_spacelike),
sass_string,
alt((
map(sass_string, UseAs::Name),
value(UseAs::Star, tag("*")),
)),
opt_spacelike,
)),
),
tag(";"),
),
|(s, n)| Item::Use(s, n),
|(s, n)| Item::Use(s, n.unwrap_or(UseAs::KeepName)),
)(input)
}

Expand Down
9 changes: 8 additions & 1 deletion src/sass/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ pub enum Item {
},
While(Value, Vec<Item>),

Use(SassString, Option<SassString>),
Use(SassString, UseAs),
Rule(Selectors, Vec<Item>),
NamespaceRule(SassString, Value, Vec<Item>),
Property(SassString, Value),
Comment(SassString),
Warn(Value),
None,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
pub enum UseAs {
KeepName,
Star,
Name(SassString),
}
2 changes: 1 addition & 1 deletion src/sass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ mod value;

pub use self::call_args::CallArgs;
pub use self::formal_args::FormalArgs;
pub use self::item::Item;
pub use self::item::{Item, UseAs};
pub use self::string::{SassString, StringPart};
pub use self::value::Value;
4 changes: 2 additions & 2 deletions src/sass/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ impl From<&str> for StringPart {
}
}

impl<'a> From<&'a str> for SassString {
fn from(s: &'a str) -> Self {
impl From<&str> for SassString {
fn from(s: &str) -> Self {
SassString {
parts: vec![StringPart::from(s)],
quotes: Quotes::None,
Expand Down
11 changes: 0 additions & 11 deletions src/variablescope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,6 @@ pub trait Scope {
}
Ok(None)
}
fn do_use(&mut self, name: &str, as_name: Option<&str>) {
use crate::functions::get_global_module;
let as_name = as_name
.or_else(|| name.rfind(':').map(|i| &name[i + 1..]))
.unwrap_or(name);
if let Some(module) = get_global_module(name) {
self.define_module(as_name, module);
} else {
eprintln!("WARNING: module {:?} not found", name);
}
}
fn define_module(&self, name: &str, module: &'static Module);
fn get_module(&self, name: &str) -> Option<&Module>;
fn get_selectors(&self) -> &Selectors;
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/core_functions/meta/load_css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ mod twice {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn different_extend() {
assert_eq!(
rsass(
Expand Down
34 changes: 17 additions & 17 deletions tests/spec/core_functions/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ mod global_variable_exists {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn chosen_prefix() {
assert_eq!(
rsass(
Expand All @@ -787,7 +787,7 @@ mod global_variable_exists {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn defined() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -947,7 +947,7 @@ mod global_variable_exists {
}
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn named() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2271,7 +2271,7 @@ mod mixin_exists {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn chosen_prefix() {
assert_eq!(
rsass(
Expand All @@ -2287,7 +2287,7 @@ mod mixin_exists {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn defined() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2447,7 +2447,7 @@ mod mixin_exists {
}
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn named() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2606,7 +2606,7 @@ mod module_functions {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn empty() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2796,7 +2796,7 @@ mod module_variables {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn test_as() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2831,7 +2831,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn dash_sensitive() {
assert_eq!(
rsass(
Expand All @@ -2849,7 +2849,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn empty() {
assert_eq!(
rsass(
Expand Down Expand Up @@ -2885,7 +2885,7 @@ mod module_variables {
// Ignoring "test_type", error tests are not supported yet.
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn multiple() {
assert_eq!(
rsass(
Expand All @@ -2903,7 +2903,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn named() {
assert_eq!(
rsass(
Expand All @@ -2924,7 +2924,7 @@ mod module_variables {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn test_as() {
assert_eq!(
rsass(
Expand All @@ -2942,7 +2942,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn bare() {
assert_eq!(
rsass(
Expand All @@ -2960,7 +2960,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn hide() {
assert_eq!(
rsass(
Expand All @@ -2978,7 +2978,7 @@ mod module_variables {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn show() {
assert_eq!(
rsass(
Expand All @@ -2997,7 +2997,7 @@ mod module_variables {
}
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn through_import() {
assert_eq!(
rsass(
Expand Down
9 changes: 4 additions & 5 deletions tests/spec/core_functions/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ mod general {
#[allow(unused)]
use super::rsass;
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn test_as() {
assert_eq!(
rsass(
Expand All @@ -374,7 +374,7 @@ mod general {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn bare() {
assert_eq!(
rsass(
Expand All @@ -398,7 +398,7 @@ mod general {
// Ignoring "show", error tests are not supported yet.
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn hide() {
assert_eq!(
rsass(
Expand All @@ -414,7 +414,7 @@ mod general {
);
}
#[test]
#[ignore] // wrong result
#[ignore] // unexepected error
fn show() {
assert_eq!(
rsass(
Expand All @@ -431,7 +431,6 @@ mod general {
}
}
#[test]
#[ignore] // unexepected error
fn global() {
assert_eq!(
rsass(
Expand Down

0 comments on commit 7b3f34f

Please sign in to comment.