implement idempotency with regard to module aliasing

This commit is contained in:
Connor Skees 2020-08-06 04:00:45 -04:00
parent de9571b3fe
commit 94becb4dcb
3 changed files with 45 additions and 2 deletions

View File

@ -28,8 +28,18 @@ pub(crate) struct Module(pub Scope);
pub(crate) struct Modules(BTreeMap<Identifier, Module>); pub(crate) struct Modules(BTreeMap<Identifier, Module>);
impl Modules { impl Modules {
pub fn insert(&mut self, name: Identifier, module: Module) { pub fn insert(&mut self, name: Identifier, module: Module, span: Span) -> SassResult<()> {
if self.0.contains_key(&name) {
return Err((
format!("There's already a module with namespace \"{}\".", name),
span,
)
.into());
}
self.0.insert(name, module); self.0.insert(name, module);
Ok(())
} }
pub fn get(&self, name: Identifier, span: Span) -> SassResult<&Module> { pub fn get(&self, name: Identifier, span: Span) -> SassResult<&Module> {

View File

@ -263,7 +263,7 @@ impl<'a> Parser<'a> {
}, },
}; };
self.modules.insert(module_name.into(), module); self.modules.insert(module_name.into(), module, span)?;
} }
Some(Token { kind: '/', .. }) => { Some(Token { kind: '/', .. }) => {
self.toks.next(); self.toks.next();

View File

@ -158,3 +158,36 @@ fn use_user_defined_function() {
&grass::from_string(input.to_string(), &grass::Options::default()).expect(input) &grass::from_string(input.to_string(), &grass::Options::default()).expect(input)
); );
} }
#[test]
fn use_idempotent_no_alias() {
let input = "@use \"use_idempotent_no_alias\";\n@use \"use_idempotent_no_alias\";\n";
tempfile!("use_idempotent_no_alias.scss", "");
assert_err!(
"Error: There's already a module with namespace \"use-idempotent-no-alias\".",
input
);
}
#[test]
fn use_idempotent_with_alias() {
let input = "@use \"use_idempotent_with_alias__a\" as foo;\n@use \"use_idempotent_with_alias__b\" as foo;\n";
tempfile!("use_idempotent_with_alias__a.scss", "");
tempfile!("use_idempotent_with_alias__b.scss", "");
assert_err!(
"Error: There's already a module with namespace \"foo\".",
input
);
}
#[test]
fn use_idempotent_builtin() {
let input = "@use \"sass:math\";\n@use \"sass:math\";\n";
assert_err!(
"Error: There's already a module with namespace \"math\".",
input
);
}