@component-compass/parser-react 0.0.4 → 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/emit.d.ts +48 -0
- package/dist/emit.js +1009 -0
- package/dist/emit.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +13 -19
- package/dist/index.js.map +1 -1
- package/dist/infer-value.d.ts +50 -0
- package/dist/infer-value.js +394 -0
- package/dist/infer-value.js.map +1 -0
- package/dist/jsx-attrs.d.ts +11 -0
- package/dist/jsx-attrs.js +72 -0
- package/dist/jsx-attrs.js.map +1 -0
- package/package.json +10 -9
- package/dist/walk-jsx.d.ts +0 -21
- package/dist/walk-jsx.js +0 -1041
- package/dist/walk-jsx.js.map +0 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { unwrapTsNoise } from "./infer-value.js";
|
|
2
|
+
/**
|
|
3
|
+
* Extract per-prop usage from a JSXOpeningElement. Drops event handlers
|
|
4
|
+
* (`onClick` etc.) — events are tracked separately (see #57). Drops empty
|
|
5
|
+
* expression containers (`{}`). Returns one entry per recognised attribute.
|
|
6
|
+
*
|
|
7
|
+
* `captureValues` controls whether literal values flow through to the artifact;
|
|
8
|
+
* when false, only the prop name + isDynamic flag is captured.
|
|
9
|
+
*/
|
|
10
|
+
export function readJsxAttrs(opening, captureValues) {
|
|
11
|
+
const props = [];
|
|
12
|
+
for (const attr of opening.attributes) {
|
|
13
|
+
if (attr.type === "JSXSpreadAttribute") {
|
|
14
|
+
if (props.some((p) => p.name === "...rest"))
|
|
15
|
+
continue;
|
|
16
|
+
props.push({ name: "...rest", isDynamic: true });
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (attr.type !== "JSXAttribute")
|
|
20
|
+
continue;
|
|
21
|
+
// TS narrows attr to JSXAttribute after the guard above — no cast needed.
|
|
22
|
+
const nameNode = attr.name;
|
|
23
|
+
const name = nameNode.type === "JSXIdentifier" ? nameNode.name : "";
|
|
24
|
+
if (!name)
|
|
25
|
+
continue;
|
|
26
|
+
if (name.startsWith("on") && name.length > 2 && /[A-Z]/.test(name.charAt(2))) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (!attr.value) {
|
|
30
|
+
const usage = { name, isDynamic: false };
|
|
31
|
+
if (captureValues)
|
|
32
|
+
usage.literalValue = true;
|
|
33
|
+
props.push(usage);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
// JSXAttributeValue = StringLiteral | JSXExpressionContainer | JSXElement | JSXFragment.
|
|
37
|
+
// Only StringLiteral has type "Literal"; TS narrows to StringLiteral here,
|
|
38
|
+
// and StringLiteral.value is already typed as string — no cast needed.
|
|
39
|
+
if (attr.value.type === "Literal") {
|
|
40
|
+
const usage = { name, isDynamic: false };
|
|
41
|
+
if (captureValues)
|
|
42
|
+
usage.literalValue = attr.value.value;
|
|
43
|
+
props.push(usage);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (attr.value.type === "JSXExpressionContainer") {
|
|
47
|
+
const rawExpr = attr.value.expression;
|
|
48
|
+
// JSXExpression = JSXEmptyExpression | Expression; drop empty containers.
|
|
49
|
+
if (rawExpr.type === "JSXEmptyExpression")
|
|
50
|
+
continue;
|
|
51
|
+
const expr = unwrapTsNoise(rawExpr);
|
|
52
|
+
if (!expr)
|
|
53
|
+
continue;
|
|
54
|
+
if (expr.type === "Literal") {
|
|
55
|
+
const litVal = expr.value;
|
|
56
|
+
const kind = typeof litVal;
|
|
57
|
+
if (kind === "string" || kind === "number" || kind === "boolean") {
|
|
58
|
+
const usage = { name, isDynamic: false };
|
|
59
|
+
if (captureValues)
|
|
60
|
+
usage.literalValue = litVal;
|
|
61
|
+
props.push(usage);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
props.push({ name, isDynamic: true });
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
props.push({ name, isDynamic: true });
|
|
68
|
+
}
|
|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AI,CAAC,KAAK,CAAC,CAAC;oBAClB,SAAS;gBACX,CAAC;gBACD,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;gBACtC,SAAS;YACX,CAAC;YAED,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@component-compass/parser-react",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Consumer-scan parser for React JSX. Detects component usages in .tsx and .jsx files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -27,16 +27,17 @@
|
|
|
27
27
|
"clean": "rm -rf dist .turbo"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@component-compass/ast-utils": "
|
|
31
|
-
"@component-compass/plugin-core": "
|
|
32
|
-
"@
|
|
33
|
-
"oxc-
|
|
30
|
+
"@component-compass/ast-utils": "0.1.0",
|
|
31
|
+
"@component-compass/plugin-core": "0.1.0",
|
|
32
|
+
"@component-compass/reference-graph": "0.1.0",
|
|
33
|
+
"@oxc-project/types": "0.130.0",
|
|
34
|
+
"oxc-walker": "1.0.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@types/node": "
|
|
37
|
-
"oxc-parser": "
|
|
38
|
-
"typescript": "
|
|
39
|
-
"vitest": "
|
|
37
|
+
"@types/node": "24.12.2",
|
|
38
|
+
"oxc-parser": "0.130.0",
|
|
39
|
+
"typescript": "5.9.3",
|
|
40
|
+
"vitest": "4.1.5"
|
|
40
41
|
},
|
|
41
42
|
"engines": {
|
|
42
43
|
"node": ">=24"
|
package/dist/walk-jsx.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Program } from "@oxc-project/types";
|
|
2
|
-
import type { DiagnosticCollector, Occurrence, FileImport, WrapperDef, Warning, LocalDefinitionIndex, LookupByTag, ResolveLazyManifest } from "@component-compass/plugin-core";
|
|
3
|
-
type WalkOptions = {
|
|
4
|
-
file: string;
|
|
5
|
-
source: string;
|
|
6
|
-
ast: Program;
|
|
7
|
-
resolveLazyManifest: ResolveLazyManifest;
|
|
8
|
-
lookupByTag: LookupByTag;
|
|
9
|
-
localIndex: LocalDefinitionIndex;
|
|
10
|
-
captureValues: boolean;
|
|
11
|
-
repoRoot: string;
|
|
12
|
-
collector?: DiagnosticCollector;
|
|
13
|
-
};
|
|
14
|
-
export type WalkOutput = {
|
|
15
|
-
occurrences: Occurrence[];
|
|
16
|
-
wrappers: WrapperDef[];
|
|
17
|
-
imports: FileImport[];
|
|
18
|
-
warnings: Warning[];
|
|
19
|
-
};
|
|
20
|
-
export declare function walkJsx(opts: WalkOptions): WalkOutput;
|
|
21
|
-
export {};
|