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

Add JSON pre-processor #17125

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure classes between `>` and `<` are properly extracted ([#17094](https://github.com/tailwindlabs/tailwindcss/pull/17094))
- Treat starting single quote as verbatim text in Slim ([#17085](https://github.com/tailwindlabs/tailwindcss/pull/17085))
- Ensure `.node` and `.wasm` files are not scanned for utilities ([#17123](https://github.com/tailwindlabs/tailwindcss/pull/17123))
- Improve performance when scanning `JSON` files ([#17125](https://github.com/tailwindlabs/tailwindcss/pull/17125))

## [4.0.12] - 2025-03-07

Expand Down
63 changes: 63 additions & 0 deletions crates/oxide/src/extractor/pre_processors/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::cursor;
use crate::extractor::pre_processors::pre_processor::PreProcessor;

#[derive(Debug, Default)]
pub struct Json;

impl PreProcessor for Json {
fn process(&self, content: &[u8]) -> Vec<u8> {
let len = content.len();
let mut result = content.to_vec();
let mut cursor = cursor::Cursor::new(content);

while cursor.pos < len {
match cursor.curr {
// Consume strings as-is
b'"' => {
cursor.advance();

while cursor.pos < len {
match cursor.curr {
// Escaped character, skip ahead to the next character
b'\\' => cursor.advance_twice(),

// End of the string
b'"' => break,

// Everything else is valid
_ => cursor.advance(),
};
}
}

// Replace brackets and curlies with spaces
b'[' | b'{' | b']' | b'}' => {
result[cursor.pos] = b' ';
}

// Consume everything else
_ => {}
};

cursor.advance();
}

result
}
}

#[cfg(test)]
mod tests {
use super::Json;
use crate::extractor::pre_processors::pre_processor::PreProcessor;

#[test]
fn test_json_pre_processor() {
let (input, expected) = (
r#"[1,[2,[3,4,["flex flex-1 content-['hello_world']"]]], {"flex": true}]"#,
r#" 1, 2, 3,4, "flex flex-1 content-['hello_world']" , "flex": true "#,
);

Json::test(input, expected);
}
}
2 changes: 2 additions & 0 deletions crates/oxide/src/extractor/pre_processors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod haml;
pub mod json;
pub mod pre_processor;
pub mod pug;
pub mod razor;
Expand All @@ -7,6 +8,7 @@ pub mod slim;
pub mod svelte;

pub use haml::*;
pub use json::*;
pub use pre_processor::*;
pub use pug::*;
pub use razor::*;
Expand Down
1 change: 1 addition & 0 deletions crates/oxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ pub fn pre_process_input(content: &[u8], extension: &str) -> Vec<u8> {
match extension {
"cshtml" | "razor" => Razor.process(content),
"haml" => Haml.process(content),
"json" => Json.process(content),
"pug" => Pug.process(content),
"rb" | "erb" => Ruby.process(content),
"slim" => Slim.process(content),
Expand Down