@squiggle.sh/babel-plugin 0.4.0 → 0.5.1
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 +38 -0
- package/dist/index.js +2 -2
- package/package.json +2 -2
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
|
|
2
|
-
`)}function
|
|
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.
|
|
3
|
+
"version": "0.5.1",
|
|
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.5.1"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@squiggle.sh/react-native": {
|