-
Notifications
You must be signed in to change notification settings - Fork 191
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
Skip generating unnecessary @supports
#878
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
#![allow(missing_docs)] | ||
|
||
use std::borrow::Borrow; | ||
|
||
use crate::vendor_prefix::VendorPrefix; | ||
use bitflags::bitflags; | ||
#[cfg(any(feature = "serde", feature = "nodejs"))] | ||
|
@@ -136,7 +138,7 @@ fn parse_version(version: &str) -> Option<u32> { | |
|
||
bitflags! { | ||
/// Features to explicitly enable or disable. | ||
#[derive(Debug, Default, Clone, Copy)] | ||
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq)] | ||
pub struct Features: u32 { | ||
const Nesting = 1 << 0; | ||
const NotSelectorList = 1 << 1; | ||
|
@@ -165,6 +167,18 @@ bitflags! { | |
} | ||
} | ||
|
||
pub(crate) trait FeaturesIterator: Sized + Iterator { | ||
fn try_union_all<T>(&mut self) -> Option<Features> | ||
where | ||
Self: Iterator<Item = Option<T>>, | ||
T: Borrow<Features>, | ||
{ | ||
self.try_fold(Features::empty(), |a, b| b.map(|b| a | *b.borrow())) | ||
} | ||
} | ||
|
||
impl<I> FeaturesIterator for I where I: Iterator {} | ||
|
||
/// Target browsers and features to compile. | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct Targets { | ||
|
@@ -225,6 +239,67 @@ impl Targets { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct TargetsWithSupportsScope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this just be part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean putting the fields in |
||
stack: Vec<Features>, | ||
pub(crate) current: Targets, | ||
} | ||
|
||
impl TargetsWithSupportsScope { | ||
pub fn new(targets: Targets) -> Self { | ||
Self { | ||
stack: Vec::new(), | ||
current: targets, | ||
} | ||
} | ||
|
||
/// Returns true if inserted | ||
pub fn enter_supports(&mut self, features: Features) -> bool { | ||
if features.is_empty() || self.current.exclude.contains(features) { | ||
// Already excluding all features | ||
return false; | ||
} | ||
|
||
let newly_excluded = features - self.current.exclude; | ||
self.stack.push(newly_excluded); | ||
self.current.exclude.insert(newly_excluded); | ||
true | ||
} | ||
|
||
/// Should be only called if inserted | ||
pub fn exit_supports(&mut self) { | ||
if let Some(last) = self.stack.pop() { | ||
self.current.exclude.remove(last); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn supports_scope_correctly() { | ||
let mut targets = TargetsWithSupportsScope::new(Targets::default()); | ||
assert!(!targets.current.exclude.contains(Features::OklabColors)); | ||
assert!(!targets.current.exclude.contains(Features::LabColors)); | ||
assert!(!targets.current.exclude.contains(Features::P3Colors)); | ||
|
||
targets.enter_supports(Features::OklabColors | Features::LabColors); | ||
assert!(targets.current.exclude.contains(Features::OklabColors)); | ||
assert!(targets.current.exclude.contains(Features::LabColors)); | ||
|
||
targets.enter_supports(Features::P3Colors | Features::LabColors); | ||
assert!(targets.current.exclude.contains(Features::OklabColors)); | ||
assert!(targets.current.exclude.contains(Features::LabColors)); | ||
assert!(targets.current.exclude.contains(Features::P3Colors)); | ||
|
||
targets.exit_supports(); | ||
assert!(targets.current.exclude.contains(Features::OklabColors)); | ||
assert!(targets.current.exclude.contains(Features::LabColors)); | ||
assert!(!targets.current.exclude.contains(Features::P3Colors)); | ||
|
||
targets.exit_supports(); | ||
assert!(!targets.current.exclude.contains(Features::OklabColors)); | ||
assert!(!targets.current.exclude.contains(Features::LabColors)); | ||
} | ||
|
||
macro_rules! should_compile { | ||
($targets: expr, $feature: ident) => { | ||
$targets.should_compile( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would only work if the value is an exact match. Perhaps a future enhancement would be to attempt to parse the value, and extract the features from that. For example, if any
lab()
color was used, set that feature rather than onlylab(0% 0 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it would be nice to expand this detection in future.