Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed May 15, 2024
1 parent 0c84b46 commit e38e6bd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 86 deletions.
10 changes: 2 additions & 8 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,7 @@ fn compile<'i>(
} else {
Default::default()
},
dashed_idents: c.dashed_idents.unwrap_or_default(),
animation: c.animation.unwrap_or_default(),
grid: c.grid.unwrap_or_default(),
custom_idents: c.custom_idents.unwrap_or_default(),
..Default::default()
}),
}
} else {
Expand Down Expand Up @@ -845,10 +842,7 @@ fn compile_bundle<
} else {
Default::default()
},
dashed_idents: c.dashed_idents.unwrap_or_default(),
animation: c.animation.unwrap_or(true),
grid: c.grid.unwrap_or(true),
custom_idents: c.custom_idents.unwrap_or(true),
..Default::default()
}),
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23090,8 +23090,7 @@ mod tests {
"#},
map! {
"foo" => "EgL3uq_foo",
"id" => "EgL3uq_id",
"test" => "EgL3uq_test" referenced: true
"id" => "EgL3uq_id"
},
HashMap::new(),
crate::css_modules::Config {
Expand Down
51 changes: 20 additions & 31 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,48 +288,37 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> {
)?;

css_module.add_local(&ident, &ident, self.loc.source_index);
} else {
serialize_identifier(ident, self)?
return Ok(());
}
} else {
serialize_identifier(ident, self)?
}

serialize_identifier(ident, self)?;
Ok(())
}

pub(crate) fn write_dashed_ident(
&mut self,
ident: &str,
is_declaration: bool,
handle_css_module: bool,
) -> Result<(), PrinterError> {
pub(crate) fn write_dashed_ident(&mut self, ident: &str, is_declaration: bool) -> Result<(), PrinterError> {
self.write_str("--")?;

if handle_css_module {
match &mut self.css_module {
Some(css_module) if css_module.config.dashed_idents => {
let dest = &mut self.dest;
css_module.config.pattern.write(
&css_module.hashes[self.loc.source_index as usize],
&css_module.sources[self.loc.source_index as usize],
&ident[2..],
|s| {
self.col += s.len() as u32;
serialize_name(s, dest)
},
)?;
match &mut self.css_module {
Some(css_module) if css_module.config.dashed_idents => {
let dest = &mut self.dest;
css_module.config.pattern.write(
&css_module.hashes[self.loc.source_index as usize],
&css_module.sources[self.loc.source_index as usize],
&ident[2..],
|s| {
self.col += s.len() as u32;
serialize_name(s, dest)
},
)?;

if is_declaration {
css_module.add_dashed(ident, self.loc.source_index);
}
}
_ => {
serialize_name(&ident[2..], self)?;
if is_declaration {
css_module.add_dashed(ident, self.loc.source_index);
}
}
} else {
serialize_name(&ident[2..], self)?;
_ => {
serialize_name(&ident[2..], self)?;
}
}

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions src/properties/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ impl<'i> ToCss for AnimationName<'i> {
W: std::fmt::Write,
{
let css_module_animation_enabled =
dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation);
dest.css_module.as_ref().map_or(false, |css_module| css_module.config.animation);

match self {
AnimationName::None => dest.write_str("none"),
AnimationName::Ident(s) => {
if let Some(css_module) = &mut dest.css_module {
css_module.reference(&s.0, dest.loc.source_index)
if css_module_animation_enabled {
if let Some(css_module) = &mut dest.css_module {
css_module.reference(&s.0, dest.loc.source_index)
}
}
s.to_css_with_options(dest, css_module_animation_enabled)
}
Expand Down
24 changes: 11 additions & 13 deletions src/properties/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,19 @@ fn write_ident<W>(name: &str, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
let css_module_grid_enabled = dest.css_module.as_mut().map_or(false, |css_module| css_module.config.grid);
let css_module_grid_enabled = dest.css_module.as_ref().map_or(false, |css_module| css_module.config.grid);
if css_module_grid_enabled {
if let Some(css_module) = &mut dest.css_module {
if css_module_grid_enabled {
if let Some(last) = css_module.config.pattern.segments.last() {
if !matches!(last, crate::css_modules::Segment::Local) {
return Err(Error {
kind: PrinterErrorKind::InvalidCssModulesPatternInGrid,
loc: Some(ErrorLocation {
filename: dest.filename().into(),
line: dest.loc.line,
column: dest.loc.column,
}),
});
}
if let Some(last) = css_module.config.pattern.segments.last() {
if !matches!(last, crate::css_modules::Segment::Local) {
return Err(Error {
kind: PrinterErrorKind::InvalidCssModulesPatternInGrid,
loc: Some(ErrorLocation {
filename: dest.filename().into(),
line: dest.loc.line,
column: dest.loc.column,
}),
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'i> ToCss for KeyframesName<'i> {
W: std::fmt::Write,
{
let css_module_animation_enabled =
dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation);
dest.css_module.as_ref().map_or(false, |css_module| css_module.config.animation);

match self {
KeyframesName::Ident(ident) => {
Expand Down
41 changes: 13 additions & 28 deletions src/values/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,20 @@ impl<'i> ToCss for CustomIdent<'i> {

impl<'i> CustomIdent<'i> {
/// Write the custom ident to CSS.
pub fn to_css_with_options<W>(
pub(crate) fn to_css_with_options<W>(
&self,
dest: &mut Printer<W>,
enabled_css_modules: bool,
) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
if enabled_css_modules {
let css_module_custom_idents_enabled = dest
let css_module_custom_idents_enabled = enabled_css_modules
&& dest
.css_module
.as_mut()
.map_or(false, |css_module| css_module.config.custom_idents);
dest.write_ident(&self.0, css_module_custom_idents_enabled)
} else {
dest.write_ident(&self.0, false)
}
dest.write_ident(&self.0, css_module_custom_idents_enabled)
}
}

Expand Down Expand Up @@ -107,12 +104,7 @@ impl<'i> ToCss for DashedIdent<'i> {
where
W: std::fmt::Write,
{
let css_module_custom_idents_enabled = dest
.css_module
.as_mut()
.map_or(false, |css_module| css_module.config.custom_idents);

dest.write_dashed_ident(&self.0, true, css_module_custom_idents_enabled)
dest.write_dashed_ident(&self.0, true)
}
}

Expand Down Expand Up @@ -179,25 +171,18 @@ impl<'i> ToCss for DashedIdentReference<'i> {
where
W: std::fmt::Write,
{
let css_module_custom_idents_enabled = dest
.css_module
.as_mut()
.map_or(false, |css_module| css_module.config.custom_idents);

if css_module_custom_idents_enabled {
match &mut dest.css_module {
Some(css_module) if css_module.config.dashed_idents => {
if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) {
dest.write_str("--")?;
serialize_name(&name, dest)?;
return Ok(());
}
match &mut dest.css_module {
Some(css_module) if css_module.config.dashed_idents => {
if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) {
dest.write_str("--")?;
serialize_name(&name, dest)?;
return Ok(());
}
_ => {}
}
_ => {}
}

dest.write_dashed_ident(&self.ident.0, false, css_module_custom_idents_enabled)
dest.write_dashed_ident(&self.ident.0, false)
}
}

Expand Down

0 comments on commit e38e6bd

Please sign in to comment.