first commit

This commit is contained in:
2026-06-24 09:48:54 +02:00
commit 41e62ddcad
33739 changed files with 4266226 additions and 0 deletions
@@ -0,0 +1,21 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export declare class CollectionIdProcessor {
private types;
private attributeUtils;
private pathUtils;
private expressionUtils;
private tracingUtils;
constructor(types: typeof t);
process(path: NodePath<t.JSXOpeningElement>): void;
private tryDirectMapInChildren;
private extractMapSource;
private traceMapCall;
private traceOptionalMapCall;
private callbackProducesJSX;
private tryGetByIdInChildren;
private checkChildForGetById;
private tryComponentRootWithCollectionProp;
private isCallInsideJSXChildren;
}
//# sourceMappingURL=collection-id-processor.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"collection-id-processor.d.ts","sourceRoot":"","sources":["../../src/processors/collection-id-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAC;AAevC,qBAAa,qBAAqB;IAMpB,OAAO,CAAC,KAAK;IALzB,OAAO,CAAC,cAAc,CAAoB;IAC1C,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,YAAY,CAAyB;gBAEzB,KAAK,EAAE,OAAO,CAAC;IAOnC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI;IAuBlD,OAAO,CAAC,sBAAsB;IAsB9B,OAAO,CAAC,gBAAgB;IA6BxB,OAAO,CAAC,YAAY;IAoBpB,OAAO,CAAC,oBAAoB;IA2B5B,OAAO,CAAC,mBAAmB;IAsB3B,OAAO,CAAC,oBAAoB;IAgC5B,OAAO,CAAC,oBAAoB;IAsB5B,OAAO,CAAC,kCAAkC;IAwC1C,OAAO,CAAC,uBAAuB;CAWhC"}
@@ -0,0 +1,199 @@
import { DATA_COLLECTION_ID, DATA_COLLECTION_REFERENCE, MAX_JSX_DEPTH, } from "../consts.js";
import { JSXUtils } from "../jsx-utils.js";
import { JSXAttributeUtils, PathNavigationUtils, ExpressionAnalysisUtils, } from "./utils/shared-utils.js";
import { CollectionTracingUtils } from "./utils/collection-tracing-utils.js";
export class CollectionIdProcessor {
types;
attributeUtils;
pathUtils;
expressionUtils;
tracingUtils;
constructor(types) {
this.types = types;
this.attributeUtils = new JSXAttributeUtils(types);
this.pathUtils = new PathNavigationUtils(types);
this.expressionUtils = new ExpressionAnalysisUtils(types);
this.tracingUtils = new CollectionTracingUtils(types);
}
process(path) {
if (this.attributeUtils.hasAttribute(path, DATA_COLLECTION_ID))
return;
const info = this.tryDirectMapInChildren(path) ??
this.tryGetByIdInChildren(path) ??
this.tryComponentRootWithCollectionProp(path);
if (!info)
return;
const target = this.pathUtils.findDOMElementTarget(path) ?? path;
this.attributeUtils.addStringAttribute(target, DATA_COLLECTION_ID, info.id);
if (info.references.length > 0) {
this.attributeUtils.addStringAttribute(target, DATA_COLLECTION_REFERENCE, info.references.join(","));
}
}
tryDirectMapInChildren(path) {
const jsxElement = path.parentPath;
if (!jsxElement?.isJSXElement())
return null;
const children = jsxElement.get("children");
for (const child of children) {
if (!child.isJSXExpressionContainer())
continue;
const expression = child.get("expression");
if (expression.isJSXEmptyExpression())
continue;
const mapSource = this.extractMapSource(expression);
if (mapSource)
return mapSource;
}
return null;
}
extractMapSource(expr) {
if (expr.isCallExpression()) {
return this.traceMapCall(expr);
}
if (expr.isLogicalExpression()) {
if (expr.node.operator === "&&") {
const right = expr.get("right");
return this.extractMapSource(right);
}
}
if (expr.isConditionalExpression()) {
const consequent = expr.get("consequent");
const alternate = expr.get("alternate");
return (this.extractMapSource(consequent) ?? this.extractMapSource(alternate));
}
if (expr.isOptionalCallExpression()) {
return this.traceOptionalMapCall(expr);
}
return null;
}
traceMapCall(callPath) {
const callee = callPath.get("callee");
if (!callee.isMemberExpression())
return null;
const property = callee.get("property");
if (!property.isIdentifier() ||
(property.node.name !== "map" && property.node.name !== "flatMap")) {
return null;
}
if (!this.callbackProducesJSX(callPath))
return null;
const arrayObj = callee.get("object");
return this.tracingUtils.traceCollectionSource(arrayObj);
}
traceOptionalMapCall(callPath) {
const callee = callPath.get("callee");
if (!callee.isMemberExpression() &&
!callee.isOptionalMemberExpression()) {
return null;
}
const property = callee.get("property");
if (!property.isIdentifier() ||
(property.node.name !== "map" && property.node.name !== "flatMap")) {
return null;
}
const arrayObj = callee.get("object");
return this.tracingUtils.traceCollectionSource(arrayObj);
}
callbackProducesJSX(callPath) {
const args = callPath.get("arguments");
const callback = args[0];
if (!callback)
return false;
if (callback.isArrowFunctionExpression()) {
const body = callback.get("body");
if (body.isBlockStatement()) {
return JSXUtils.doesBlockReturnJSX(body.node);
}
return JSXUtils.producesJSX(body.node);
}
if (callback.isFunctionExpression()) {
return JSXUtils.doesBlockReturnJSX(callback.node.body);
}
return false;
}
tryGetByIdInChildren(path, depth = 0) {
if (depth > MAX_JSX_DEPTH)
return null;
const jsxElement = path.parentPath;
if (!jsxElement?.isJSXElement())
return null;
const children = jsxElement.get("children");
for (const child of children) {
const info = this.checkChildForGetById(child, depth);
if (info)
return info;
}
for (const attr of path.node.attributes) {
if (this.types.isJSXAttribute(attr) &&
attr.value &&
this.types.isJSXExpressionContainer(attr.value)) {
const expr = attr.value.expression;
if (this.types.isMemberExpression(expr)) {
const info = this.tracingUtils.traceGetByIdSource(path);
if (info)
return info;
}
}
}
return null;
}
checkChildForGetById(child, depth) {
if (child.isJSXExpressionContainer()) {
const expr = child.get("expression");
if (expr.isMemberExpression() ||
expr.isOptionalMemberExpression()) {
return this.tracingUtils.traceGetByIdSource(child);
}
}
if (child.isJSXElement()) {
const childOpening = child.get("openingElement");
return this.tryGetByIdInChildren(childOpening, depth + 1);
}
return null;
}
tryComponentRootWithCollectionProp(path) {
if (!this.pathUtils.isRootReturnElement(path))
return null;
const fn = this.pathUtils.findEnclosingFunction(path);
if (!fn)
return null;
let info = null;
fn.traverse({
CallExpression: (callPath) => {
if (info)
return;
const callee = callPath.get("callee");
if (!callee.isMemberExpression())
return;
const prop = callee.get("property");
if (!prop.isIdentifier() ||
(prop.node.name !== "map" && prop.node.name !== "flatMap")) {
return;
}
if (!this.callbackProducesJSX(callPath))
return;
// Skip map calls that are directly embedded as JSX children — those
// are already handled by tryDirectMapInChildren on their immediate
// parent, so attributing them to the component root would be wrong.
if (this.isCallInsideJSXChildren(callPath))
return;
const arrayObj = callee.get("object");
info = this.tracingUtils.traceCollectionSource(arrayObj);
},
});
return info;
}
isCallInsideJSXChildren(path) {
let current = path.parentPath;
while (current) {
if (current.isFunction())
break;
if (current.isJSXExpressionContainer()) {
return current.parentPath?.isJSXElement() ?? false;
}
current = current.parentPath;
}
return false;
}
}
//# sourceMappingURL=collection-id-processor.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export declare class DataItemFieldProcessor {
private types;
private attributeUtils;
private expressionUtils;
private staticUtils;
constructor(types: typeof t);
process(path: NodePath<t.JSXOpeningElement>): void;
private extractFieldFromChildren;
private extractFieldFromChild;
private extractFromExpressionContainer;
private extractFromMemberExpression;
private extractFromSimpleIdentifier;
private extractFromChildElement;
private extractFromImageElement;
private extractFromConditionalImage;
private tryAddItemId;
/**
* Build the ID expression for a given root object name.
* If a specific idField was resolved from a key attribute, generates root?.id (or root?._id).
* If unknown (cross-file component), generates root?.id || root?._id to cover both conventions.
*/
private buildIdExpression;
/**
* Walk up the JSX tree to find a key attribute that accesses .id or ._id.
* Returns null when no key is found (e.g. cross-file component receiving props).
*/
private resolveIdFieldName;
private extractIdFieldFromKey;
/**
* For simple identifiers from destructured component props like ({ name, price }),
* find or inject the id field in the ObjectPattern so we can reference it directly.
*/
private tryAddItemIdFromDestructuredParams;
private tryAddItemIdFromParentComponent;
private findKeyWithId;
}
//# sourceMappingURL=collection-item-field-processor.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"collection-item-field-processor.d.ts","sourceRoot":"","sources":["../../src/processors/collection-item-field-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAC;AAavC,qBAAa,sBAAsB;IAKrB,OAAO,CAAC,KAAK;IAJzB,OAAO,CAAC,cAAc,CAAoB;IAC1C,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,WAAW,CAAmB;gBAElB,KAAK,EAAE,OAAO,CAAC;IAMnC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI;IAoBlD,OAAO,CAAC,wBAAwB;IAahC,OAAO,CAAC,qBAAqB;IAc7B,OAAO,CAAC,8BAA8B;IAwCtC,OAAO,CAAC,2BAA2B;IA0BnC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,uBAAuB;IAqB/B,OAAO,CAAC,uBAAuB;IAyC/B,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,YAAY;IAoCpB;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IA8BzB;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAe1B,OAAO,CAAC,qBAAqB;IAwB7B;;;OAGG;IACH,OAAO,CAAC,kCAAkC;IAqE1C,OAAO,CAAC,+BAA+B;IA8BvC,OAAO,CAAC,aAAa;CAmBtB"}
@@ -0,0 +1,300 @@
import { DATA_COLLECTION_ITEM_FIELD, DATA_COLLECTION_ITEM_ID, EXCLUDED_FIELDS, } from "../consts.js";
import { JSXUtils } from "../jsx-utils.js";
import { JSXAttributeUtils, ExpressionAnalysisUtils, StaticValueUtils, } from "./utils/shared-utils.js";
export class DataItemFieldProcessor {
types;
attributeUtils;
expressionUtils;
staticUtils;
constructor(types) {
this.types = types;
this.attributeUtils = new JSXAttributeUtils(types);
this.expressionUtils = new ExpressionAnalysisUtils(types);
this.staticUtils = new StaticValueUtils(types);
}
process(path) {
if (this.attributeUtils.hasAttribute(path, DATA_COLLECTION_ITEM_FIELD)) {
return;
}
const jsxElement = path.parentPath;
if (!jsxElement?.isJSXElement())
return;
const result = this.extractFieldFromChildren(jsxElement);
if (!result)
return;
this.attributeUtils.addStringAttribute(path, DATA_COLLECTION_ITEM_FIELD, result.fieldPath);
this.tryAddItemId(path, result.rootIdentifier, result.fieldPath);
}
extractFieldFromChildren(jsxElement) {
const children = jsxElement.get("children");
for (const child of children) {
const result = this.extractFieldFromChild(child);
if (result)
return result;
}
return null;
}
extractFieldFromChild(child) {
if (child.isJSXExpressionContainer()) {
return this.extractFromExpressionContainer(child);
}
if (child.isJSXElement()) {
return this.extractFromChildElement(child);
}
return null;
}
extractFromExpressionContainer(container) {
const expression = container.get("expression");
if (expression.isJSXEmptyExpression())
return null;
const expr = expression;
if (expr.isMemberExpression() || expr.isOptionalMemberExpression()) {
return this.extractFromMemberExpression(expr);
}
if (expr.isLogicalExpression() && expr.node.operator === "&&") {
const left = expr.get("left");
if (left.isMemberExpression() ||
left.isOptionalMemberExpression()) {
return this.extractFromMemberExpression(left);
}
}
if (expr.isCallExpression()) {
const callee = expr.get("callee");
if (callee.isMemberExpression()) {
const obj = callee.get("object");
if (obj.isMemberExpression() || obj.isOptionalMemberExpression()) {
return this.extractFromMemberExpression(obj);
}
}
}
if (expr.isIdentifier()) {
return this.extractFromSimpleIdentifier(expr);
}
return null;
}
extractFromMemberExpression(expr) {
const rootId = this.expressionUtils.extractRootIdentifier(expr.node);
if (!rootId)
return null;
if (this.staticUtils.isDerivedFromStaticData(rootId.name, expr)) {
return null;
}
const parts = this.expressionUtils.collectMemberExpressionPath(expr.node);
if (parts.length < 2)
return null;
const fieldParts = parts.slice(1);
const fieldPath = fieldParts.join(".");
if (EXCLUDED_FIELDS.includes(fieldPath))
return null;
if (fieldParts.some((p) => EXCLUDED_FIELDS.includes(p)))
return null;
return { fieldPath, rootIdentifier: rootId };
}
extractFromSimpleIdentifier(expr) {
const name = expr.node.name;
if (EXCLUDED_FIELDS.includes(name))
return null;
if (this.staticUtils.isDerivedFromStaticData(name, expr))
return null;
return { fieldPath: name, rootIdentifier: expr.node };
}
extractFromChildElement(child) {
const elementName = JSXUtils.getElementName(child.node.openingElement);
if (!elementName)
return null;
if (elementName === "Image" || elementName === "img") {
return this.extractFromImageElement(child);
}
const logicalParent = child.parentPath;
if (logicalParent?.isJSXExpressionContainer()) {
const logicalExpr = logicalParent.parentPath;
if (logicalExpr?.isLogicalExpression()) {
return this.extractFromConditionalImage(child);
}
}
return null;
}
extractFromImageElement(imageElement) {
const openingElement = imageElement.get("openingElement");
const attrs = openingElement.node.attributes;
for (const attr of attrs) {
if (this.types.isJSXAttribute(attr) &&
this.types.isJSXIdentifier(attr.name) &&
attr.name.name === "src") {
if (attr.value &&
this.types.isJSXExpressionContainer(attr.value) &&
this.types.isExpression(attr.value.expression) &&
!this.types.isJSXEmptyExpression(attr.value.expression)) {
const expr = attr.value.expression;
if (this.types.isMemberExpression(expr) ||
this.types.isOptionalMemberExpression(expr)) {
const rootId = this.expressionUtils.extractRootIdentifier(expr);
if (!rootId)
return null;
const parts = this.expressionUtils.collectMemberExpressionPath(expr);
if (parts.length < 2)
return null;
const fieldPath = parts.slice(1).join(".");
if (EXCLUDED_FIELDS.includes(fieldPath))
return null;
return { fieldPath, rootIdentifier: rootId };
}
}
}
}
return null;
}
extractFromConditionalImage(child) {
const elementName = JSXUtils.getElementName(child.node.openingElement);
if (elementName === "Image" || elementName === "img") {
return this.extractFromImageElement(child);
}
return null;
}
tryAddItemId(path, rootIdentifier, fieldPath) {
if (this.attributeUtils.hasAttribute(path, DATA_COLLECTION_ITEM_ID)) {
return;
}
const idField = this.resolveIdFieldName(path);
if (rootIdentifier.name === fieldPath) {
this.tryAddItemIdFromDestructuredParams(path, idField);
return;
}
const idExpr = this.buildIdExpression(rootIdentifier.name, idField);
const binding = path.scope.getBinding(rootIdentifier.name);
if (!binding) {
this.tryAddItemIdFromParentComponent(path, rootIdentifier, idField);
return;
}
if (binding.path.isVariableDeclarator() ||
binding.kind === "param") {
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, idExpr);
}
}
/**
* Build the ID expression for a given root object name.
* If a specific idField was resolved from a key attribute, generates root?.id (or root?._id).
* If unknown (cross-file component), generates root?.id || root?._id to cover both conventions.
*/
buildIdExpression(rootName, idField) {
if (idField) {
return this.types.optionalMemberExpression(this.types.identifier(rootName), this.types.identifier(idField), false, true);
}
return this.types.logicalExpression("||", this.types.optionalMemberExpression(this.types.identifier(rootName), this.types.identifier("id"), false, true), this.types.optionalMemberExpression(this.types.identifier(rootName), this.types.identifier("_id"), false, true));
}
/**
* Walk up the JSX tree to find a key attribute that accesses .id or ._id.
* Returns null when no key is found (e.g. cross-file component receiving props).
*/
resolveIdFieldName(path) {
let current = path.parentPath;
while (current) {
if (current.isJSXElement()) {
const opening = current.get("openingElement");
const field = this.extractIdFieldFromKey(opening);
if (field)
return field;
}
current = current.parentPath;
}
return null;
}
extractIdFieldFromKey(path) {
for (const attr of path.node.attributes) {
if (this.types.isJSXAttribute(attr) &&
this.types.isJSXIdentifier(attr.name) &&
attr.name.name === "key" &&
attr.value &&
this.types.isJSXExpressionContainer(attr.value)) {
const expr = attr.value.expression;
if (this.types.isMemberExpression(expr) &&
this.types.isIdentifier(expr.property)) {
const name = expr.property.name;
if (name === "id" || name === "_id")
return name;
}
}
}
return null;
}
/**
* For simple identifiers from destructured component props like ({ name, price }),
* find or inject the id field in the ObjectPattern so we can reference it directly.
*/
tryAddItemIdFromDestructuredParams(path, idField) {
const fn = path.getFunctionParent();
if (!fn)
return;
const params = fn.get("params");
for (const param of Array.isArray(params) ? params : [params]) {
if (!param.isObjectPattern())
continue;
for (const prop of param.get("properties")) {
if (!prop.isObjectProperty())
continue;
const key = prop.node.key;
const val = prop.node.value;
// Reuse a forwarding param already injected by processRootElement
if (this.types.isStringLiteral(key) &&
key.value === DATA_COLLECTION_ITEM_ID &&
this.types.isIdentifier(val)) {
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, this.types.identifier(val.name));
return;
}
// Use an existing id / _id destructured prop
if (this.types.isIdentifier(key) &&
(key.name === "id" || key.name === "_id") &&
this.types.isIdentifier(val)) {
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, this.types.identifier(val.name));
return;
}
}
const fieldToInject = idField ?? "id";
const newProp = this.types.objectProperty(this.types.identifier(fieldToInject), this.types.identifier(fieldToInject), false, true);
const props = param.node.properties;
const lastProp = props[props.length - 1];
if (lastProp && this.types.isRestElement(lastProp)) {
props.splice(props.length - 1, 0, newProp);
}
else {
props.push(newProp);
}
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, this.types.identifier(fieldToInject));
return;
}
}
tryAddItemIdFromParentComponent(path, rootIdentifier, idField) {
let current = path.parentPath;
while (current) {
if (current.isJSXElement()) {
const opening = current.get("openingElement");
const name = JSXUtils.getElementName(opening.node);
if (name && JSXUtils.isCustomComponent(name)) {
const keyAttr = this.findKeyWithId(opening);
if (keyAttr) {
const idExpr = this.buildIdExpression(rootIdentifier.name, idField);
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, idExpr);
return;
}
}
}
current = current.parentPath;
}
}
findKeyWithId(path) {
for (const attr of path.node.attributes) {
if (this.types.isJSXAttribute(attr) &&
this.types.isJSXIdentifier(attr.name) &&
attr.name.name === "key" &&
attr.value &&
this.types.isJSXExpressionContainer(attr.value)) {
const expr = attr.value.expression;
if (this.types.isExpression(expr)) {
return this.expressionUtils.isIdAccess(expr);
}
}
}
return false;
}
}
//# sourceMappingURL=collection-item-field-processor.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export declare class DataItemIdProcessor {
private types;
private attributeUtils;
private expressionUtils;
private pathUtils;
constructor(types: typeof t);
process(path: NodePath<t.JSXOpeningElement>): void;
/**
* Called for every root DOM element of a component. Adds a `data-collection-item-id`
* forwarding attribute so that when the component is used as a collection item
* (with `data-collection-item-id` passed as a prop from a parent `.map()`), the
* attribute flows through to the actual DOM element.
*
* Skipped when:
* - The root element already spreads props (e.g. `{...props}`) — forwarding is automatic
* - The enclosing function maps over a collection — it's a container, not an item
*/
processRootElement(path: NodePath<t.JSXOpeningElement>): void;
private hasSpreadAttribute;
/**
* Returns true when `fn` is a direct callback argument to a `.map()` or
* `.flatMap()` call — i.e. the function is an iteration callback, not a
* component definition. This prevents injecting prop-forwarding into
* patterns like `[...Array(3)].map((_, i) => <div />)` where the first
* param may be `undefined`.
*/
private isMapCallbackFunction;
private functionContainsMapCall;
/**
* Returns an expression for reading `data-collection-item-id` from the component's
* props, modifying the function's params destructuring if necessary.
*/
private buildCollectionItemIdExpression;
private findKeyAttribute;
private extractKeyExpression;
}
//# sourceMappingURL=collection-item-id-processor.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"collection-item-id-processor.d.ts","sourceRoot":"","sources":["../../src/processors/collection-item-id-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAC;AAWvC,qBAAa,mBAAmB;IAKlB,OAAO,CAAC,KAAK;IAJzB,OAAO,CAAC,cAAc,CAAoB;IAC1C,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,SAAS,CAAsB;gBAEnB,KAAK,EAAE,OAAO,CAAC;IAMnC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI;IAwBlD;;;;;;;;;OASG;IACH,kBAAkB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI;IA6B7D,OAAO,CAAC,kBAAkB;IAM1B;;;;;;OAMG;IACH,OAAO,CAAC,qBAAqB;IAW7B,OAAO,CAAC,uBAAuB;IA2B/B;;;OAGG;IACH,OAAO,CAAC,+BAA+B;IAsDvC,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,oBAAoB;CAY7B"}
@@ -0,0 +1,177 @@
import { DATA_COLLECTION_ITEM_ID } from "../consts.js";
import { JSXUtils } from "../jsx-utils.js";
import { JSXAttributeUtils, ExpressionAnalysisUtils, PathNavigationUtils, } from "./utils/shared-utils.js";
const FORWARDING_PARAM_NAME = "__dataCollectionItemId";
export class DataItemIdProcessor {
types;
attributeUtils;
expressionUtils;
pathUtils;
constructor(types) {
this.types = types;
this.attributeUtils = new JSXAttributeUtils(types);
this.expressionUtils = new ExpressionAnalysisUtils(types);
this.pathUtils = new PathNavigationUtils(types);
}
process(path) {
if (this.attributeUtils.hasAttribute(path, DATA_COLLECTION_ITEM_ID)) {
return;
}
const keyAttr = this.findKeyAttribute(path);
if (!keyAttr)
return;
const expression = this.extractKeyExpression(keyAttr);
if (!expression)
return;
if (!this.expressionUtils.isIdAccess(expression))
return;
const optionalExpr = this.types.isMemberExpression(expression)
? this.expressionUtils.createOptionalChainExpression(expression)
: expression;
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, optionalExpr);
}
/**
* Called for every root DOM element of a component. Adds a `data-collection-item-id`
* forwarding attribute so that when the component is used as a collection item
* (with `data-collection-item-id` passed as a prop from a parent `.map()`), the
* attribute flows through to the actual DOM element.
*
* Skipped when:
* - The root element already spreads props (e.g. `{...props}`) — forwarding is automatic
* - The enclosing function maps over a collection — it's a container, not an item
*/
processRootElement(path) {
if (this.attributeUtils.hasAttribute(path, DATA_COLLECTION_ITEM_ID))
return;
// Only DOM or allowed custom elements (e.g. motion.div)
const name = JSXUtils.getElementName(path.node);
if (!name || !JSXUtils.isDOMOrAllowed(name))
return;
// Only the root return element of a component function
if (!this.pathUtils.isRootReturnElement(path))
return;
// Skip if root element already spreads all props — forwarding is handled naturally
if (this.hasSpreadAttribute(path))
return;
const fn = this.pathUtils.findEnclosingFunction(path);
if (!fn)
return;
// Skip if this function IS a .map()/.flatMap() callback — it's an iteration
// function, not a component (e.g. [...Array(3)].map((_, i) => <div />))
if (this.isMapCallbackFunction(fn))
return;
// Skip if this function maps over a collection — it's a container, not an item
if (this.functionContainsMapCall(fn))
return;
const expr = this.buildCollectionItemIdExpression(fn);
if (!expr)
return;
this.attributeUtils.addExpressionAttribute(path, DATA_COLLECTION_ITEM_ID, expr);
}
hasSpreadAttribute(path) {
return path.node.attributes.some((attr) => this.types.isJSXSpreadAttribute(attr));
}
/**
* Returns true when `fn` is a direct callback argument to a `.map()` or
* `.flatMap()` call — i.e. the function is an iteration callback, not a
* component definition. This prevents injecting prop-forwarding into
* patterns like `[...Array(3)].map((_, i) => <div />)` where the first
* param may be `undefined`.
*/
isMapCallbackFunction(fn) {
const parent = fn.parentPath;
if (!parent?.isCallExpression())
return false;
const callee = parent.node.callee;
return (this.types.isMemberExpression(callee) &&
this.types.isIdentifier(callee.property) &&
(callee.property.name === "map" || callee.property.name === "flatMap"));
}
functionContainsMapCall(fn) {
let found = false;
const types = this.types;
fn.traverse({
// Stop at nested functions — event handlers / callbacks defined inside the
// component should not cause it to be treated as a collection container.
Function(innerFnPath) {
innerFnPath.skip();
},
CallExpression(callPath) {
if (found) {
callPath.stop();
return;
}
const callee = callPath.node.callee;
if (types.isMemberExpression(callee) &&
types.isIdentifier(callee.property) &&
(callee.property.name === "map" || callee.property.name === "flatMap")) {
found = true;
}
},
});
return found;
}
/**
* Returns an expression for reading `data-collection-item-id` from the component's
* props, modifying the function's params destructuring if necessary.
*/
buildCollectionItemIdExpression(fn) {
const params = fn.get("params");
const firstParam = Array.isArray(params) ? params[0] : params;
if (!firstParam)
return null;
if (firstParam.isObjectPattern()) {
// Check if already destructured (key must be a string literal — hyphens in the
// attribute name rule out a plain identifier)
for (const prop of firstParam.get("properties")) {
if (!prop.isObjectProperty())
continue;
const key = prop.node.key;
const val = prop.node.value;
if (this.types.isStringLiteral(key) &&
key.value === DATA_COLLECTION_ITEM_ID &&
this.types.isIdentifier(val)) {
return this.types.identifier(val.name);
}
}
// Add "data-collection-item-id": __dataCollectionItemId to destructuring
const newProp = this.types.objectProperty(this.types.stringLiteral(DATA_COLLECTION_ITEM_ID), this.types.identifier(FORWARDING_PARAM_NAME));
const restIdx = firstParam.node.properties.findIndex((p) => this.types.isRestElement(p));
if (restIdx === -1) {
firstParam.node.properties.push(newProp);
}
else {
firstParam.node.properties.splice(restIdx, 0, newProp);
}
return this.types.identifier(FORWARDING_PARAM_NAME);
}
if (firstParam.isIdentifier()) {
// props?.["data-collection-item-id"] — optional access in case param is nullish
return this.types.optionalMemberExpression(this.types.identifier(firstParam.node.name), this.types.stringLiteral(DATA_COLLECTION_ITEM_ID), true, // computed
true // optional
);
}
return null;
}
findKeyAttribute(path) {
for (const attr of path.node.attributes) {
if (this.types.isJSXAttribute(attr) &&
this.types.isJSXIdentifier(attr.name) &&
attr.name.name === "key") {
return attr;
}
}
return null;
}
extractKeyExpression(attr) {
if (!attr.value)
return null;
if (this.types.isJSXExpressionContainer(attr.value)) {
const expr = attr.value.expression;
if (this.types.isExpression(expr))
return expr;
}
return null;
}
}
//# sourceMappingURL=collection-item-id-processor.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,30 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export declare class StaticArrayProcessor {
private types;
private attributeUtils;
private staticValueUtils;
constructor(types: typeof t);
process(path: NodePath<t.JSXOpeningElement>): void;
private addDataAttributes;
private findTextContentFieldPath;
private findImageSrcFieldPath;
private extractFieldPathFromChild;
private extractFieldPath;
private extractKeyedLookupFieldPath;
private collectMemberExpressionParts;
private ensureIndexParam;
private addIndexParamToCallback;
private getMapCallback;
private findParentArrayMap;
private tryExtractMapInfo;
private isMapCall;
private extractArrayMapInfo;
private extractIndexParam;
private extractArrayVariableName;
private isStaticArray;
private resolveArrayExpression;
private resolveIdentifierToArray;
private isStaticArrayExpression;
}
//# sourceMappingURL=static-array-processor.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"static-array-processor.d.ts","sourceRoot":"","sources":["../../src/processors/static-array-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAC;AAmBvC,qBAAa,oBAAoB;IAInB,OAAO,CAAC,KAAK;IAHzB,OAAO,CAAC,cAAc,CAAoB;IAC1C,OAAO,CAAC,gBAAgB,CAAmB;gBAEvB,KAAK,EAAE,OAAO,CAAC;IAKnC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI;IAUlD,OAAO,CAAC,iBAAiB;IA4BzB,OAAO,CAAC,wBAAwB;IAkBhC,OAAO,CAAC,qBAAqB;IA6B7B,OAAO,CAAC,yBAAyB;IAYjC,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,2BAA2B;IAcnC,OAAO,CAAC,4BAA4B;IAmBpC,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,uBAAuB;IAY/B,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,kBAAkB;IAkB1B,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,SAAS;IAQjB,OAAO,CAAC,mBAAmB;IAsB3B,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,aAAa;IAKrB,OAAO,CAAC,sBAAsB;IAc9B,OAAO,CAAC,wBAAwB;IAWhC,OAAO,CAAC,uBAAuB;CAKhC"}
@@ -0,0 +1,212 @@
import { DATA_ARR_INDEX, DATA_ARR_VARIABLE_NAME, DATA_ARR_FIELD, MAX_JSX_DEPTH, } from "../consts.js";
import { JSXAttributeUtils, StaticValueUtils } from "./utils/shared-utils.js";
const GENERATED_INDEX_PARAM = "__arrIdx__";
export class StaticArrayProcessor {
types;
attributeUtils;
staticValueUtils;
constructor(types) {
this.types = types;
this.attributeUtils = new JSXAttributeUtils(types);
this.staticValueUtils = new StaticValueUtils(types);
}
process(path) {
const arrayInfo = this.findParentArrayMap(path);
if (!arrayInfo)
return;
if (!this.isStaticArray(arrayInfo.arrayExpression))
return;
const indexIdentifier = this.ensureIndexParam(arrayInfo);
this.addDataAttributes(path, arrayInfo, indexIdentifier);
}
addDataAttributes(path, arrayInfo, indexIdentifier) {
this.attributeUtils.addExpressionAttribute(path, DATA_ARR_INDEX, this.types.identifier(indexIdentifier));
if (arrayInfo.arrayVariableName) {
this.attributeUtils.addStringAttribute(path, DATA_ARR_VARIABLE_NAME, arrayInfo.arrayVariableName);
}
const fieldPath = this.findTextContentFieldPath(path, arrayInfo.callbackParam);
if (fieldPath) {
this.attributeUtils.addStringAttribute(path, DATA_ARR_FIELD, fieldPath);
}
}
findTextContentFieldPath(path, callbackParam) {
const imageSrcFieldPath = this.findImageSrcFieldPath(path, callbackParam);
if (imageSrcFieldPath)
return imageSrcFieldPath;
const parentElement = path.parentPath;
if (!parentElement?.isJSXElement())
return null;
for (const child of parentElement.get("children")) {
const fieldPath = this.extractFieldPathFromChild(child, callbackParam);
if (fieldPath)
return fieldPath;
}
return null;
}
findImageSrcFieldPath(path, callbackParam) {
const elementName = this.types.isJSXIdentifier(path.node.name)
? path.node.name.name
: null;
if (elementName !== "img" && elementName !== "Image")
return null;
for (const attr of path.get("attributes")) {
if (!attr.isJSXAttribute())
continue;
const attrName = attr.get("name");
if (!attrName.isJSXIdentifier() || attrName.node.name !== "src") {
continue;
}
const value = attr.get("value");
if (!value.isJSXExpressionContainer())
return null;
const expression = value.get("expression");
if (!expression.isMemberExpression())
return null;
return this.extractFieldPath(expression, callbackParam);
}
return null;
}
extractFieldPathFromChild(child, callbackParam) {
if (!child.isJSXExpressionContainer())
return null;
const expression = child.get("expression");
if (!expression.isMemberExpression())
return null;
return this.extractFieldPath(expression, callbackParam);
}
extractFieldPath(expr, callbackParam) {
const keyedLookup = this.extractKeyedLookupFieldPath(expr, callbackParam);
if (keyedLookup)
return keyedLookup;
const parts = this.collectMemberExpressionParts(expr);
if (!parts)
return null;
const { rootName, propertyNames } = parts;
return rootName === callbackParam ? propertyNames.join(".") : null;
}
extractKeyedLookupFieldPath(expr, callbackParam) {
const object = expr.get("object");
if (!expr.node.computed || !object.isIdentifier())
return null;
const property = expr.get("property");
if (!property.isMemberExpression())
return null;
const keyFieldPath = this.extractFieldPath(property, callbackParam);
return keyFieldPath ? `${object.node.name}[${keyFieldPath}]` : null;
}
collectMemberExpressionParts(expr) {
const propertyNames = [];
let current = expr;
while (current.isMemberExpression()) {
const property = current.get("property");
if (!property.isIdentifier())
return null;
propertyNames.unshift(property.node.name);
current = current.get("object");
}
if (!current.isIdentifier())
return null;
return { rootName: current.node.name, propertyNames };
}
ensureIndexParam(arrayInfo) {
if (arrayInfo.indexParam) {
return arrayInfo.indexParam;
}
this.addIndexParamToCallback(arrayInfo.mapCallPath);
return GENERATED_INDEX_PARAM;
}
addIndexParamToCallback(mapCallPath) {
const callback = this.getMapCallback(mapCallPath);
if (!callback)
return;
const params = callback.get("params");
if (params.length === 1) {
callback.node.params.push(this.types.identifier(GENERATED_INDEX_PARAM));
}
}
getMapCallback(mapCallPath) {
const args = mapCallPath.get("arguments");
const firstArg = args[0];
if (firstArg && firstArg.isFunction()) {
return firstArg;
}
return null;
}
findParentArrayMap(path, maxDepth = MAX_JSX_DEPTH) {
let currentPath = path;
let depth = 0;
while (currentPath.parentPath && depth < maxDepth) {
const mapInfo = this.tryExtractMapInfo(currentPath.parentPath);
if (mapInfo)
return mapInfo;
currentPath = currentPath.parentPath;
depth++;
}
return null;
}
tryExtractMapInfo(parent) {
if (!parent.isCallExpression())
return null;
if (!this.isMapCall(parent))
return null;
return this.extractArrayMapInfo(parent);
}
isMapCall(callExpr) {
const callee = callExpr.get("callee");
if (!callee.isMemberExpression())
return false;
const property = callee.get("property");
return property.isIdentifier() && property.node.name === "map";
}
extractArrayMapInfo(mapCall) {
const callback = this.getMapCallback(mapCall);
if (!callback)
return null;
const params = callback.get("params");
const firstParam = params[0];
if (!firstParam || !firstParam.isIdentifier())
return null;
const callee = mapCall.get("callee");
const arrayExpression = callee.get("object");
return {
arrayExpression,
callbackParam: firstParam.node.name,
indexParam: this.extractIndexParam(params),
arrayVariableName: this.extractArrayVariableName(arrayExpression),
mapCallPath: mapCall,
};
}
extractIndexParam(params) {
const secondParam = params[1];
return secondParam && secondParam.isIdentifier()
? secondParam.node.name
: null;
}
extractArrayVariableName(arrayExpression) {
return arrayExpression.isIdentifier() ? arrayExpression.node.name : null;
}
isStaticArray(arrayExpression) {
const arrayExpr = this.resolveArrayExpression(arrayExpression);
return arrayExpr ? this.isStaticArrayExpression(arrayExpr) : false;
}
resolveArrayExpression(expression) {
if (expression.isArrayExpression()) {
return expression;
}
if (expression.isIdentifier()) {
return this.resolveIdentifierToArray(expression);
}
return null;
}
resolveIdentifierToArray(identifier) {
const binding = identifier.scope.getBinding(identifier.node.name);
if (!binding?.path.isVariableDeclarator())
return null;
const init = binding.path.get("init");
return init.isArrayExpression() ? init : null;
}
isStaticArrayExpression(arrayExpression) {
return this.staticValueUtils.isStaticArrayExpression(arrayExpression);
}
}
//# sourceMappingURL=static-array-processor.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,36 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
import type { CollectionInfo } from "../../capabilities/inline-edit/types.js";
export declare class CollectionTracingUtils {
private types;
private bindingUtils;
private callUtils;
private visitedSetters;
private visitedGetByIdSetters;
constructor(types: typeof t);
resetVisited(): void;
traceCollectionSource(path: NodePath<t.Expression>): CollectionInfo | null;
private traceExpression;
private traceIdentifierToCollection;
private traceVariableDeclarator;
private traceUseStateSetter;
private findThenCallWithSetter;
private traceMemberExpression;
private traceCallExpression;
/**
* Trace useQuery({ queryFn: () => base44.entities.X.list() }) patterns.
* Extracts the collection from the queryFn return expression.
*/
private traceUseQueryCall;
private checkDirectServiceCall;
private traceArrayDestructuring;
private traceObjectDestructuring;
private tracePromiseAllElement;
private traceParameterToCollection;
private traceArrayExpression;
private getFunctionName;
private getJSXElementName;
traceGetByIdSource(path: NodePath): CollectionInfo | null;
private findGetByIdInScope;
}
//# sourceMappingURL=collection-tracing-utils.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"collection-tracing-utils.d.ts","sourceRoot":"","sources":["../../../src/processors/utils/collection-tracing-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,KAAK,CAAC,MAAM,cAAc,CAAC;AACvC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAM9E,qBAAa,sBAAsB;IAMrB,OAAO,CAAC,KAAK;IALzB,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,cAAc,CAAqB;IAC3C,OAAO,CAAC,qBAAqB,CAAqB;gBAE9B,KAAK,EAAE,OAAO,CAAC;IAKnC,YAAY,IAAI,IAAI;IAKpB,qBAAqB,CACnB,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,GAC3B,cAAc,GAAG,IAAI;IAKxB,OAAO,CAAC,eAAe;IA6BvB,OAAO,CAAC,2BAA2B;IAkBnC,OAAO,CAAC,uBAAuB;IA0D/B,OAAO,CAAC,mBAAmB;IAkC3B,OAAO,CAAC,sBAAsB;IA6B9B,OAAO,CAAC,qBAAqB;IAgC7B,OAAO,CAAC,mBAAmB;IAoB3B;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAyCzB,OAAO,CAAC,sBAAsB;IAgC9B,OAAO,CAAC,uBAAuB;IA4B/B,OAAO,CAAC,wBAAwB;IAmChC,OAAO,CAAC,sBAAsB;IA2B9B,OAAO,CAAC,0BAA0B;IAgElC,OAAO,CAAC,oBAAoB;IAgB5B,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,iBAAiB;IAOzB,kBAAkB,CAChB,IAAI,EAAE,QAAQ,GACb,cAAc,GAAG,IAAI;IAKxB,OAAO,CAAC,kBAAkB;CA0B3B"}
@@ -0,0 +1,428 @@
import { BindingUtils, CallExpressionUtils, } from "./shared-utils.js";
export class CollectionTracingUtils {
types;
bindingUtils;
callUtils;
visitedSetters = new Set();
visitedGetByIdSetters = new Set();
constructor(types) {
this.types = types;
this.bindingUtils = new BindingUtils(types);
this.callUtils = new CallExpressionUtils(types);
}
resetVisited() {
this.visitedSetters.clear();
this.visitedGetByIdSetters.clear();
}
traceCollectionSource(path) {
this.resetVisited();
return this.traceExpression(path);
}
traceExpression(path) {
if (path.isIdentifier()) {
return this.traceIdentifierToCollection(path);
}
if (path.isMemberExpression()) {
return this.traceMemberExpression(path);
}
if (path.isCallExpression()) {
return this.traceCallExpression(path);
}
if (path.isAwaitExpression()) {
const argument = path.get("argument");
if (argument.isExpression()) {
return this.traceExpression(argument);
}
}
if (path.isArrayExpression()) {
return this.traceArrayExpression(path);
}
return null;
}
traceIdentifierToCollection(path) {
const name = path.node.name;
const binding = path.scope.getBinding(name);
if (!binding)
return null;
if (binding.path.isVariableDeclarator()) {
return this.traceVariableDeclarator(binding.path, name);
}
if (this.bindingUtils.isFunctionParameter(name, path)) {
return this.traceParameterToCollection(name, path);
}
return null;
}
traceVariableDeclarator(declaratorPath, variableName) {
const init = declaratorPath.get("init");
if (init.isCallExpression()) {
const directResult = this.checkDirectServiceCall(init);
if (directResult)
return directResult;
const useStateInfo = this.bindingUtils.isUseStateCall(init);
if (useStateInfo?.setterName) {
return this.traceUseStateSetter(useStateInfo.setterName, declaratorPath);
}
}
if (init.isAwaitExpression()) {
const awaitArg = init.get("argument");
if (awaitArg.isCallExpression()) {
const directResult = this.checkDirectServiceCall(awaitArg);
if (directResult)
return directResult;
}
return this.traceExpression(init);
}
if (init.isConditionalExpression()) {
const consequent = init.get("consequent");
const alternate = init.get("alternate");
const result = this.traceExpression(consequent);
if (result)
return result;
return this.traceExpression(alternate);
}
if (init.isMemberExpression()) {
return this.traceExpression(init);
}
if (init.isIdentifier()) {
return this.traceExpression(init);
}
if (init.isCallExpression()) {
return this.traceExpression(init);
}
const id = declaratorPath.get("id");
if (id.isArrayPattern()) {
return this.traceArrayDestructuring(declaratorPath, id, variableName);
}
if (id.isObjectPattern()) {
return this.traceObjectDestructuring(declaratorPath, id, variableName);
}
return null;
}
traceUseStateSetter(setterName, declaratorPath) {
if (this.visitedSetters.has(setterName))
return null;
this.visitedSetters.add(setterName);
const functionScope = declaratorPath.getFunctionParent() ?? declaratorPath.scope.path;
// Pattern 1: setData(result) — direct call
const setterCall = this.bindingUtils.findSetterCallInScope(setterName, functionScope);
if (setterCall) {
const args = setterCall.get("arguments");
const firstArg = args[0];
if (!firstArg || !firstArg.isExpression())
return null;
return this.traceExpression(firstArg);
}
// Pattern 2: somePromise.then(setData) — setter passed as .then callback
const thenCall = this.findThenCallWithSetter(setterName, functionScope);
if (thenCall) {
const callee = thenCall.get("callee");
if (callee.isMemberExpression()) {
const promiseExpr = callee.get("object");
return this.traceExpression(promiseExpr);
}
}
return null;
}
findThenCallWithSetter(setterName, scope) {
let result = null;
scope.traverse({
CallExpression: (callPath) => {
if (result) {
callPath.stop();
return;
}
const callee = callPath.node.callee;
if (this.types.isMemberExpression(callee) &&
this.types.isIdentifier(callee.property) &&
callee.property.name === "then" &&
callPath.node.arguments.length > 0 &&
this.types.isIdentifier(callPath.node.arguments[0]) &&
callPath.node.arguments[0].name === setterName) {
result = callPath;
}
},
});
return result;
}
traceMemberExpression(path) {
const property = path.get("property");
const object = path.get("object");
if (property.isIdentifier() &&
property.node.name === "items") {
if (object.isExpression()) {
return this.traceExpression(object);
}
}
if (object.isIdentifier()) {
const objResult = this.traceIdentifierToCollection(object);
if (objResult && property.isIdentifier()) {
if (objResult.references.includes(property.node.name)) {
return { id: property.node.name, references: [] };
}
}
return objResult;
}
if (object.isExpression()) {
return this.traceExpression(object);
}
return null;
}
traceCallExpression(path) {
const directResult = this.checkDirectServiceCall(path);
if (directResult)
return directResult;
const useQueryResult = this.traceUseQueryCall(path);
if (useQueryResult)
return useQueryResult;
if (this.callUtils.isChainedArrayMethod(path.node)) {
const callee = path.get("callee");
if (callee.isMemberExpression()) {
const obj = callee.get("object");
return this.traceExpression(obj);
}
}
return null;
}
/**
* Trace useQuery({ queryFn: () => base44.entities.X.list() }) patterns.
* Extracts the collection from the queryFn return expression.
*/
traceUseQueryCall(path) {
const callee = path.get("callee");
if (!callee.isIdentifier() || callee.node.name !== "useQuery")
return null;
const args = path.get("arguments");
const configArg = args[0];
if (!configArg?.isObjectExpression())
return null;
for (const prop of configArg.get("properties")) {
if (!prop.isObjectProperty())
continue;
const key = prop.get("key");
if (!key.isIdentifier() || key.node.name !== "queryFn")
continue;
const value = prop.get("value");
if (value.isArrowFunctionExpression() || value.isFunctionExpression()) {
const fnBody = value.get("body");
if (fnBody.isCallExpression()) {
return this.checkDirectServiceCall(fnBody);
}
if (fnBody.isBlockStatement()) {
let result = null;
fnBody.traverse({
ReturnStatement: (retPath) => {
if (result)
return;
const arg = retPath.get("argument");
if (arg.isCallExpression()) {
result = this.checkDirectServiceCall(arg);
}
},
});
if (result)
return result;
}
}
}
return null;
}
checkDirectServiceCall(path) {
const getAllInfo = this.callUtils.isGetAllCall(path.node);
if (getAllInfo) {
return {
id: getAllInfo.collectionName,
references: getAllInfo.references,
};
}
const getByIdInfo = this.callUtils.isGetByIdCall(path.node);
if (getByIdInfo) {
return {
id: getByIdInfo.collectionName,
references: getByIdInfo.multiRefFields,
};
}
const base44List = this.callUtils.isBase44EntityListCall(path.node);
if (base44List) {
return { id: base44List.collectionName, references: [] };
}
const base44Get = this.callUtils.isBase44EntityGetCall(path.node);
if (base44Get) {
return { id: base44Get.collectionName, references: [] };
}
return null;
}
traceArrayDestructuring(declaratorPath, pattern, variableName) {
const elements = pattern.get("elements");
const index = elements.findIndex((el) => el.isIdentifier() && el.node.name === variableName);
if (index === -1)
return null;
const init = declaratorPath.get("init");
if (init.hasNode() && this.bindingUtils.isPromiseAllCall(init)) {
return this.tracePromiseAllElement(init, index);
}
if (init.isCallExpression()) {
const useStateInfo = this.bindingUtils.isUseStateCall(init);
if (useStateInfo && index === 0 && useStateInfo.setterName) {
return this.traceUseStateSetter(useStateInfo.setterName, declaratorPath);
}
}
return null;
}
traceObjectDestructuring(declaratorPath, pattern, variableName) {
const init = declaratorPath.get("init");
if (!init.isExpression())
return null;
const prop = pattern.node.properties.find((p) => this.types.isObjectProperty(p) &&
((this.types.isIdentifier(p.value) && p.value.name === variableName) ||
(this.types.isAssignmentPattern(p.value) &&
this.types.isIdentifier(p.value.left) &&
p.value.left.name === variableName)));
if (!prop || !this.types.isObjectProperty(prop))
return null;
const key = prop.key;
const propertyName = this.types.isIdentifier(key)
? key.name
: this.types.isStringLiteral(key)
? key.value
: null;
if (propertyName === "items") {
return this.traceExpression(init);
}
return this.traceExpression(init);
}
tracePromiseAllElement(init, index) {
let callExpr;
if (init.isAwaitExpression()) {
const arg = init.get("argument");
if (!arg.isCallExpression())
return null;
callExpr = arg;
}
else if (init.isCallExpression()) {
callExpr = init;
}
else {
return null;
}
const args = callExpr.get("arguments");
const firstArg = args[0];
if (!firstArg?.isArrayExpression())
return null;
const elements = firstArg.get("elements");
const targetElement = elements[index];
if (!targetElement || !targetElement.isExpression())
return null;
return this.traceExpression(targetElement);
}
traceParameterToCollection(paramName, path) {
const fn = path.getFunctionParent();
if (!fn)
return null;
const fnName = this.getFunctionName(fn);
if (!fnName)
return null;
const programPath = fn.findParent((p) => p.isProgram());
if (!programPath)
return null;
let result = null;
programPath.traverse({
JSXOpeningElement: (jsxPath) => {
if (result)
return;
const elementName = this.getJSXElementName(jsxPath.node);
if (elementName !== fnName)
return;
for (const attr of jsxPath.node.attributes) {
if (!this.types.isJSXAttribute(attr))
continue;
if (!this.types.isJSXIdentifier(attr.name))
continue;
if (attr.name.name !== paramName)
continue;
if (attr.value &&
this.types.isJSXExpressionContainer(attr.value) &&
this.types.isExpression(attr.value.expression)) {
const exprPath = jsxPath
.get("attributes")
.find((a) => a.isJSXAttribute() &&
this.types.isJSXIdentifier(a.node.name) &&
a.node.name.name === paramName);
if (exprPath && exprPath.isJSXAttribute()) {
const valPath = exprPath.get("value");
if (valPath.isJSXExpressionContainer()) {
const expr = valPath.get("expression");
if (expr.isExpression()) {
result = this.traceExpression(expr);
}
}
}
}
}
},
});
if (!result) {
return { id: paramName, references: [] };
}
return result;
}
traceArrayExpression(path) {
const elements = path.get("elements");
for (const el of elements) {
if (el.isSpreadElement()) {
const arg = el.get("argument");
if (arg.isExpression()) {
const result = this.traceExpression(arg);
if (result)
return result;
}
}
}
return null;
}
getFunctionName(fn) {
if (fn.isFunctionDeclaration() && fn.node.id) {
return fn.node.id.name;
}
const parent = fn.parentPath;
if (parent?.isVariableDeclarator()) {
const id = parent.get("id");
if (id.isIdentifier())
return id.node.name;
}
return null;
}
getJSXElementName(node) {
if (this.types.isJSXIdentifier(node.name)) {
return node.name.name;
}
return null;
}
traceGetByIdSource(path) {
this.visitedGetByIdSetters.clear();
return this.findGetByIdInScope(path);
}
findGetByIdInScope(path) {
const fn = path.getFunctionParent() ?? path.scope.path;
let result = null;
fn.traverse({
CallExpression: (callPath) => {
if (result)
return;
const info = this.callUtils.isGetByIdCall(callPath.node);
if (info) {
result = {
id: info.collectionName,
references: info.multiRefFields,
};
return;
}
const base44Get = this.callUtils.isBase44EntityGetCall(callPath.node);
if (base44Get) {
result = { id: base44Get.collectionName, references: [] };
}
},
});
return result;
}
}
//# sourceMappingURL=collection-tracing-utils.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,96 @@
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export declare class JSXAttributeUtils {
private types;
constructor(types: typeof t);
hasAttribute(path: NodePath<t.JSXOpeningElement>, attributeName: string): boolean;
getAttributeValue(path: NodePath<t.JSXOpeningElement>, attributeName: string): t.JSXAttribute["value"] | null;
getAttributeStringValue(path: NodePath<t.JSXOpeningElement>, attributeName: string): string | null;
addStringAttribute(path: NodePath<t.JSXOpeningElement>, attributeName: string, value: string): void;
addExpressionAttribute(path: NodePath<t.JSXOpeningElement>, attributeName: string, expression: t.Expression): void;
findAncestorWithAttribute(path: NodePath, attributeName: string): NodePath<t.JSXOpeningElement> | null;
}
export declare class PathNavigationUtils {
private types;
constructor(types: typeof t);
findParentJSXElement(path: NodePath): NodePath<t.JSXElement> | null;
findEnclosingFunction(path: NodePath): NodePath<t.Function> | null;
findReturnStatement(path: NodePath): NodePath<t.ReturnStatement> | null;
isRootReturnElement(path: NodePath<t.JSXOpeningElement>): boolean;
findDOMElementTarget(path: NodePath<t.JSXOpeningElement>): NodePath<t.JSXOpeningElement> | null;
private isDOMOrAllowedElement;
}
export declare class ExpressionAnalysisUtils {
private types;
constructor(types: typeof t);
isIdAccess(node: t.Expression): boolean;
isItemsAccess(node: t.Expression): boolean;
isLengthAccess(node: t.Expression): boolean;
extractRootIdentifier(node: t.Expression): t.Identifier | null;
unwrapLogicalExpression(node: t.Expression): t.Expression;
collectMemberExpressionPath(node: t.Expression): string[];
createOptionalChainExpression(node: t.MemberExpression): t.OptionalMemberExpression;
getFieldPathFromExpression(node: t.Expression, rootName: string): string | null;
}
export declare class BindingUtils {
private types;
constructor(types: typeof t);
isFunctionParameter(identifierName: string, path: NodePath): boolean;
isUseStateCall(init: NodePath<t.Node>): {
stateIndex: number;
setterName: string | null;
} | null;
isPromiseAllCall(init: NodePath<t.Node>): boolean;
private isPromiseAllCallee;
extractDestructuredProperties(pattern: t.ObjectPattern): string[];
findSetterCallInScope(setterName: string, scope: NodePath): NodePath<t.CallExpression> | null;
}
export declare class CallExpressionUtils {
private types;
constructor(types: typeof t);
isGetAllCall(node: t.CallExpression): {
collectionName: string;
references: string[];
} | null;
isGetByIdCall(node: t.CallExpression): {
collectionName: string;
multiRefFields: string[];
} | null;
private extractReferencesFromArg;
private extractMultiRefFromOptions;
isArrayMethod(node: t.CallExpression, methodName: string): boolean;
isChainedArrayMethod(node: t.CallExpression): boolean;
/**
* Detect base44.entities.EntityName.list() or .getAll() patterns.
* Returns the entity name as the collection name.
*/
isBase44EntityListCall(node: t.CallExpression): {
collectionName: string;
} | null;
/**
* Detect base44.entities.EntityName.getById() or .get() patterns.
*/
isBase44EntityGetCall(node: t.CallExpression): {
collectionName: string;
} | null;
getCallbackArgument(callExpr: t.CallExpression): t.ArrowFunctionExpression | t.FunctionExpression | null;
}
export declare class StaticValueUtils {
private types;
constructor(types: typeof t);
isPrimitiveLiteral(path: NodePath<t.Node>): boolean;
isStaticValue(path: NodePath<t.Node>, visited?: Set<string>): boolean;
isStaticIdentifier(path: NodePath<t.Identifier>, visited?: Set<string>): boolean;
isStaticObject(path: NodePath<t.ObjectExpression>, visited?: Set<string>): boolean;
isStaticArrayExpression(arrayExpression: NodePath<t.ArrayExpression>, visited?: Set<string>): boolean;
isDerivedFromStaticData(identifierName: string, path: NodePath): boolean;
}
export declare class TypeCheckUtils {
private types;
constructor(types: typeof t);
isArrayIsArrayCheck(node: t.Expression): boolean;
isTypeofObjectCheck(node: t.Expression): boolean;
isReferenceTypeCheck(node: t.Expression): boolean;
isLengthCheck(node: t.Expression): boolean;
}
//# sourceMappingURL=shared-utils.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"shared-utils.d.ts","sourceRoot":"","sources":["../../../src/processors/utils/shared-utils.ts"],"names":[],"mappings":"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"}
+600
View File
@@ -0,0 +1,600 @@
import { JSXUtils } from "../../jsx-utils.js";
import { ALLOWED_CUSTOM_COMPONENTS, EXCLUDED_FIELDS } from "../../consts.js";
export class JSXAttributeUtils {
types;
constructor(types) {
this.types = types;
}
hasAttribute(path, attributeName) {
return path.node.attributes.some((attr) => this.types.isJSXAttribute(attr) &&
JSXUtils.getAttributeName(attr) === attributeName);
}
getAttributeValue(path, attributeName) {
for (const attr of path.node.attributes) {
if (this.types.isJSXAttribute(attr) &&
JSXUtils.getAttributeName(attr) === attributeName) {
return attr.value;
}
}
return null;
}
getAttributeStringValue(path, attributeName) {
const value = this.getAttributeValue(path, attributeName);
if (value && this.types.isStringLiteral(value)) {
return value.value;
}
return null;
}
addStringAttribute(path, attributeName, value) {
if (!this.hasAttribute(path, attributeName)) {
path.node.attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier(attributeName), this.types.stringLiteral(value)));
}
}
addExpressionAttribute(path, attributeName, expression) {
if (!this.hasAttribute(path, attributeName)) {
path.node.attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier(attributeName), this.types.jsxExpressionContainer(expression)));
}
}
findAncestorWithAttribute(path, attributeName) {
let current = path.parentPath;
while (current) {
if (current.isJSXElement()) {
const opening = current.get("openingElement");
if (this.hasAttribute(opening, attributeName)) {
return opening;
}
}
current = current.parentPath;
}
return null;
}
}
export class PathNavigationUtils {
types;
constructor(types) {
this.types = types;
}
findParentJSXElement(path) {
let current = path.parentPath;
while (current) {
if (current.isJSXElement())
return current;
current = current.parentPath;
}
return null;
}
findEnclosingFunction(path) {
let current = path.parentPath;
while (current) {
if (current.isFunction())
return current;
current = current.parentPath;
}
return null;
}
findReturnStatement(path) {
let current = path.parentPath;
while (current) {
if (current.isReturnStatement())
return current;
current = current.parentPath;
}
return null;
}
isRootReturnElement(path) {
const jsxElement = path.parentPath;
if (!jsxElement?.isJSXElement())
return false;
const parent = jsxElement.parentPath;
if (!parent)
return false;
if (parent.isReturnStatement())
return true;
if (parent.isArrowFunctionExpression())
return true;
if (parent.isParenthesizedExpression()) {
const grandparent = parent.parentPath;
if (grandparent?.isReturnStatement())
return true;
if (grandparent?.isArrowFunctionExpression())
return true;
}
return false;
}
findDOMElementTarget(path) {
if (this.isDOMOrAllowedElement(path))
return path;
let current = path.parentPath;
while (current) {
if (current.isJSXElement()) {
const opening = current.get("openingElement");
if (this.isDOMOrAllowedElement(opening))
return opening;
}
current = current.parentPath;
}
return null;
}
isDOMOrAllowedElement(path) {
const name = JSXUtils.getElementName(path.node);
if (!name)
return false;
if (name.charAt(0) === name.charAt(0).toLowerCase())
return true;
return ALLOWED_CUSTOM_COMPONENTS.includes(name);
}
}
export class ExpressionAnalysisUtils {
types;
constructor(types) {
this.types = types;
}
isIdAccess(node) {
if (!this.types.isMemberExpression(node))
return false;
if (this.types.isIdentifier(node.property)) {
return node.property.name === "_id" || node.property.name === "id";
}
if (this.types.isStringLiteral(node.property)) {
return node.property.value === "_id" || node.property.value === "id";
}
return false;
}
isItemsAccess(node) {
return (this.types.isMemberExpression(node) &&
this.types.isIdentifier(node.property) &&
node.property.name === "items");
}
isLengthAccess(node) {
return (this.types.isMemberExpression(node) &&
this.types.isIdentifier(node.property) &&
node.property.name === "length");
}
extractRootIdentifier(node) {
if (this.types.isIdentifier(node))
return node;
if (this.types.isMemberExpression(node)) {
return this.extractRootIdentifier(node.object);
}
if (this.types.isOptionalMemberExpression(node)) {
return this.extractRootIdentifier(node.object);
}
if (this.types.isCallExpression(node) && this.types.isMemberExpression(node.callee)) {
return this.extractRootIdentifier(node.callee.object);
}
return null;
}
unwrapLogicalExpression(node) {
if (this.types.isLogicalExpression(node)) {
if (node.operator === "&&")
return node.right;
if (node.operator === "||" || node.operator === "??") {
return node.left;
}
}
return node;
}
collectMemberExpressionPath(node) {
const parts = [];
let current = node;
while (this.types.isMemberExpression(current) ||
this.types.isOptionalMemberExpression(current)) {
const prop = current.property;
if (this.types.isIdentifier(prop)) {
parts.unshift(prop.name);
}
else if (this.types.isStringLiteral(prop)) {
parts.unshift(prop.value);
}
current = current.object;
}
if (this.types.isIdentifier(current)) {
parts.unshift(current.name);
}
return parts;
}
createOptionalChainExpression(node) {
const object = this.types.isOptionalMemberExpression(node.object)
? node.object
: this.types.isMemberExpression(node.object)
? this.createOptionalChainExpression(node.object)
: node.object;
const property = node.property;
return this.types.optionalMemberExpression(object, property, node.computed, true);
}
getFieldPathFromExpression(node, rootName) {
const parts = this.collectMemberExpressionPath(node);
if (parts.length < 2)
return null;
if (parts[0] !== rootName)
return null;
const fieldParts = parts.slice(1);
const fieldPath = fieldParts.join(".");
if (EXCLUDED_FIELDS.includes(fieldPath))
return null;
return fieldPath;
}
}
export class BindingUtils {
types;
constructor(types) {
this.types = types;
}
isFunctionParameter(identifierName, path) {
const fn = path.getFunctionParent();
if (!fn)
return false;
const params = fn.get("params");
for (const param of (Array.isArray(params) ? params : [params])) {
if (param.isIdentifier() && param.node.name === identifierName) {
return true;
}
if (param.isObjectPattern()) {
for (const prop of param.get("properties")) {
if (prop.isObjectProperty() &&
this.types.isIdentifier(prop.node.value) &&
prop.node.value.name === identifierName) {
return true;
}
}
}
if (param.isArrayPattern()) {
for (const el of param.get("elements")) {
if (el.isIdentifier() && el.node.name === identifierName) {
return true;
}
}
}
}
return false;
}
isUseStateCall(init) {
if (!init.isCallExpression())
return null;
const callee = init.get("callee");
if (!callee.isIdentifier() || callee.node.name !== "useState")
return null;
const declarator = init.parentPath;
if (!declarator?.isVariableDeclarator())
return null;
const id = declarator.get("id");
if (!id.isArrayPattern())
return null;
const elements = id.get("elements");
const setterEl = elements[1];
const setterName = setterEl && setterEl.isIdentifier() ? setterEl.node.name : null;
return { stateIndex: 0, setterName };
}
isPromiseAllCall(init) {
if (!init.isAwaitExpression()) {
if (init.isCallExpression()) {
const callee = init.get("callee");
return this.isPromiseAllCallee(callee);
}
return false;
}
const argument = init.get("argument");
if (!argument.isCallExpression())
return false;
const callee = argument.get("callee");
return this.isPromiseAllCallee(callee);
}
isPromiseAllCallee(callee) {
if (!callee.isMemberExpression())
return false;
const obj = callee.get("object");
const prop = callee.get("property");
return (obj.isIdentifier() &&
obj.node.name === "Promise" &&
prop.isIdentifier() &&
prop.node.name === "all");
}
extractDestructuredProperties(pattern) {
const properties = [];
for (const prop of pattern.properties) {
if (this.types.isObjectProperty(prop)) {
if (this.types.isIdentifier(prop.key)) {
properties.push(prop.key.name);
}
else if (this.types.isStringLiteral(prop.key)) {
properties.push(prop.key.value);
}
}
}
return properties;
}
findSetterCallInScope(setterName, scope) {
let result = null;
scope.traverse({
CallExpression(callPath) {
if (result)
return;
const callee = callPath.get("callee");
if (callee.isIdentifier() && callee.node.name === setterName) {
result = callPath;
}
},
});
return result;
}
}
export class CallExpressionUtils {
types;
constructor(types) {
this.types = types;
}
isGetAllCall(node) {
const callee = node.callee;
if (!this.types.isMemberExpression(callee))
return null;
const obj = callee.object;
const prop = callee.property;
if (!this.types.isIdentifier(obj) ||
!this.types.isIdentifier(prop) ||
prop.name !== "getAll") {
return null;
}
const args = node.arguments;
if (args.length < 1)
return null;
const firstArg = args[0];
if (!this.types.isStringLiteral(firstArg))
return null;
const collectionName = firstArg.value;
const references = this.extractReferencesFromArg(args[1]);
return { collectionName, references };
}
isGetByIdCall(node) {
const callee = node.callee;
if (!this.types.isMemberExpression(callee))
return null;
const obj = callee.object;
const prop = callee.property;
if (!this.types.isIdentifier(obj) ||
!this.types.isIdentifier(prop) ||
prop.name !== "getById") {
return null;
}
const args = node.arguments;
if (args.length < 1)
return null;
const firstArg = args[0];
if (!this.types.isStringLiteral(firstArg))
return null;
const collectionName = firstArg.value;
const multiRefFields = this.extractMultiRefFromOptions(args[2]);
return { collectionName, multiRefFields };
}
extractReferencesFromArg(arg) {
if (!arg || !this.types.isArrayExpression(arg))
return [];
return arg.elements
.filter((el) => this.types.isStringLiteral(el))
.map((el) => el.value);
}
extractMultiRefFromOptions(arg) {
if (!arg || !this.types.isObjectExpression(arg))
return [];
for (const prop of arg.properties) {
if (this.types.isObjectProperty(prop) &&
this.types.isIdentifier(prop.key) &&
prop.key.name === "multiRef" &&
this.types.isArrayExpression(prop.value)) {
return prop.value.elements
.filter((el) => this.types.isStringLiteral(el))
.map((el) => el.value);
}
}
return [];
}
isArrayMethod(node, methodName) {
const callee = node.callee;
return (this.types.isMemberExpression(callee) &&
this.types.isIdentifier(callee.property) &&
callee.property.name === methodName);
}
isChainedArrayMethod(node) {
const chainMethods = ["filter", "sort", "slice", "concat", "reverse", "flat"];
return chainMethods.some((m) => this.isArrayMethod(node, m));
}
/**
* Detect base44.entities.EntityName.list() or .getAll() patterns.
* Returns the entity name as the collection name.
*/
isBase44EntityListCall(node) {
const callee = node.callee;
if (!this.types.isMemberExpression(callee))
return null;
const method = callee.property;
if (!this.types.isIdentifier(method) ||
(method.name !== "list" && method.name !== "getAll" && method.name !== "filter")) {
return null;
}
const entityAccess = callee.object;
if (!this.types.isMemberExpression(entityAccess))
return null;
const entityName = entityAccess.property;
if (!this.types.isIdentifier(entityName))
return null;
const entitiesAccess = entityAccess.object;
if (!this.types.isMemberExpression(entitiesAccess))
return null;
const entitiesProp = entitiesAccess.property;
if (!this.types.isIdentifier(entitiesProp) ||
entitiesProp.name !== "entities") {
return null;
}
return { collectionName: entityName.name };
}
/**
* Detect base44.entities.EntityName.getById() or .get() patterns.
*/
isBase44EntityGetCall(node) {
const callee = node.callee;
if (!this.types.isMemberExpression(callee))
return null;
const method = callee.property;
if (!this.types.isIdentifier(method) ||
(method.name !== "get" && method.name !== "getById")) {
return null;
}
const entityAccess = callee.object;
if (!this.types.isMemberExpression(entityAccess))
return null;
const entityName = entityAccess.property;
if (!this.types.isIdentifier(entityName))
return null;
const entitiesAccess = entityAccess.object;
if (!this.types.isMemberExpression(entitiesAccess))
return null;
const entitiesProp = entitiesAccess.property;
if (!this.types.isIdentifier(entitiesProp) ||
entitiesProp.name !== "entities") {
return null;
}
return { collectionName: entityName.name };
}
getCallbackArgument(callExpr) {
const firstArg = callExpr.arguments[0];
if (this.types.isArrowFunctionExpression(firstArg) ||
this.types.isFunctionExpression(firstArg)) {
return firstArg;
}
return null;
}
}
export class StaticValueUtils {
types;
constructor(types) {
this.types = types;
}
isPrimitiveLiteral(path) {
return (path.isStringLiteral() ||
path.isNumericLiteral() ||
path.isBooleanLiteral() ||
path.isNullLiteral());
}
isStaticValue(path, visited = new Set()) {
if (this.isPrimitiveLiteral(path))
return true;
if (path.isIdentifier())
return this.isStaticIdentifier(path, visited);
if (path.isObjectExpression())
return this.isStaticObject(path, visited);
if (path.isArrayExpression())
return this.isStaticArrayExpression(path, visited);
return false;
}
isStaticIdentifier(path, visited = new Set()) {
const binding = path.scope.getBinding(path.node.name);
if (!binding)
return false;
if (binding.kind === "module")
return true;
if (binding.kind === "const" && binding.path.isVariableDeclarator()) {
const name = path.node.name;
if (visited.has(name))
return false;
visited.add(name);
const init = binding.path.get("init");
if (init.hasNode()) {
return this.isStaticValue(init, visited);
}
}
return false;
}
isStaticObject(path, visited = new Set()) {
return path.get("properties").every((prop) => {
if (!prop.isObjectProperty())
return false;
return this.isStaticValue(prop.get("value"), visited);
});
}
isStaticArrayExpression(arrayExpression, visited = new Set()) {
return arrayExpression.get("elements").every((element) => {
if (!element.node || element.isSpreadElement())
return true;
return this.isStaticValue(element, visited);
});
}
isDerivedFromStaticData(identifierName, path) {
const binding = path.scope.getBinding(identifierName);
if (!binding)
return false;
if (binding.path.isVariableDeclarator()) {
const init = binding.path.get("init");
if (init.isArrayExpression() || init.isObjectExpression()) {
return this.isStaticValue(init);
}
}
const fnParent = path.getFunctionParent();
if (!fnParent)
return false;
const params = fnParent.get("params");
for (const param of (Array.isArray(params) ? params : [params])) {
if (param.isIdentifier() && param.node.name === identifierName) {
const mapCall = fnParent.parentPath;
if (mapCall?.isCallExpression()) {
const callee = mapCall.get("callee");
if (callee.isMemberExpression() &&
callee.get("property").isIdentifier()) {
const propName = callee.get("property").node.name;
if (propName === "map" || propName === "flatMap") {
const arrayObj = callee.get("object");
if (arrayObj.isIdentifier()) {
return this.isDerivedFromStaticData(arrayObj.node.name, arrayObj);
}
if (arrayObj.isArrayExpression()) {
return this.isStaticArrayExpression(arrayObj);
}
}
}
}
}
}
return false;
}
}
export class TypeCheckUtils {
types;
constructor(types) {
this.types = types;
}
isArrayIsArrayCheck(node) {
if (!this.types.isCallExpression(node))
return false;
const callee = node.callee;
return (this.types.isMemberExpression(callee) &&
this.types.isIdentifier(callee.object) &&
callee.object.name === "Array" &&
this.types.isIdentifier(callee.property) &&
callee.property.name === "isArray");
}
isTypeofObjectCheck(node) {
if (!this.types.isBinaryExpression(node))
return false;
if (node.operator !== "===" && node.operator !== "==")
return false;
const isTypeof = (side) => this.types.isUnaryExpression(side) && side.operator === "typeof";
const isObjectString = (side) => this.types.isStringLiteral(side) && side.value === "object";
return ((isTypeof(node.left) &&
isObjectString(node.right)) ||
(isObjectString(node.left) &&
isTypeof(node.right)));
}
isReferenceTypeCheck(node) {
return this.isArrayIsArrayCheck(node) || this.isTypeofObjectCheck(node);
}
isLengthCheck(node) {
if (this.types.isMemberExpression(node)) {
return (this.types.isIdentifier(node.property) &&
node.property.name === "length");
}
if (this.types.isOptionalMemberExpression(node)) {
return (this.types.isIdentifier(node.property) &&
node.property.name === "length");
}
return false;
}
}
//# sourceMappingURL=shared-utils.js.map
File diff suppressed because one or more lines are too long