@tanstack/devtools-vite 0.6.0 → 0.7.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/esm/ast-utils.d.ts +11 -0
- package/dist/esm/ast-utils.js +44 -0
- package/dist/esm/ast-utils.js.map +1 -0
- package/dist/esm/devtools-packages.d.ts +2 -0
- package/dist/esm/devtools-packages.js +15 -0
- package/dist/esm/devtools-packages.js.map +1 -0
- package/dist/esm/editor.js +18 -27
- package/dist/esm/editor.js.map +1 -1
- package/dist/esm/enhance-logs.d.ts +4 -1
- package/dist/esm/enhance-logs.js +48 -81
- package/dist/esm/enhance-logs.js.map +1 -1
- package/dist/esm/index.js +1 -5
- package/dist/esm/inject-plugin.d.ts +5 -4
- package/dist/esm/inject-plugin.js +171 -220
- package/dist/esm/inject-plugin.js.map +1 -1
- package/dist/esm/inject-source.d.ts +4 -1
- package/dist/esm/inject-source.js +111 -205
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/matcher.js +11 -16
- package/dist/esm/matcher.js.map +1 -1
- package/dist/esm/offset-to-loc.d.ts +14 -0
- package/dist/esm/offset-to-loc.js +29 -0
- package/dist/esm/offset-to-loc.js.map +1 -0
- package/dist/esm/package-manager.js +90 -131
- package/dist/esm/package-manager.js.map +1 -1
- package/dist/esm/plugin.js +329 -448
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.d.ts +4 -1
- package/dist/esm/remove-devtools.js +93 -134
- package/dist/esm/remove-devtools.js.map +1 -1
- package/dist/esm/utils.js +149 -162
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/virtual-console.js +170 -14
- package/dist/esm/virtual-console.js.map +1 -1
- package/package.json +4 -10
- package/skills/devtools-vite-plugin/SKILL.md +7 -7
- package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
- package/src/ast-utils.ts +54 -0
- package/src/devtools-packages.ts +12 -0
- package/src/enhance-logs.ts +48 -78
- package/src/inject-plugin.test.ts +200 -147
- package/src/inject-plugin.ts +195 -238
- package/src/inject-source.test.ts +165 -115
- package/src/inject-source.ts +183 -231
- package/src/offset-to-loc.ts +39 -0
- package/src/plugin.ts +35 -29
- package/src/remove-devtools.test.ts +76 -57
- package/src/remove-devtools.ts +160 -168
- package/src/virtual-console.test.ts +172 -1
- package/src/virtual-console.ts +146 -7
- package/dist/esm/babel.d.ts +0 -18
- package/dist/esm/babel.js +0 -19
- package/dist/esm/babel.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/src/babel.ts +0 -18
|
@@ -1,208 +1,114 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { forEachChild } from "./ast-utils.js";
|
|
2
|
+
import { createLocMapper } from "./offset-to-loc.js";
|
|
3
3
|
import { matcher } from "./matcher.js";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
(attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === "data-tsd-source"
|
|
86
|
-
);
|
|
87
|
-
const hasSpread = element.node.attributes.some(
|
|
88
|
-
(attr) => attr.type === "JSXSpreadAttribute" && attr.argument.type === "Identifier" && attr.argument.name === propsName
|
|
89
|
-
);
|
|
90
|
-
if (hasSpread || hasDataSource) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
element.node.attributes.push(
|
|
94
|
-
t.jsxAttribute(
|
|
95
|
-
t.jsxIdentifier("data-tsd-source"),
|
|
96
|
-
t.stringLiteral(`${file}:${line}:${column + 1}`)
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
return true;
|
|
100
|
-
};
|
|
101
|
-
const transform = (ast, file, ignorePatterns) => {
|
|
102
|
-
let didTransform = false;
|
|
103
|
-
trav(ast, {
|
|
104
|
-
FunctionDeclaration(functionDeclaration) {
|
|
105
|
-
const propsName = getPropsNameFromFunctionDeclaration(
|
|
106
|
-
functionDeclaration.node
|
|
107
|
-
);
|
|
108
|
-
functionDeclaration.traverse({
|
|
109
|
-
JSXOpeningElement(element) {
|
|
110
|
-
const transformed = transformJSX(
|
|
111
|
-
element,
|
|
112
|
-
propsName,
|
|
113
|
-
file,
|
|
114
|
-
ignorePatterns
|
|
115
|
-
);
|
|
116
|
-
if (transformed) {
|
|
117
|
-
didTransform = true;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
},
|
|
122
|
-
ArrowFunctionExpression(path) {
|
|
123
|
-
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
124
|
-
path.traverse({
|
|
125
|
-
JSXOpeningElement(element) {
|
|
126
|
-
const transformed = transformJSX(
|
|
127
|
-
element,
|
|
128
|
-
propsName,
|
|
129
|
-
file,
|
|
130
|
-
ignorePatterns
|
|
131
|
-
);
|
|
132
|
-
if (transformed) {
|
|
133
|
-
didTransform = true;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
},
|
|
138
|
-
FunctionExpression(path) {
|
|
139
|
-
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
140
|
-
path.traverse({
|
|
141
|
-
JSXOpeningElement(element) {
|
|
142
|
-
const transformed = transformJSX(
|
|
143
|
-
element,
|
|
144
|
-
propsName,
|
|
145
|
-
file,
|
|
146
|
-
ignorePatterns
|
|
147
|
-
);
|
|
148
|
-
if (transformed) {
|
|
149
|
-
didTransform = true;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
VariableDeclaration(path) {
|
|
155
|
-
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
156
|
-
return decl.init?.type === "ArrowFunctionExpression" || decl.init?.type === "FunctionExpression";
|
|
157
|
-
});
|
|
158
|
-
if (!functionDeclaration) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration);
|
|
162
|
-
path.traverse({
|
|
163
|
-
JSXOpeningElement(element) {
|
|
164
|
-
const transformed = transformJSX(
|
|
165
|
-
element,
|
|
166
|
-
propsName,
|
|
167
|
-
file,
|
|
168
|
-
ignorePatterns
|
|
169
|
-
);
|
|
170
|
-
if (transformed) {
|
|
171
|
-
didTransform = true;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return didTransform;
|
|
178
|
-
};
|
|
4
|
+
import { normalizePath } from "vite";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
|
+
import { parseSync } from "oxc-parser";
|
|
7
|
+
//#region src/inject-source.ts
|
|
8
|
+
/**
|
|
9
|
+
* Extract the props name from a function's first parameter.
|
|
10
|
+
* Handles: `(props) => {}`, `({ ...props }) => {}`, `function(props) {}`
|
|
11
|
+
*/
|
|
12
|
+
function getPropsName(params) {
|
|
13
|
+
const first = params[0];
|
|
14
|
+
if (!first) return null;
|
|
15
|
+
if (first.type === "Identifier") return first.name;
|
|
16
|
+
if (first.type === "ObjectPattern") {
|
|
17
|
+
for (const prop of first.properties) if (prop.type === "RestElement" && prop.argument.type === "Identifier") return prop.argument.name;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the display name from a JSX element name node.
|
|
23
|
+
*/
|
|
24
|
+
function getNameOfElement(name) {
|
|
25
|
+
if (name.type === "JSXIdentifier") return name.name;
|
|
26
|
+
if (name.type === "JSXMemberExpression") return `${getNameOfElement(name.object)}.${name.property.name}`;
|
|
27
|
+
return `${name.namespace.name}:${name.name.name}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Check whether a JSXOpeningElement should receive `data-tsd-source`.
|
|
31
|
+
*/
|
|
32
|
+
function shouldTransform(node, propsName, ignorePatterns) {
|
|
33
|
+
const nameOfElement = getNameOfElement(node.name);
|
|
34
|
+
if (nameOfElement === "Fragment" || nameOfElement === "React.Fragment") return false;
|
|
35
|
+
if (matcher(ignorePatterns, nameOfElement)) return false;
|
|
36
|
+
if (node.attributes.some((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "data-tsd-source")) return false;
|
|
37
|
+
if (node.attributes.some((a) => a.type === "JSXSpreadAttribute" && a.argument.type === "Identifier" && a.argument.name === propsName)) return false;
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
function isFunctionLike(node) {
|
|
41
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Walk a subtree collecting JSXOpeningElements owned by the current function.
|
|
45
|
+
* Function-like nodes are traversal boundaries so inner components keep their
|
|
46
|
+
* own propsName context and aren't annotated with an outer function's props.
|
|
47
|
+
*/
|
|
48
|
+
function collectJsx(node, out) {
|
|
49
|
+
if (node.type === "JSXOpeningElement") {
|
|
50
|
+
out.push(node);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (isFunctionLike(node)) return;
|
|
54
|
+
forEachChild(node, (child) => collectJsx(child, out));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Walk the AST to find all function-like nodes (in document order).
|
|
58
|
+
* For each, extract propsName and collect JSX elements from its body.
|
|
59
|
+
*/
|
|
60
|
+
function visitFunctions(node, annotated, file, ignorePatterns, offsetToLoc, s, code) {
|
|
61
|
+
let didTransform = false;
|
|
62
|
+
const processFunction = (params, body) => {
|
|
63
|
+
const propsName = getPropsName(params);
|
|
64
|
+
const jsxNodes = [];
|
|
65
|
+
collectJsx(body, jsxNodes);
|
|
66
|
+
for (const jsx of jsxNodes) {
|
|
67
|
+
if (annotated.has(jsx.start)) continue;
|
|
68
|
+
if (!shouldTransform(jsx, propsName, ignorePatterns)) continue;
|
|
69
|
+
const loc = offsetToLoc(jsx.start);
|
|
70
|
+
const attrStr = ` data-tsd-source="${file}:${loc.line}:${loc.column + 1}"`;
|
|
71
|
+
if (jsx.selfClosing) s.appendLeft(jsx.end - 2, attrStr);
|
|
72
|
+
else s.appendLeft(jsx.end - 1, attrStr);
|
|
73
|
+
annotated.add(jsx.start);
|
|
74
|
+
didTransform = true;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression") {
|
|
78
|
+
if (node.body) processFunction(node.params, node.body);
|
|
79
|
+
} else if (node.type === "ArrowFunctionExpression") processFunction(node.params, node.body);
|
|
80
|
+
forEachChild(node, (child) => {
|
|
81
|
+
if (visitFunctions(child, annotated, file, ignorePatterns, offsetToLoc, s, code)) didTransform = true;
|
|
82
|
+
});
|
|
83
|
+
return didTransform;
|
|
84
|
+
}
|
|
179
85
|
function addSourceToJsx(code, id, ignore = {}) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
86
|
+
const filePath = id.split("?")[0];
|
|
87
|
+
const location = filePath.replace(normalizePath(process.cwd()), "");
|
|
88
|
+
if (matcher(ignore.files || [], location)) return;
|
|
89
|
+
try {
|
|
90
|
+
const result = parseSync(filePath, code, {
|
|
91
|
+
sourceType: "module",
|
|
92
|
+
lang: "tsx"
|
|
93
|
+
});
|
|
94
|
+
if (result.errors.length > 0) return;
|
|
95
|
+
const offsetToLoc = createLocMapper(code);
|
|
96
|
+
const s = new MagicString(code);
|
|
97
|
+
const annotated = /* @__PURE__ */ new Set();
|
|
98
|
+
if (!visitFunctions(result.program, annotated, location, ignore.components || [], offsetToLoc, s, code)) return;
|
|
99
|
+
return {
|
|
100
|
+
code: s.toString(),
|
|
101
|
+
map: s.generateMap({
|
|
102
|
+
source: filePath,
|
|
103
|
+
file: id,
|
|
104
|
+
includeContent: true
|
|
105
|
+
})
|
|
106
|
+
};
|
|
107
|
+
} catch (e) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
204
110
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
//# sourceMappingURL=inject-source.js.map
|
|
111
|
+
//#endregion
|
|
112
|
+
export { addSourceToJsx };
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=inject-source.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-source.js","sources":["../../src/inject-source.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport { gen, parse, t, trav } from './babel'\nimport { matcher } from './matcher'\nimport type { types as Babel, NodePath } from '@babel/core'\nimport type { ParseResult } from '@babel/parser'\n\nconst getPropsNameFromFunctionDeclaration = (\n functionDeclaration:\n | t.VariableDeclarator\n | t.FunctionExpression\n | t.FunctionDeclaration\n | t.ArrowFunctionExpression,\n) => {\n let propsName: string | null = null\n\n if (functionDeclaration.type === 'FunctionExpression') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n if (functionDeclaration.type === 'ArrowFunctionExpression') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n if (functionDeclaration.type === 'FunctionDeclaration') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n // Arrow function case\n if (\n functionDeclaration.init?.type === 'ArrowFunctionExpression' ||\n functionDeclaration.init?.type === 'FunctionExpression'\n ) {\n const firstArgument = functionDeclaration.init.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n }\n return propsName\n}\n\nconst getNameOfElement = (\n element: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,\n): string => {\n if (element.type === 'JSXIdentifier') {\n return element.name\n }\n if (element.type === 'JSXMemberExpression') {\n return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`\n }\n\n return `${element.namespace.name}:${element.name.name}`\n}\n\nconst transformJSX = (\n element: NodePath<t.JSXOpeningElement>,\n propsName: string | null,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\n const loc = element.node.loc\n if (!loc) return\n const line = loc.start.line\n const column = loc.start.column\n const nameOfElement = getNameOfElement(element.node.name)\n const isIgnored = matcher(ignorePatterns, nameOfElement)\n if (\n nameOfElement === 'Fragment' ||\n nameOfElement === 'React.Fragment' ||\n isIgnored\n ) {\n return\n }\n const hasDataSource = element.node.attributes.some(\n (attr) =>\n attr.type === 'JSXAttribute' &&\n attr.name.type === 'JSXIdentifier' &&\n attr.name.name === 'data-tsd-source',\n )\n // Check if props are spread\n const hasSpread = element.node.attributes.some(\n (attr) =>\n attr.type === 'JSXSpreadAttribute' &&\n attr.argument.type === 'Identifier' &&\n attr.argument.name === propsName,\n )\n\n if (hasSpread || hasDataSource) {\n // Do not inject if props are spread\n return\n }\n\n // Inject data-source as a string: \"<file>:<line>:<column>\"\n element.node.attributes.push(\n t.jsxAttribute(\n t.jsxIdentifier('data-tsd-source'),\n t.stringLiteral(`${file}:${line}:${column + 1}`),\n ),\n )\n\n return true\n}\n\nconst transform = (\n ast: ParseResult<Babel.File>,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\n let didTransform = false\n\n trav(ast, {\n FunctionDeclaration(functionDeclaration) {\n const propsName = getPropsNameFromFunctionDeclaration(\n functionDeclaration.node,\n )\n functionDeclaration.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n ArrowFunctionExpression(path) {\n const propsName = getPropsNameFromFunctionDeclaration(path.node)\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n FunctionExpression(path) {\n const propsName = getPropsNameFromFunctionDeclaration(path.node)\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n VariableDeclaration(path) {\n const functionDeclaration = path.node.declarations.find((decl) => {\n return (\n decl.init?.type === 'ArrowFunctionExpression' ||\n decl.init?.type === 'FunctionExpression'\n )\n })\n if (!functionDeclaration) {\n return\n }\n const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration)\n\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(\n code: string,\n id: string,\n ignore: {\n files?: Array<string | RegExp>\n components?: Array<string | RegExp>\n } = {},\n) {\n const [filePath] = id.split('?')\n // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n const location = filePath?.replace(normalizePath(process.cwd()), '')!\n\n const fileIgnored = matcher(ignore.files || [], location)\n if (fileIgnored) {\n return\n }\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location, ignore.components || [])\n if (!didTransform) {\n return\n }\n return gen(ast, {\n sourceMaps: true,\n retainLines: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return\n }\n}\n"],"names":[],"mappings":";;;;;AAMA,MAAM,sCAAsC,CAC1C,wBAKG;AACH,MAAI,YAA2B;AAE/B,MAAI,oBAAoB,SAAS,sBAAsB;AACrD,UAAM,gBAAgB,oBAAoB,OAAO,CAAC;AAElD,QAAI,iBAAiB,cAAc,SAAS,cAAc;AACxD,kBAAY,cAAc;AAAA,IAC5B;AAEA,QAAI,iBAAiB,cAAc,SAAS,iBAAiB;AAC3D,oBAAc,WAAW,QAAQ,CAAC,SAAS;AACzC,YACE,KAAK,SAAS,iBACd,KAAK,SAAS,SAAS,cACvB;AACA,sBAAY,KAAK,SAAS;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACH;AACA,WAAO;AAAA,EACT;AACA,MAAI,oBAAoB,SAAS,2BAA2B;AAC1D,UAAM,gBAAgB,oBAAoB,OAAO,CAAC;AAElD,QAAI,iBAAiB,cAAc,SAAS,cAAc;AACxD,kBAAY,cAAc;AAAA,IAC5B;AAEA,QAAI,iBAAiB,cAAc,SAAS,iBAAiB;AAC3D,oBAAc,WAAW,QAAQ,CAAC,SAAS;AACzC,YACE,KAAK,SAAS,iBACd,KAAK,SAAS,SAAS,cACvB;AACA,sBAAY,KAAK,SAAS;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACH;AACA,WAAO;AAAA,EACT;AACA,MAAI,oBAAoB,SAAS,uBAAuB;AACtD,UAAM,gBAAgB,oBAAoB,OAAO,CAAC;AAElD,QAAI,iBAAiB,cAAc,SAAS,cAAc;AACxD,kBAAY,cAAc;AAAA,IAC5B;AAEA,QAAI,iBAAiB,cAAc,SAAS,iBAAiB;AAC3D,oBAAc,WAAW,QAAQ,CAAC,SAAS;AACzC,YACE,KAAK,SAAS,iBACd,KAAK,SAAS,SAAS,cACvB;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,MAAA,CACD;AAAA,IACH;AAAA,IACA,oBAAoB,MAAM;AACxB,YAAM,sBAAsB,KAAK,KAAK,aAAa,KAAK,CAAC,SAAS;AAChE,eACE,KAAK,MAAM,SAAS,6BACpB,KAAK,MAAM,SAAS;AAAA,MAExB,CAAC;AACD,UAAI,CAAC,qBAAqB;AACxB;AAAA,MACF;AACA,YAAM,YAAY,oCAAoC,mBAAmB;AAEzE,WAAK,SAAS;AAAA,QACZ,kBAAkB,SAAS;AACzB,gBAAM,cAAc;AAAA,YAClB;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAEF,cAAI,aAAa;AACf,2BAAe;AAAA,UACjB;AAAA,QACF;AAAA,MAAA,CACD;AAAA,IACH;AAAA,EAAA,CACD;AAED,SAAO;AACT;AAEO,SAAS,eACd,MACA,IACA,SAGI,CAAA,GACJ;AACA,QAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAE/B,QAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,IAAA,CAAK,GAAG,EAAE;AAEnE,QAAM,cAAc,QAAQ,OAAO,SAAS,CAAA,GAAI,QAAQ;AACxD,MAAI,aAAa;AACf;AAAA,EACF;AACA,MAAI;AACF,UAAM,MAAM,MAAM,MAAM;AAAA,MACtB,YAAY;AAAA,MACZ,SAAS,CAAC,OAAO,YAAY;AAAA,IAAA,CAC9B;AACD,UAAM,eAAe,UAAU,KAAK,UAAU,OAAO,cAAc,EAAE;AACrE,QAAI,CAAC,cAAc;AACjB;AAAA,IACF;AACA,WAAO,IAAI,KAAK;AAAA,MACd,YAAY;AAAA,MACZ,aAAa;AAAA,MACb,UAAU;AAAA,MACV,gBAAgB;AAAA,IAAA,CACjB;AAAA,EACH,SAAS,GAAG;AACV;AAAA,EACF;AACF;"}
|
|
1
|
+
{"version":3,"file":"inject-source.js","names":[],"sources":["../../src/inject-source.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport MagicString from 'magic-string'\nimport { parseSync } from 'oxc-parser'\nimport { createLocMapper } from './offset-to-loc'\nimport { matcher } from './matcher'\nimport { forEachChild } from './ast-utils'\nimport type {\n JSXElementName,\n JSXOpeningElement,\n Node,\n ParamPattern,\n} from 'oxc-parser'\nimport type { Loc } from './offset-to-loc'\n\n/**\n * Extract the props name from a function's first parameter.\n * Handles: `(props) => {}`, `({ ...props }) => {}`, `function(props) {}`\n */\nfunction getPropsName(params: Array<ParamPattern>): string | null {\n const first = params[0]\n if (!first) return null\n\n // (props) => {} or function(props) {}\n if (first.type === 'Identifier') return first.name\n\n // ({ ...rest }) => {} or function({ ...rest }) {}\n if (first.type === 'ObjectPattern') {\n for (const prop of first.properties) {\n if (prop.type === 'RestElement' && prop.argument.type === 'Identifier') {\n return prop.argument.name\n }\n }\n }\n\n return null\n}\n\n/**\n * Get the display name from a JSX element name node.\n */\nfunction getNameOfElement(name: JSXElementName): string {\n if (name.type === 'JSXIdentifier') return name.name\n if (name.type === 'JSXMemberExpression') {\n return `${getNameOfElement(name.object)}.${name.property.name}`\n }\n // JSXNamespacedName\n return `${name.namespace.name}:${name.name.name}`\n}\n\n/**\n * Check whether a JSXOpeningElement should receive `data-tsd-source`.\n */\nfunction shouldTransform(\n node: JSXOpeningElement,\n propsName: string | null,\n ignorePatterns: Array<string | RegExp>,\n): boolean {\n const nameOfElement = getNameOfElement(node.name)\n\n if (nameOfElement === 'Fragment' || nameOfElement === 'React.Fragment') {\n return false\n }\n if (matcher(ignorePatterns, nameOfElement)) return false\n\n // Already annotated?\n if (\n node.attributes.some(\n (a) =>\n a.type === 'JSXAttribute' &&\n a.name.type === 'JSXIdentifier' &&\n a.name.name === 'data-tsd-source',\n )\n ) {\n return false\n }\n\n // Props spread?\n if (\n node.attributes.some(\n (a) =>\n a.type === 'JSXSpreadAttribute' &&\n a.argument.type === 'Identifier' &&\n a.argument.name === propsName,\n )\n ) {\n return false\n }\n\n return true\n}\n\nfunction isFunctionLike(node: Node): boolean {\n return (\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression' ||\n node.type === 'ArrowFunctionExpression'\n )\n}\n\n/**\n * Walk a subtree collecting JSXOpeningElements owned by the current function.\n * Function-like nodes are traversal boundaries so inner components keep their\n * own propsName context and aren't annotated with an outer function's props.\n */\nfunction collectJsx(node: Node, out: Array<JSXOpeningElement>) {\n if (node.type === 'JSXOpeningElement') {\n out.push(node)\n return\n }\n if (isFunctionLike(node)) return\n forEachChild(node, (child) => collectJsx(child, out))\n}\n\n/**\n * Walk the AST to find all function-like nodes (in document order).\n * For each, extract propsName and collect JSX elements from its body.\n */\nfunction visitFunctions(\n node: Node,\n annotated: Set<number>,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n offsetToLoc: (offset: number) => Loc,\n s: MagicString,\n code: string,\n): boolean {\n let didTransform = false\n\n const processFunction = (params: Array<ParamPattern>, body: Node) => {\n const propsName = getPropsName(params)\n const jsxNodes: Array<JSXOpeningElement> = []\n collectJsx(body, jsxNodes)\n\n for (const jsx of jsxNodes) {\n if (annotated.has(jsx.start)) continue\n if (!shouldTransform(jsx, propsName, ignorePatterns)) continue\n\n const loc = offsetToLoc(jsx.start)\n const attrStr = ` data-tsd-source=\"${file}:${loc.line}:${loc.column + 1}\"`\n\n // Insert before '>' or '/>' at the end of the opening element\n if (jsx.selfClosing) {\n // ends with '/>' — insert before '/>'\n s.appendLeft(jsx.end - 2, attrStr)\n } else {\n // ends with '>' — insert before '>'\n s.appendLeft(jsx.end - 1, attrStr)\n }\n\n annotated.add(jsx.start)\n didTransform = true\n }\n }\n\n // Check if this node is a function-like node. Variable-initialized\n // functions (`const Foo = () => {}`) are handled when forEachChild reaches\n // their init, so VariableDeclaration doesn't need a special case here.\n if (\n node.type === 'FunctionDeclaration' ||\n node.type === 'FunctionExpression'\n ) {\n if (node.body) processFunction(node.params, node.body)\n } else if (node.type === 'ArrowFunctionExpression') {\n processFunction(node.params, node.body)\n }\n\n // Recurse into children to find nested functions\n forEachChild(node, (child) => {\n if (\n visitFunctions(\n child,\n annotated,\n file,\n ignorePatterns,\n offsetToLoc,\n s,\n code,\n )\n ) {\n didTransform = true\n }\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(\n code: string,\n id: string,\n ignore: {\n files?: Array<string | RegExp>\n components?: Array<string | RegExp>\n } = {},\n) {\n const filePath = id.split('?')[0]!\n const location = filePath.replace(normalizePath(process.cwd()), '')\n\n const fileIgnored = matcher(ignore.files || [], location)\n if (fileIgnored) return\n\n try {\n const result = parseSync(filePath, code, {\n sourceType: 'module',\n lang: 'tsx',\n })\n if (result.errors.length > 0) return\n\n const offsetToLoc = createLocMapper(code)\n const s = new MagicString(code)\n const annotated = new Set<number>()\n\n const didTransform = visitFunctions(\n result.program,\n annotated,\n location,\n ignore.components || [],\n offsetToLoc,\n s,\n code,\n )\n\n if (!didTransform) return\n\n return {\n code: s.toString(),\n map: s.generateMap({\n source: filePath,\n file: id,\n includeContent: true,\n }),\n }\n } catch (e) {\n return\n }\n}\n"],"mappings":";;;;;;;;;;;AAkBA,SAAS,aAAa,QAA4C;CAChE,MAAM,QAAQ,OAAO;CACrB,IAAI,CAAC,OAAO,OAAO;CAGnB,IAAI,MAAM,SAAS,cAAc,OAAO,MAAM;CAG9C,IAAI,MAAM,SAAS;OACZ,MAAM,QAAQ,MAAM,YACvB,IAAI,KAAK,SAAS,iBAAiB,KAAK,SAAS,SAAS,cACxD,OAAO,KAAK,SAAS;;CAK3B,OAAO;;;;;AAMT,SAAS,iBAAiB,MAA8B;CACtD,IAAI,KAAK,SAAS,iBAAiB,OAAO,KAAK;CAC/C,IAAI,KAAK,SAAS,uBAChB,OAAO,GAAG,iBAAiB,KAAK,OAAO,CAAC,GAAG,KAAK,SAAS;CAG3D,OAAO,GAAG,KAAK,UAAU,KAAK,GAAG,KAAK,KAAK;;;;;AAM7C,SAAS,gBACP,MACA,WACA,gBACS;CACT,MAAM,gBAAgB,iBAAiB,KAAK,KAAK;CAEjD,IAAI,kBAAkB,cAAc,kBAAkB,kBACpD,OAAO;CA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I,KAAK,SAAS,2BACvB,gBAAgB,KAAK,QAAQ,KAAK,KAAK;CAIzC,aAAa,OAAO,UAAU;EAC5B,IACE,eACE,OACA,WACA,MACA,gBACA,aACA,GACA,KACD,EAED,eAAe;GAEjB;CAEF,OAAO;;AAGT,SAAgB,eACd,MACA,IACA,SAGI,EAAE,EACN;CACA,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;CAC/B,MAAM,WAAW,SAAS,QAAQ,cAAc,QAAQ,KAAK,CAAC,EAAE,GAAG;CAGnE,IADoB,QAAQ,OAAO,SAAS,EAAE,EAAE,SAC5C,EAAa;CAEjB,IAAI;EACF,MAAM,SAAS,UAAU,UAAU,MAAM;GACvC,YAAY;GACZ,MAAM;GACP,CAAC;EACF,IAAI,OAAO,OAAO,SAAS,GAAG;EAE9B,MAAM,cAAc,gBAAgB,KAAK;EACzC,MAAM,IAAI,IAAI,YAAY,KAAK;EAC/B,MAAM,4BAAY,IAAI,KAAa;EAYnC,IAAI,CAViB,eACnB,OAAO,SACP,WACA,UACA,OAAO,cAAc,EAAE,EACvB,aACA,GACA,KAGG,EAAc;EAEnB,OAAO;GACL,MAAM,EAAE,UAAU;GAClB,KAAK,EAAE,YAAY;IACjB,QAAQ;IACR,MAAM;IACN,gBAAgB;IACjB,CAAC;GACH;UACM,GAAG;EACV"}
|
package/dist/esm/matcher.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
import picomatch from "picomatch";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} else {
|
|
10
|
-
return (s) => pattern.test(s);
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
return matchers.some((isMatch) => isMatch(str));
|
|
2
|
+
//#region src/matcher.ts
|
|
3
|
+
var matcher = (patterns, str) => {
|
|
4
|
+
if (patterns.length === 0) return false;
|
|
5
|
+
return patterns.map((pattern) => {
|
|
6
|
+
if (typeof pattern === "string") return picomatch(pattern);
|
|
7
|
+
else return (s) => pattern.test(s);
|
|
8
|
+
}).some((isMatch) => isMatch(str));
|
|
14
9
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=matcher.js.map
|
|
10
|
+
//#endregion
|
|
11
|
+
export { matcher };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=matcher.js.map
|
package/dist/esm/matcher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matcher.js","sources":["../../src/matcher.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\nexport const matcher = (\n patterns: Array<string | RegExp>,\n str: string,\n): boolean => {\n if (patterns.length === 0) {\n return false\n }\n const matchers = patterns.map((pattern) => {\n if (typeof pattern === 'string') {\n return picomatch(pattern)\n } else {\n return (s: string) => pattern.test(s)\n }\n })\n\n return matchers.some((isMatch) => isMatch(str))\n}\n"],"
|
|
1
|
+
{"version":3,"file":"matcher.js","names":[],"sources":["../../src/matcher.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\nexport const matcher = (\n patterns: Array<string | RegExp>,\n str: string,\n): boolean => {\n if (patterns.length === 0) {\n return false\n }\n const matchers = patterns.map((pattern) => {\n if (typeof pattern === 'string') {\n return picomatch(pattern)\n } else {\n return (s: string) => pattern.test(s)\n }\n })\n\n return matchers.some((isMatch) => isMatch(str))\n}\n"],"mappings":";;AAEA,IAAa,WACX,UACA,QACY;CACZ,IAAI,SAAS,WAAW,GACtB,OAAO;CAUT,OARiB,SAAS,KAAK,YAAY;EACzC,IAAI,OAAO,YAAY,UACrB,OAAO,UAAU,QAAQ;OAEzB,QAAQ,MAAc,QAAQ,KAAK,EAAE;GAIlC,CAAS,MAAM,YAAY,QAAQ,IAAI,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface Loc {
|
|
2
|
+
/** 1-based line number */
|
|
3
|
+
line: number;
|
|
4
|
+
/** 0-based column offset */
|
|
5
|
+
column: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Pre-compute line start offsets for fast byte-offset to line/column conversion.
|
|
9
|
+
* Returns a function that maps a byte offset to `{ line, column }`.
|
|
10
|
+
*
|
|
11
|
+
* Build: O(n) single pass over the source string.
|
|
12
|
+
* Lookup: O(log n) binary search.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createLocMapper(source: string): (offset: number) => Loc;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/offset-to-loc.ts
|
|
2
|
+
/**
|
|
3
|
+
* Pre-compute line start offsets for fast byte-offset to line/column conversion.
|
|
4
|
+
* Returns a function that maps a byte offset to `{ line, column }`.
|
|
5
|
+
*
|
|
6
|
+
* Build: O(n) single pass over the source string.
|
|
7
|
+
* Lookup: O(log n) binary search.
|
|
8
|
+
*/
|
|
9
|
+
function createLocMapper(source) {
|
|
10
|
+
const lineStarts = [0];
|
|
11
|
+
for (let i = 0; i < source.length; i++) if (source.charCodeAt(i) === 10) lineStarts.push(i + 1);
|
|
12
|
+
return (offset) => {
|
|
13
|
+
let lo = 0;
|
|
14
|
+
let hi = lineStarts.length - 1;
|
|
15
|
+
while (lo < hi) {
|
|
16
|
+
const mid = lo + hi + 1 >> 1;
|
|
17
|
+
if (lineStarts[mid] <= offset) lo = mid;
|
|
18
|
+
else hi = mid - 1;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
line: lo + 1,
|
|
22
|
+
column: offset - lineStarts[lo]
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { createLocMapper };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=offset-to-loc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"offset-to-loc.js","names":[],"sources":["../../src/offset-to-loc.ts"],"sourcesContent":["export interface Loc {\n /** 1-based line number */\n line: number\n /** 0-based column offset */\n column: number\n}\n\n/**\n * Pre-compute line start offsets for fast byte-offset to line/column conversion.\n * Returns a function that maps a byte offset to `{ line, column }`.\n *\n * Build: O(n) single pass over the source string.\n * Lookup: O(log n) binary search.\n */\nexport function createLocMapper(source: string): (offset: number) => Loc {\n const lineStarts: Array<number> = [0]\n for (let i = 0; i < source.length; i++) {\n if (source.charCodeAt(i) === 10 /* \\n */) {\n lineStarts.push(i + 1)\n }\n }\n\n return (offset: number): Loc => {\n let lo = 0\n let hi = lineStarts.length - 1\n while (lo < hi) {\n const mid = (lo + hi + 1) >> 1\n if (lineStarts[mid]! <= offset) {\n lo = mid\n } else {\n hi = mid - 1\n }\n }\n return {\n line: lo + 1,\n column: offset - lineStarts[lo]!,\n }\n }\n}\n"],"mappings":";;;;;;;;AAcA,SAAgB,gBAAgB,QAAyC;CACvE,MAAM,aAA4B,CAAC,EAAE;CACrC,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KACjC,IAAI,OAAO,WAAW,EAAE,KAAK,IAC3B,WAAW,KAAK,IAAI,EAAE;CAI1B,QAAQ,WAAwB;EAC9B,IAAI,KAAK;EACT,IAAI,KAAK,WAAW,SAAS;EAC7B,OAAO,KAAK,IAAI;GACd,MAAM,MAAO,KAAK,KAAK,KAAM;GAC7B,IAAI,WAAW,QAAS,QACtB,KAAK;QAEL,KAAK,MAAM;;EAGf,OAAO;GACL,MAAM,KAAK;GACX,QAAQ,SAAS,WAAW;GAC7B"}
|