@squiggle.sh/babel-plugin 0.2.0 → 0.3.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
@@ -12,6 +12,8 @@ type BabelTypes = {
12
12
  variableDeclaration(kind: string, declarations: unknown[]): unknown;
13
13
  variableDeclarator(id: unknown, init: unknown): unknown;
14
14
  callExpression(callee: unknown, args: unknown[]): unknown;
15
+ logicalExpression(operator: '||', left: unknown, right: unknown): unknown;
16
+ arrowFunctionExpression(params: unknown[], body: unknown): unknown;
15
17
  jsxExpressionContainer(expr: unknown): unknown;
16
18
  arrayExpression(elements: unknown[]): unknown;
17
19
  };
@@ -72,6 +74,17 @@ type Scope = {
72
74
  generateUidIdentifier(name: string): {
73
75
  name: string;
74
76
  };
77
+ getBinding(name: string): {
78
+ path: {
79
+ node: {
80
+ init?: {
81
+ callee?: {
82
+ name?: string;
83
+ };
84
+ };
85
+ };
86
+ };
87
+ } | undefined;
75
88
  getProgramParent(): Scope & {
76
89
  path: ProgramPath;
77
90
  };
@@ -98,6 +111,7 @@ type BabelState = {
98
111
  filename?: string;
99
112
  squiggleHelperLocals?: Record<string, string>;
100
113
  squiggleFileLocal?: string;
114
+ squiggleStoreFactories?: Set<string>;
101
115
  squiggleSites?: {
102
116
  line: number;
103
117
  col: number;
@@ -121,6 +135,7 @@ interface SquiggleBabelOptions {
121
135
  declare function squiggleBabelPlugin(_api: PluginApi, options?: SquiggleBabelOptions): {
122
136
  name: string;
123
137
  visitor: {
138
+ Identifier(path: NodePath): void;
124
139
  Program: {
125
140
  exit(path: NodePath, state: BabelState): void;
126
141
  };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var{defineProperty:I,getOwnPropertyNames:h,getOwnPropertyDescriptor:m}=Object,r=Object.prototype.hasOwnProperty;function c(q){return this[q]}var p=(q)=>{var j=(N??=new WeakMap).get(q),z;if(j)return j;if(j=I({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var Y of h(q))if(!r.call(j,Y))I(j,Y,{get:c.bind(q,Y),enumerable:!(z=m(q,Y))||z.enumerable})}return N.set(q,j),j},N;var n=(q)=>q;function i(q,j){this[q]=n.bind(null,j)}var o=(q,j)=>{for(var z in j)I(q,z,{get:j[z],enumerable:!0,configurable:!0,set:i.bind(j,z)})};var xj={};o(xj,{default:()=>_});module.exports=p(xj);var v=require("node:path"),s="@squiggle.sh/react-native",k="node_modules/";function l(q){let j=q.lastIndexOf(k);return j===-1?q:k+q.slice(j+k.length)}var e={modules:new Set(["zustand","zustand/vanilla","zustand/react"]),names:new Set(["create","createStore"]),helper:"__squiggleInstrumentZustandFactory"},a={modules:new Set(["react-native-mmkv"]),names:new Set(["createMMKV","useMMKV"]),helper:"__squiggleInstrumentMmkvFactory"},t={modules:new Set(["@react-native-async-storage/async-storage"]),names:new Set,defaultExport:!0,helper:"__squiggleInstrumentAsyncStorage"},jj={modules:new Set(["@op-engineering/op-sqlite"]),names:new Set(["open"]),helper:"__squiggleInstrumentOpSqliteFactory"},qj={modules:new Set(["expo-sqlite"]),names:new Set(["openDatabaseAsync","openDatabaseSync","useSQLiteContext"]),helper:"__squiggleInstrumentExpoSqliteFactory"},zj={modules:new Set(["@legendapp/state"]),names:new Set(["observable"]),helper:"__squiggleInstrumentLegendStateFactory"},Wj={modules:new Set(["@legendapp/state/react"]),names:new Set(["useObservable"]),helper:"__squiggleInstrumentLegendStateFactory"};function M(q){return/^[A-Z]/.test(q)}function b(q){if(!q)return!1;return q.some((j)=>{let z=j;return z?.type==="ReturnStatement"&&(z.argument?.type==="JSXElement"||z.argument?.type==="JSXFragment")})}function E(q){let j=q?.[0];if(j?.type!=="VariableDeclaration")return!1;return j.declarations?.some((z)=>z.init?.callee?.name==="_c")??!1}function T(q){if(q.type==="ArrowFunctionExpression"){let j=q.body;if(!j)return!1;if(j.type==="JSXElement"||j.type==="JSXFragment")return!0;if(j.type==="BlockStatement"){let z=j.body;return b(z)||E(z)}return!1}if(q.type==="FunctionExpression"||q.type==="FunctionDeclaration"){let j=q.body;return b(j?.body)||E(j?.body)}return!1}function $j(q){let j=()=>(process.env.BABEL_ENV??"production")==="development";if(typeof q.caller!=="function")return j();return q.caller((z)=>z?.isDev??j())===!0}function Bj(q){if(!q)return!1;if(q.type==="JSXIdentifier"){let j=typeof q.name==="string"?q.name:"";return M(j)&&j!=="Fragment"}if(q.type==="JSXMemberExpression")return q.property?.name!=="Fragment";return!1}function Xj(q,j,z,Y){let X=JSON.stringify(j),Q=z.map((V)=>`{ file: ${X}, line: ${V.line}, col: ${V.col} }`).join(", ");return[`function ${q.tag}(e, i) {`," if (e != null && typeof e === 'object' && e.props) {"," var m = globalThis.__squiggleSites || (globalThis.__squiggleSites = new WeakMap());",` m.set(e.props, (${q.sites} || ${q.init}())[i]);`," }"," return e;","}",`var ${q.sites};`,`function ${q.init}() {`,` globalThis.__squiggleSourceRoot || (globalThis.__squiggleSourceRoot = ${JSON.stringify(Y)});`,` return (${q.sites} = [${Q}]);`,"}"].join(`
2
- `)}function Yj(q){let j=q.callee;if(!j)return;if(j.type==="Identifier"&&typeof j.name==="string")return O(j.name)?{hook:j.name,expr:j.name}:void 0;if(j.type==="MemberExpression"&&!j.computed&&j.object?.type==="Identifier"&&j.property?.type==="Identifier"&&typeof j.property.name==="string"&&O(j.property.name))return{hook:j.property.name,expr:`${j.object.name}.${j.property.name}`};return}function O(q){return q==="use"||/^use[A-Z0-9]/.test(q)}var Zj=new Set(["TSAsExpression","TSNonNullExpression","TSSatisfiesExpression","TypeCastExpression","ParenthesizedExpression"]);function Kj(q){let{node:j,parentPath:z}=q;while(z&&Zj.has(z.node.type))j=z.node,z=z.parentPath;let Y=z?.node;if(Y?.type!=="VariableDeclarator"||Y.init!==j)return;let X=Y.id;if(!X)return;if(X.type==="Identifier")return X.name;if(X.type==="ArrayPattern"){let Q=X.elements?.[0];return Q?.type==="Identifier"?Q.name:void 0}if(X.type==="ObjectPattern"){let Q=(X.properties??[]).map((V)=>V.type==="ObjectProperty"?V.key?.name:void 0).filter((V)=>typeof V==="string");return Q.length>0?`{ ${Q.join(", ")} }`:void 0}return}var Qj=new Set(["use","useState","useReducer","useRef","useMemo","useCallback","useEffect","useLayoutEffect","useInsertionEffect","useImperativeHandle","useContext","useId","useTransition","useDeferredValue","useSyncExternalStore","useDebugValue","useOptimistic","useActionState","useFormState","useFormStatus","useEffectEvent"]);function Fj(q){let j=[];return q.traverse({Function(z){z.skip()},CallExpression:{exit(z){let Y=Yj(z.node);if(!Y)return;let X={hook:Y.hook},Q=Kj(z);if(Q!==void 0)X.name=Q;if(!Qj.has(Y.hook))X.ref=Y.expr;j.push(X)}}}),j}function Vj(q,j){let z=j.map((Y)=>{let X=[`hook: ${JSON.stringify(Y.hook)}`];if(Y.name!==void 0)X.push(`name: ${JSON.stringify(Y.name)}`);if(Y.ref!==void 0)X.push(`ref: function () { return ${Y.ref}; }`);return`{ ${X.join(", ")} }`});return`${q}.__squiggleHooks = [${z.join(", ")}];`}function Jj(q){let j=q.parentPath?.node.type;return j==="Program"||j==="ExportNamedDeclaration"||j==="ExportDefaultDeclaration"}function _(q,j={}){let z=q.types,Y=j.callSites??$j(q),X=[];if(j.instrumentZustand!==!1)X.push(e);if(j.instrumentMmkv!==!1)X.push(a);if(j.instrumentAsyncStorage!==!1)X.push(t);if(j.instrumentSqlite!==!1)X.push(jj,qj);if(j.instrumentLegendState!==!1)X.push(zj,Wj);let Q=j.helperModule??j.zustandHelperModule??s,V=j.rootDir??process.cwd(),u=j.stripInProduction===!0&&!0;function y(W){let B=v.isAbsolute(W)?v.relative(V,W).split("\\").join("/"):W;return l(B)}function D(W,B){let $=B.squiggleFileLocal;if(!$){$=W.scope.generateUidIdentifier("sqFile").name,B.squiggleFileLocal=$;let Z=y(B.filename??"<unknown>");W.scope.getProgramParent().path.unshiftContainer("body",z.variableDeclaration("const",[z.variableDeclarator(z.identifier($),z.stringLiteral(Z))]))}return $}function g(W,B,$){let Z=W.node;if(Z.type!=="ImportDeclaration")return;if(!$.modules.has(Z.source.value))return;let K=[];for(let F of Z.specifiers){if(F.type==="ImportDefaultSpecifier"){if(!$.defaultExport)continue}else if(F.type==="ImportSpecifier"){let L=F.imported,S=L?.type==="Identifier"?L.name:void 0;if(!S||!$.names.has(S))continue}else continue;let x=F.local.name,w=W.scope.generateUidIdentifier(x).name;F.local=z.identifier(w),K.push({localName:x,uid:w})}if(K.length===0)return;let U=B.squiggleHelperLocals??={},J=U[$.helper];if(!J){J=W.scope.generateUidIdentifier("squiggleInstrument").name,U[$.helper]=J;let F=z.importDeclaration([z.importSpecifier(z.identifier(J),z.identifier($.helper))],z.stringLiteral(Q));W.scope.getProgramParent().path.unshiftContainer("body",F)}let H=K.map(({localName:F,uid:x})=>z.variableDeclarator(z.identifier(F),z.callExpression(z.identifier(J),[z.identifier(x)])));W.insertAfter(z.variableDeclaration("const",H))}function R(W,B,$,Z){return z.expressionStatement(z.assignmentExpression("=",z.memberExpression(z.identifier(W),z.identifier("__squiggleSrc")),z.objectExpression([z.objectProperty(z.identifier("file"),z.identifier(B)),z.objectProperty(z.identifier("line"),z.numericLiteral($)),z.objectProperty(z.identifier("col"),z.numericLiteral(Z))])))}function C(W){let B=W.node.loc?.start;return{line:B?.line??0,col:B?.column??0}}let P=new WeakSet;function f(W,B){let $=W.node;if(P.has($))return;if(!Bj($.openingElement?.name))return;let Z=$.loc?.start;if(!Z||typeof Z.line!=="number")return;let K=W.scope.getProgramParent(),U=B.squiggleSites??=[],J=B.squiggleTagLocal??=K.generateUidIdentifier("sqTag").name;B.squiggleSitesLocal??=K.generateUidIdentifier("sqSites").name,B.squiggleSitesInit??=K.generateUidIdentifier("sqSitesInit").name;let H=U.length;U.push({line:Z.line,col:Z.column??0}),P.add($);let F=z.callExpression(z.identifier(J),[$,z.numericLiteral(H)]),x=W.parentPath?.node.type,w=x==="JSXElement"||x==="JSXFragment"||x==="JSXAttribute";W.replaceWith(w?z.jsxExpressionContainer(F):F)}function d(W,B){let $=B.squiggleSites;if(!$||$.length===0)return;if(!q.template)return;W.scope.getProgramParent().path.unshiftContainer("body",q.template.statements.ast(Xj({tag:B.squiggleTagLocal,sites:B.squiggleSitesLocal,init:B.squiggleSitesInit},y(B.filename??"<unknown>"),$,V)))}function G(W){return Y&&!(W.filename??"").includes("/node_modules/")}function A(W,B,$,Z){if(!G(Z)||!q.template)return;if(!M(B)&&!O(B))return;if(!Jj($))return;let K=Fj(W);if(K.length===0)return;$.insertAfter(q.template.statements.ast(Vj(B,K)))}return{name:"squiggle",visitor:{Program:{exit(W,B){if(!G(B))return;try{d(W,B)}catch{}}},JSXElement:{exit(W,B){if(!G(B))return;try{f(W,B)}catch{}}},ImportDeclaration(W,B){for(let $ of X)try{g(W,B,$)}catch{}},FunctionDeclaration(W,B){try{let $=W.node.id?.name;if($)A(W,$,W,B)}catch{}if(u)return;try{let $=W.node.id?.name;if(!$||!M($))return;if(!T(W.node))return;let{line:Z,col:K}=C(W);W.insertAfter(R($,D(W,B),Z,K))}catch{}},VariableDeclarator(W,B){try{let $=W.node.id,Z=W.node.init;if($?.type==="Identifier"&&$.name&&W.parentPath&&(Z?.type==="ArrowFunctionExpression"||Z?.type==="FunctionExpression"))A(W.get("init"),$.name,W.parentPath,B)}catch{}if(u)return;try{let $=W.node.id;if(!$||$.type!=="Identifier")return;let Z=$.name;if(!Z||!M(Z))return;let K=W.node.init;if(!K)return;if(!T(K))return;let{line:U,col:J}=C(W);W.parentPath?.insertAfter(R(Z,D(W,B),U,J))}catch{}}}}}
1
+ var N=Object.defineProperty;var p=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 p($))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.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 o(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"){o(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{}}}}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiggle.sh/babel-plugin",
3
- "version": "0.2.0",
3
+ "version": "0.3.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.2.0"
39
+ "@squiggle.sh/react-native": "0.3.0"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@squiggle.sh/react-native": {