react-code-locator 0.1.9 → 0.1.13
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/README.md +49 -44
- package/dist/babel.cjs +428 -40
- package/dist/babel.cjs.map +1 -1
- package/dist/babel.d.cts +12 -1
- package/dist/babel.d.ts +12 -1
- package/dist/babel.js +425 -29
- package/dist/babel.js.map +1 -1
- package/dist/babelInjectComponentSource.cjs +400 -38
- package/dist/babelInjectComponentSource.cjs.map +1 -1
- package/dist/babelInjectComponentSource.d.cts +3 -4
- package/dist/babelInjectComponentSource.d.ts +3 -4
- package/dist/babelInjectComponentSource.js +400 -28
- package/dist/babelInjectComponentSource.js.map +1 -1
- package/dist/client-sm5wi0uT.d.cts +15 -0
- package/dist/client-sm5wi0uT.d.ts +15 -0
- package/dist/client.cjs +157 -28
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +1 -14
- package/dist/client.d.ts +1 -14
- package/dist/client.js +157 -28
- package/dist/client.js.map +1 -1
- package/dist/esbuild.cjs +616 -0
- package/dist/esbuild.cjs.map +1 -0
- package/dist/esbuild.d.cts +25 -0
- package/dist/esbuild.d.ts +25 -0
- package/dist/esbuild.js +589 -0
- package/dist/esbuild.js.map +1 -0
- package/dist/index.cjs +843 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +830 -29
- package/dist/index.js.map +1 -1
- package/dist/sourceAdapter-DLWo_ABo.d.cts +15 -0
- package/dist/sourceAdapter-DLWo_ABo.d.ts +15 -0
- package/dist/swc.cjs +589 -0
- package/dist/swc.cjs.map +1 -0
- package/dist/swc.d.cts +29 -0
- package/dist/swc.d.ts +29 -0
- package/dist/swc.js +560 -0
- package/dist/swc.js.map +1 -0
- package/dist/vite.cjs +529 -84
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +20 -6
- package/dist/vite.d.ts +20 -6
- package/dist/vite.js +524 -72
- package/dist/vite.js.map +1 -1
- package/dist/webpackRuntimeEntry.cjs +157 -28
- package/dist/webpackRuntimeEntry.cjs.map +1 -1
- package/dist/webpackRuntimeEntry.js +157 -28
- package/dist/webpackRuntimeEntry.js.map +1 -1
- package/package.json +12 -1
package/dist/esbuild.cjs
ADDED
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/esbuild.ts
|
|
21
|
+
var esbuild_exports = {};
|
|
22
|
+
__export(esbuild_exports, {
|
|
23
|
+
createEsbuildSourceAdapter: () => createEsbuildSourceAdapter,
|
|
24
|
+
esbuildSourceAdapter: () => esbuildSourceAdapter,
|
|
25
|
+
esbuildSourceTransformPlugin: () => esbuildSourceTransformPlugin
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(esbuild_exports);
|
|
28
|
+
var import_promises = require("fs/promises");
|
|
29
|
+
|
|
30
|
+
// src/sourceAdapter.ts
|
|
31
|
+
function defineSourceAdapter(descriptor) {
|
|
32
|
+
return descriptor;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/sourceTransform.ts
|
|
36
|
+
var import_core2 = require("@babel/core");
|
|
37
|
+
|
|
38
|
+
// src/babelInjectComponentSource.ts
|
|
39
|
+
var import_core = require("@babel/core");
|
|
40
|
+
|
|
41
|
+
// src/constants.ts
|
|
42
|
+
var SOURCE_PROP = "__componentSourceLoc";
|
|
43
|
+
var JSX_SOURCE_PROP = "$componentSourceLoc";
|
|
44
|
+
var JSX_SOURCE_REGISTRY_SYMBOL = "react-code-locator.jsxSourceRegistry";
|
|
45
|
+
|
|
46
|
+
// src/sourceMetadata.ts
|
|
47
|
+
function normalizeSlashes(value) {
|
|
48
|
+
return value.replace(/\\/g, "/");
|
|
49
|
+
}
|
|
50
|
+
function trimTrailingSlash(value) {
|
|
51
|
+
return value.replace(/\/+$/, "");
|
|
52
|
+
}
|
|
53
|
+
function splitPathSegments(value) {
|
|
54
|
+
return normalizeSlashes(value).split("/").filter(Boolean);
|
|
55
|
+
}
|
|
56
|
+
function computeRelativePath(fromPath, toPath) {
|
|
57
|
+
const fromSegments = splitPathSegments(fromPath);
|
|
58
|
+
const toSegments = splitPathSegments(toPath);
|
|
59
|
+
let sharedIndex = 0;
|
|
60
|
+
while (sharedIndex < fromSegments.length && sharedIndex < toSegments.length && fromSegments[sharedIndex] === toSegments[sharedIndex]) {
|
|
61
|
+
sharedIndex += 1;
|
|
62
|
+
}
|
|
63
|
+
const upSegments = new Array(Math.max(0, fromSegments.length - sharedIndex)).fill("..");
|
|
64
|
+
const downSegments = toSegments.slice(sharedIndex);
|
|
65
|
+
const relativeSegments = [...upSegments, ...downSegments];
|
|
66
|
+
return relativeSegments.length > 0 ? relativeSegments.join("/") : ".";
|
|
67
|
+
}
|
|
68
|
+
function normalizeProjectRoot(projectRoot) {
|
|
69
|
+
if (projectRoot) {
|
|
70
|
+
return trimTrailingSlash(normalizeSlashes(projectRoot));
|
|
71
|
+
}
|
|
72
|
+
return "";
|
|
73
|
+
}
|
|
74
|
+
function toRelativeSource(filename, loc, projectRoot) {
|
|
75
|
+
if (!filename || !loc) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const root = normalizeProjectRoot(projectRoot);
|
|
79
|
+
const normalizedFilename = normalizeSlashes(filename);
|
|
80
|
+
const relPath = root && normalizedFilename.startsWith(`${root}/`) ? normalizedFilename.slice(root.length + 1) : root ? computeRelativePath(root, normalizedFilename) : normalizedFilename;
|
|
81
|
+
return `${relPath}:${loc.line}:${loc.column + 1}`;
|
|
82
|
+
}
|
|
83
|
+
function isProjectLocalFile(filename, projectRoot) {
|
|
84
|
+
if (!filename) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const root = normalizeProjectRoot(projectRoot);
|
|
88
|
+
const normalizedFilename = normalizeSlashes(filename);
|
|
89
|
+
if (!root) {
|
|
90
|
+
return !normalizedFilename.startsWith("../") && !normalizedFilename.startsWith("/") && !/^[A-Za-z]:\//.test(normalizedFilename);
|
|
91
|
+
}
|
|
92
|
+
if (normalizedFilename.startsWith(`${root}/`) || normalizedFilename === root) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
const relativePath = computeRelativePath(root, normalizedFilename);
|
|
96
|
+
return !relativePath.startsWith("../");
|
|
97
|
+
}
|
|
98
|
+
function isExternalToProjectRoot(filename, projectRoot) {
|
|
99
|
+
return !isProjectLocalFile(filename, projectRoot);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/babelInjectComponentSource.ts
|
|
103
|
+
var SOURCE_PROP_LOCAL = "_componentSourceLoc";
|
|
104
|
+
var SOURCE_PROPS_REST = "__reactCodeLocatorProps";
|
|
105
|
+
function isComponentName(name) {
|
|
106
|
+
return /^[A-Z]/.test(name);
|
|
107
|
+
}
|
|
108
|
+
function isCustomComponentTag(name) {
|
|
109
|
+
if (import_core.types.isJSXIdentifier(name)) {
|
|
110
|
+
return isComponentName(name.name);
|
|
111
|
+
}
|
|
112
|
+
if (import_core.types.isJSXMemberExpression(name)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
function isIntrinsicElementTag(name) {
|
|
118
|
+
return import_core.types.isJSXIdentifier(name) && /^[a-z]/.test(name.name);
|
|
119
|
+
}
|
|
120
|
+
function isElementFactoryIdentifier(name) {
|
|
121
|
+
return name === "jsx" || name === "jsxs" || name === "jsxDEV" || name === "_jsx" || name === "_jsxs" || name === "_jsxDEV" || name === "createElement";
|
|
122
|
+
}
|
|
123
|
+
function isReactElementFactoryCall(pathNode) {
|
|
124
|
+
const callee = pathNode.node.callee;
|
|
125
|
+
if (import_core.types.isIdentifier(callee)) {
|
|
126
|
+
return isElementFactoryIdentifier(callee.name);
|
|
127
|
+
}
|
|
128
|
+
return import_core.types.isMemberExpression(callee) && import_core.types.isIdentifier(callee.object, { name: "React" }) && import_core.types.isIdentifier(callee.property, { name: "createElement" });
|
|
129
|
+
}
|
|
130
|
+
function getRootJsxIdentifierName(name) {
|
|
131
|
+
if (import_core.types.isJSXIdentifier(name)) {
|
|
132
|
+
return name.name;
|
|
133
|
+
}
|
|
134
|
+
if (import_core.types.isJSXMemberExpression(name)) {
|
|
135
|
+
return getRootJsxIdentifierName(name.object);
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
function isStyledModuleImport(binding) {
|
|
140
|
+
if (!binding) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
if (!binding.path.isImportSpecifier() && !binding.path.isImportDefaultSpecifier() && !binding.path.isImportNamespaceSpecifier()) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const source = binding.path.parentPath.isImportDeclaration() ? binding.path.parentPath.node.source.value : null;
|
|
147
|
+
if (typeof source !== "string") {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
const normalized = source.replace(/\\/g, "/");
|
|
151
|
+
return normalized === "./styled" || normalized === "../styled" || normalized.endsWith("/styled");
|
|
152
|
+
}
|
|
153
|
+
function isSupportedComponentInit(node) {
|
|
154
|
+
if (!node) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (import_core.types.isArrowFunctionExpression(node) || import_core.types.isFunctionExpression(node)) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
if (!import_core.types.isCallExpression(node)) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
if (import_core.types.isIdentifier(node.callee) && (node.callee.name === "memo" || node.callee.name === "forwardRef")) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return import_core.types.isMemberExpression(node.callee) && import_core.types.isIdentifier(node.callee.object, { name: "React" }) && import_core.types.isIdentifier(node.callee.property) && (node.callee.property.name === "memo" || node.callee.property.name === "forwardRef");
|
|
167
|
+
}
|
|
168
|
+
function hasSourcePropBinding(pattern) {
|
|
169
|
+
return pattern.properties.some((property) => {
|
|
170
|
+
if (!import_core.types.isObjectProperty(property)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
return import_core.types.isIdentifier(property.key) && property.key.name === JSX_SOURCE_PROP;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function injectSourcePropBinding(pattern) {
|
|
177
|
+
if (hasSourcePropBinding(pattern)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const sourceBinding = import_core.types.objectProperty(
|
|
181
|
+
import_core.types.identifier(JSX_SOURCE_PROP),
|
|
182
|
+
import_core.types.identifier(SOURCE_PROP_LOCAL),
|
|
183
|
+
false,
|
|
184
|
+
false
|
|
185
|
+
);
|
|
186
|
+
const restIndex = pattern.properties.findIndex(
|
|
187
|
+
(property) => import_core.types.isRestElement(property)
|
|
188
|
+
);
|
|
189
|
+
if (restIndex === -1) {
|
|
190
|
+
pattern.properties.push(sourceBinding);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
pattern.properties.splice(restIndex, 0, sourceBinding);
|
|
194
|
+
}
|
|
195
|
+
function injectSourcePropIntoIdentifierParam(node, param) {
|
|
196
|
+
if (!import_core.types.isBlockStatement(node.body)) {
|
|
197
|
+
node.body = import_core.types.blockStatement([import_core.types.returnStatement(node.body)]);
|
|
198
|
+
}
|
|
199
|
+
const alreadyInjected = node.body.body.some(
|
|
200
|
+
(statement) => import_core.types.isVariableDeclaration(statement) && statement.declarations.some(
|
|
201
|
+
(declaration) => import_core.types.isIdentifier(declaration.id) && declaration.id.name === SOURCE_PROPS_REST
|
|
202
|
+
)
|
|
203
|
+
);
|
|
204
|
+
if (alreadyInjected) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
node.body.body.unshift(
|
|
208
|
+
import_core.types.variableDeclaration("const", [
|
|
209
|
+
import_core.types.variableDeclarator(
|
|
210
|
+
import_core.types.objectPattern([
|
|
211
|
+
import_core.types.objectProperty(
|
|
212
|
+
import_core.types.identifier(JSX_SOURCE_PROP),
|
|
213
|
+
import_core.types.identifier(SOURCE_PROP_LOCAL),
|
|
214
|
+
false,
|
|
215
|
+
false
|
|
216
|
+
),
|
|
217
|
+
import_core.types.restElement(import_core.types.identifier(SOURCE_PROPS_REST))
|
|
218
|
+
]),
|
|
219
|
+
param
|
|
220
|
+
)
|
|
221
|
+
]),
|
|
222
|
+
import_core.types.expressionStatement(
|
|
223
|
+
import_core.types.assignmentExpression(
|
|
224
|
+
"=",
|
|
225
|
+
import_core.types.identifier(param.name),
|
|
226
|
+
import_core.types.identifier(SOURCE_PROPS_REST)
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
function injectSourcePropIntoFunctionParams(node) {
|
|
232
|
+
const firstParam = node.params[0];
|
|
233
|
+
if (!firstParam) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (import_core.types.isObjectPattern(firstParam)) {
|
|
237
|
+
injectSourcePropBinding(firstParam);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (import_core.types.isIdentifier(firstParam)) {
|
|
241
|
+
injectSourcePropIntoIdentifierParam(node, firstParam);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function injectSourcePropIntoExpression(node) {
|
|
245
|
+
if (!node) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (import_core.types.isFunctionExpression(node) || import_core.types.isArrowFunctionExpression(node)) {
|
|
249
|
+
injectSourcePropIntoFunctionParams(node);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!import_core.types.isCallExpression(node)) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const firstArg = node.arguments[0];
|
|
256
|
+
if (firstArg && !import_core.types.isSpreadElement(firstArg) && (import_core.types.isFunctionExpression(firstArg) || import_core.types.isArrowFunctionExpression(firstArg))) {
|
|
257
|
+
injectSourcePropIntoFunctionParams(firstArg);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function getSourceValue(state, loc, projectRoot) {
|
|
261
|
+
const filename = state.file?.opts?.filename;
|
|
262
|
+
if (!filename || !loc) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
return toRelativeSource(filename, loc, projectRoot);
|
|
266
|
+
}
|
|
267
|
+
function buildAssignment(name, sourceValue) {
|
|
268
|
+
return import_core.types.expressionStatement(
|
|
269
|
+
import_core.types.assignmentExpression(
|
|
270
|
+
"=",
|
|
271
|
+
import_core.types.memberExpression(import_core.types.identifier(name), import_core.types.identifier(SOURCE_PROP)),
|
|
272
|
+
import_core.types.stringLiteral(sourceValue)
|
|
273
|
+
)
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
function buildIntrinsicSourceHelper() {
|
|
277
|
+
return import_core.types.functionDeclaration(
|
|
278
|
+
import_core.types.identifier("_markIntrinsicElementSource"),
|
|
279
|
+
[import_core.types.identifier("element"), import_core.types.identifier("source")],
|
|
280
|
+
import_core.types.blockStatement([
|
|
281
|
+
import_core.types.variableDeclaration("const", [
|
|
282
|
+
import_core.types.variableDeclarator(
|
|
283
|
+
import_core.types.identifier("registryKey"),
|
|
284
|
+
import_core.types.callExpression(
|
|
285
|
+
import_core.types.memberExpression(import_core.types.identifier("Symbol"), import_core.types.identifier("for")),
|
|
286
|
+
[import_core.types.stringLiteral(JSX_SOURCE_REGISTRY_SYMBOL)]
|
|
287
|
+
)
|
|
288
|
+
)
|
|
289
|
+
]),
|
|
290
|
+
import_core.types.variableDeclaration("let", [
|
|
291
|
+
import_core.types.variableDeclarator(
|
|
292
|
+
import_core.types.identifier("registry"),
|
|
293
|
+
import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true)
|
|
294
|
+
)
|
|
295
|
+
]),
|
|
296
|
+
import_core.types.ifStatement(
|
|
297
|
+
import_core.types.unaryExpression(
|
|
298
|
+
"!",
|
|
299
|
+
import_core.types.binaryExpression("instanceof", import_core.types.identifier("registry"), import_core.types.identifier("WeakMap"))
|
|
300
|
+
),
|
|
301
|
+
import_core.types.blockStatement([
|
|
302
|
+
import_core.types.expressionStatement(
|
|
303
|
+
import_core.types.assignmentExpression(
|
|
304
|
+
"=",
|
|
305
|
+
import_core.types.identifier("registry"),
|
|
306
|
+
import_core.types.assignmentExpression(
|
|
307
|
+
"=",
|
|
308
|
+
import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true),
|
|
309
|
+
import_core.types.newExpression(import_core.types.identifier("WeakMap"), [])
|
|
310
|
+
)
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
])
|
|
314
|
+
),
|
|
315
|
+
import_core.types.ifStatement(
|
|
316
|
+
import_core.types.logicalExpression(
|
|
317
|
+
"&&",
|
|
318
|
+
import_core.types.identifier("element"),
|
|
319
|
+
import_core.types.logicalExpression(
|
|
320
|
+
"&&",
|
|
321
|
+
import_core.types.binaryExpression("===", import_core.types.unaryExpression("typeof", import_core.types.identifier("element")), import_core.types.stringLiteral("object")),
|
|
322
|
+
import_core.types.binaryExpression(
|
|
323
|
+
"===",
|
|
324
|
+
import_core.types.unaryExpression("typeof", import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props"))),
|
|
325
|
+
import_core.types.stringLiteral("object")
|
|
326
|
+
)
|
|
327
|
+
)
|
|
328
|
+
),
|
|
329
|
+
import_core.types.blockStatement([
|
|
330
|
+
import_core.types.expressionStatement(
|
|
331
|
+
import_core.types.callExpression(
|
|
332
|
+
import_core.types.memberExpression(import_core.types.identifier("registry"), import_core.types.identifier("set")),
|
|
333
|
+
[import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props")), import_core.types.identifier("source")]
|
|
334
|
+
)
|
|
335
|
+
)
|
|
336
|
+
])
|
|
337
|
+
),
|
|
338
|
+
import_core.types.returnStatement(import_core.types.identifier("element"))
|
|
339
|
+
])
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
function ensureIntrinsicSourceHelper(programPath, state) {
|
|
343
|
+
if (state.injectedIntrinsicHelper) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const alreadyExists = programPath.node.body.some(
|
|
347
|
+
(node) => import_core.types.isFunctionDeclaration(node) && import_core.types.isIdentifier(node.id, { name: "_markIntrinsicElementSource" })
|
|
348
|
+
);
|
|
349
|
+
if (!alreadyExists) {
|
|
350
|
+
programPath.unshiftContainer("body", buildIntrinsicSourceHelper());
|
|
351
|
+
}
|
|
352
|
+
state.injectedIntrinsicHelper = true;
|
|
353
|
+
}
|
|
354
|
+
function visitDeclaration(declarationPath, insertAfterPath, state, seen, projectRoot) {
|
|
355
|
+
if (declarationPath.isFunctionDeclaration() || declarationPath.isClassDeclaration()) {
|
|
356
|
+
const name = declarationPath.node.id?.name;
|
|
357
|
+
if (!name || !isComponentName(name) || seen.has(name)) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (declarationPath.isFunctionDeclaration()) {
|
|
361
|
+
injectSourcePropIntoFunctionParams(declarationPath.node);
|
|
362
|
+
}
|
|
363
|
+
const sourceValue = getSourceValue(
|
|
364
|
+
state,
|
|
365
|
+
declarationPath.node.loc?.start,
|
|
366
|
+
projectRoot
|
|
367
|
+
);
|
|
368
|
+
if (!sourceValue) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
seen.add(name);
|
|
372
|
+
insertAfterPath.insertAfter(buildAssignment(name, sourceValue));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (!declarationPath.isVariableDeclaration()) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const assignments = declarationPath.node.declarations.flatMap(
|
|
379
|
+
(declarator) => {
|
|
380
|
+
if (!import_core.types.isIdentifier(declarator.id) || !isComponentName(declarator.id.name) || seen.has(declarator.id.name)) {
|
|
381
|
+
return [];
|
|
382
|
+
}
|
|
383
|
+
if (!declarator.init) {
|
|
384
|
+
return [];
|
|
385
|
+
}
|
|
386
|
+
if (!isSupportedComponentInit(declarator.init)) {
|
|
387
|
+
return [];
|
|
388
|
+
}
|
|
389
|
+
injectSourcePropIntoExpression(declarator.init);
|
|
390
|
+
const sourceValue = getSourceValue(
|
|
391
|
+
state,
|
|
392
|
+
declarator.loc?.start ?? declarator.init.loc?.start,
|
|
393
|
+
projectRoot
|
|
394
|
+
);
|
|
395
|
+
if (!sourceValue) {
|
|
396
|
+
return [];
|
|
397
|
+
}
|
|
398
|
+
seen.add(declarator.id.name);
|
|
399
|
+
return [buildAssignment(declarator.id.name, sourceValue)];
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
if (assignments.length > 0) {
|
|
403
|
+
insertAfterPath.insertAfter(assignments);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
function babelInjectComponentSource(options = {}) {
|
|
407
|
+
const {
|
|
408
|
+
injectJsxSource = true,
|
|
409
|
+
injectComponentSource = true,
|
|
410
|
+
projectRoot
|
|
411
|
+
} = options;
|
|
412
|
+
return {
|
|
413
|
+
name: "babel-inject-component-source",
|
|
414
|
+
visitor: {
|
|
415
|
+
CallExpression(pathNode, state) {
|
|
416
|
+
if (!injectJsxSource) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (!isReactElementFactoryCall(pathNode)) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, {
|
|
423
|
+
name: "_markIntrinsicElementSource"
|
|
424
|
+
})) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const sourceValue = getSourceValue(
|
|
428
|
+
state,
|
|
429
|
+
pathNode.node.loc?.start,
|
|
430
|
+
projectRoot
|
|
431
|
+
);
|
|
432
|
+
if (!sourceValue) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const programPath = pathNode.findParent((parent) => parent.isProgram());
|
|
436
|
+
if (!programPath || !programPath.isProgram()) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
ensureIntrinsicSourceHelper(programPath, state);
|
|
440
|
+
pathNode.replaceWith(
|
|
441
|
+
import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
|
|
442
|
+
pathNode.node,
|
|
443
|
+
import_core.types.stringLiteral(sourceValue)
|
|
444
|
+
])
|
|
445
|
+
);
|
|
446
|
+
pathNode.skip();
|
|
447
|
+
},
|
|
448
|
+
JSXElement: {
|
|
449
|
+
exit(pathNode, state) {
|
|
450
|
+
if (!injectJsxSource) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (!isIntrinsicElementTag(pathNode.node.openingElement.name)) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, { name: "_markIntrinsicElementSource" })) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const sourceValue = getSourceValue(
|
|
460
|
+
state,
|
|
461
|
+
pathNode.node.openingElement.loc?.start,
|
|
462
|
+
projectRoot
|
|
463
|
+
);
|
|
464
|
+
if (!sourceValue) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const programPath = pathNode.findParent((parent) => parent.isProgram());
|
|
468
|
+
if (!programPath || !programPath.isProgram()) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
ensureIntrinsicSourceHelper(programPath, state);
|
|
472
|
+
const wrappedNode = import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
|
|
473
|
+
pathNode.node,
|
|
474
|
+
import_core.types.stringLiteral(sourceValue)
|
|
475
|
+
]);
|
|
476
|
+
if (pathNode.parentPath.isJSXElement() || pathNode.parentPath.isJSXFragment()) {
|
|
477
|
+
pathNode.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (pathNode.parentPath.isJSXExpressionContainer()) {
|
|
481
|
+
pathNode.parentPath.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
pathNode.replaceWith(wrappedNode);
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
JSXOpeningElement(pathNode, state) {
|
|
488
|
+
if (!injectJsxSource) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (!isCustomComponentTag(pathNode.node.name)) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const rootIdentifierName = getRootJsxIdentifierName(pathNode.node.name);
|
|
495
|
+
if (rootIdentifierName && isExternalToProjectRoot(state.file?.opts?.filename, projectRoot) && isStyledModuleImport(pathNode.scope.getBinding(rootIdentifierName))) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const hasSourceProp = pathNode.node.attributes.some(
|
|
499
|
+
(attr) => import_core.types.isJSXAttribute(attr) && import_core.types.isJSXIdentifier(attr.name) && attr.name.name === JSX_SOURCE_PROP
|
|
500
|
+
);
|
|
501
|
+
if (hasSourceProp) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const filename = state.file?.opts?.filename;
|
|
505
|
+
const loc = pathNode.node.loc?.start;
|
|
506
|
+
if (!filename || !loc) {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
pathNode.node.attributes.push(
|
|
510
|
+
import_core.types.jsxAttribute(
|
|
511
|
+
import_core.types.jsxIdentifier(JSX_SOURCE_PROP),
|
|
512
|
+
import_core.types.stringLiteral(
|
|
513
|
+
getSourceValue(state, loc, projectRoot) ?? `${filename.replace(/\\/g, "/")}:${loc.line}:${loc.column + 1}`
|
|
514
|
+
)
|
|
515
|
+
)
|
|
516
|
+
);
|
|
517
|
+
},
|
|
518
|
+
Program(programPath, state) {
|
|
519
|
+
if (!injectComponentSource) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
const seen = /* @__PURE__ */ new Set();
|
|
523
|
+
for (const childPath of programPath.get("body")) {
|
|
524
|
+
if (childPath.isExportNamedDeclaration() || childPath.isExportDefaultDeclaration()) {
|
|
525
|
+
const declarationPath = childPath.get("declaration");
|
|
526
|
+
if (!Array.isArray(declarationPath) && declarationPath.node) {
|
|
527
|
+
visitDeclaration(declarationPath, childPath, state, seen, projectRoot);
|
|
528
|
+
}
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
visitDeclaration(childPath, childPath, state, seen, projectRoot);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// src/sourceTransform.ts
|
|
539
|
+
async function transformSourceWithLocator(code, options) {
|
|
540
|
+
const { filename, sourceMaps = true, projectRoot = process.cwd(), ...pluginOptions } = options;
|
|
541
|
+
const result = await (0, import_core2.transformAsync)(code, {
|
|
542
|
+
filename,
|
|
543
|
+
babelrc: false,
|
|
544
|
+
configFile: false,
|
|
545
|
+
sourceMaps,
|
|
546
|
+
parserOpts: {
|
|
547
|
+
sourceType: "module",
|
|
548
|
+
plugins: ["jsx", "typescript"]
|
|
549
|
+
},
|
|
550
|
+
generatorOpts: {
|
|
551
|
+
retainLines: true
|
|
552
|
+
},
|
|
553
|
+
plugins: [[babelInjectComponentSource, { ...pluginOptions, projectRoot }]]
|
|
554
|
+
});
|
|
555
|
+
return {
|
|
556
|
+
code: result?.code ?? code,
|
|
557
|
+
map: result?.map ?? null
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// src/esbuild.ts
|
|
562
|
+
function getEsbuildLoader(filename) {
|
|
563
|
+
if (filename.endsWith(".tsx")) {
|
|
564
|
+
return "tsx";
|
|
565
|
+
}
|
|
566
|
+
if (filename.endsWith(".ts")) {
|
|
567
|
+
return "ts";
|
|
568
|
+
}
|
|
569
|
+
if (filename.endsWith(".jsx")) {
|
|
570
|
+
return "jsx";
|
|
571
|
+
}
|
|
572
|
+
return "js";
|
|
573
|
+
}
|
|
574
|
+
function esbuildSourceTransformPlugin(options = {}) {
|
|
575
|
+
return {
|
|
576
|
+
name: "react-code-locator-source-transform",
|
|
577
|
+
setup(build) {
|
|
578
|
+
build.onLoad({ filter: /\.[mc]?[jt]sx?$/ }, async ({ path }) => {
|
|
579
|
+
if (path.includes("/node_modules/")) {
|
|
580
|
+
return null;
|
|
581
|
+
}
|
|
582
|
+
const code = await (0, import_promises.readFile)(path, "utf8");
|
|
583
|
+
const result = await transformSourceWithLocator(code, {
|
|
584
|
+
filename: path,
|
|
585
|
+
...options
|
|
586
|
+
});
|
|
587
|
+
return {
|
|
588
|
+
contents: result.code,
|
|
589
|
+
loader: getEsbuildLoader(path)
|
|
590
|
+
};
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
function createEsbuildSourceAdapter(options = {}) {
|
|
596
|
+
const resolvedOptions = {
|
|
597
|
+
projectRoot: process.cwd(),
|
|
598
|
+
...options
|
|
599
|
+
};
|
|
600
|
+
return defineSourceAdapter({
|
|
601
|
+
kind: "esbuild",
|
|
602
|
+
name: "react-code-locator/esbuild",
|
|
603
|
+
options: resolvedOptions,
|
|
604
|
+
config: {
|
|
605
|
+
plugins: [esbuildSourceTransformPlugin(resolvedOptions)]
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
var esbuildSourceAdapter = createEsbuildSourceAdapter();
|
|
610
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
611
|
+
0 && (module.exports = {
|
|
612
|
+
createEsbuildSourceAdapter,
|
|
613
|
+
esbuildSourceAdapter,
|
|
614
|
+
esbuildSourceTransformPlugin
|
|
615
|
+
});
|
|
616
|
+
//# sourceMappingURL=esbuild.cjs.map
|