@kivacore/dom-selector-client 0.0.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/package.json CHANGED
@@ -1,11 +1,48 @@
1
1
  {
2
2
  "name": "@kivacore/dom-selector-client",
3
- "version": "0.0.0",
4
- "description": "Placeholder that lets CI publish this package with npm trusted publishing. Install the latest version.",
5
- "license": "MIT",
3
+ "version": "0.7.0",
6
4
  "repository": {
7
5
  "type": "git",
8
6
  "url": "https://github.com/Docyrus/docyrus-devkit.git",
9
7
  "directory": "packages/dom-selector-client"
8
+ },
9
+ "description": "DOM selector CLI client",
10
+ "author": "Docyrus, Inc.",
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "dom",
14
+ "selector",
15
+ "cli"
16
+ ],
17
+ "type": "module",
18
+ "main": "./dist/index.js",
19
+ "bin": {
20
+ "dom-selector": "./dist/index.js"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src/dom-selector-helper.js",
25
+ "src/babel-plugin-component-annotate.ts"
26
+ ],
27
+ "dependencies": {
28
+ "chalk": "5.6.2",
29
+ "commander": "15.1.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "26.6.6",
33
+ "tsup": "9.0.0",
34
+ "tsx": "4.23.15",
35
+ "typescript": "7.0.2",
36
+ "vitest": "5.1.0"
37
+ },
38
+ "scripts": {
39
+ "build": "tsup",
40
+ "dev": "tsx watch src/index.ts",
41
+ "lint": "eslint src",
42
+ "format": "eslint src --fix",
43
+ "typecheck": "eslint src && tsc --noEmit",
44
+ "test": "vitest run --passWithNoTests",
45
+ "test:watch": "vitest",
46
+ "deploy": "pnpm run build && pnpm publish --access public"
10
47
  }
11
- }
48
+ }
@@ -0,0 +1,419 @@
1
+ import * as nodePath from 'node:path';
2
+
3
+ const COMPONENT_NAME_ATTR = 'data-component-name';
4
+ const COMPONENT_PATH_ATTR = 'data-component-path';
5
+ const CURRENT_FILE_PATH_ATTR = 'data-current-file-path';
6
+
7
+ const COMPONENT_NAME_LOCAL = '__docyrusComponentAnnotateName';
8
+ const CURRENT_FILE_PATH_LOCAL = '__docyrusComponentAnnotateCurrentFilePath';
9
+
10
+ interface ComponentInfo {
11
+ name: string;
12
+ hasRuntimeBindings: boolean;
13
+ }
14
+
15
+ interface BabelTypes {
16
+ isAssignmentPattern: (node: any) => boolean;
17
+ isArrowFunctionExpression: (node: any) => boolean;
18
+ isFunctionExpression: (node: any) => boolean;
19
+ isIdentifier: (node: any) => boolean;
20
+ isJSXAttribute: (node: any) => boolean;
21
+ isJSXIdentifier: (node: any) => boolean;
22
+ isJSXMemberExpression: (node: any) => boolean;
23
+ isJSXNamespacedName: (node: any) => boolean;
24
+ isObjectPattern: (node: any) => boolean;
25
+ isRestElement: (node: any) => boolean;
26
+ blockStatement: (body: Array<any>) => any;
27
+ cloneNode: (node: any) => any;
28
+ identifier: (name: string) => any;
29
+ jsxAttribute: (name: any, value: any) => any;
30
+ jsxExpressionContainer: (expression: any) => any;
31
+ jsxIdentifier: (name: string) => any;
32
+ logicalExpression: (operator: '||', left: any, right: any) => any;
33
+ objectPattern: (properties: Array<any>) => any;
34
+ objectProperty: (
35
+ key: any,
36
+ value: any,
37
+ computed?: boolean,
38
+ shorthand?: boolean
39
+ ) => any;
40
+ returnStatement: (argument: any) => any;
41
+ stringLiteral: (value: string) => any;
42
+ variableDeclaration: (kind: 'const', declarations: Array<any>) => any;
43
+ variableDeclarator: (id: any, init: any) => any;
44
+ }
45
+
46
+ interface PluginObj {
47
+ visitor: {
48
+ [key: string]: any;
49
+ };
50
+ }
51
+
52
+ interface BabelPath {
53
+ node: any;
54
+ }
55
+
56
+ interface BabelState {
57
+ file: {
58
+ opts: {
59
+ cwd?: string;
60
+ filename?: string;
61
+ };
62
+ };
63
+ relativeFilePath?: string;
64
+ }
65
+
66
+ export default function componentAnnotatePlugin({
67
+ types: t
68
+ }: {
69
+ types: BabelTypes;
70
+ }): PluginObj {
71
+ const componentStack: Array<ComponentInfo> = [];
72
+
73
+ function getRelativeLocation(
74
+ filePath: string,
75
+ line: number,
76
+ column: number
77
+ ): string {
78
+ return `${filePath}:${line}:${column}`;
79
+ }
80
+
81
+ function getPatternPropertyKeyName(property: any): string | null {
82
+ if (!property || property.type !== 'ObjectProperty') return null;
83
+ if (!property.computed && property.key?.type === 'Identifier') {
84
+ return property.key.name;
85
+ }
86
+ if (property.key?.type === 'StringLiteral') {
87
+ return property.key.value;
88
+ }
89
+
90
+ return null;
91
+ }
92
+
93
+ function hasAttribute(openingElement: any, attributeName: string): boolean {
94
+ return openingElement.attributes.some(
95
+ (attribute: any) => t.isJSXAttribute(attribute)
96
+ && t.isJSXIdentifier(attribute.name)
97
+ && attribute.name.name === attributeName
98
+ );
99
+ }
100
+
101
+ function createStringAttribute(name: string, value: string) {
102
+ return t.jsxAttribute(t.jsxIdentifier(name), t.stringLiteral(value));
103
+ }
104
+
105
+ function createExpressionAttribute(name: string, expression: any) {
106
+ return t.jsxAttribute(
107
+ t.jsxIdentifier(name),
108
+ t.jsxExpressionContainer(expression)
109
+ );
110
+ }
111
+
112
+ function createBindingProperty(attributeName: string, localName: string) {
113
+ return t.objectProperty(
114
+ t.stringLiteral(attributeName),
115
+ t.identifier(localName),
116
+ false,
117
+ false
118
+ );
119
+ }
120
+
121
+ function insertBindingIntoPattern(
122
+ pattern: any,
123
+ attributeName: string,
124
+ localName: string
125
+ ) {
126
+ const alreadyBound = pattern.properties.some(
127
+ (property: any) => getPatternPropertyKeyName(property) === attributeName
128
+ );
129
+
130
+ if (alreadyBound) return;
131
+
132
+ const restIndex = pattern.properties.findIndex((property: any) => t.isRestElement(property));
133
+
134
+ const insertIndex = restIndex === -1 ? pattern.properties.length : restIndex;
135
+
136
+ pattern.properties.splice(
137
+ insertIndex,
138
+ 0,
139
+ createBindingProperty(attributeName, localName)
140
+ );
141
+ }
142
+
143
+ function ensureBlockBody(functionNode: any) {
144
+ if (functionNode.body.type === 'BlockStatement') return;
145
+
146
+ functionNode.body = t.blockStatement([t.returnStatement(functionNode.body)]);
147
+ }
148
+
149
+ function createBindingDeclaration(propsIdentifier: any) {
150
+ return t.variableDeclaration('const', [
151
+ t.variableDeclarator(
152
+ t.objectPattern([
153
+ createBindingProperty(COMPONENT_NAME_ATTR, COMPONENT_NAME_LOCAL),
154
+ createBindingProperty(
155
+ CURRENT_FILE_PATH_ATTR,
156
+ CURRENT_FILE_PATH_LOCAL
157
+ )
158
+ ]),
159
+ t.cloneNode(propsIdentifier)
160
+ )
161
+ ]);
162
+ }
163
+
164
+ function ensureRuntimeBindings(functionNode: any): boolean {
165
+ const firstParam = functionNode.params[0];
166
+
167
+ if (!firstParam) {
168
+ functionNode.params[0] = t.objectPattern([createBindingProperty(COMPONENT_NAME_ATTR, COMPONENT_NAME_LOCAL), createBindingProperty(CURRENT_FILE_PATH_ATTR, CURRENT_FILE_PATH_LOCAL)]);
169
+
170
+ return true;
171
+ }
172
+
173
+ if (t.isObjectPattern(firstParam)) {
174
+ insertBindingIntoPattern(
175
+ firstParam,
176
+ COMPONENT_NAME_ATTR,
177
+ COMPONENT_NAME_LOCAL
178
+ );
179
+ insertBindingIntoPattern(
180
+ firstParam,
181
+ CURRENT_FILE_PATH_ATTR,
182
+ CURRENT_FILE_PATH_LOCAL
183
+ );
184
+
185
+ return true;
186
+ }
187
+
188
+ if (
189
+ t.isAssignmentPattern(firstParam)
190
+ && t.isObjectPattern(firstParam.left)
191
+ ) {
192
+ insertBindingIntoPattern(
193
+ firstParam.left,
194
+ COMPONENT_NAME_ATTR,
195
+ COMPONENT_NAME_LOCAL
196
+ );
197
+ insertBindingIntoPattern(
198
+ firstParam.left,
199
+ CURRENT_FILE_PATH_ATTR,
200
+ CURRENT_FILE_PATH_LOCAL
201
+ );
202
+
203
+ return true;
204
+ }
205
+
206
+ const propsIdentifier = t.isIdentifier(firstParam) ? firstParam : t.isAssignmentPattern(firstParam) && t.isIdentifier(firstParam.left) ? firstParam.left : null;
207
+
208
+ if (!propsIdentifier) return false;
209
+
210
+ ensureBlockBody(functionNode);
211
+ functionNode.body.body.unshift(createBindingDeclaration(propsIdentifier));
212
+
213
+ return true;
214
+ }
215
+
216
+ function getElementName(node: any): string | null {
217
+ if (t.isJSXIdentifier(node)) return node.name;
218
+
219
+ if (t.isJSXMemberExpression(node)) {
220
+ const objectName = getElementName(node.object);
221
+ const propertyName = getElementName(node.property);
222
+
223
+ if (!objectName || !propertyName) return null;
224
+
225
+ return `${objectName}.${propertyName}`;
226
+ }
227
+
228
+ if (t.isJSXNamespacedName(node)) {
229
+ const namespaceName = getElementName(node.namespace);
230
+ const localName = getElementName(node.name);
231
+
232
+ if (!namespaceName || !localName) return null;
233
+
234
+ return `${namespaceName}:${localName}`;
235
+ }
236
+
237
+ return null;
238
+ }
239
+
240
+ function isIntrinsicElement(node: any): boolean {
241
+ return t.isJSXIdentifier(node) && /^[a-z]/.test(node.name);
242
+ }
243
+
244
+ function isFragmentElement(node: any): boolean {
245
+ if (t.isJSXIdentifier(node)) return node.name === 'Fragment';
246
+
247
+ if (t.isJSXMemberExpression(node)) {
248
+ return t.isJSXIdentifier(node.property) && node.property.name === 'Fragment';
249
+ }
250
+
251
+ return false;
252
+ }
253
+
254
+ function getCurrentComponent(): ComponentInfo | undefined {
255
+ return componentStack[componentStack.length - 1];
256
+ }
257
+
258
+ function createRuntimeBackedValue(
259
+ localName: string,
260
+ fallbackValue: string,
261
+ hasRuntimeBindings: boolean
262
+ ) {
263
+ if (!hasRuntimeBindings) {
264
+ return t.stringLiteral(fallbackValue);
265
+ }
266
+
267
+ return t.logicalExpression(
268
+ '||',
269
+ t.identifier(localName),
270
+ t.stringLiteral(fallbackValue)
271
+ );
272
+ }
273
+
274
+ function annotateIntrinsicElement(
275
+ openingElement: any,
276
+ elementName: string,
277
+ state: BabelState
278
+ ) {
279
+ const currentComponent = getCurrentComponent();
280
+ const { loc } = openingElement;
281
+ const componentPath = getRelativeLocation(
282
+ state.relativeFilePath || '',
283
+ loc?.start.line || 0,
284
+ loc?.start.column || 0
285
+ );
286
+
287
+ if (!hasAttribute(openingElement, COMPONENT_NAME_ATTR)) {
288
+ const fallbackName = currentComponent?.name || elementName;
289
+
290
+ openingElement.attributes.push(
291
+ createExpressionAttribute(
292
+ COMPONENT_NAME_ATTR,
293
+ createRuntimeBackedValue(
294
+ COMPONENT_NAME_LOCAL,
295
+ fallbackName,
296
+ Boolean(currentComponent?.hasRuntimeBindings)
297
+ )
298
+ )
299
+ );
300
+ }
301
+
302
+ if (!hasAttribute(openingElement, COMPONENT_PATH_ATTR)) {
303
+ openingElement.attributes.push(
304
+ createStringAttribute(COMPONENT_PATH_ATTR, componentPath)
305
+ );
306
+ }
307
+
308
+ if (!hasAttribute(openingElement, CURRENT_FILE_PATH_ATTR)) {
309
+ openingElement.attributes.push(
310
+ createExpressionAttribute(
311
+ CURRENT_FILE_PATH_ATTR,
312
+ createRuntimeBackedValue(
313
+ CURRENT_FILE_PATH_LOCAL,
314
+ componentPath,
315
+ Boolean(currentComponent?.hasRuntimeBindings)
316
+ )
317
+ )
318
+ );
319
+ }
320
+ }
321
+
322
+ function annotateComponentUsage(
323
+ openingElement: any,
324
+ elementName: string,
325
+ state: BabelState
326
+ ) {
327
+ const { loc } = openingElement;
328
+ const currentFilePath = getRelativeLocation(
329
+ state.relativeFilePath || '',
330
+ loc?.start.line || 0,
331
+ loc?.start.column || 0
332
+ );
333
+
334
+ if (!hasAttribute(openingElement, COMPONENT_NAME_ATTR)) {
335
+ openingElement.attributes.push(
336
+ createStringAttribute(COMPONENT_NAME_ATTR, elementName)
337
+ );
338
+ }
339
+
340
+ if (!hasAttribute(openingElement, CURRENT_FILE_PATH_ATTR)) {
341
+ openingElement.attributes.push(
342
+ createStringAttribute(CURRENT_FILE_PATH_ATTR, currentFilePath)
343
+ );
344
+ }
345
+ }
346
+
347
+ function enterComponent(componentName: string, functionNode: any) {
348
+ componentStack.push({
349
+ name: componentName,
350
+ hasRuntimeBindings: ensureRuntimeBindings(functionNode)
351
+ });
352
+ }
353
+
354
+ return {
355
+ visitor: {
356
+ Program(_path: BabelPath, state: BabelState) {
357
+ componentStack.length = 0;
358
+
359
+ const filename = state.file.opts.filename || '';
360
+ const cwd = state.file.opts.cwd || process.cwd();
361
+
362
+ state.relativeFilePath = nodePath.relative(cwd, filename);
363
+ },
364
+
365
+ FunctionDeclaration: {
366
+ enter(path: BabelPath) {
367
+ if (path.node.id && /^[A-Z]/.test(path.node.id.name)) {
368
+ enterComponent(path.node.id.name, path.node);
369
+ }
370
+ },
371
+ exit(path: BabelPath) {
372
+ if (path.node.id && /^[A-Z]/.test(path.node.id.name)) {
373
+ componentStack.pop();
374
+ }
375
+ }
376
+ },
377
+
378
+ VariableDeclarator: {
379
+ enter(path: BabelPath) {
380
+ if (
381
+ t.isIdentifier(path.node.id)
382
+ && /^[A-Z]/.test(path.node.id.name)
383
+ && (t.isArrowFunctionExpression(path.node.init)
384
+ || t.isFunctionExpression(path.node.init))
385
+ ) {
386
+ enterComponent(path.node.id.name, path.node.init);
387
+ }
388
+ },
389
+ exit(path: BabelPath) {
390
+ if (
391
+ t.isIdentifier(path.node.id)
392
+ && /^[A-Z]/.test(path.node.id.name)
393
+ && (t.isArrowFunctionExpression(path.node.init)
394
+ || t.isFunctionExpression(path.node.init))
395
+ ) {
396
+ componentStack.pop();
397
+ }
398
+ }
399
+ },
400
+
401
+ JSXOpeningElement(path: BabelPath, state: BabelState) {
402
+ const openingElement = path.node;
403
+ const elementName = getElementName(openingElement.name);
404
+
405
+ if (!elementName) return;
406
+
407
+ if (isFragmentElement(openingElement.name)) return;
408
+
409
+ if (isIntrinsicElement(openingElement.name)) {
410
+ annotateIntrinsicElement(openingElement, elementName, state);
411
+
412
+ return;
413
+ }
414
+
415
+ annotateComponentUsage(openingElement, elementName, state);
416
+ }
417
+ }
418
+ };
419
+ }