Skip to content

Commit

Permalink
Fix compat logic
Browse files Browse the repository at this point in the history
Fixes #77
  • Loading branch information
devongovett committed Jan 31, 2022
1 parent 46cca00 commit f1bb3cf
Show file tree
Hide file tree
Showing 3 changed files with 688 additions and 564 deletions.
22 changes: 12 additions & 10 deletions scripts/build-prefixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,14 @@ let prefixMapping = {

let enumify = (f) => f.replace(/^@([a-z])/, (_, x) => 'At' + x.toUpperCase()).replace(/^::([a-z])/, (_, x) => 'PseudoElement' + x.toUpperCase()).replace(/^:([a-z])/, (_, x) => 'PseudoClass' + x.toUpperCase()).replace(/(^|-)([a-z])/g, (_, a, x) => x.toUpperCase())

let allBrowsers = Object.keys(browsers).filter(b => !(b in BROWSER_MAPPING)).sort();
let targets = `// This file is autogenerated by build-prefixes.js. DO NOT EDIT!
use serde::{Deserialize, Serialize};
#[derive(Serialize, Debug, Deserialize, Clone, Copy, Default)]
pub struct Browsers {
pub ${Object.keys(browsers).filter(b => !(b in BROWSER_MAPPING)).sort().join(': Option<u32>,\n pub ')}: Option<u32>
pub ${allBrowsers.join(': Option<u32>,\n pub ')}: Option<u32>
}
`;

Expand All @@ -237,7 +238,7 @@ fs.writeFileSync('src/targets.rs', targets);
let targets_dts = `// This file is autogenerated by build-prefixes.js. DO NOT EDIT!
export interface Targets {
${Object.keys(browsers).filter(b => !(b in BROWSER_MAPPING)).sort().join('?: number,\n ')}?: number
${allBrowsers.join('?: number,\n ')}?: number
}
`;

Expand Down Expand Up @@ -315,19 +316,20 @@ pub enum Feature {
impl Feature {
pub fn is_compatible(&self, browsers: Browsers) -> bool {
match self {
${[...compat].map(([features, browsers]) =>
`${features.map(name => `Feature::${enumify(name)}`).join(' |\n ')} => {` + (Object.entries(browsers).length === 0 ? '}' : `
${Object.entries(browsers).map(([browser, min]) =>
${[...compat].map(([features, supportedBrowsers]) =>
`${features.map(name => `Feature::${enumify(name)}`).join(' |\n ')} => {` + (Object.entries(supportedBrowsers).length === 0 ? '\n return false\n }' : `
${Object.entries(supportedBrowsers).map(([browser, min]) =>
`if let Some(version) = browsers.${browser} {
if version >= ${min} {
return true
if version < ${min} {
return false
}
}`
).join('\n ')}
}`).join('\n ')}${Object.keys(supportedBrowsers).length === allBrowsers.length ? '' : `\n if ${allBrowsers.filter(b => !supportedBrowsers[b]).map(browser => `browsers.${browser}.is_some()`).join(' || ')} {
return false
}`}
}`
)).join('\n ')}
}
false
true
}
}
`;
Expand Down
Loading

0 comments on commit f1bb3cf

Please sign in to comment.