Skip to content

Commit

Permalink
Fix the issue on the Rust side
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Oct 8, 2024
1 parent cd8a8f6 commit 43fe42b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
29 changes: 22 additions & 7 deletions crates/oxide/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,13 +861,17 @@ impl<'a> Extractor<'a> {
ParseAction::SingleCandidate(candidate)
}
Bracketing::Included(sliceable) | Bracketing::Wrapped(sliceable) => {
let parts = vec![candidate, sliceable];
let parts = parts
.into_iter()
.filter(|v| !v.is_empty())
.collect::<Vec<_>>();
if candidate == sliceable {
ParseAction::SingleCandidate(candidate)
} else {
let parts = vec![candidate, sliceable];
let parts = parts
.into_iter()
.filter(|v| !v.is_empty())
.collect::<Vec<_>>();

ParseAction::MultipleCandidates(parts)
ParseAction::MultipleCandidates(parts)
}
}
}
}
Expand Down Expand Up @@ -1185,7 +1189,7 @@ mod test {
fn bad_003() {
// TODO: This seems… wrong
let candidates = run(r"[𕤵:]", false);
assert_eq!(candidates, vec!["𕤵", "𕤵:"]);
assert_eq!(candidates, vec!["𕤵", "𕤵:",]);
}

#[test]
Expand Down Expand Up @@ -1436,4 +1440,15 @@ mod test {
.unwrap();
assert_eq!(result, Some("[.foo_&]:px-[0]"));
}

#[test]
fn does_not_emit_the_same_slice_multiple_times() {
let candidates: Vec<_> =
Extractor::with_positions("<div class=\"flex\"></div>".as_bytes(), Default::default())
.into_iter()
.map(|(s, p)| unsafe { (std::str::from_utf8_unchecked(s), p) })
.collect();

assert_eq!(candidates, vec![("div", 1), ("class", 5), ("flex", 12),]);
}
}
17 changes: 4 additions & 13 deletions packages/@tailwindcss-upgrade/src/template/candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@ import type { DesignSystem } from '../../../tailwindcss/src/design-system'

export async function extractRawCandidates(
content: string,
extension: string,
): Promise<{ rawCandidate: string; start: number; end: number }[]> {
let scanner = new Scanner({})
let result = scanner.getCandidatesWithPositions({ content, extension })

// Create a map to remove duplicates
let candidatesMap = new Map<string, { rawCandidate: string; start: number; end: number }>()
let result = scanner.getCandidatesWithPositions({ content, extension: 'html' })

let candidates: { rawCandidate: string; start: number; end: number }[] = []
for (let { candidate: rawCandidate, position: start } of result) {
let end = start + rawCandidate.length
candidatesMap.set(`${start}:${end}:${rawCandidate}`, {
rawCandidate,
start,
end,
})
candidates.push({ rawCandidate, start, end: start + rawCandidate.length })
}

return [...candidatesMap.values()]
return candidates
}

export function printCandidate(designSystem: DesignSystem, candidate: Candidate) {
Expand Down

0 comments on commit 43fe42b

Please sign in to comment.