@squiggle.sh/babel-plugin 0.2.1 → 0.4.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 +5 -0
- package/dist/index.js +2 -2
- package/package.json +2 -2
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;
|
|
@@ -128,10 +131,12 @@ interface SquiggleBabelOptions {
|
|
|
128
131
|
rootDir?: string;
|
|
129
132
|
callSites?: boolean;
|
|
130
133
|
stripInProduction?: boolean;
|
|
134
|
+
sourceAnnotations?: boolean;
|
|
131
135
|
}
|
|
132
136
|
declare function squiggleBabelPlugin(_api: PluginApi, options?: SquiggleBabelOptions): {
|
|
133
137
|
name: string;
|
|
134
138
|
visitor: {
|
|
139
|
+
Identifier(path: NodePath): void;
|
|
135
140
|
Program: {
|
|
136
141
|
exit(path: NodePath, state: BabelState): void;
|
|
137
142
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)}function
|
|
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{}}}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squiggle.sh/babel-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.4.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@squiggle.sh/react-native": {
|