@rn-ave/babel-plugin 0.1.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 +6 -0
- package/dist/index.js +111 -0
- package/package.json +37 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PluginObj, PluginPass } from '@babel/core';
|
|
2
|
+
/**
|
|
3
|
+
* Babel plugin to inject source location into JSX elements
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: (api: object, options: Record<string, any> | null | undefined, dirname: string) => PluginObj<PluginPass>;
|
|
6
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const helper_plugin_utils_1 = require("@babel/helper-plugin-utils");
|
|
37
|
+
const t = __importStar(require("@babel/types"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
/**
|
|
40
|
+
* Babel plugin to inject source location into JSX elements
|
|
41
|
+
*/
|
|
42
|
+
exports.default = (0, helper_plugin_utils_1.declare)((api) => {
|
|
43
|
+
api.assertVersion(7);
|
|
44
|
+
return {
|
|
45
|
+
name: 'rn-ave-source',
|
|
46
|
+
visitor: {
|
|
47
|
+
JSXOpeningElement(nodePath, state) {
|
|
48
|
+
const rawFilename = state.filename || state.file?.opts?.filename;
|
|
49
|
+
const opts = state.opts;
|
|
50
|
+
// Skip if no filename
|
|
51
|
+
if (!rawFilename || rawFilename === 'unknown')
|
|
52
|
+
return;
|
|
53
|
+
// Resolve absolute path
|
|
54
|
+
const absolutePath = path.isAbsolute(rawFilename)
|
|
55
|
+
? rawFilename
|
|
56
|
+
: path.resolve(process.cwd(), rawFilename);
|
|
57
|
+
// ONLY tag files that are inside the project root and NOT in node_modules
|
|
58
|
+
const isNodeModules = absolutePath.includes('node_modules');
|
|
59
|
+
const isInProject = opts.rootDir ? absolutePath.startsWith(opts.rootDir) : true;
|
|
60
|
+
if (isNodeModules || !isInProject) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// --- Nuke Fragments ---
|
|
64
|
+
const nameNode = nodePath.node.name;
|
|
65
|
+
// 1. Check for <Fragment>
|
|
66
|
+
if (t.isJSXIdentifier(nameNode) && (nameNode.name === 'Fragment' || nameNode.name === 'React.Fragment')) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// 2. Check for <React.Fragment> or similar
|
|
70
|
+
if (t.isJSXMemberExpression(nameNode)) {
|
|
71
|
+
let current = nameNode;
|
|
72
|
+
let isFrag = false;
|
|
73
|
+
while (current) {
|
|
74
|
+
const partName = t.isJSXIdentifier(current.property) ? current.property.name :
|
|
75
|
+
t.isJSXIdentifier(current) ? current.name : null;
|
|
76
|
+
if (partName === 'Fragment') {
|
|
77
|
+
isFrag = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
current = current.object;
|
|
81
|
+
}
|
|
82
|
+
if (isFrag)
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Skip if already has __rnAve attribute
|
|
86
|
+
if (nodePath.node.attributes.some(attr => t.isJSXAttribute(attr) &&
|
|
87
|
+
t.isJSXIdentifier(attr.name) &&
|
|
88
|
+
attr.name.name === '__rnAve'))
|
|
89
|
+
return;
|
|
90
|
+
// Get source location
|
|
91
|
+
const loc = nodePath.node.loc;
|
|
92
|
+
if (!loc)
|
|
93
|
+
return;
|
|
94
|
+
const { line, column } = loc.start;
|
|
95
|
+
// Log tagging for debugging (limited)
|
|
96
|
+
if (!state._loggedTagging) {
|
|
97
|
+
console.log(`[rn-ave-babel] Tagging file: ${absolutePath}`);
|
|
98
|
+
state._loggedTagging = true;
|
|
99
|
+
}
|
|
100
|
+
// Create __rnAve prop value
|
|
101
|
+
const inspectProp = t.jsxAttribute(t.jsxIdentifier('__rnAve'), t.jsxExpressionContainer(t.objectExpression([
|
|
102
|
+
t.objectProperty(t.identifier('file'), t.stringLiteral(absolutePath)),
|
|
103
|
+
t.objectProperty(t.identifier('line'), t.numericLiteral(line)),
|
|
104
|
+
t.objectProperty(t.identifier('column'), t.numericLiteral(column)),
|
|
105
|
+
])));
|
|
106
|
+
// Add the attribute to the JSX element
|
|
107
|
+
nodePath.node.attributes.push(inspectProp);
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rn-ave/babel-plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Babel plugin for rn-ave to inject source locations",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"dev": "tsc --watch"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"babel-plugin",
|
|
19
|
+
"react-native",
|
|
20
|
+
"rn-ave",
|
|
21
|
+
"visual-edit"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@babel/helper-plugin-utils": "^7.24.0",
|
|
26
|
+
"@babel/types": "^7.24.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@babel/core": "^7.24.0",
|
|
30
|
+
"@types/babel__core": "^7.20.0",
|
|
31
|
+
"@types/babel__helper-plugin-utils": "^7.10.0",
|
|
32
|
+
"typescript": "^5.0.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@babel/core": "^7.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|