@squiggle.sh/babel-plugin 0.5.5 → 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 +1 -0
- package/dist/index.js +33 -2
- package/package.json +2 -2
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
|
|
2
|
-
|
|
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.
|
|
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.
|
|
39
|
+
"@squiggle.sh/react-native": "0.6.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@squiggle.sh/react-native": {
|