@squiggle.sh/babel-plugin 0.4.0 → 0.5.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
@@ -132,8 +132,46 @@ interface SquiggleBabelOptions {
132
132
  callSites?: boolean;
133
133
  stripInProduction?: boolean;
134
134
  sourceAnnotations?: boolean;
135
+ release?: boolean;
135
136
  }
136
137
  declare function squiggleBabelPlugin(_api: PluginApi, options?: SquiggleBabelOptions): {
138
+ name: string;
139
+ visitor: {
140
+ ImportDeclaration: (path: {
141
+ node: {
142
+ source?: {
143
+ value?: unknown;
144
+ } | null;
145
+ };
146
+ }) => void;
147
+ ExportNamedDeclaration: (path: {
148
+ node: {
149
+ source?: {
150
+ value?: unknown;
151
+ } | null;
152
+ };
153
+ }) => void;
154
+ ExportAllDeclaration: (path: {
155
+ node: {
156
+ source?: {
157
+ value?: unknown;
158
+ } | null;
159
+ };
160
+ }) => void;
161
+ CallExpression(path: {
162
+ node: {
163
+ callee: {
164
+ type: string;
165
+ name?: string;
166
+ };
167
+ arguments: {
168
+ type: string;
169
+ value?: unknown;
170
+ }[];
171
+ };
172
+ }): void;
173
+ };
174
+ } | {
137
175
  name: string;
138
176
  visitor: {
139
177
  Identifier(path: NodePath): void;
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var N=Object.defineProperty;var o=Object.getOwnPropertyNames;var n=Object.getOwnPropertyDescriptor;var i=Object.prototype.hasOwnProperty;function s($){return this[$]}var l=($)=>{var q=(T??=new WeakMap).get($),z;if(q)return q;q=N({},"__esModule",{value:true});if($&&typeof $==="object"||typeof $==="function"){for(var K of o($))if(!i.call(q,K))N(q,K,{get:s.bind($,K),enumerable:!(z=n($,K))||z.enumerable})}T.set($,q);return q};var T;var a=($)=>$;function t($,q){this[$]=a.bind(null,q)}var e=($,q)=>{for(var z in q)N($,z,{get:q[z],enumerable:true,configurable:true,set:t.bind(q,z)})};var yq={};e(yq,{default:()=>d});module.exports=l(yq);var I=require("node:path");var qq="@squiggle.sh/react-native";var g="__SQUIGGLE_SOURCE_ROOT__";var u="node_modules/";function zq($){const q=$.lastIndexOf(u);return q===-1?$:u+$.slice(q+u.length)}var $q="__squiggleStoreName";function Xq($){let q=2166136261;for(let z=0;z<$.length;z++){q^=$.charCodeAt(z);q=Math.imul(q,16777619)>>>0}return q.toString(16).padStart(8,"0")}var Yq={modules:new Set(["zustand","zustand/vanilla","zustand/react"]),names:new Set(["create","createStore"]),helper:"__squiggleInstrumentZustandFactory",namesStores:true};var Zq={modules:new Set(["react-native-mmkv"]),names:new Set(["createMMKV","useMMKV"]),helper:"__squiggleInstrumentMmkvFactory"};var jq={modules:new Set(["@react-native-async-storage/async-storage"]),names:new Set,defaultExport:true,helper:"__squiggleInstrumentAsyncStorage"};var Qq={modules:new Set(["@op-engineering/op-sqlite"]),names:new Set(["open"]),helper:"__squiggleInstrumentOpSqliteFactory"};var Bq={modules:new Set(["expo-sqlite"]),names:new Set(["openDatabaseAsync","openDatabaseSync","useSQLiteContext"]),helper:"__squiggleInstrumentExpoSqliteFactory"};var Kq={modules:new Set(["@legendapp/state"]),names:new Set(["observable"]),helper:"__squiggleInstrumentLegendStateFactory",namesStores:true};var Wq={modules:new Set(["@legendapp/state/react"]),names:new Set(["useObservable"]),helper:"__squiggleInstrumentLegendStateFactory"};function H($){return/^[A-Z]/.test($)}function _($){if(!$)return false;return $.some((q)=>{const z=q;return z?.type==="ReturnStatement"&&(z.argument?.type==="JSXElement"||z.argument?.type==="JSXFragment")})}function E($){const q=$?.[0];if(q?.type!=="VariableDeclaration")return false;return q.declarations?.some((z)=>z.init?.callee?.name==="_c")??false}function L($){if($.type==="ArrowFunctionExpression"){const q=$.body;if(!q)return false;if(q.type==="JSXElement"||q.type==="JSXFragment")return true;if(q.type==="BlockStatement"){const z=q.body;return _(z)||E(z)}return false}if($.type==="FunctionExpression"||$.type==="FunctionDeclaration"){const q=$.body;return _(q?.body)||E(q?.body)}return false}var f={memo:"type",forwardRef:"render"};function Vq($){if($?.type==="SequenceExpression"){$=$.expressions?.at(-1)}if(!$)return;const q=$.type==="Identifier"?$.name:$.type==="MemberExpression"?$.property?.name:undefined;return typeof q==="string"&&q in f?q:undefined}function Jq($,q){if(q?.type!=="Identifier"||typeof q.name!=="string"){return false}const z=$.scope.getBinding(q.name)?.path.node.init;return z?.callee?.name==="$RefreshSig$"}function Fq($){let q=$;const z=[];const K=(V)=>q.get(V);for(let V=0;V<8;V++){const J=q.node;if(J.type==="AssignmentExpression"){q=K("right");continue}if(J.type!=="CallExpression"||!J.arguments?.[0])break;const G=Vq(J.callee);if(G!==undefined)z.push(f[G]);else if(!Jq(q,J.callee))return;q=K("arguments.0")}if(z.length===0)return;const Q=q.node;if(Q.type!=="ArrowFunctionExpression"&&Q.type!=="FunctionExpression"){return}return L(Q)?{fnPath:q,fields:z}:undefined}function Uq($){const q=()=>(process.env["BABEL_ENV"]??process.env["NODE_ENV"])==="development";if(typeof $.caller!=="function")return q();return $.caller((z)=>z?.isDev??q())===true}function xq($){if(!$)return false;if($.type==="JSXIdentifier"){const q=typeof $.name==="string"?$.name:"";return H(q)&&q!=="Fragment"}if($.type==="JSXMemberExpression"){return $.property?.name!=="Fragment"}return false}function vq($,q,z,K){const Q=JSON.stringify(q);const V=z.map((J)=>`{ file: ${Q}, line: ${J.line}, col: ${J.col} }`).join(", ");return[`function ${$.tag}(e, i) {`,` if (e != null && typeof e === 'object' && e.props) {`,` var m = globalThis.__squiggleSites || (globalThis.__squiggleSites = new WeakMap());`,` m.set(e.props, (${$.sites} || ${$.init}())[i]);`,` }`,` return e;`,`}`,`var ${$.sites};`,`function ${$.init}() {`,` globalThis.__squiggleSourceRoot || (globalThis.__squiggleSourceRoot = ${JSON.stringify(K)});`,` return (${$.sites} = [${V}]);`,`}`].join(`
2
- `)}function Mq($){const q=$.callee;if(!q)return;if(q.type==="Identifier"&&typeof q.name==="string"){return A(q.name)?{hook:q.name,expr:q.name}:undefined}if(q.type==="MemberExpression"&&!q.computed&&q.object?.type==="Identifier"&&q.property?.type==="Identifier"&&typeof q.property.name==="string"&&A(q.property.name)){return{hook:q.property.name,expr:`${q.object.name}.${q.property.name}`}}return}function A($){return $==="use"||/^use[A-Z0-9]/.test($)}var Gq=new Set(["TSAsExpression","TSNonNullExpression","TSSatisfiesExpression","TypeCastExpression","ParenthesizedExpression"]);function Hq($){let q=$.node;let z=$.parentPath;while(z&&Gq.has(z.node.type)){q=z.node;z=z.parentPath}const K=z?.node;if(K?.type!=="VariableDeclarator"||K.init!==q){return}const Q=K.id;if(!Q)return;if(Q.type==="Identifier")return Q.name;if(Q.type==="ArrayPattern"){const V=Q.elements?.[0];return V?.type==="Identifier"?V.name:undefined}if(Q.type==="ObjectPattern"){const V=(Q.properties??[]).map((J)=>J.type==="ObjectProperty"?J.key?.name:undefined).filter((J)=>typeof J==="string");return V.length>0?`{ ${V.join(", ")} }`:undefined}return}var Iq=new Set(["use","useState","useReducer","useRef","useMemo","useCallback","useEffect","useLayoutEffect","useInsertionEffect","useImperativeHandle","useContext","useId","useTransition","useDeferredValue","useSyncExternalStore","useDebugValue","useOptimistic","useActionState","useFormState","useFormStatus","useEffectEvent"]);function kq($){const q=[];$.traverse({Function(z){z.skip()},CallExpression:{exit(z){const K=Mq(z.node);if(!K)return;const Q={hook:K.hook};const V=Hq(z);if(V!==undefined)Q.name=V;if(!Iq.has(K.hook))Q.ref=K.expr;q.push(Q)}}});return q}function wq($,q){const z=q.map((K)=>{const Q=[`hook: ${JSON.stringify(K.hook)}`];if(K.name!==undefined)Q.push(`name: ${JSON.stringify(K.name)}`);if(K.ref!==undefined){Q.push(`ref: function () { return ${K.ref}; }`)}return`{ ${Q.join(", ")} }`});return`${$}.__squiggleHooks = [${z.join(", ")}];`}function Dq($){const q=$.parentPath?.node.type;return q==="Program"||q==="ExportNamedDeclaration"||q==="ExportDefaultDeclaration"}function d($,q={}){const z=$.types;const K=q.callSites??Uq($);const Q=[];if(q.instrumentZustand!==false)Q.push(Yq);if(q.instrumentMmkv!==false)Q.push(Zq);if(q.instrumentAsyncStorage!==false)Q.push(jq);if(q.instrumentSqlite!==false)Q.push(Qq,Bq);if(q.instrumentLegendState!==false)Q.push(Kq,Wq);const V=q.helperModule??q.zustandHelperModule??qq;const J=q.rootDir??process.cwd();const G=q.sourceAnnotations===false||q.stripInProduction===true&&process.env["NODE_ENV"]==="production";function k(X){const Y=I.isAbsolute(X)?I.relative(J,X).split("\\").join("/"):X;return zq(Y)}function w(X,Y){let Z=Y.squiggleFileLocal;if(!Z){Z=X.scope.generateUidIdentifier("sqFile").name;Y.squiggleFileLocal=Z;const j=k(Y.filename??"<unknown>");X.scope.getProgramParent().path.unshiftContainer("body",z.variableDeclaration("const",[z.variableDeclarator(z.identifier(Z),z.stringLiteral(j))]))}return Z}function m(X,Y,Z){const j=X.node;if(j.type!=="ImportDeclaration")return;if(!Z.modules.has(j.source.value))return;const B=[];for(const F of j.specifiers){if(F.type==="ImportDefaultSpecifier"){if(!Z.defaultExport)continue}else if(F.type==="ImportSpecifier"){const M=F.imported;const b=M?.type==="Identifier"?M.name:undefined;if(!b||!Z.names.has(b))continue}else{continue}const U=F.local.name;const v=X.scope.generateUidIdentifier(U).name;F.local=z.identifier(v);B.push({localName:U,uid:v})}if(B.length===0)return;if(Z.namesStores)for(const{localName:F}of B)(Y.squiggleStoreFactories??=new Set).add(F);const W=R(X.scope,Y,Z.helper);const x=B.map(({localName:F,uid:U})=>z.variableDeclarator(z.identifier(F),z.callExpression(z.identifier(W),[z.identifier(U)])));X.insertAfter(z.variableDeclaration("const",x))}function R(X,Y,Z){const j=Y.squiggleHelperLocals??={};const B=j[Z];if(B)return B;const W=X.generateUidIdentifier("squiggleInstrument").name;j[Z]=W;X.getProgramParent().path.unshiftContainer("body",z.importDeclaration([z.importSpecifier(z.identifier(W),z.identifier(Z))],z.stringLiteral(V)));return W}function h(X,Y){const Z=Y.squiggleStoreFactories;if(!Z)return;const j=X.node;const B=j.id;if(B?.type!=="Identifier"||!B.name)return;const W=X.parentPath;const x=W?.parentPath?.node.type==="ExportNamedDeclaration"?W.parentPath.parentPath:W?.parentPath;if(x?.node.type!=="Program")return;const F=j.init;if(F?.type!=="CallExpression")return;const U=F.callee;const v=U?.type==="Identifier"?U.name:U?.type==="CallExpression"&&U.arguments?.length===0&&U.callee?.type==="Identifier"?U.callee.name:undefined;if(v===undefined||!Z.has(v))return;const M=z.logicalExpression("||",z.identifier(R(X.scope,Y,$q)),z.arrowFunctionExpression([z.identifier("_n"),z.identifier("s")],z.identifier("s")));j.init=z.callExpression(M,[z.stringLiteral(B.name),F,z.stringLiteral(Xq(k(Y.filename??"<unknown>")))])}function S(X,Y){return Y.reduce((Z,j)=>z.memberExpression(Z,z.identifier(j)),z.identifier(X))}function D(X,Y,Z,j,B=[]){return z.expressionStatement(z.assignmentExpression("=",z.memberExpression(S(X,B),z.identifier("__squiggleSrc")),z.objectExpression([z.objectProperty(z.identifier("file"),z.identifier(Y)),z.objectProperty(z.identifier("line"),z.numericLiteral(Z)),z.objectProperty(z.identifier("col"),z.numericLiteral(j))])))}function y(X){const Y=X.node.loc?.start;return{line:Y?.line??0,col:Y?.column??0}}const P=new WeakSet;function r(X,Y){const Z=X.node;if(P.has(Z))return;if(!xq(Z.openingElement?.name))return;const j=Z.loc?.start;if(!j||typeof j.line!=="number")return;const B=X.scope.getProgramParent();const W=Y.squiggleSites??=[];const x=Y.squiggleTagLocal??=B.generateUidIdentifier("sqTag").name;Y.squiggleSitesLocal??=B.generateUidIdentifier("sqSites").name;Y.squiggleSitesInit??=B.generateUidIdentifier("sqSitesInit").name;const F=W.length;W.push({line:j.line,col:j.column??0});P.add(Z);const U=z.callExpression(z.identifier(x),[Z,z.numericLiteral(F)]);const v=X.parentPath?.node.type;const M=v==="JSXElement"||v==="JSXFragment"||v==="JSXAttribute";X.replaceWith(M?z.jsxExpressionContainer(U):U)}function c(X,Y){const Z=Y.squiggleSites;if(!Z||Z.length===0)return;if(!$.template)return;X.scope.getProgramParent().path.unshiftContainer("body",$.template.statements.ast(vq({tag:Y.squiggleTagLocal,sites:Y.squiggleSitesLocal,init:Y.squiggleSitesInit},k(Y.filename??"<unknown>"),Z,J)))}function O(X){return K&&!(X.filename??"").includes("/node_modules/")}function C(X,Y,Z,j,B=Y){if(!O(j)||!$.template)return;if(!H(Y)&&!A(Y))return;if(!Dq(Z))return;const W=kq(X);if(W.length===0)return;Z.insertAfter($.template.statements.ast(wq(B,W)))}function p(X,Y,Z){const j=Fq(X.get("init"));if(j===undefined||!X.parentPath)return;const{fnPath:B,fields:W}=j;const x=X.parentPath;try{C(B,Y,x,Z,[Y,...W].join("."))}catch{}const{line:F,col:U}=y(X);x.insertAfter(D(Y,w(X,Z),F,U,W));if(B.node.id?.name===undefined){x.insertAfter(z.expressionStatement(z.assignmentExpression("=",z.memberExpression(S(Y,W),z.identifier("displayName")),z.stringLiteral(Y))))}}return{name:"squiggle",visitor:{Identifier(X){if(!K)return;try{if(X.node.name!==g)return;const Y=X;if(!Y.isReferencedIdentifier())return;if(Y.scope.hasBinding(g))return;Y.replaceWith(z.stringLiteral(J))}catch{}},Program:{exit(X,Y){if(!O(Y))return;try{c(X,Y)}catch{}}},JSXElement:{exit(X,Y){if(!O(Y))return;try{r(X,Y)}catch{}}},ImportDeclaration(X,Y){for(const Z of Q){try{m(X,Y,Z)}catch{}}},FunctionDeclaration(X,Y){try{const Z=X.node.id?.name;if(Z)C(X,Z,X,Y)}catch{}if(G)return;try{const Z=X.node.id?.name;if(!Z||!H(Z))return;if(!L(X.node))return;const{line:j,col:B}=y(X);X.insertAfter(D(Z,w(X,Y),j,B))}catch{}},VariableDeclarator(X,Y){try{h(X,Y)}catch{}try{const Z=X.node.id;const j=X.node.init;if(Z?.type==="Identifier"&&Z.name&&X.parentPath&&(j?.type==="ArrowFunctionExpression"||j?.type==="FunctionExpression")){C(X.get("init"),Z.name,X.parentPath,Y)}}catch{}if(G)return;try{const Z=X.node.id;if(!Z||Z.type!=="Identifier")return;const j=Z.name;if(!j||!H(j))return;const B=X.node.init;if(!B)return;if(B.type==="CallExpression"){p(X,j,Y);return}if(!L(B))return;const{line:W,col:x}=y(X);X.parentPath?.insertAfter(D(j,w(X,Y),W,x))}catch{}}}}}
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{}}}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiggle.sh/babel-plugin",
3
- "version": "0.4.0",
3
+ "version": "0.5.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.4.0"
39
+ "@squiggle.sh/react-native": "0.5.0"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@squiggle.sh/react-native": {