import * as lite from "caniuse-lite" ; import { STANDARD_TARGET_NAME_MAPPING , AstNodeTypes } from "../constants" ; import { AstMetadataApiWithTargetsResolver , Target } from "../types" ; /**
 * Take a target's id and return it's full name by using `targetNameMappings`
 * ex. {target: and_ff, version: 40} => 'Android FireFox 40'
 */ function formatTargetNames ( target ) { const name = STANDARD_TARGET_NAME_MAPPING [ target . target ] || target . target ; return `${ name } ${ target . version }` ; } /**
 * Check if a browser version is in the range format
 * ex. 10.0-10.2
 */ function versionIsRange ( version ) { return version . includes ( "-" ) ; } /**
 * Parse version from caniuse and compare with parsed version from browserslist.
 */ function areVersionsEqual ( targetVersion , statsVersion ) { return targetVersion === parseFloat ( statsVersion ) ; } function isSupportedByCanIUse ( node , { version , target , parsedVersion } ) { const data = lite . feature ( lite . features [ node . caniuseId ] ) ; if ( ! data ) return true ; const { stats } = data ; if ( ! ( target in stats ) ) return true ; const targetStats = stats [ target ] ; if ( typeof version === "string" && versionIsRange ( version ) ) { return Object . keys ( targetStats ) . some ( ( statsVersion ) : boolean => versionIsRange ( statsVersion ) && areVersionsEqual ( parsedVersion , statsVersion ) ? ! targetStats [ statsVersion ] . includes ( "y" ) : true ) ; } if ( ! ( version in targetStats ) ) return true ; if ( ! targetStats [ version ] ) return true ; return targetStats [ version ] . includes ( "y" ) ; } /**
 * Return an array of all unsupported targets
 */ export function getUnsupportedTargets ( node , targets ) { return targets . filter ( ( target ) => ! isSupportedByCanIUse ( node , target ) ) . map ( formatTargetNames ) ; } const CanIUseProvider = [ { caniuseId : "serviceworkers" , astNodeType : AstNodeTypes . NewExpression , object : "ServiceWorker" , } , { caniuseId : "serviceworkers" , astNodeType : AstNodeTypes . MemberExpression , object : "navigator" , property : "serviceWorker" , } , { caniuseId : "queryselector" , astNodeType : AstNodeTypes . MemberExpression , object : "document" , property : "querySelector" , } , { caniuseId : "intersectionobserver" , astNodeType : AstNodeTypes . NewExpression , object : "IntersectionObserver" , } , { caniuseId : "resizeobserver" , astNodeType : AstNodeTypes . NewExpression , object : "ResizeObserver" , } , { caniuseId : "payment-request" , astNodeType : AstNodeTypes . NewExpression , object : "PaymentRequest" , } , { caniuseId : "promises" , astNodeType : AstNodeTypes . NewExpression , object : "Promise" , } , { caniuseId : "promises" , astNodeType : AstNodeTypes . MemberExpression , object : "Promise" , property : "resolve" , } , { caniuseId : "promises" , astNodeType : AstNodeTypes . MemberExpression , object : "Promise" , property : "all" , } , { caniuseId : "promises" , astNodeType : AstNodeTypes . MemberExpression , object : "Promise" , property : "race" , } , { caniuseId : "promises" , astNodeType : AstNodeTypes . MemberExpression , object : "Promise" , property : "reject" , } , { caniuseId : "fetch" , astNodeType : AstNodeTypes . CallExpression , object : "fetch" , } , { caniuseId : "document-currentscript" , astNodeType : AstNodeTypes . MemberExpression , object : "document" , property : "currentScript" , } , { caniuseId : "url" , astNodeType : AstNodeTypes . NewExpression , object : "URL" , } , { caniuseId : "urlsearchparams" , astNodeType : AstNodeTypes . NewExpression , object : "URLSearchParams" , } , { caniuseId : "high-resolution-time" , astNodeType : AstNodeTypes . MemberExpression , object : "performance" , property : "now" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "TypedArray" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Int8Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Uint8Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Uint8ClampedArray" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Int16Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Uint16Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Int32Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Uint32Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Float32Array" , } , { caniuseId : "typedarrays" , astNodeType : AstNodeTypes . NewExpression , object : "Float64Array" , } , ] . map ( ( rule ) => ( { ... rule , getUnsupportedTargets , id : rule . property ? `${ rule . object }.${ rule . property }` : rule . object , protoChainId : rule . property ? `${ rule . object }.${ rule . property }` : rule . object , protoChain : rule . property ? [ rule . object , rule . property ] : [ rule . object ] , } ) ) ; export default CanIUseProvider ;