Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(table): Phase 2c — Plugin validation and error boundaries (#1062)

Cindy Zhang9c48d4cdc60c
diff --git a/packages/core/src/Table/XDSBaseTable.tsx b/packages/core/src/Table/XDSBaseTable.tsx
index 9c6e21d20c..a397c8f18a 100644
--- a/packages/core/src/Table/XDSBaseTable.tsx
+++ b/packages/core/src/Table/XDSBaseTable.tsx
@@ -46,6 +46,9 @@ const styles = stylex.create({
/**
* Run a value through a pipeline of plugin transform functions.
+ * Wraps each transform in a try-catch so a single broken plugin
+ * doesn't crash the entire table. In development, logs a warning
+ * with the plugin index and error details.
*/
function applyPlugins<TPlugin, TProps, TArgs extends unknown[]>(
plugins: TPlugin[],
@@ -55,9 +58,18 @@ function applyPlugins<TPlugin, TProps, TArgs extends unknown[]>(
initial: TProps,
...args: TArgs
): TProps {
- return plugins.reduce<TProps>((acc, plugin) => {
+ return plugins.reduce<TProps>((acc, plugin, index) => {
const transform = getter(plugin);
- return transform ? transform(acc, ...args) : acc;
+ if (!transform) return acc;
+ try {
+ return transform(acc, ...args);
+ } catch (error) {
+ console.error(
+ `[XDSTable] Plugin at index ${index} threw in transform:`,
+ error,
+ );
+ return acc;
+ }
}, initial);
}
@@ -375,7 +387,14 @@ function XDSBaseTableInner<T extends Record<string, unknown>>({
for (let i = plugins.length - 1; i >= 0; i--) {
const plugin = plugins[i];
if (plugin.transformTableContext) {
- tableElement = plugin.transformTableContext(tableElement);
+ try {
+ tableElement = plugin.transformTableContext(tableElement);
+ } catch (error) {
+ console.error(
+ '[XDSTable] Plugin threw in transformTableContext:',
+ error,
+ );
+ }
}
}
diff --git a/packages/core/src/Table/useXDSBaseTablePlugins.ts b/packages/core/src/Table/useXDSBaseTablePlugins.ts
index 4848e1200a..668ce179a2 100644
--- a/packages/core/src/Table/useXDSBaseTablePlugins.ts
+++ b/packages/core/src/Table/useXDSBaseTablePlugins.ts
@@ -34,10 +34,8 @@
import {useRef} from 'react';
import type {TablePlugin} from './types';
-// =============================================================================
-// Canonical Plugin Order
-// =============================================================================
-
+// ======================================================================// Canonical Plugin Order
+// ======================================================================
/**
* Canonical ordering for first-party plugin names.
* Plugins are sorted by their position in this array.
@@ -75,9 +73,72 @@ function sortPluginEntries<T extends Record<string, unknown>>(
});
}
-// =============================================================================
-// Hook
-// =============================================================================
+// ======================================================================// Hook
+// ======================================================================
+// Plugin Validation (dev mode only)
+// ======================================================================
+/** Known transform method names on the TablePlugin interface. */
+const VALID_TRANSFORM_KEYS = new Set([
+ 'transformColumns',
+ 'transformTable',
+ 'transformHeaderRow',
+ 'transformHeaderCell',
+ 'transformBodyRow',
+ 'transformBodyCell',
+ 'transformTableContext',
+]);
+
+/**
+ * Validate a plugin object in development mode.
+ * Warns about common mistakes like misspelled transform names,
+ * non-function values where functions are expected, or
+ * completely empty plugins that add pipeline overhead for nothing.
+ */
+function validatePlugin<T extends Record<string, unknown>>(
+ name: string,
+ plugin: TablePlugin<T>,
+): void {
+ // Validation always runs — warnings are cheap and help catch plugin bugs
+
+ const keys = Object.keys(plugin);
+
+ // Warn about unknown keys (likely misspelled transform names)
+ for (const key of keys) {
+ if (!VALID_TRANSFORM_KEYS.has(key)) {
+ console.warn(
+ `[XDSTable] Plugin "${name}" has unknown key "${key}". ` +
+ `Valid keys: ${[...VALID_TRANSFORM_KEYS].join(', ')}. ` +
+ `This key will be ignored.`,
+ );
+ }
+ }
+
+ // Warn about non-function values on known transform keys
+ for (const key of keys) {
+ if (VALID_TRANSFORM_KEYS.has(key)) {
+ const value = (plugin as Record<string, unknown>)[key];
+ if (value != null && typeof value !== 'function') {
+ console.warn(
+ `[XDSTable] Plugin "${name}" has non-function value for "${key}" ` +
+ `(got ${typeof value}). Transform will be skipped.`,
+ );
+ }
+ }
+ }
+
+ // Warn about empty plugins that add pipeline overhead
+ const hasTransforms = keys.some(
+ key =>
+ VALID_TRANSFORM_KEYS.has(key) &&
+ typeof (plugin as Record<string, unknown>)[key] === 'function',
+ );
+ if (!hasTransforms) {
+ console.warn(
+ `[XDSTable] Plugin "${name}" has no transform methods. ` +
+ `It will be included in the pipeline but won't do anything.`,
+ );
+ }
+}
/**
* Converts a named plugin record (`Record<string, TablePlugin>`) to a stable
@@ -135,6 +196,13 @@ export function useXDSBaseTablePlugins<T extends Record<string, unknown>>(
const result = [...basePlugins, ...sortedUserPlugins];
+ // Validate plugins in dev mode
+ if (userPlugins) {
+ for (const [name, plugin] of Object.entries(userPlugins)) {
+ validatePlugin(name, plugin);
+ }
+ }
+
prevRef.current = {basePlugins, userPlugins, result};
return result;
}