trickle-observe 0.2.113 → 0.2.114

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.
@@ -1726,8 +1726,12 @@ ingestUrl) {
1726
1726
  const catchInsertions = traceVars ? findCatchVars(source) : [];
1727
1727
  // Find function parameter names for tracing
1728
1728
  const funcParamInsertions = traceVars ? findFunctionParams(source, isReactFile) : [];
1729
- // Find JSX text expressions for tracing (React files only)
1730
- const jsxExprInsertions = (traceVars && isReactFile) ? findJsxExpressions(source) : [];
1729
+ // Find JSX text expressions for tracing (React files only).
1730
+ // Skip if JSX has already been compiled to _jsxDEV/jsx/jsxs calls (e.g. by Vite's React plugin).
1731
+ // In that case, the `{` characters in the source are plain JS (function bodies, object literals)
1732
+ // and findJsxExpressions would corrupt them by injecting __trickle_tv() calls.
1733
+ const jsxAlreadyCompiled = /\b_?jsxDEV\b|\bjsxs?\s*\(/.test(source);
1734
+ const jsxExprInsertions = (traceVars && isReactFile && !jsxAlreadyCompiled) ? findJsxExpressions(source) : [];
1731
1735
  if (funcInsertions.length === 0 && importInsertions.length === 0 && varInsertions.length === 0 && destructInsertions.length === 0 && reassignInsertions.length === 0 && forLoopInsertions.length === 0 && catchInsertions.length === 0 && funcParamInsertions.length === 0 && jsxExprInsertions.length === 0 && bodyInsertions.length === 0 && hookInsertions.length === 0 && stateInsertions.length === 0 && conciseBodyInsertions.length === 0)
1732
1736
  return source;
1733
1737
  // Fix line numbers: Vite transforms (TypeScript stripping) may change line numbers.
@@ -1713,8 +1713,12 @@ ingestUrl) {
1713
1713
  const catchInsertions = traceVars ? findCatchVars(source) : [];
1714
1714
  // Find function parameter names for tracing
1715
1715
  const funcParamInsertions = traceVars ? findFunctionParams(source, isReactFile) : [];
1716
- // Find JSX text expressions for tracing (React files only)
1717
- const jsxExprInsertions = (traceVars && isReactFile) ? findJsxExpressions(source) : [];
1716
+ // Find JSX text expressions for tracing (React files only).
1717
+ // Skip if JSX has already been compiled to _jsxDEV/jsx/jsxs calls (e.g. by Vite's React plugin).
1718
+ // In that case, the `{` characters in the source are plain JS (function bodies, object literals)
1719
+ // and findJsxExpressions would corrupt them by injecting __trickle_tv() calls.
1720
+ const jsxAlreadyCompiled = /\b_?jsxDEV\b|\bjsxs?\s*\(/.test(source);
1721
+ const jsxExprInsertions = (traceVars && isReactFile && !jsxAlreadyCompiled) ? findJsxExpressions(source) : [];
1718
1722
  if (funcInsertions.length === 0 && importInsertions.length === 0 && varInsertions.length === 0 && destructInsertions.length === 0 && reassignInsertions.length === 0 && forLoopInsertions.length === 0 && catchInsertions.length === 0 && funcParamInsertions.length === 0 && jsxExprInsertions.length === 0 && bodyInsertions.length === 0 && hookInsertions.length === 0 && stateInsertions.length === 0 && conciseBodyInsertions.length === 0)
1719
1723
  return source;
1720
1724
  // Fix line numbers: Vite transforms (TypeScript stripping) may change line numbers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trickle-observe",
3
- "version": "0.2.113",
3
+ "version": "0.2.114",
4
4
  "description": "Runtime type observability for JavaScript applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1658,8 +1658,12 @@ export function transformEsmSource(
1658
1658
  // Find function parameter names for tracing
1659
1659
  const funcParamInsertions = traceVars ? findFunctionParams(source, isReactFile) : [];
1660
1660
 
1661
- // Find JSX text expressions for tracing (React files only)
1662
- const jsxExprInsertions = (traceVars && isReactFile) ? findJsxExpressions(source) : [];
1661
+ // Find JSX text expressions for tracing (React files only).
1662
+ // Skip if JSX has already been compiled to _jsxDEV/jsx/jsxs calls (e.g. by Vite's React plugin).
1663
+ // In that case, the `{` characters in the source are plain JS (function bodies, object literals)
1664
+ // and findJsxExpressions would corrupt them by injecting __trickle_tv() calls.
1665
+ const jsxAlreadyCompiled = /\b_?jsxDEV\b|\bjsxs?\s*\(/.test(source);
1666
+ const jsxExprInsertions = (traceVars && isReactFile && !jsxAlreadyCompiled) ? findJsxExpressions(source) : [];
1663
1667
 
1664
1668
  if (funcInsertions.length === 0 && importInsertions.length === 0 && varInsertions.length === 0 && destructInsertions.length === 0 && reassignInsertions.length === 0 && forLoopInsertions.length === 0 && catchInsertions.length === 0 && funcParamInsertions.length === 0 && jsxExprInsertions.length === 0 && bodyInsertions.length === 0 && hookInsertions.length === 0 && stateInsertions.length === 0 && conciseBodyInsertions.length === 0) return source;
1665
1669
 
@@ -1 +0,0 @@
1
- export {};
@@ -1,160 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- /**
7
- * Unit tests for the Vite plugin transform (React component tracking).
8
- *
9
- * Run with: node --experimental-strip-types --test src/vite-plugin.test.ts
10
- * Or after build: node --test dist/vite-plugin.test.js
11
- */
12
- const node_test_1 = require("node:test");
13
- const strict_1 = __importDefault(require("node:assert/strict"));
14
- const vite_plugin_js_1 = require("../dist/vite-plugin.js");
15
- // Helper: transform code as if it came from a .tsx file
16
- function transformTsx(code) {
17
- const plugin = (0, vite_plugin_js_1.tricklePlugin)({ debug: false, traceVars: false });
18
- const result = plugin.transform(code, '/test/App.tsx');
19
- return result ? result.code : null;
20
- }
21
- function transformTs(code) {
22
- const plugin = (0, vite_plugin_js_1.tricklePlugin)({ debug: false, traceVars: false });
23
- const result = plugin.transform(code, '/test/util.ts');
24
- return result ? result.code : null;
25
- }
26
- // ── React file detection ─────────────────────────────────────────────────────
27
- (0, node_test_1.describe)('React file detection', () => {
28
- (0, node_test_1.it)('tracks uppercase components in .tsx files', () => {
29
- const code = `function UserCard(props) { return null; }`;
30
- const out = transformTsx(code);
31
- strict_1.default.ok(out, 'should transform');
32
- strict_1.default.ok(out.includes('__trickle_rc'), 'should inject render tracker');
33
- });
34
- (0, node_test_1.it)('does not inject render tracker for .ts files', () => {
35
- const code = `function UserCard(props) { return null; }`;
36
- const out = transformTs(code);
37
- // May still transform for function wrapping, but not for render tracking
38
- if (out) {
39
- strict_1.default.ok(!out.includes('__trickle_rc'), 'should NOT inject render tracker in .ts files');
40
- }
41
- });
42
- (0, node_test_1.it)('does not track lowercase functions as components', () => {
43
- const code = `function helper(x) { return x + 1; }`;
44
- const out = transformTsx(code);
45
- if (out) {
46
- strict_1.default.ok(!out.includes('__trickle_rc'), 'lowercase function should not be tracked');
47
- }
48
- });
49
- });
50
- // ── Props capture: function declarations ─────────────────────────────────────
51
- (0, node_test_1.describe)('Props capture — function declarations', () => {
52
- (0, node_test_1.it)('uses arguments[0] for simple param: function Component(props)', () => {
53
- const code = `function MyComponent(props) { return null; }`;
54
- const out = transformTsx(code);
55
- strict_1.default.ok(out, 'should transform');
56
- strict_1.default.ok(out.includes('arguments[0]'), 'should pass arguments[0] as props');
57
- });
58
- (0, node_test_1.it)('uses arguments[0] for destructured param: function Component({ name })', () => {
59
- const code = `function UserCard({ name, age }) { return null; }`;
60
- const out = transformTsx(code);
61
- strict_1.default.ok(out, 'should transform');
62
- strict_1.default.ok(out.includes('arguments[0]'), 'should pass arguments[0] for destructured params');
63
- });
64
- (0, node_test_1.it)('injects __trickle_rc call at start of function body', () => {
65
- const code = `function MyComponent(props) {\n const x = 1;\n return null;\n}`;
66
- const out = transformTsx(code);
67
- strict_1.default.ok(out, 'should transform');
68
- // __trickle_rc should appear before body statements
69
- const rcIdx = out.indexOf('__trickle_rc');
70
- const bodyIdx = out.indexOf('const x = 1');
71
- strict_1.default.ok(rcIdx !== -1, '__trickle_rc should be present');
72
- strict_1.default.ok(bodyIdx !== -1, 'body code should be present');
73
- strict_1.default.ok(rcIdx < bodyIdx, '__trickle_rc should come before body statements');
74
- });
75
- (0, node_test_1.it)('includes correct component name and line in __trickle_rc call', () => {
76
- const code = `function UserCard(props) { return null; }`;
77
- const out = transformTsx(code);
78
- strict_1.default.ok(out, 'should transform');
79
- strict_1.default.ok(out.includes('"UserCard"'), 'should include component name');
80
- strict_1.default.ok(out.includes('__trickle_rc("UserCard"'), 'should call with component name');
81
- });
82
- });
83
- // ── Props capture: arrow function components ──────────────────────────────────
84
- (0, node_test_1.describe)('Props capture — arrow function components', () => {
85
- (0, node_test_1.it)('uses single param name for simple arrow: const C = (props) => {}', () => {
86
- const code = `const Dashboard = (props) => { return null; };`;
87
- const out = transformTsx(code);
88
- strict_1.default.ok(out, 'should transform');
89
- strict_1.default.ok(out.includes('__trickle_rc'), 'should inject render tracker');
90
- // props should be the param variable, not arguments[0]
91
- strict_1.default.ok(out.includes('__trickle_rc("Dashboard"'), 'should use component name');
92
- // should NOT use arguments[0] for arrow functions
93
- const rcCall = out.match(/__trickle_rc\("Dashboard",[^)]+\)/);
94
- strict_1.default.ok(rcCall, 'should have __trickle_rc call');
95
- strict_1.default.ok(!rcCall[0].includes('arguments[0]'), 'arrow functions should not use arguments[0]');
96
- });
97
- (0, node_test_1.it)('reconstructs object for destructured arrow: const C = ({ a, b }) => {}', () => {
98
- const code = `const Counter = ({ count, label }) => { return null; };`;
99
- const out = transformTsx(code);
100
- strict_1.default.ok(out, 'should transform');
101
- strict_1.default.ok(out.includes('__trickle_rc'), 'should inject render tracker');
102
- // Should reconstruct { count, label }
103
- const rcCall = out.match(/__trickle_rc\("Counter",[^,]+,([^)]+)\)/);
104
- if (rcCall) {
105
- strict_1.default.ok(rcCall[1].includes('count') && rcCall[1].includes('label'), 'should reconstruct props object from destructured fields');
106
- }
107
- });
108
- (0, node_test_1.it)('passes undefined for no-param arrow: const C = () => {}', () => {
109
- const code = `const NoProps = () => { return null; };`;
110
- const out = transformTsx(code);
111
- if (out && out.includes('__trickle_rc')) {
112
- strict_1.default.ok(out.includes('undefined'), 'should pass undefined for no-param component');
113
- }
114
- });
115
- });
116
- // ── render count tracking ─────────────────────────────────────────────────────
117
- (0, node_test_1.describe)('Render count tracking', () => {
118
- (0, node_test_1.it)('includes react_render kind in emitted record code', () => {
119
- const code = `function Card(props) { return null; }`;
120
- const out = transformTsx(code);
121
- strict_1.default.ok(out, 'should transform');
122
- strict_1.default.ok(out.includes("'react_render'"), 'emitted record should have kind react_render');
123
- });
124
- (0, node_test_1.it)('includes props data in emitted record', () => {
125
- const code = `function Card(props) { return null; }`;
126
- const out = transformTsx(code);
127
- strict_1.default.ok(out, 'should transform');
128
- strict_1.default.ok(out.includes('rec.props'), 'should capture props onto the record');
129
- strict_1.default.ok(out.includes('propKeys'), 'should include propKeys');
130
- });
131
- (0, node_test_1.it)('tracks multiple components in one file', () => {
132
- const code = [
133
- `function Header(props) { return null; }`,
134
- `function Footer(props) { return null; }`,
135
- `function helper(x) { return x; }`,
136
- ].join('\n');
137
- const out = transformTsx(code);
138
- strict_1.default.ok(out, 'should transform');
139
- strict_1.default.ok(out.includes('"Header"'), 'should track Header');
140
- strict_1.default.ok(out.includes('"Footer"'), 'should track Footer');
141
- // helper should not be tracked as a component
142
- const rcCalls = out.match(/__trickle_rc\("helper"/g);
143
- strict_1.default.ok(!rcCalls, 'lowercase helper should not be tracked');
144
- });
145
- });
146
- // ── findFunctionBodyBrace — destructured params don't confuse brace finding ───
147
- (0, node_test_1.describe)('Correct function body brace detection', () => {
148
- (0, node_test_1.it)('finds body brace even with destructured object params', () => {
149
- const code = `function Form({ onSubmit, title }) {\n const x = 1;\n return null;\n}`;
150
- const out = transformTsx(code);
151
- strict_1.default.ok(out, 'should transform');
152
- // __trickle_rc should be INSIDE the function body (before 'const x = 1')
153
- const rcIdx = out.indexOf('__trickle_rc');
154
- const bodyIdx = out.indexOf('const x = 1');
155
- strict_1.default.ok(rcIdx < bodyIdx, 'render tracker must be inside the function body, before first statement');
156
- // The wrap insertion should be AFTER the closing brace of the function
157
- const wrapIdx = out.indexOf('__trickle_wrap');
158
- strict_1.default.ok(wrapIdx > bodyIdx, 'function wrap should be after the function body');
159
- });
160
- });