@squiggle.sh/babel-plugin 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +3 -3
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- var T=Object.defineProperty;var ee=Object.getOwnPropertyNames;var ne=Object.getOwnPropertyDescriptor;var te=Object.prototype.hasOwnProperty;function re(t){return this[t]}var ie=(t)=>{var e=(O??=new WeakMap).get(t),n;if(e)return e;e=T({},"__esModule",{value:true});if(t&&typeof t==="object"||typeof t==="function"){for(var s of ee(t))if(!te.call(e,s))T(e,s,{get:re.bind(t,s),enumerable:!(n=ne(t,s))||n.enumerable})}O.set(t,e);return e};var O;var oe=(t)=>t;function se(t,e){this[t]=oe.bind(null,e)}var ae=(t,e)=>{for(var n in e)T(t,n,{get:e[n],enumerable:true,configurable:true,set:se.bind(e,n)})};var Ce={};ae(Ce,{default:()=>K});module.exports=ie(Ce);var U=require("node:module");var b=require("node:path");var k="@squiggle.sh/react-native";var F=`${k}/release`;var j="__SQUIGGLE_SOURCE_ROOT__";var _="node_modules/";function le(t){const e=t.lastIndexOf(_);return e===-1?t:_+t.slice(e+_.length)}var ue="__squiggleStoreName";function de(t){let e=2166136261;for(let n=0;n<t.length;n++){e^=t.charCodeAt(n);e=Math.imul(e,16777619)>>>0}return e.toString(16).padStart(8,"0")}var ce={modules:new Set(["zustand","zustand/vanilla","zustand/react"]),names:new Set(["create","createStore"]),helper:"__squiggleInstrumentZustandFactory",namesStores:true};var ge={modules:new Set(["react-native-mmkv"]),names:new Set(["createMMKV","useMMKV"]),helper:"__squiggleInstrumentMmkvFactory"};var fe={modules:new Set(["@react-native-async-storage/async-storage"]),names:new Set,defaultExport:true,helper:"__squiggleInstrumentAsyncStorage"};var pe={modules:new Set(["@op-engineering/op-sqlite"]),names:new Set(["open"]),helper:"__squiggleInstrumentOpSqliteFactory"};var me={modules:new Set(["expo-sqlite"]),names:new Set(["openDatabaseAsync","openDatabaseSync","useSQLiteContext"]),helper:"__squiggleInstrumentExpoSqliteFactory"};var ye={modules:new Set(["@legendapp/state"]),names:new Set(["observable"]),helper:"__squiggleInstrumentLegendStateFactory",namesStores:true};var be={modules:new Set(["@legendapp/state/react"]),names:new Set(["useObservable"]),helper:"__squiggleInstrumentLegendStateFactory"};function w(t){return/^[A-Z]/.test(t)}function M(t){if(!t)return false;return t.some((e)=>{const n=e;return n?.type==="ReturnStatement"&&(n.argument?.type==="JSXElement"||n.argument?.type==="JSXFragment")})}function H(t){const e=t?.[0];if(e?.type!=="VariableDeclaration")return false;return e.declarations?.some((n)=>n.init?.callee?.name==="_c")??false}function R(t){if(t.type==="ArrowFunctionExpression"){const e=t.body;if(!e)return false;if(e.type==="JSXElement"||e.type==="JSXFragment")return true;if(e.type==="BlockStatement"){const n=e.body;return M(n)||H(n)}return false}if(t.type==="FunctionExpression"||t.type==="FunctionDeclaration"){const e=t.body;return M(e?.body)||H(e?.body)}return false}var X={memo:"type",forwardRef:"render"};function Se(t){if(t?.type==="SequenceExpression"){t=t.expressions?.at(-1)}if(!t)return;const e=t.type==="Identifier"?t.name:t.type==="MemberExpression"?t.property?.name:undefined;return typeof e==="string"&&e in X?e:undefined}function he(t,e){if(e?.type!=="Identifier"||typeof e.name!=="string"){return false}const n=t.scope.getBinding(e.name)?.path.node.init;return n?.callee?.name==="$RefreshSig$"}function we(t){let e=t;const n=[];const s=(d)=>e.get(d);for(let d=0;d<8;d++){const c=e.node;if(c.type==="AssignmentExpression"){e=s("right");continue}if(c.type!=="CallExpression"||!c.arguments?.[0])break;const h=Se(c.callee);if(h!==undefined)n.push(X[h]);else if(!he(e,c.callee))return;e=s("arguments.0")}if(n.length===0)return;const l=e.node;if(l.type!=="ArrowFunctionExpression"&&l.type!=="FunctionExpression"){return}return R(l)?{fnPath:e,fields:n}:undefined}function ke(t){const e=()=>(process.env["BABEL_ENV"]??process.env["NODE_ENV"])==="development";if(typeof t.caller!=="function")return e();return t.caller((n)=>n?.isDev??e())===true}function J(t){const e=()=>(process.env["BABEL_ENV"]??process.env["NODE_ENV"])==="production";if(typeof t.caller!=="function")return e();return t.caller((n)=>n?.isDev===undefined?e():!n.isDev)}function Ee(t){try{const e=U.createRequire(b.join(t,"package.json"))("@squiggle.sh/react-native/release-opt-in");return{keeps:e.keepsSquiggleInRelease(t),config:e.releaseConfigPath?.(t),stranded:e.strandedReleaseConfig?.(t)}}catch{return{keeps:true}}}var Pe=/[\\/]react-native(?:-macos)?[\\/]Libraries[\\/]Renderer[\\/]implementations[\\/][^\\/]+\.js$/;var Ne=`if (typeof globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') {
1
+ var _=Object.defineProperty;var ee=Object.getOwnPropertyNames;var ne=Object.getOwnPropertyDescriptor;var te=Object.prototype.hasOwnProperty;function re(t){return this[t]}var ie=(t)=>{var e=(O??=new WeakMap).get(t),n;if(e)return e;e=_({},"__esModule",{value:true});if(t&&typeof t==="object"||typeof t==="function"){for(var a of ee(t))if(!te.call(e,a))_(e,a,{get:re.bind(t,a),enumerable:!(n=ne(t,a))||n.enumerable})}O.set(t,e);return e};var O;var oe=(t)=>t;function se(t,e){this[t]=oe.bind(null,e)}var ae=(t,e)=>{for(var n in e)_(t,n,{get:e[n],enumerable:true,configurable:true,set:se.bind(e,n)})};var De={};ae(De,{default:()=>K});module.exports=ie(De);var U=require("node:module");var S=require("node:path");var E="@squiggle.sh/react-native";var F=`${E}/release`;var j="__SQUIGGLE_SOURCE_ROOT__";var T="node_modules/";function le(t){const e=t.lastIndexOf(T);return e===-1?t:T+t.slice(e+T.length)}var ue="__squiggleStoreName";function de(t){let e=2166136261;for(let n=0;n<t.length;n++){e^=t.charCodeAt(n);e=Math.imul(e,16777619)>>>0}return e.toString(16).padStart(8,"0")}var ce={modules:new Set(["zustand","zustand/vanilla","zustand/react"]),names:new Set(["create","createStore"]),helper:"__squiggleInstrumentZustandFactory",namesStores:true};var ge={modules:new Set(["react-native-mmkv"]),names:new Set(["createMMKV","useMMKV"]),helper:"__squiggleInstrumentMmkvFactory"};var fe={modules:new Set(["@react-native-async-storage/async-storage"]),names:new Set,defaultExport:true,helper:"__squiggleInstrumentAsyncStorage"};var pe={modules:new Set(["@op-engineering/op-sqlite"]),names:new Set(["open"]),helper:"__squiggleInstrumentOpSqliteFactory"};var me={modules:new Set(["expo-sqlite"]),names:new Set(["openDatabaseAsync","openDatabaseSync","useSQLiteContext"]),helper:"__squiggleInstrumentExpoSqliteFactory"};var ye={modules:new Set(["@legendapp/state"]),names:new Set(["observable"]),helper:"__squiggleInstrumentLegendStateFactory",namesStores:true};var be={modules:new Set(["@legendapp/state/react"]),names:new Set(["useObservable"]),helper:"__squiggleInstrumentLegendStateFactory"};function w(t){return/^[A-Z]/.test(t)}function M(t){if(!t)return false;return t.some((e)=>{const n=e;return n?.type==="ReturnStatement"&&(n.argument?.type==="JSXElement"||n.argument?.type==="JSXFragment")})}function H(t){const e=t?.[0];if(e?.type!=="VariableDeclaration")return false;return e.declarations?.some((n)=>n.init?.callee?.name==="_c")??false}function R(t){if(t.type==="ArrowFunctionExpression"){const e=t.body;if(!e)return false;if(e.type==="JSXElement"||e.type==="JSXFragment")return true;if(e.type==="BlockStatement"){const n=e.body;return M(n)||H(n)}return false}if(t.type==="FunctionExpression"||t.type==="FunctionDeclaration"){const e=t.body;return M(e?.body)||H(e?.body)}return false}var X={memo:"type",forwardRef:"render"};function Se(t){if(t?.type==="SequenceExpression"){t=t.expressions?.at(-1)}if(!t)return;const e=t.type==="Identifier"?t.name:t.type==="MemberExpression"?t.property?.name:undefined;return typeof e==="string"&&e in X?e:undefined}function he(t,e){if(e?.type!=="Identifier"||typeof e.name!=="string"){return false}const n=t.scope.getBinding(e.name)?.path.node.init;return n?.callee?.name==="$RefreshSig$"}function we(t){let e=t;const n=[];const a=(g)=>e.get(g);for(let g=0;g<8;g++){const d=e.node;if(d.type==="AssignmentExpression"){e=a("right");continue}if(d.type!=="CallExpression"||!d.arguments?.[0])break;const h=Se(d.callee);if(h!==undefined)n.push(X[h]);else if(!he(e,d.callee))return;e=a("arguments.0")}if(n.length===0)return;const u=e.node;if(u.type!=="ArrowFunctionExpression"&&u.type!=="FunctionExpression"){return}return R(u)?{fnPath:e,fields:n}:undefined}function Ee(t){const e=()=>(process.env["BABEL_ENV"]??process.env["NODE_ENV"])==="development";if(typeof t.caller!=="function")return e();return t.caller((n)=>n?.isDev??e())===true}function J(t){const e=()=>(process.env["BABEL_ENV"]??process.env["NODE_ENV"])==="production";if(typeof t.caller!=="function")return e();return t.caller((n)=>n?.isDev===undefined?e():!n.isDev)}function ke(t){try{const e=U.createRequire(S.join(t,"package.json"))("@squiggle.sh/react-native/release-opt-in");return{keeps:e.keepsSquiggleInRelease(t),config:e.releaseConfigPath?.(t)}}catch{return{keeps:true}}}var Pe=/[\\/]react-native(?:-macos)?[\\/]Libraries[\\/]Renderer[\\/]implementations[\\/][^\\/]+\.js$/;var Ne=`if (typeof globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') {
2
2
  (function () {
3
3
  var renderers = new Map();
4
4
  var roots = new Map();
@@ -29,5 +29,5 @@ var T=Object.defineProperty;var ee=Object.getOwnPropertyNames;var ne=Object.getO
29
29
  onPostCommitFiberRoot: function () {},
30
30
  };
31
31
  })();
32
- }`;var V=new Set;function ve(t){const{config:e,stranded:n}=t;const s=`${e??""}\x00${n??""}`;if(V.has(s))return;V.add(s);const l=`[squiggle] This production bundle gets Squiggle's release stand-in: the SDK is left out and the app will not connect.`;const d=e===undefined?undefined:b.dirname(e);console.warn(n!==undefined&&d!==undefined?`${l} ${n} says "release": true, but nothing reads .squiggle/ in the app's folder: Squiggle reads this project's store at ${d}/. Move its config.jsonc keys and flows/ there, then delete it. Then run pod install and rebuild.`:`${l} To keep Squiggle in release builds, add "release": true to ${e??`.squiggle/config.jsonc`}, then run pod install and rebuild.`)}function G(t){const e=(n)=>{if(n.node.source?.value===k)n.node.source=t.stringLiteral(F)};return{name:"squiggle",visitor:{ImportDeclaration:e,ExportNamedDeclaration:e,ExportAllDeclaration:e,CallExpression(n){const{callee:s,arguments:l}=n.node;const d=s.type==="Import"||s.type==="Identifier"&&s.name==="require";const c=l[0];if(d&&c?.value===k)l[0]=t.stringLiteral(F)}}}}function xe(t){if(!t)return false;if(t.type==="JSXIdentifier"){const e=typeof t.name==="string"?t.name:"";return w(e)&&e!=="Fragment"}if(t.type==="JSXMemberExpression"){return t.property?.name!=="Fragment"}return false}function Ie(t,e,n,s){const l=JSON.stringify(e);const d=n.map((c)=>`{ file: ${l}, line: ${c.line}, col: ${c.col} }`).join(", ");return[`function ${t.tag}(e, i) {`,` if (e != null && typeof e === 'object' && e.props) {`,` var m = globalThis.__squiggleSites || (globalThis.__squiggleSites = new WeakMap());`,` m.set(e.props, (${t.sites} || ${t.init}())[i]);`,` }`,` return e;`,`}`,`var ${t.sites};`,`function ${t.init}() {`,` globalThis.__squiggleSourceRoot || (globalThis.__squiggleSourceRoot = ${JSON.stringify(s)});`,` return (${t.sites} = [${d}]);`,`}`].join(`
33
- `)}function Te(t){const e=t.callee;if(!e)return;if(e.type==="Identifier"&&typeof e.name==="string"){return A(e.name)?{hook:e.name,expr:e.name}:undefined}if(e.type==="MemberExpression"&&!e.computed&&e.object?.type==="Identifier"&&e.property?.type==="Identifier"&&typeof e.property.name==="string"&&A(e.property.name)){return{hook:e.property.name,expr:`${e.object.name}.${e.property.name}`}}return}function A(t){return t==="use"||/^use[A-Z0-9]/.test(t)}var _e=new Set(["TSAsExpression","TSNonNullExpression","TSSatisfiesExpression","TypeCastExpression","ParenthesizedExpression"]);function Re(t){let e=t.node;let n=t.parentPath;while(n&&_e.has(n.node.type)){e=n.node;n=n.parentPath}const s=n?.node;if(s?.type!=="VariableDeclarator"||s.init!==e){return}const l=s.id;if(!l)return;if(l.type==="Identifier")return l.name;if(l.type==="ArrayPattern"){const d=l.elements?.[0];return d?.type==="Identifier"?d.name:undefined}if(l.type==="ObjectPattern"){const d=(l.properties??[]).map((c)=>c.type==="ObjectProperty"?c.key?.name:undefined).filter((c)=>typeof c==="string");return d.length>0?`{ ${d.join(", ")} }`:undefined}return}var Ae=new Set(["use","useState","useReducer","useRef","useMemo","useCallback","useEffect","useLayoutEffect","useInsertionEffect","useImperativeHandle","useContext","useId","useTransition","useDeferredValue","useSyncExternalStore","useDebugValue","useOptimistic","useActionState","useFormState","useFormStatus","useEffectEvent"]);function qe(t){const e=[];t.traverse({Function(n){n.skip()},CallExpression:{exit(n){const s=Te(n.node);if(!s)return;const l={hook:s.hook};const d=Re(n);if(d!==undefined)l.name=d;if(!Ae.has(s.hook))l.ref=s.expr;e.push(l)}}});return e}function Le(t,e){const n=e.map((s)=>{const l=[`hook: ${JSON.stringify(s.hook)}`];if(s.name!==undefined)l.push(`name: ${JSON.stringify(s.name)}`);if(s.ref!==undefined){l.push(`ref: function () { return ${s.ref}; }`)}return`{ ${l.join(", ")} }`});return`${t}.__squiggleHooks = [${n.join(", ")}];`}function Be(t){const e=t.parentPath?.node.type;return e==="Program"||e==="ExportNamedDeclaration"||e==="ExportDefaultDeclaration"}function K(t,e={}){const n=t.types;const s=e.rootDir??process.cwd();if(J(t)&&e.release!==true){if(e.release===false)return G(n);const r=Ee(s);if(!r.keeps){ve(r);return G(n)}}const l=e.callSites??ke(t);const d=J(t);const c=[];if(e.instrumentZustand!==false)c.push(ce);if(e.instrumentMmkv!==false)c.push(ge);if(e.instrumentAsyncStorage!==false)c.push(fe);if(e.instrumentSqlite!==false)c.push(pe,me);if(e.instrumentLegendState!==false)c.push(ye,be);const h=e.helperModule??e.zustandHelperModule??k;const q=e.sourceAnnotations===false||e.stripInProduction===true&&process.env["NODE_ENV"]==="production";function E(r){const i=b.isAbsolute(r)?b.relative(s,r).split("\\").join("/"):r;return le(i)}function P(r,i){let o=i.squiggleFileLocal;if(!o){o=r.scope.generateUidIdentifier("sqFile").name;i.squiggleFileLocal=o;const a=E(i.filename??"<unknown>");r.scope.getProgramParent().path.unshiftContainer("body",n.variableDeclaration("const",[n.variableDeclarator(n.identifier(o),n.stringLiteral(a))]))}return o}function W(r,i,o){const a=r.node;if(a.type!=="ImportDeclaration")return;if(!o.modules.has(a.source.value))return;const u=[];for(const f of a.specifiers){if(f.type==="ImportDefaultSpecifier"){if(!o.defaultExport)continue}else if(f.type==="ImportSpecifier"){const S=f.imported;const D=S?.type==="Identifier"?S.name:undefined;if(!D||!o.names.has(D))continue}else{continue}const p=f.local.name;const y=r.scope.generateUidIdentifier(p).name;f.local=n.identifier(y);u.push({localName:p,uid:y})}if(u.length===0)return;if(o.namesStores)for(const{localName:f}of u)(i.squiggleStoreFactories??=new Set).add(f);const g=L(r.scope,i,o.helper);const m=u.map(({localName:f,uid:p})=>n.variableDeclarator(n.identifier(f),n.callExpression(n.identifier(g),[n.identifier(p)])));r.insertAfter(n.variableDeclaration("const",m))}function L(r,i,o){const a=i.squiggleHelperLocals??={};const u=a[o];if(u)return u;const g=r.generateUidIdentifier("squiggleInstrument").name;a[o]=g;r.getProgramParent().path.unshiftContainer("body",n.importDeclaration([n.importSpecifier(n.identifier(g),n.identifier(o))],n.stringLiteral(h)));return g}function z(r,i){const o=i.squiggleStoreFactories;if(!o)return;const a=r.node;const u=a.id;if(u?.type!=="Identifier"||!u.name)return;const g=r.parentPath;const m=g?.parentPath?.node.type==="ExportNamedDeclaration"?g.parentPath.parentPath:g?.parentPath;if(m?.node.type!=="Program")return;const f=a.init;if(f?.type!=="CallExpression")return;const p=f.callee;const y=p?.type==="Identifier"?p.name:p?.type==="CallExpression"&&p.arguments?.length===0&&p.callee?.type==="Identifier"?p.callee.name:undefined;if(y===undefined||!o.has(y))return;const S=n.logicalExpression("||",n.identifier(L(r.scope,i,ue)),n.arrowFunctionExpression([n.identifier("_n"),n.identifier("s")],n.identifier("s")));a.init=n.callExpression(S,[n.stringLiteral(u.name),f,n.stringLiteral(de(E(i.filename??"<unknown>")))])}function B(r,i){return i.reduce((o,a)=>n.memberExpression(o,n.identifier(a)),n.identifier(r))}function N(r,i,o,a,u=[]){return n.expressionStatement(n.assignmentExpression("=",n.memberExpression(B(r,u),n.identifier("__squiggleSrc")),n.objectExpression([n.objectProperty(n.identifier("file"),n.identifier(i)),n.objectProperty(n.identifier("line"),n.numericLiteral(o)),n.objectProperty(n.identifier("col"),n.numericLiteral(a))])))}function v(r){const i=r.node.loc?.start;return{line:i?.line??0,col:i?.column??0}}const C=new WeakSet;function Z(r,i){const o=r.node;if(C.has(o))return;if(!xe(o.openingElement?.name))return;const a=o.loc?.start;if(!a||typeof a.line!=="number")return;const u=r.scope.getProgramParent();const g=i.squiggleSites??=[];const m=i.squiggleTagLocal??=u.generateUidIdentifier("sqTag").name;i.squiggleSitesLocal??=u.generateUidIdentifier("sqSites").name;i.squiggleSitesInit??=u.generateUidIdentifier("sqSitesInit").name;const f=g.length;g.push({line:a.line,col:a.column??0});C.add(o);const p=n.callExpression(n.identifier(m),[o,n.numericLiteral(f)]);const y=r.parentPath?.node.type;const S=y==="JSXElement"||y==="JSXFragment"||y==="JSXAttribute";r.replaceWith(S?n.jsxExpressionContainer(p):p)}function Q(r,i){const o=i.squiggleSites;if(!o||o.length===0)return;if(!t.template)return;r.scope.getProgramParent().path.unshiftContainer("body",t.template.statements.ast(Ie({tag:i.squiggleTagLocal,sites:i.squiggleSitesLocal,init:i.squiggleSitesInit},E(i.filename??"<unknown>"),o,s)))}function x(r){return l&&!(r.filename??"").includes("/node_modules/")}function I(r,i,o,a,u=i){if(!x(a)||!t.template)return;if(!w(i)&&!A(i))return;if(!Be(o))return;const g=qe(r);if(g.length===0)return;o.insertAfter(t.template.statements.ast(Le(u,g)))}function Y(r,i,o){const a=we(r.get("init"));if(a===undefined||!r.parentPath)return;const{fnPath:u,fields:g}=a;const m=r.parentPath;try{I(u,i,m,o,[i,...g].join("."))}catch{}const{line:f,col:p}=v(r);m.insertAfter(N(i,P(r,o),f,p,g));if(u.node.id?.name===undefined){m.insertAfter(n.expressionStatement(n.assignmentExpression("=",n.memberExpression(B(i,g),n.identifier("displayName")),n.stringLiteral(i))))}}return{name:"squiggle",visitor:{Identifier(r){if(!l)return;try{if(r.node.name!==j)return;const i=r;if(!i.isReferencedIdentifier())return;if(i.scope.hasBinding(j))return;i.replaceWith(n.stringLiteral(s))}catch{}},Program:{enter(r,i){if(!d||!t.template)return;if(!Pe.test(i.filename??""))return;try{r.scope.getProgramParent().path.unshiftContainer("body",t.template.statements.ast(Ne))}catch{}},exit(r,i){if(!x(i))return;try{Q(r,i)}catch{}}},JSXElement:{exit(r,i){if(!x(i))return;try{Z(r,i)}catch{}}},ImportDeclaration(r,i){for(const o of c){try{W(r,i,o)}catch{}}},FunctionDeclaration(r,i){try{const o=r.node.id?.name;if(o)I(r,o,r,i)}catch{}if(q)return;try{const o=r.node.id?.name;if(!o||!w(o))return;if(!R(r.node))return;const{line:a,col:u}=v(r);r.insertAfter(N(o,P(r,i),a,u))}catch{}},VariableDeclarator(r,i){try{z(r,i)}catch{}try{const o=r.node.id;const a=r.node.init;if(o?.type==="Identifier"&&o.name&&r.parentPath&&(a?.type==="ArrowFunctionExpression"||a?.type==="FunctionExpression")){I(r.get("init"),o.name,r.parentPath,i)}}catch{}if(q)return;try{const o=r.node.id;if(!o||o.type!=="Identifier")return;const a=o.name;if(!a||!w(a))return;const u=r.node.init;if(!u)return;if(u.type==="CallExpression"){Y(r,a,i);return}if(!R(u))return;const{line:g,col:m}=v(r);r.parentPath?.insertAfter(N(a,P(r,i),g,m))}catch{}}}}}
32
+ }`;var V=new Set;function ve(t){const e=t??"";if(V.has(e))return;V.add(e);console.warn(`[squiggle] This production bundle gets Squiggle's release stand-in: the SDK is left out and the app will not connect. To keep Squiggle in release builds, add "release": true to ${t??`.squiggle/config.jsonc`}, then run pod install and rebuild.`)}function G(t){const e=(n)=>{if(n.node.source?.value===E)n.node.source=t.stringLiteral(F)};return{name:"squiggle",visitor:{ImportDeclaration:e,ExportNamedDeclaration:e,ExportAllDeclaration:e,CallExpression(n){const{callee:a,arguments:u}=n.node;const g=a.type==="Import"||a.type==="Identifier"&&a.name==="require";const d=u[0];if(g&&d?.value===E)u[0]=t.stringLiteral(F)}}}}function xe(t){if(!t)return false;if(t.type==="JSXIdentifier"){const e=typeof t.name==="string"?t.name:"";return w(e)&&e!=="Fragment"}if(t.type==="JSXMemberExpression"){return t.property?.name!=="Fragment"}return false}function Ie(t,e,n,a){const u=JSON.stringify(e);const g=n.map((d)=>`{ file: ${u}, line: ${d.line}, col: ${d.col} }`).join(", ");return[`function ${t.tag}(e, i) {`,` if (e != null && typeof e === 'object' && e.props) {`,` var m = globalThis.__squiggleSites || (globalThis.__squiggleSites = new WeakMap());`,` m.set(e.props, (${t.sites} || ${t.init}())[i]);`,` }`,` return e;`,`}`,`var ${t.sites};`,`function ${t.init}() {`,` globalThis.__squiggleSourceRoot || (globalThis.__squiggleSourceRoot = ${JSON.stringify(a)});`,` return (${t.sites} = [${g}]);`,`}`].join(`
33
+ `)}function _e(t){const e=t.callee;if(!e)return;if(e.type==="Identifier"&&typeof e.name==="string"){return A(e.name)?{hook:e.name,expr:e.name}:undefined}if(e.type==="MemberExpression"&&!e.computed&&e.object?.type==="Identifier"&&e.property?.type==="Identifier"&&typeof e.property.name==="string"&&A(e.property.name)){return{hook:e.property.name,expr:`${e.object.name}.${e.property.name}`}}return}function A(t){return t==="use"||/^use[A-Z0-9]/.test(t)}var Te=new Set(["TSAsExpression","TSNonNullExpression","TSSatisfiesExpression","TypeCastExpression","ParenthesizedExpression"]);function Re(t){let e=t.node;let n=t.parentPath;while(n&&Te.has(n.node.type)){e=n.node;n=n.parentPath}const a=n?.node;if(a?.type!=="VariableDeclarator"||a.init!==e){return}const u=a.id;if(!u)return;if(u.type==="Identifier")return u.name;if(u.type==="ArrayPattern"){const g=u.elements?.[0];return g?.type==="Identifier"?g.name:undefined}if(u.type==="ObjectPattern"){const g=(u.properties??[]).map((d)=>d.type==="ObjectProperty"?d.key?.name:undefined).filter((d)=>typeof d==="string");return g.length>0?`{ ${g.join(", ")} }`:undefined}return}var Ae=new Set(["use","useState","useReducer","useRef","useMemo","useCallback","useEffect","useLayoutEffect","useInsertionEffect","useImperativeHandle","useContext","useId","useTransition","useDeferredValue","useSyncExternalStore","useDebugValue","useOptimistic","useActionState","useFormState","useFormStatus","useEffectEvent"]);function qe(t){const e=[];t.traverse({Function(n){n.skip()},CallExpression:{exit(n){const a=_e(n.node);if(!a)return;const u={hook:a.hook};const g=Re(n);if(g!==undefined)u.name=g;if(!Ae.has(a.hook))u.ref=a.expr;e.push(u)}}});return e}function Le(t,e){const n=e.map((a)=>{const u=[`hook: ${JSON.stringify(a.hook)}`];if(a.name!==undefined)u.push(`name: ${JSON.stringify(a.name)}`);if(a.ref!==undefined){u.push(`ref: function () { return ${a.ref}; }`)}return`{ ${u.join(", ")} }`});return`${t}.__squiggleHooks = [${n.join(", ")}];`}function Be(t){const e=t.parentPath?.node.type;return e==="Program"||e==="ExportNamedDeclaration"||e==="ExportDefaultDeclaration"}function K(t,e={}){const n=t.types;const a=e.rootDir??process.cwd();if(J(t)&&e.release!==true){if(e.release===false)return G(n);const r=ke(a);if(!r.keeps){ve(r.config);return G(n)}}const u=e.callSites??Ee(t);const g=J(t);const d=[];if(e.instrumentZustand!==false)d.push(ce);if(e.instrumentMmkv!==false)d.push(ge);if(e.instrumentAsyncStorage!==false)d.push(fe);if(e.instrumentSqlite!==false)d.push(pe,me);if(e.instrumentLegendState!==false)d.push(ye,be);const h=e.helperModule??e.zustandHelperModule??E;const q=e.sourceAnnotations===false||e.stripInProduction===true&&process.env["NODE_ENV"]==="production";function k(r){const i=S.isAbsolute(r)?S.relative(a,r).split("\\").join("/"):r;return le(i)}function P(r,i){let o=i.squiggleFileLocal;if(!o){o=r.scope.generateUidIdentifier("sqFile").name;i.squiggleFileLocal=o;const s=k(i.filename??"<unknown>");r.scope.getProgramParent().path.unshiftContainer("body",n.variableDeclaration("const",[n.variableDeclarator(n.identifier(o),n.stringLiteral(s))]))}return o}function W(r,i,o){const s=r.node;if(s.type!=="ImportDeclaration")return;if(!o.modules.has(s.source.value))return;const l=[];for(const f of s.specifiers){if(f.type==="ImportDefaultSpecifier"){if(!o.defaultExport)continue}else if(f.type==="ImportSpecifier"){const b=f.imported;const C=b?.type==="Identifier"?b.name:undefined;if(!C||!o.names.has(C))continue}else{continue}const p=f.local.name;const y=r.scope.generateUidIdentifier(p).name;f.local=n.identifier(y);l.push({localName:p,uid:y})}if(l.length===0)return;if(o.namesStores)for(const{localName:f}of l)(i.squiggleStoreFactories??=new Set).add(f);const c=L(r.scope,i,o.helper);const m=l.map(({localName:f,uid:p})=>n.variableDeclarator(n.identifier(f),n.callExpression(n.identifier(c),[n.identifier(p)])));r.insertAfter(n.variableDeclaration("const",m))}function L(r,i,o){const s=i.squiggleHelperLocals??={};const l=s[o];if(l)return l;const c=r.generateUidIdentifier("squiggleInstrument").name;s[o]=c;r.getProgramParent().path.unshiftContainer("body",n.importDeclaration([n.importSpecifier(n.identifier(c),n.identifier(o))],n.stringLiteral(h)));return c}function z(r,i){const o=i.squiggleStoreFactories;if(!o)return;const s=r.node;const l=s.id;if(l?.type!=="Identifier"||!l.name)return;const c=r.parentPath;const m=c?.parentPath?.node.type==="ExportNamedDeclaration"?c.parentPath.parentPath:c?.parentPath;if(m?.node.type!=="Program")return;const f=s.init;if(f?.type!=="CallExpression")return;const p=f.callee;const y=p?.type==="Identifier"?p.name:p?.type==="CallExpression"&&p.arguments?.length===0&&p.callee?.type==="Identifier"?p.callee.name:undefined;if(y===undefined||!o.has(y))return;const b=n.logicalExpression("||",n.identifier(L(r.scope,i,ue)),n.arrowFunctionExpression([n.identifier("_n"),n.identifier("s")],n.identifier("s")));s.init=n.callExpression(b,[n.stringLiteral(l.name),f,n.stringLiteral(de(k(i.filename??"<unknown>")))])}function B(r,i){return i.reduce((o,s)=>n.memberExpression(o,n.identifier(s)),n.identifier(r))}function N(r,i,o,s,l=[]){return n.expressionStatement(n.assignmentExpression("=",n.memberExpression(B(r,l),n.identifier("__squiggleSrc")),n.objectExpression([n.objectProperty(n.identifier("file"),n.identifier(i)),n.objectProperty(n.identifier("line"),n.numericLiteral(o)),n.objectProperty(n.identifier("col"),n.numericLiteral(s))])))}function v(r){const i=r.node.loc?.start;return{line:i?.line??0,col:i?.column??0}}const D=new WeakSet;function Z(r,i){const o=r.node;if(D.has(o))return;if(!xe(o.openingElement?.name))return;const s=o.loc?.start;if(!s||typeof s.line!=="number")return;const l=r.scope.getProgramParent();const c=i.squiggleSites??=[];const m=i.squiggleTagLocal??=l.generateUidIdentifier("sqTag").name;i.squiggleSitesLocal??=l.generateUidIdentifier("sqSites").name;i.squiggleSitesInit??=l.generateUidIdentifier("sqSitesInit").name;const f=c.length;c.push({line:s.line,col:s.column??0});D.add(o);const p=n.callExpression(n.identifier(m),[o,n.numericLiteral(f)]);const y=r.parentPath?.node.type;const b=y==="JSXElement"||y==="JSXFragment"||y==="JSXAttribute";r.replaceWith(b?n.jsxExpressionContainer(p):p)}function Q(r,i){const o=i.squiggleSites;if(!o||o.length===0)return;if(!t.template)return;r.scope.getProgramParent().path.unshiftContainer("body",t.template.statements.ast(Ie({tag:i.squiggleTagLocal,sites:i.squiggleSitesLocal,init:i.squiggleSitesInit},k(i.filename??"<unknown>"),o,a)))}function x(r){return u&&!(r.filename??"").includes("/node_modules/")}function I(r,i,o,s,l=i){if(!x(s)||!t.template)return;if(!w(i)&&!A(i))return;if(!Be(o))return;const c=qe(r);if(c.length===0)return;o.insertAfter(t.template.statements.ast(Le(l,c)))}function Y(r,i,o){const s=we(r.get("init"));if(s===undefined||!r.parentPath)return;const{fnPath:l,fields:c}=s;const m=r.parentPath;try{I(l,i,m,o,[i,...c].join("."))}catch{}const{line:f,col:p}=v(r);m.insertAfter(N(i,P(r,o),f,p,c));if(l.node.id?.name===undefined){m.insertAfter(n.expressionStatement(n.assignmentExpression("=",n.memberExpression(B(i,c),n.identifier("displayName")),n.stringLiteral(i))))}}return{name:"squiggle",visitor:{Identifier(r){if(!u)return;try{if(r.node.name!==j)return;const i=r;if(!i.isReferencedIdentifier())return;if(i.scope.hasBinding(j))return;i.replaceWith(n.stringLiteral(a))}catch{}},Program:{enter(r,i){if(!g||!t.template)return;if(!Pe.test(i.filename??""))return;try{r.scope.getProgramParent().path.unshiftContainer("body",t.template.statements.ast(Ne))}catch{}},exit(r,i){if(!x(i))return;try{Q(r,i)}catch{}}},JSXElement:{exit(r,i){if(!x(i))return;try{Z(r,i)}catch{}}},ImportDeclaration(r,i){for(const o of d){try{W(r,i,o)}catch{}}},FunctionDeclaration(r,i){try{const o=r.node.id?.name;if(o)I(r,o,r,i)}catch{}if(q)return;try{const o=r.node.id?.name;if(!o||!w(o))return;if(!R(r.node))return;const{line:s,col:l}=v(r);r.insertAfter(N(o,P(r,i),s,l))}catch{}},VariableDeclarator(r,i){try{z(r,i)}catch{}try{const o=r.node.id;const s=r.node.init;if(o?.type==="Identifier"&&o.name&&r.parentPath&&(s?.type==="ArrowFunctionExpression"||s?.type==="FunctionExpression")){I(r.get("init"),o.name,r.parentPath,i)}}catch{}if(q)return;try{const o=r.node.id;if(!o||o.type!=="Identifier")return;const s=o.name;if(!s||!w(s))return;const l=r.node.init;if(!l)return;if(l.type==="CallExpression"){Y(r,s,i);return}if(!R(l))return;const{line:c,col:m}=v(r);r.parentPath?.insertAfter(N(s,P(r,i),c,m))}catch{}}}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiggle.sh/babel-plugin",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "Squiggle Babel plugin: injects stable component source locations (definition and, in dev, each element's call site) into React components",
5
5
  "keywords": [
6
6
  "babel-plugin",
@@ -36,7 +36,7 @@
36
36
  "sideEffects": true,
37
37
  "peerDependencies": {
38
38
  "@babel/core": ">=7",
39
- "@squiggle.sh/react-native": "0.6.1"
39
+ "@squiggle.sh/react-native": "0.6.3"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@squiggle.sh/react-native": {