@squiggle.sh/babel-plugin 0.5.4 → 0.6.0

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.
package/dist/index.d.ts CHANGED
@@ -176,6 +176,7 @@ declare function squiggleBabelPlugin(_api: PluginApi, options?: SquiggleBabelOpt
176
176
  visitor: {
177
177
  Identifier(path: NodePath): void;
178
178
  Program: {
179
+ enter(path: NodePath, state: BabelState): void;
179
180
  exit(path: NodePath, state: BabelState): void;
180
181
  };
181
182
  JSXElement: {
package/dist/index.js CHANGED
@@ -1,2 +1,33 @@
1
- var T=Object.defineProperty;var K=Object.getOwnPropertyNames;var Z=Object.getOwnPropertyDescriptor;var Q=Object.prototype.hasOwnProperty;function Y(t){return this[t]}var ee=(t)=>{var e=(C??=new WeakMap).get(t),n;if(e)return e;e=T({},"__esModule",{value:true});if(t&&typeof t==="object"||typeof t==="function"){for(var a of K(t))if(!Q.call(e,a))T(e,a,{get:Y.bind(t,a),enumerable:!(n=Z(t,a))||n.enumerable})}C.set(t,e);return e};var C;var ne=(t)=>t;function te(t,e){this[t]=ne.bind(null,e)}var re=(t,e)=>{for(var n in e)T(t,n,{get:e[n],enumerable:true,configurable:true,set:te.bind(e,n)})};var Ae={};re(Ae,{default:()=>U});module.exports=ee(Ae);var H=require("node:module");var b=require("node:path");var k="@squiggle.sh/react-native";var F=`${k}/release`;var O="__SQUIGGLE_SOURCE_ROOT__";var _="node_modules/";function ie(t){const e=t.lastIndexOf(_);return e===-1?t:_+t.slice(e+_.length)}var oe="__squiggleStoreName";function se(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 ae={modules:new Set(["zustand","zustand/vanilla","zustand/react"]),names:new Set(["create","createStore"]),helper:"__squiggleInstrumentZustandFactory",namesStores:true};var le={modules:new Set(["react-native-mmkv"]),names:new Set(["createMMKV","useMMKV"]),helper:"__squiggleInstrumentMmkvFactory"};var ue={modules:new Set(["@react-native-async-storage/async-storage"]),names:new Set,defaultExport:true,helper:"__squiggleInstrumentAsyncStorage"};var ce={modules:new Set(["@op-engineering/op-sqlite"]),names:new Set(["open"]),helper:"__squiggleInstrumentOpSqliteFactory"};var de={modules:new Set(["expo-sqlite"]),names:new Set(["openDatabaseAsync","openDatabaseSync","useSQLiteContext"]),helper:"__squiggleInstrumentExpoSqliteFactory"};var ge={modules:new Set(["@legendapp/state"]),names:new Set(["observable"]),helper:"__squiggleInstrumentLegendStateFactory",namesStores:true};var pe={modules:new Set(["@legendapp/state/react"]),names:new Set(["useObservable"]),helper:"__squiggleInstrumentLegendStateFactory"};function h(t){return/^[A-Z]/.test(t)}function j(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 M(t){const e=t?.[0];if(e?.type!=="VariableDeclaration")return false;return e.declarations?.some((n)=>n.init?.callee?.name==="_c")??false}function A(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 j(n)||M(n)}return false}if(t.type==="FunctionExpression"||t.type==="FunctionDeclaration"){const e=t.body;return j(e?.body)||M(e?.body)}return false}var J={memo:"type",forwardRef:"render"};function fe(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 J?e:undefined}function me(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 ye(t){let e=t;const n=[];const a=(c)=>e.get(c);for(let c=0;c<8;c++){const g=e.node;if(g.type==="AssignmentExpression"){e=a("right");continue}if(g.type!=="CallExpression"||!g.arguments?.[0])break;const w=fe(g.callee);if(w!==undefined)n.push(J[w]);else if(!me(e,g.callee))return;e=a("arguments.0")}if(n.length===0)return;const u=e.node;if(u.type!=="ArrowFunctionExpression"&&u.type!=="FunctionExpression"){return}return A(u)?{fnPath:e,fields:n}:undefined}function Se(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 be(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 we(t){try{const e=H.createRequire(b.join(t,"package.json"))("@squiggle.sh/react-native/release-opt-in");return e.keepsSquiggleInRelease(t)}catch{return true}}function he(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:a,arguments:u}=n.node;const c=a.type==="Import"||a.type==="Identifier"&&a.name==="require";const g=u[0];if(c&&g?.value===k)u[0]=t.stringLiteral(F)}}}}function ke(t){if(!t)return false;if(t.type==="JSXIdentifier"){const e=typeof t.name==="string"?t.name:"";return h(e)&&e!=="Fragment"}if(t.type==="JSXMemberExpression"){return t.property?.name!=="Fragment"}return false}function Ee(t,e,n,a){const u=JSON.stringify(e);const c=n.map((g)=>`{ file: ${u}, line: ${g.line}, col: ${g.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} = [${c}]);`,`}`].join(`
2
- `)}function Ne(t){const e=t.callee;if(!e)return;if(e.type==="Identifier"&&typeof e.name==="string"){return q(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"&&q(e.property.name)){return{hook:e.property.name,expr:`${e.object.name}.${e.property.name}`}}return}function q(t){return t==="use"||/^use[A-Z0-9]/.test(t)}var Pe=new Set(["TSAsExpression","TSNonNullExpression","TSSatisfiesExpression","TypeCastExpression","ParenthesizedExpression"]);function xe(t){let e=t.node;let n=t.parentPath;while(n&&Pe.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 c=u.elements?.[0];return c?.type==="Identifier"?c.name:undefined}if(u.type==="ObjectPattern"){const c=(u.properties??[]).map((g)=>g.type==="ObjectProperty"?g.key?.name:undefined).filter((g)=>typeof g==="string");return c.length>0?`{ ${c.join(", ")} }`:undefined}return}var ve=new Set(["use","useState","useReducer","useRef","useMemo","useCallback","useEffect","useLayoutEffect","useInsertionEffect","useImperativeHandle","useContext","useId","useTransition","useDeferredValue","useSyncExternalStore","useDebugValue","useOptimistic","useActionState","useFormState","useFormStatus","useEffectEvent"]);function Ie(t){const e=[];t.traverse({Function(n){n.skip()},CallExpression:{exit(n){const a=Ne(n.node);if(!a)return;const u={hook:a.hook};const c=xe(n);if(c!==undefined)u.name=c;if(!ve.has(a.hook))u.ref=a.expr;e.push(u)}}});return e}function Te(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 _e(t){const e=t.parentPath?.node.type;return e==="Program"||e==="ExportNamedDeclaration"||e==="ExportDefaultDeclaration"}function U(t,e={}){const n=t.types;const a=e.rootDir??process.cwd();if(be(t)&&!(e.release??we(a))){return he(n)}const u=e.callSites??Se(t);const c=[];if(e.instrumentZustand!==false)c.push(ae);if(e.instrumentMmkv!==false)c.push(le);if(e.instrumentAsyncStorage!==false)c.push(ue);if(e.instrumentSqlite!==false)c.push(ce,de);if(e.instrumentLegendState!==false)c.push(ge,pe);const g=e.helperModule??e.zustandHelperModule??k;const w=e.sourceAnnotations===false||e.stripInProduction===true&&process.env["NODE_ENV"]==="production";function E(r){const i=b.isAbsolute(r)?b.relative(a,r).split("\\").join("/"):r;return ie(i)}function N(r,i){let o=i.squiggleFileLocal;if(!o){o=r.scope.generateUidIdentifier("sqFile").name;i.squiggleFileLocal=o;const s=E(i.filename??"<unknown>");r.scope.getProgramParent().path.unshiftContainer("body",n.variableDeclaration("const",[n.variableDeclarator(n.identifier(o),n.stringLiteral(s))]))}return o}function V(r,i,o){const s=r.node;if(s.type!=="ImportDeclaration")return;if(!o.modules.has(s.source.value))return;const l=[];for(const p of s.specifiers){if(p.type==="ImportDefaultSpecifier"){if(!o.defaultExport)continue}else if(p.type==="ImportSpecifier"){const S=p.imported;const D=S?.type==="Identifier"?S.name:undefined;if(!D||!o.names.has(D))continue}else{continue}const f=p.local.name;const y=r.scope.generateUidIdentifier(f).name;p.local=n.identifier(y);l.push({localName:f,uid:y})}if(l.length===0)return;if(o.namesStores)for(const{localName:p}of l)(i.squiggleStoreFactories??=new Set).add(p);const d=R(r.scope,i,o.helper);const m=l.map(({localName:p,uid:f})=>n.variableDeclarator(n.identifier(p),n.callExpression(n.identifier(d),[n.identifier(f)])));r.insertAfter(n.variableDeclaration("const",m))}function R(r,i,o){const s=i.squiggleHelperLocals??={};const l=s[o];if(l)return l;const d=r.generateUidIdentifier("squiggleInstrument").name;s[o]=d;r.getProgramParent().path.unshiftContainer("body",n.importDeclaration([n.importSpecifier(n.identifier(d),n.identifier(o))],n.stringLiteral(g)));return d}function G(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 d=r.parentPath;const m=d?.parentPath?.node.type==="ExportNamedDeclaration"?d.parentPath.parentPath:d?.parentPath;if(m?.node.type!=="Program")return;const p=s.init;if(p?.type!=="CallExpression")return;const f=p.callee;const y=f?.type==="Identifier"?f.name:f?.type==="CallExpression"&&f.arguments?.length===0&&f.callee?.type==="Identifier"?f.callee.name:undefined;if(y===undefined||!o.has(y))return;const S=n.logicalExpression("||",n.identifier(R(r.scope,i,oe)),n.arrowFunctionExpression([n.identifier("_n"),n.identifier("s")],n.identifier("s")));s.init=n.callExpression(S,[n.stringLiteral(l.name),p,n.stringLiteral(se(E(i.filename??"<unknown>")))])}function B(r,i){return i.reduce((o,s)=>n.memberExpression(o,n.identifier(s)),n.identifier(r))}function P(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 x(r){const i=r.node.loc?.start;return{line:i?.line??0,col:i?.column??0}}const L=new WeakSet;function X(r,i){const o=r.node;if(L.has(o))return;if(!ke(o.openingElement?.name))return;const s=o.loc?.start;if(!s||typeof s.line!=="number")return;const l=r.scope.getProgramParent();const d=i.squiggleSites??=[];const m=i.squiggleTagLocal??=l.generateUidIdentifier("sqTag").name;i.squiggleSitesLocal??=l.generateUidIdentifier("sqSites").name;i.squiggleSitesInit??=l.generateUidIdentifier("sqSitesInit").name;const p=d.length;d.push({line:s.line,col:s.column??0});L.add(o);const f=n.callExpression(n.identifier(m),[o,n.numericLiteral(p)]);const y=r.parentPath?.node.type;const S=y==="JSXElement"||y==="JSXFragment"||y==="JSXAttribute";r.replaceWith(S?n.jsxExpressionContainer(f):f)}function W(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(Ee({tag:i.squiggleTagLocal,sites:i.squiggleSitesLocal,init:i.squiggleSitesInit},E(i.filename??"<unknown>"),o,a)))}function v(r){return u&&!(r.filename??"").includes("/node_modules/")}function I(r,i,o,s,l=i){if(!v(s)||!t.template)return;if(!h(i)&&!q(i))return;if(!_e(o))return;const d=Ie(r);if(d.length===0)return;o.insertAfter(t.template.statements.ast(Te(l,d)))}function z(r,i,o){const s=ye(r.get("init"));if(s===undefined||!r.parentPath)return;const{fnPath:l,fields:d}=s;const m=r.parentPath;try{I(l,i,m,o,[i,...d].join("."))}catch{}const{line:p,col:f}=x(r);m.insertAfter(P(i,N(r,o),p,f,d));if(l.node.id?.name===undefined){m.insertAfter(n.expressionStatement(n.assignmentExpression("=",n.memberExpression(B(i,d),n.identifier("displayName")),n.stringLiteral(i))))}}return{name:"squiggle",visitor:{Identifier(r){if(!u)return;try{if(r.node.name!==O)return;const i=r;if(!i.isReferencedIdentifier())return;if(i.scope.hasBinding(O))return;i.replaceWith(n.stringLiteral(a))}catch{}},Program:{exit(r,i){if(!v(i))return;try{W(r,i)}catch{}}},JSXElement:{exit(r,i){if(!v(i))return;try{X(r,i)}catch{}}},ImportDeclaration(r,i){for(const o of c){try{V(r,i,o)}catch{}}},FunctionDeclaration(r,i){try{const o=r.node.id?.name;if(o)I(r,o,r,i)}catch{}if(w)return;try{const o=r.node.id?.name;if(!o||!h(o))return;if(!A(r.node))return;const{line:s,col:l}=x(r);r.insertAfter(P(o,N(r,i),s,l))}catch{}},VariableDeclarator(r,i){try{G(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(w)return;try{const o=r.node.id;if(!o||o.type!=="Identifier")return;const s=o.name;if(!s||!h(s))return;const l=r.node.init;if(!l)return;if(l.type==="CallExpression"){z(r,s,i);return}if(!A(l))return;const{line:d,col:m}=x(r);r.parentPath?.insertAfter(P(s,N(r,i),d,m))}catch{}}}}}
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') {
2
+ (function () {
3
+ var renderers = new Map();
4
+ var roots = new Map();
5
+ var nextId = 1;
6
+ var rootsOf = function (id) {
7
+ var set = roots.get(id);
8
+ if (set === undefined) {
9
+ set = new Set();
10
+ roots.set(id, set);
11
+ }
12
+ return set;
13
+ };
14
+ globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
15
+ isDisabled: false,
16
+ supportsFiber: true,
17
+ renderers: renderers,
18
+ inject: function (renderer) {
19
+ var id = nextId++;
20
+ renderers.set(id, renderer);
21
+ return id;
22
+ },
23
+ getFiberRoots: rootsOf,
24
+ onScheduleFiberRoot: function () {},
25
+ onCommitFiberRoot: function (id, root) {
26
+ rootsOf(id).add(root);
27
+ },
28
+ onCommitFiberUnmount: function () {},
29
+ onPostCommitFiberRoot: function () {},
30
+ };
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{}}}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiggle.sh/babel-plugin",
3
- "version": "0.5.4",
3
+ "version": "0.6.0",
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.5.4"
39
+ "@squiggle.sh/react-native": "0.6.0"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@squiggle.sh/react-native": {