Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css/modules): Preserve spans of CSS class names #7185

Merged
merged 9 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/swc_css_ast/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span};
use crate::Function;

#[ast_node("Ident")]
#[derive(Eq, Hash)]
#[derive(Eq, PartialOrd, Ord, Hash)]
pub struct Ident {
pub span: Span,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
Expand Down
52 changes: 35 additions & 17 deletions crates/swc_css_modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc_hash::FxHashMap;
use serde::Serialize;
use swc_atoms::{js_word, JsWord};
use swc_common::util::take::Take;
use swc_common::{util::take::Take, Span};
use swc_css_ast::{
ComplexSelector, ComplexSelectorChildren, ComponentValue, Declaration, DeclarationName,
Delimiter, DelimiterValue, FunctionName, Ident, KeyframesName, PseudoClassSelectorChildren,
Expand Down Expand Up @@ -33,14 +33,14 @@ pub trait TransformConfig {
pub enum CssClassName {
Local {
/// Tranformed css class name
name: JsWord,
name: Ident,
},
Global {
name: JsWord,
name: Ident,
},
Import {
/// The exported class name. This is the value specified by the user.
name: JsWord,
name: Ident,
/// The module specifier.
from: JsWord,
},
Expand Down Expand Up @@ -100,6 +100,7 @@ where
n.raw = None;

rename(
n.span,
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
Expand All @@ -111,6 +112,7 @@ where
n.raw = None;

rename(
n.span,
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
Expand Down Expand Up @@ -265,11 +267,9 @@ where
ComponentValue::Str(import_source),
) => {
for class_name in n.value.iter().take(n.value.len() - 2) {
if let ComponentValue::Ident(box Ident { value, .. }) =
class_name
{
if let ComponentValue::Ident(value) = class_name {
composes_for_current.push(CssClassName::Import {
name: value.clone(),
name: *value.clone(),
from: import_source.value.clone(),
});
}
Expand All @@ -288,11 +288,9 @@ where
}),
) => {
for class_name in n.value.iter().take(n.value.len() - 2) {
if let ComponentValue::Ident(box Ident { value, .. }) =
class_name
{
if let ComponentValue::Ident(value) = class_name {
composes_for_current.push(CssClassName::Global {
name: value.clone(),
name: *value.clone(),
});
}
}
Expand All @@ -303,10 +301,14 @@ where
}

for class_name in n.value.iter() {
if let ComponentValue::Ident(box Ident { value, .. }) = class_name {
if let ComponentValue::Ident(box Ident { span, value, .. }) = class_name {
if let Some(value) = self.data.orig_to_renamed.get(value) {
composes_for_current.push(CssClassName::Local {
name: value.clone(),
name: Ident {
span: *span,
value: value.clone(),
raw: None,
},
});
}
}
Expand All @@ -328,7 +330,9 @@ where

for v in &mut n.value {
match v {
ComponentValue::Ident(box Ident { value, raw, .. }) => {
ComponentValue::Ident(box Ident {
span, value, raw, ..
}) => {
if !can_change {
continue;
}
Expand Down Expand Up @@ -388,6 +392,7 @@ where
*raw = None;

rename(
*span,
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
Expand Down Expand Up @@ -438,10 +443,14 @@ where
}
js_word!("animation-name") => {
for v in &mut n.value {
if let ComponentValue::Ident(box Ident { value, raw, .. }) = v {
if let ComponentValue::Ident(box Ident {
span, value, raw, ..
}) = v
{
*raw = None;

rename(
*span,
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
Expand Down Expand Up @@ -565,6 +574,7 @@ where
}

fn rename<C>(
span: Span,
config: &mut C,
result: &mut TransformResult,
orig_to_renamed: &mut FxHashMap<JsWord, JsWord>,
Expand All @@ -586,7 +596,13 @@ fn rename<C>(
{
let e = result.renamed.entry(name.clone()).or_default();

let v = CssClassName::Local { name: new.clone() };
let v = CssClassName::Local {
name: Ident {
span,
value: new.clone(),
raw: None,
},
};
if !e.contains(&v) {
e.push(v);
}
Expand All @@ -609,6 +625,7 @@ fn process_local<C>(
sel.text.raw = None;

rename(
sel.span,
config,
result,
orig_to_renamed,
Expand All @@ -620,6 +637,7 @@ fn process_local<C>(
sel.text.raw = None;

rename(
sel.span,
config,
result,
orig_to_renamed,
Expand Down