@component-compass/ast-utils 0.0.1 → 0.0.3
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/find-owner.d.ts +32 -0
- package/dist/find-owner.js +106 -0
- package/dist/find-owner.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walk a Babel NodePath upward to find the nearest enclosing component
|
|
3
|
+
* binding. A "component binding" is a function or class declaration / variable
|
|
4
|
+
* declarator whose binding name is PascalCase, OR an anonymous `export default`.
|
|
5
|
+
*
|
|
6
|
+
* HOC wrappers (`memo`, `forwardRef`, `observer`, `React.memo`, `React.forwardRef`)
|
|
7
|
+
* are unwrapped: the binding name comes from the enclosing variable declarator,
|
|
8
|
+
* not the call expression.
|
|
9
|
+
*
|
|
10
|
+
* Returns `null` when no enclosing tracked component is found before reaching
|
|
11
|
+
* module scope (e.g. JSX at module top-level).
|
|
12
|
+
*/
|
|
13
|
+
import type { NodePath } from "@babel/traverse";
|
|
14
|
+
export type EnclosingComponentBinding = {
|
|
15
|
+
/** Binding name; `"default"` for anonymous default exports. */
|
|
16
|
+
name: string;
|
|
17
|
+
/** True for `export default ...` cases. */
|
|
18
|
+
isDefault: boolean;
|
|
19
|
+
/** Declaration site (line/column from the node's source position). */
|
|
20
|
+
loc: {
|
|
21
|
+
line: number;
|
|
22
|
+
column: number;
|
|
23
|
+
} | null;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Optional predicate supplied by the caller. When provided, a candidate
|
|
27
|
+
* binding is only returned if `isKnownComponent(name)` returns true; otherwise
|
|
28
|
+
* the walk continues upward. When omitted, any PascalCase binding (or
|
|
29
|
+
* anonymous `export default`) is accepted — preserving backward compatibility.
|
|
30
|
+
*/
|
|
31
|
+
export type IsKnownComponent = (name: string) => boolean;
|
|
32
|
+
export declare function findEnclosingComponentBinding(path: NodePath, isKnownComponent?: IsKnownComponent): EnclosingComponentBinding | null;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
const HOC_NAMES = new Set(["memo", "forwardRef", "observer"]);
|
|
2
|
+
export function findEnclosingComponentBinding(path, isKnownComponent) {
|
|
3
|
+
let current = path.parentPath;
|
|
4
|
+
while (current) {
|
|
5
|
+
if (current.isClassDeclaration() || current.isClassExpression()) {
|
|
6
|
+
const node = current.node;
|
|
7
|
+
const id = node.id;
|
|
8
|
+
if (id && isPascalCase(id.name)) {
|
|
9
|
+
if (!isKnownComponent || isKnownComponent(id.name)) {
|
|
10
|
+
return { name: id.name, isDefault: false, loc: nodeLoc(node) };
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const parent = current.parentPath;
|
|
14
|
+
if (parent?.isExportDefaultDeclaration()) {
|
|
15
|
+
if (!isKnownComponent || isKnownComponent("default")) {
|
|
16
|
+
return { name: "default", isDefault: true, loc: nodeLoc(node) };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (current.isFunctionDeclaration()) {
|
|
21
|
+
const node = current.node;
|
|
22
|
+
const id = node.id;
|
|
23
|
+
const parent = current.parentPath;
|
|
24
|
+
if (parent?.isExportDefaultDeclaration()) {
|
|
25
|
+
const name = id && isPascalCase(id.name) ? id.name : "default";
|
|
26
|
+
if (!isKnownComponent || isKnownComponent(name)) {
|
|
27
|
+
return {
|
|
28
|
+
name,
|
|
29
|
+
isDefault: true,
|
|
30
|
+
loc: nodeLoc(node),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (id && isPascalCase(id.name)) {
|
|
35
|
+
if (!isKnownComponent || isKnownComponent(id.name)) {
|
|
36
|
+
return { name: id.name, isDefault: false, loc: nodeLoc(node) };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (current.isArrowFunctionExpression() || current.isFunctionExpression()) {
|
|
41
|
+
const binding = ownerFromFunctionExprAncestor(current, isKnownComponent);
|
|
42
|
+
if (binding)
|
|
43
|
+
return binding;
|
|
44
|
+
}
|
|
45
|
+
current = current.parentPath;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function ownerFromFunctionExprAncestor(path, isKnownComponent) {
|
|
50
|
+
const parent = path.parentPath;
|
|
51
|
+
if (!parent)
|
|
52
|
+
return null;
|
|
53
|
+
if (parent.isVariableDeclarator()) {
|
|
54
|
+
return bindingFromVariableDeclarator(parent.node, path.node, isKnownComponent);
|
|
55
|
+
}
|
|
56
|
+
if (parent.isCallExpression() && isHocCall(parent.node)) {
|
|
57
|
+
const grand = parent.parentPath;
|
|
58
|
+
if (!grand)
|
|
59
|
+
return null;
|
|
60
|
+
if (grand.isVariableDeclarator()) {
|
|
61
|
+
return bindingFromVariableDeclarator(grand.node, path.node, isKnownComponent);
|
|
62
|
+
}
|
|
63
|
+
if (grand.isExportDefaultDeclaration()) {
|
|
64
|
+
if (!isKnownComponent || isKnownComponent("default")) {
|
|
65
|
+
return { name: "default", isDefault: true, loc: nodeLoc(path.node) };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (parent.isExportDefaultDeclaration()) {
|
|
70
|
+
if (!isKnownComponent || isKnownComponent("default")) {
|
|
71
|
+
return { name: "default", isDefault: true, loc: nodeLoc(path.node) };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
function bindingFromVariableDeclarator(decl, fnNode, isKnownComponent) {
|
|
77
|
+
if (decl.id.type !== "Identifier")
|
|
78
|
+
return null;
|
|
79
|
+
if (!isPascalCase(decl.id.name))
|
|
80
|
+
return null;
|
|
81
|
+
if (isKnownComponent && !isKnownComponent(decl.id.name))
|
|
82
|
+
return null;
|
|
83
|
+
return { name: decl.id.name, isDefault: false, loc: nodeLoc(decl) ?? nodeLoc(fnNode) };
|
|
84
|
+
}
|
|
85
|
+
function isHocCall(node) {
|
|
86
|
+
const callee = node.callee;
|
|
87
|
+
if (callee.type === "Identifier")
|
|
88
|
+
return HOC_NAMES.has(callee.name);
|
|
89
|
+
if (callee.type === "MemberExpression" &&
|
|
90
|
+
callee.object.type === "Identifier" &&
|
|
91
|
+
callee.object.name === "React" &&
|
|
92
|
+
callee.property.type === "Identifier") {
|
|
93
|
+
return HOC_NAMES.has(callee.property.name);
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
function isPascalCase(name) {
|
|
98
|
+
return /^[A-Z]/.test(name);
|
|
99
|
+
}
|
|
100
|
+
function nodeLoc(node) {
|
|
101
|
+
if (!node)
|
|
102
|
+
return null;
|
|
103
|
+
const s = node.loc?.start;
|
|
104
|
+
return s ? { line: s.line, column: s.column } : null;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=find-owner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"find-owner.js","sourceRoot":"","sources":["../src/find-owner.ts"],"names":[],"mappings":"AAgCA,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC,CAAC;AAE9D,MAAM,UAAU,6BAA6B,CAC3C,IAAc,EACd,gBAAmC;IAEnC,IAAI,OAAO,GAAoB,IAAI,CAAC,UAAU,CAAC;IAC/C,OAAO,OAAO,EAAE,CAAC;QACf,IAAI,OAAO,CAAC,kBAAkB,EAAE,IAAI,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC;YAChE,MAAM,IAAI,GAAG,OAAO,CAAC,IAA8C,CAAC;YACpE,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;YACnB,IAAI,EAAE,IAAI,YAAY,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;gBAChC,IAAI,CAAC,gBAAgB,IAAI,gBAAgB,CAAC,EAAE,CAAC,I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AAM,CAAC,MAAM,CAAC,IAAI,KAAK,YAAY;QACnC,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,OAAO;QAC9B,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,YAAY,EACrC,CAAC;QACD,OAAO,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,YAAY,CAAC,IAAY;IAChC,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC;AAED,SAAS,OAAO,CAAC,IAA+B;IAC9C,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACvB,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC;IAC1B,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACvD,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,3 +4,5 @@ export { posixPath } from "./posix.js";
|
|
|
4
4
|
export { errorMessage, errorStack } from "./errors.js";
|
|
5
5
|
export { classifyImportOrigin } from "./classify-import.js";
|
|
6
6
|
export type { ImportOrigin, ClassifyOptions } from "./classify-import.js";
|
|
7
|
+
export { findEnclosingComponentBinding } from "./find-owner.js";
|
|
8
|
+
export type { EnclosingComponentBinding } from "./find-owner.js";
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { traverse } from "./babel-interop.js";
|
|
|
3
3
|
export { posixPath } from "./posix.js";
|
|
4
4
|
export { errorMessage, errorStack } from "./errors.js";
|
|
5
5
|
export { classifyImportOrigin } from "./classify-import.js";
|
|
6
|
+
export { findEnclosingComponentBinding } from "./find-owner.js";
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,6BAA6B,EAAE,MAAM,iBAAiB,CAAC"}
|
package/package.json
CHANGED