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