Skip to content

Commit

Permalink
feat: add strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 3, 2022
1 parent 27e71c9 commit e85d0d1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ opt-level = "s"
itertools = "0.10.3"
nom = "7.1.1"
nom_locate = "4.0.0"
once_cell = "1.15.0"
serde = "1"
serde_json = "1.0.83"
serde_path_to_error = "0.1.8"
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use serde::Deserialize;
pub struct AppConfig<'a> {
#[serde(borrow)]
pub config: TailwindConfig<'a>,

/// Strict mode throws an error when an unknown class is used.
#[serde(default)]
pub strict: bool,
}

#[derive(Deserialize, Debug)]
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod util;

use config::TailwindConfig;
use nom_locate::LocatedSpan;
use once_cell::sync::OnceCell;
use swc_core::{
common::{util::take::Take, Span, DUMMY_SP},
ecma::{
Expand Down Expand Up @@ -45,6 +46,8 @@ impl<'a> TransformVisitor<'a> {
}
}

static STRICT: OnceCell<bool> = OnceCell::new();

/**
* Implement necessary visit_mut_* methods for actual custom transform.
* A comprehensive list of possible visitor methods can be found here:
Expand Down Expand Up @@ -256,6 +259,10 @@ pub fn process_transform(program: Program, metadata: TransformPluginProgramMetad
let deser = &mut serde_json::Deserializer::from_str(&config);
let config: Result<AppConfig, _> = serde_path_to_error::deserialize(deser);

if let Ok(c) = &config {
STRICT.set(c.strict).expect("failed to set strict mode");
}

match config {
Ok(config) => program.fold_with(&mut as_folder(TransformVisitor::new(config.config))),
Err(error) => {
Expand Down
23 changes: 18 additions & 5 deletions src/parse/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use swc_core::plugin::errors::HANDLER;
use crate::config::Screens;
use crate::config::TailwindConfig;
use crate::util::merge_literals;
use crate::STRICT;

use super::literal::parse_literal;
use super::nom::Directive;
Expand All @@ -38,13 +39,20 @@ pub fn literal_from_exp<'a>(span: Span, val: Expression<'a>, config: &TailwindCo
let mut object: ObjectLit = match literal_from_subject(span, val.subject, config) {
Ok(object) => object,
Err(text) => {
HANDLER.with(|handler| {
handler
.struct_span_err(
HANDLER.with(|h| {
if STRICT.get().copied().unwrap_or_default() {
h.struct_span_err(
val.span.unwrap_or(span),
&format!("unknown subject `{}`", text),
)
.emit()
} else {
h.struct_span_warn(
val.span.unwrap_or(span),
&format!("unknown subject `{}`", text),
)
.emit()
}
});
return ObjectLit {
span: DUMMY_SP,
Expand Down Expand Up @@ -147,8 +155,13 @@ pub fn literal_from_exp<'a>(span: Span, val: Expression<'a>, config: &TailwindCo
"group-hover" => ".group:hover &",
x => {
HANDLER.with(|h| {
h.struct_span_err(span, &format!("unknown modifier `{}`", x))
.emit()
if STRICT.get().copied().unwrap_or_default() {
h.struct_span_err(span, &format!("unknown modifier `{}`", x))
.emit()
} else {
h.struct_span_warn(span, &format!("unknown modifier `{}`", x))
.emit()
}
});
continue;
}
Expand Down

0 comments on commit e85d0d1

Please sign in to comment.