A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.
import { customJsxCatalogComponentByName } from "../core/component-catalog";
import {
customJsxBindableComponentNames,
customJsxComponentByName,
customJsxSupportedPropsByName,
resolveCustomJsxComponentName,
} from "../core/component-registry";
import type { AstNode } from "./analyzer-ast";
import { containsEscapingCallback, nodeOf, nodesOf, staticPropertyName } from "./analyzer-ast";
import { closestCustomJsxComponentName, customJsxTagName, isSafeLiteralCustomJsxUrl } from "./analyzer-language";
import {
CUSTOM_JSX_BINDING_IDENTIFIER_PATTERN,
CUSTOM_JSX_LIMITS,
CUSTOM_JSX_URL_PROPS,
isBlockedCustomJsxProp,
} from "./policy";
import { getInvalidCustomJsxPropValueReason } from "./runtime-component-policy";
export type CustomJsxRequestParameterCheck = (
parameters: ReadonlyMap<string, ReadonlySet<string>>,
add: (node: AstNode, message: string) => void,
) => void;
interface AnalyzerJsxContext {
requestParameterChecks?: CustomJsxRequestParameterCheck[];
requestParameters?: ReadonlyMap<string, ReadonlySet<string>>;
add(node: AstNode, message: string, severity?: "error" | "warning"): void;
visit(node: AstNode, depth: number, bindings: ReadonlySet<string>): void;
visitArrow(node: AstNode, depth: number, bindings: ReadonlySet<string>): void;
}
const componentsRequiringRequestId = new Set(["SubFetch", "ActionButton", "ToggleSwitch"]);
const componentsWithLiteralRequestId = new Set([...componentsRequiringRequestId, "RefreshButton"]);
function staticParameterKeys(expression: AstNode | null, depth = 0): { keys: Set<string>; complete: boolean } {
const keys = new Set<string>();
if (!expression) return { keys, complete: true };
if (expression.type !== "ObjectExpression" || depth > CUSTOM_JSX_LIMITS.astDepth) return { keys, complete: false };
let complete = true;
for (const property of nodesOf(expression.properties)) {
if (property.type === "SpreadElement") {
const spread = staticParameterKeys(nodeOf(property.argument), depth + 1);
spread.keys.forEach((key) => keys.add(key));
complete &&= spread.complete;
continue;
}
const key = nodeOf(property.key);
let name = staticPropertyName(key);
if (!property.computed && key?.type === "Identifier") name = String(key.name);
if (name === undefined) complete = false;
else keys.add(name);
}
return { keys, complete };
}
const interactiveTriggerContentComponentNames = new Set([
"ActionButton",
"ActionIcon",
"Anchor",
"Autocomplete",
"Checkbox",
"Checkbox.Card",
"Chip",
"CloseButton",
"ColorInput",
"ColorPicker",
"DateInput",
"DatePicker",
"DatePickerInput",
"DateTimePicker",
"InlineDateTimePicker",
"Input",
"JsonInput",
"MaskInput",
"MonthPicker",
"MonthPickerInput",
"MultiSelect",
"NativeSelect",
"NumberInput",
"PasswordInput",
"PinInput",
"Radio",
"Radio.Card",
"RangeSlider",
"Rating",
"SegmentedControl",
"Select",
"Slider",
"Switch",
"TagsInput",
"TextInput",
"Textarea",
"TimeInput",
"TimePicker",
"TreeSelect",
"UnstyledButton",
"YearPickerInput",
"Button",
"RefreshButton",
"ToggleSwitch",
]);
const isManualSubFetchTrigger = (attributes: readonly AstNode[]) => {
const triggerAttribute = attributes.find((attribute) => {
if (attribute.type !== "JSXAttribute") return false;
const attributeName = nodeOf(attribute.name);
return attributeName?.type === "JSXIdentifier" && attributeName.name === "trigger";
});
const triggerValue = nodeOf(triggerAttribute?.value);
const triggerExpression =
triggerValue?.type === "JSXExpressionContainer" ? nodeOf(triggerValue.expression) : triggerValue;
if (triggerExpression?.type === "Literal") return triggerExpression.value === "manual";
return triggerExpression !== null;
};
const findInteractiveTriggerContentNode = (root: AstNode) => {
const pending = [root];
while (pending.length > 0) {
const current = pending.pop();
if (!current) continue;
if (current.type === "JSXElement") {
const opening = nodeOf(current.openingElement);
const nameNode = opening ? nodeOf(opening.name) : null;
const name = nameNode ? customJsxTagName(nameNode) : null;
const resolvedName = name ? resolveCustomJsxComponentName(name) : null;
if (resolvedName && interactiveTriggerContentComponentNames.has(resolvedName)) return opening ?? current;
}
for (const [key, value] of Object.entries(current)) {
if (["end", "loc", "start", "type"].includes(key)) continue;
if (Array.isArray(value)) {
pending.push(...nodesOf(value));
continue;
}
const child = nodeOf(value);
if (child) pending.push(child);
}
}
return null;
};
export function analyzeCustomJsxElement(
node: AstNode,
depth: number,
bindings: ReadonlySet<string>,
context: AnalyzerJsxContext,
): void {
const opening = nodeOf(node.openingElement);
if (!opening) {
context.add(node, "Invalid JSX element");
return;
}
const nameNode = nodeOf(opening.name);
const name = nameNode ? customJsxTagName(nameNode) : null;
const resolvedName = name ? resolveCustomJsxComponentName(name) : null;
const descriptor = resolvedName ? customJsxComponentByName.get(resolvedName) : undefined;
const attributes = nodesOf(opening.attributes);
if (resolvedName && componentsWithLiteralRequestId.has(resolvedName)) {
const requestIdAttribute = attributes.find((attribute) => {
if (attribute.type !== "JSXAttribute") return false;
const attributeName = nodeOf(attribute.name);
return attributeName?.type === "JSXIdentifier" && attributeName.name === "requestId";
});
const requestIdValue = requestIdAttribute ? nodeOf(requestIdAttribute.value) : null;
const requestIdExpression =
requestIdValue?.type === "JSXExpressionContainer" ? nodeOf(requestIdValue.expression) : null;
const requestIdLiteral = requestIdValue?.type === "Literal" ? requestIdValue : requestIdExpression;
const requiresRequestId = componentsRequiringRequestId.has(resolvedName);
const hasInvalidRequestId =
requestIdAttribute !== undefined &&
(requestIdLiteral?.type !== "Literal" ||
typeof requestIdLiteral.value !== "string" ||
requestIdLiteral.value.trim().length === 0);
if ((!requestIdAttribute && requiresRequestId) || hasInvalidRequestId) {
context.add(requestIdAttribute ?? opening, `${resolvedName} must use a literal requestId`);
}
if (
componentsRequiringRequestId.has(resolvedName) &&
typeof requestIdLiteral?.value === "string" &&
(context.requestParameters || context.requestParameterChecks)
) {
const requestId = requestIdLiteral.value;
let parameterProps = ["params"];
if (resolvedName === "ToggleSwitch") parameterProps = ["enabledParams", "disabledParams"];
for (const parameterProp of parameterProps) {
const paramsAttribute = attributes.find((attribute) => nodeOf(attribute.name)?.name === parameterProp);
const params = nodeOf(nodeOf(paramsAttribute?.value)?.expression);
const check: CustomJsxRequestParameterCheck = (parameters, add) => {
const expected = parameters.get(requestId);
// Unknown keys stay runtime-checked, but must not conceal known extra keys.
if (!expected || (paramsAttribute && params?.type !== "ObjectExpression")) return;
const actual = staticParameterKeys(params);
const missing = [...expected].filter((parameter) => actual.complete && !actual.keys.has(parameter));
const extra = [...actual.keys].filter((parameter) => !expected.has(parameter));
if (missing.length > 0 || extra.length > 0) {
add(
paramsAttribute ?? opening,
`REQUEST_PARAMS_MISMATCH: ${resolvedName}.${parameterProp} '${requestId}' expects only [${[...expected].join(", ")}]; missing [${missing.join(", ")}], extra [${extra.join(", ")}]. Match params to the request's $param references; fixed query values belong in the request, not params.`,
);
}
};
if (context.requestParameters) check(context.requestParameters, context.add);
context.requestParameterChecks?.push(check);
}
}
}
if (resolvedName === "TablerIcon") {
const attributeNames = new Set<string>();
for (const attribute of attributes) {
if (attribute.type !== "JSXAttribute") continue;
const attributeName = nodeOf(attribute.name);
if (attributeName?.type === "JSXIdentifier") attributeNames.add(String(attributeName.name));
}
for (const prop of customJsxCatalogComponentByName.get(resolvedName)?.props ?? []) {
if (prop.required && !attributeNames.has(prop.name)) {
context.add(opening, `MISSING_REQUIRED_PROP: '${prop.name}' on ${name} is required`);
}
}
}
if (descriptor?.safety === "denied") {
context.add(
opening,
`BLOCKED_CAPABILITY: '${name}' is not available${descriptor.reason ? ` because it ${descriptor.reason.toLowerCase()}` : ""}`,
);
} else if (!descriptor) {
const suggestion = name ? closestCustomJsxComponentName(name) : undefined;
context.add(
opening,
name
? `UNKNOWN_COMPONENT: '${name}' is not available${suggestion ? `. Did you mean '${suggestion}'?` : ""}`
: "UNKNOWN_COMPONENT: Invalid JSX component name",
);
}
for (const attribute of attributes) {
if (attribute.type === "JSXSpreadAttribute") {
const argument = nodeOf(attribute.argument);
if (argument) context.visit(argument, depth + 1, bindings);
continue;
}
if (attribute.type !== "JSXAttribute") {
context.add(attribute, `Unsupported JSX attribute '${attribute.type}'`);
continue;
}
const attributeNameNode = nodeOf(attribute.name);
const attributeName = attributeNameNode?.type === "JSXIdentifier" ? String(attributeNameNode.name) : "";
if (resolvedName === "SubFetch" && attributeName === "triggerContent" && isManualSubFetchTrigger(attributes)) {
const triggerContent = nodeOf(attribute.value);
const triggerContentExpression =
triggerContent?.type === "JSXExpressionContainer" ? nodeOf(triggerContent.expression) : null;
const interactiveNode = triggerContentExpression
? findInteractiveTriggerContentNode(triggerContentExpression)
: null;
if (interactiveNode) {
context.add(
interactiveNode,
"SubFetch triggerContent cannot contain interactive descendants because the manual trigger wraps it in a button",
);
}
}
const componentBlockedProp = descriptor?.blockedProps.find(({ name: propName }) => propName === attributeName);
if (componentBlockedProp) {
context.add(
attribute,
`BLOCKED_CAPABILITY: Prop '${attributeName}' on '${name}' is not allowed because ${componentBlockedProp.reason.toLowerCase()}`,
);
} else if (isBlockedCustomJsxProp(attributeName)) {
context.add(attribute, `BLOCKED_CAPABILITY: Prop '${attributeName}' is not allowed`);
} else if (
resolvedName &&
attributeName !== "bind" &&
!customJsxSupportedPropsByName.get(resolvedName)?.has(attributeName)
) {
if (resolvedName === "TablerIcon") {
context.add(attribute, `UNKNOWN_COMPONENT_PROP: '${attributeName}' on ${name} is not supported`);
} else {
context.add(attribute, `UNKNOWN_MANTINE_PROP: '${attributeName}' on ${name} will be passed through`, "warning");
}
} else if (resolvedName && attributeName === "bind" && !customJsxBindableComponentNames.has(resolvedName)) {
context.add(attribute, `BINDING_UNAVAILABLE: '${name}' does not have a declarative binding adapter`, "warning");
}
if (attributeName === "bind") analyzeBindAttribute(attribute, context);
analyzeAttributeValue(resolvedName, attributeName, attribute, depth, bindings, context);
}
nodesOf(node.children).forEach((child) => {
const expression = child.type === "JSXExpressionContainer" ? nodeOf(child.expression) : null;
if (resolvedName === "SubFetch" && expression?.type === "ArrowFunctionExpression") {
context.visitArrow(expression, depth + 1, bindings);
return;
}
context.visit(child, depth + 1, bindings);
});
}
function analyzeBindAttribute(attribute: AstNode, context: AnalyzerJsxContext): void {
const value = nodeOf(attribute.value);
if (value?.type !== "Literal" || typeof value.value !== "string") {
context.add(attribute, "bind must use a literal input name");
} else if (!CUSTOM_JSX_BINDING_IDENTIFIER_PATTERN.test(value.value)) {
context.add(attribute, `Invalid bind input name '${value.value}'`);
}
}
function analyzeAttributeValue(
componentName: string | null,
attributeName: string,
attribute: AstNode,
depth: number,
bindings: ReadonlySet<string>,
context: AnalyzerJsxContext,
): void {
const value = nodeOf(attribute.value);
const literalValue =
value?.type === "Literal"
? value
: value?.type === "JSXExpressionContainer" && nodeOf(value.expression)?.type === "Literal"
? nodeOf(value.expression)
: null;
if (componentName && literalValue?.type === "Literal") {
const reason = getInvalidCustomJsxPropValueReason(componentName, attributeName, literalValue.value);
if (reason) context.add(attribute, `INVALID_PROP_VALUE: ${reason}`);
}
const expression = value?.type === "JSXExpressionContainer" ? nodeOf(value.expression) : null;
if (attributeName === "resetKey" && ["ArrayExpression", "ObjectExpression"].includes(expression?.type ?? "")) {
context.add(attribute, "INVALID_PROP_VALUE: resetKey must resolve to a finite number, string, boolean, or null");
}
if (
CUSTOM_JSX_URL_PROPS.has(attributeName) &&
value?.type === "Literal" &&
typeof value.value === "string" &&
!isSafeLiteralCustomJsxUrl(value.value)
) {
context.add(attribute, `INVALID_PROP_VALUE: '${attributeName}' contains an unsafe URL`);
}
if (value?.type !== "JSXExpressionContainer") return;
if (expression && containsEscapingCallback(expression)) {
context.add(attribute, `BLOCKED_CAPABILITY: Callback prop '${attributeName}' is not allowed`);
} else if (expression && expression.type !== "JSXEmptyExpression") {
context.visit(expression, depth + 1, bindings);
}
}