Address @junlarsen's PR feedback and improve createDragSource API

This commit addresses all feedback from @junlarsen in PR #7801:

1. **Remove default generic parameter from ComponentConfig**: Added explicit
   AnyComponentConfig type alias instead of default parameter, improving type
   clarity and preventing accidental usage of overly broad types.

2. **Keep typeof pattern for component configs**: After consideration, we kept
   the `ComponentConfig<typeof COMPONENT_NAME>` pattern rather than creating
   individual type aliases (CompilerComponentConfig, etc.) because:
   - Maintains single source of truth (component name constant drives everything)
   - Zero maintenance burden (no additional aliases to maintain)
   - Better refactoring safety (changing component name only requires updating constant)
   - Type safety is preserved (misspelled constants caught at compile time)

3. **Use Pick types for options parameters**: Replaced inline object types with
   `Pick<AnyComponentConfig, ...>` and `Pick<LayoutItem, ...>` to ensure options
   match the actual interface properties exactly.

4. **Remove section comment**: Removed the large comment block as suggested,
   keeping the code cleaner and more focused.

5. **Document GoldenLayout type issues**: The existing TODO and comments properly
   explain the TypeScript definition issues with GoldenLayout.

6. **Add targeted Sentry reporting**: Added SentryCapture only for the specific
   case that should never happen with proper typing (unknown component names).

**Additional improvement**: Simplified createDragSource API by returning the
EventEmitter directly instead of requiring callers to access ._dragListener.
Updated 25+ call sites from:
  `createDragSource(...).&#95;dragListener.on(...)`
to:
  `createDragSource(...).on(...)`

This hides implementation details and provides a cleaner, more intuitive API.

Thanks to @junlarsen for the thorough code review and excellent suggestions\!

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Matt Godbolt
2025-06-17 17:19:54 -05:00
co-authored by Claude
parent 58942c07d9
commit fcfd5cb863
6 changed files with 78 additions and 91 deletions
+8 -3
View File
@@ -379,7 +379,7 @@ export interface ComponentStateMap {
* - componentName must be a valid component name from ComponentStateMap
* - componentState must match the expected type for that component
*/
export interface ComponentConfig<K extends keyof ComponentStateMap = keyof ComponentStateMap> {
export interface ComponentConfig<K extends keyof ComponentStateMap> {
type: 'component';
componentName: K;
componentState: ComponentStateMap[K];
@@ -390,6 +390,11 @@ export interface ComponentConfig<K extends keyof ComponentStateMap = keyof Compo
height?: number;
}
/**
* Type alias for any component configuration
*/
export type AnyComponentConfig = ComponentConfig<keyof ComponentStateMap>;
/**
* Layout item types (row, column, stack) with typed content
*/
@@ -406,7 +411,7 @@ export interface LayoutItem {
/**
* Union type for all valid item configurations
*/
export type ItemConfig = ComponentConfig | LayoutItem;
export type ItemConfig = AnyComponentConfig | LayoutItem;
/**
* Type-safe GoldenLayout configuration. We extend GoldenLayout.Config but replace the 'content' field because the
@@ -421,7 +426,7 @@ export interface GoldenLayoutConfig extends Omit<GoldenLayout.Config, 'content'>
* Type guard to check if an item is a component configuration
* TODO(#7808): Use this for configuration validation in fromGoldenLayoutConfig
*/
export function isComponentConfig(item: ItemConfig): item is ComponentConfig {
export function isComponentConfig(item: ItemConfig): item is AnyComponentConfig {
return item.type === 'component';
}
+12 -35
View File
@@ -26,12 +26,14 @@ import GoldenLayout from 'golden-layout';
import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js';
import {GccDumpViewState} from './panes/gccdump-view.interfaces.js';
import {SentryCapture} from './sentry.js';
import {ConfiguredOverrides} from '../types/compilation/compiler-overrides.interfaces.js';
import {ConfiguredRuntimeTools} from '../types/execution/execution.interfaces.js';
import {LanguageKey} from '../types/languages.interfaces.js';
import {
AST_VIEW_COMPONENT_NAME,
AnyComponentConfig,
CFG_VIEW_COMPONENT_NAME,
CLANGIR_VIEW_COMPONENT_NAME,
COMPILER_COMPONENT_NAME,
@@ -939,34 +941,13 @@ export function getDeviceViewWith(
};
}
// =============================================================================
// GoldenLayout Type Safety Utility Functions
// =============================================================================
//
// IMPLEMENTATION STATUS:
// ✅ Phase 1 (COMPLETED): Type-safe component creation and configuration
// - All component factory functions return strongly-typed configs
// - ComponentConfig<K> enforces valid component names and state types
// - No more 'as any' casts when creating drag sources or components
//
// TODO(#7807): Implement type-safe serialization/deserialization
// TODO(#7808): Enable configuration validation and fix remaining type gaps
//
// =============================================================================
/**
* Helper function to create a typed component configuration
*/
export function createComponentConfig<K extends keyof ComponentStateMap>(
componentName: K,
componentState: ComponentStateMap[K],
options?: {
title?: string;
isClosable?: boolean;
reorderEnabled?: boolean;
width?: number;
height?: number;
},
options?: Pick<AnyComponentConfig, 'title' | 'isClosable' | 'reorderEnabled' | 'width' | 'height'>,
): ComponentConfig<K> {
return {
type: 'component',
@@ -982,13 +963,7 @@ export function createComponentConfig<K extends keyof ComponentStateMap>(
export function createLayoutItem(
type: 'row' | 'column' | 'stack',
content: ItemConfig[],
options?: {
isClosable?: boolean;
reorderEnabled?: boolean;
width?: number;
height?: number;
activeItemIndex?: number;
},
options?: Pick<LayoutItem, 'isClosable' | 'reorderEnabled' | 'width' | 'height' | 'activeItemIndex'>,
): LayoutItem {
return {
type,
@@ -1060,7 +1035,7 @@ function validateItemConfig(item: any, index?: number): ItemConfig {
/**
* Validates a component configuration
*/
function validateComponentConfig(config: any, location: string): ComponentConfig {
function validateComponentConfig(config: any, location: string): AnyComponentConfig {
if (!config.componentName) {
throw new Error(`Invalid ${location}: missing 'componentName' property`);
}
@@ -1117,8 +1092,8 @@ export function toGoldenLayoutConfig(config: GoldenLayoutConfig): GoldenLayout.C
}
/**
* Typed wrapper for createDragSource that avoids the need for 'as any'.
* Returns the result with _dragListener property for event handling.
* Typed wrapper for createDragSource that returns the drag event emitter directly.
* This simplifies the API by hiding the internal _dragListener implementation detail.
*
* Note: We still need to cast internally because GoldenLayout's TypeScript
* definitions don't properly type the second parameter as accepting a function.
@@ -1127,10 +1102,11 @@ export function createDragSource<K extends keyof ComponentStateMap>(
layout: GoldenLayout,
element: HTMLElement | JQuery,
factory: DragSourceFactory<K>,
): any {
): GoldenLayout.EventEmitter {
// TODO(#7808): Fix GoldenLayout TypeScript definitions to eliminate 'as any' cast
// createDragSource returns object with _dragListener but types say void.
return layout.createDragSource(element, factory as any);
// Both the factory parameter type and return type are incorrectly defined
const result = layout.createDragSource(element, factory as any) as any;
return result._dragListener;
}
/**
@@ -1207,6 +1183,7 @@ function validateComponentState(componentName: string, state: any): boolean {
default:
// Unknown component name - this should not happen with proper typing
SentryCapture(componentName, `Unknown component name in validateComponentState: ${componentName}`);
return false;
}
}
+1 -1
View File
@@ -684,7 +684,7 @@ function start() {
}
function setupAdd<K extends keyof ComponentStateMap>(thing: JQuery, func: () => ComponentConfig<K>) {
createDragSource(layout, thing, func)._dragListener.on('dragStart', () => {
createDragSource(layout, thing, func).on('dragStart', () => {
const addDropdown = unwrap(
BootstrapUtils.getDropdownInstance('#addDropdown'),
'Dropdown instance not found for #addDropdown',
+49 -42
View File
@@ -688,7 +688,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
// the second parameter can be a function that returns the config!
createDragSource(this.container.layoutManager, this.domRoot.find('.btn.add-compiler'), () =>
cloneComponent(),
)._dragListener.on('dragStart', hidePaneAdder);
).on('dragStart', hidePaneAdder);
this.domRoot.find('.btn.add-compiler').on('click', () => {
const insertPoint =
@@ -697,7 +697,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(cloneComponent());
});
createDragSource(this.container.layoutManager, this.optButton, () => createOptView())._dragListener.on(
createDragSource(this.container.layoutManager, this.optButton, () => createOptView()).on(
'dragStart',
hidePaneAdder,
);
@@ -709,9 +709,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createOptView());
});
createDragSource(this.container.layoutManager, this.stackUsageButton, () =>
createStackUsageView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.stackUsageButton, () => createStackUsageView()).on(
'dragStart',
hidePaneAdder,
);
this.stackUsageButton.on('click', () => {
const insertPoint =
@@ -722,7 +723,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
if (this.flagsButton) {
const popularArgumentsMenu = this.domRoot.find('div.populararguments div.dropdown-menu');
createDragSource(this.container.layoutManager, this.flagsButton, () => createFlagsView())._dragListener.on(
createDragSource(this.container.layoutManager, this.flagsButton, () => createFlagsView()).on(
'dragStart',
() => BootstrapUtils.hideDropdown(popularArgumentsMenu),
);
@@ -737,7 +738,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
popularArgumentsMenu.append(this.flagsButton);
}
createDragSource(this.container.layoutManager, this.ppButton, () => createPpView())._dragListener.on(
createDragSource(this.container.layoutManager, this.ppButton, () => createPpView()).on(
'dragStart',
hidePaneAdder,
);
@@ -749,7 +750,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createPpView());
});
createDragSource(this.container.layoutManager, this.astButton, () => createAstView())._dragListener.on(
createDragSource(this.container.layoutManager, this.astButton, () => createAstView()).on(
'dragStart',
hidePaneAdder,
);
@@ -761,7 +762,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createAstView());
});
createDragSource(this.container.layoutManager, this.irButton, () => createIrView())._dragListener.on(
createDragSource(this.container.layoutManager, this.irButton, () => createIrView()).on(
'dragStart',
hidePaneAdder,
);
@@ -773,7 +774,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createIrView());
});
createDragSource(this.container.layoutManager, this.clangirButton, () => createClangirView())._dragListener.on(
createDragSource(this.container.layoutManager, this.clangirButton, () => createClangirView()).on(
'dragStart',
hidePaneAdder,
);
@@ -785,9 +786,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createClangirView());
});
createDragSource(this.container.layoutManager, this.optPipelineButton, () =>
createOptPipelineView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.optPipelineButton, () => createOptPipelineView()).on(
'dragStart',
hidePaneAdder,
);
this.optPipelineButton.on('click', () => {
const insertPoint =
@@ -796,7 +798,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createOptPipelineView());
});
createDragSource(this.container.layoutManager, this.deviceButton, () => createDeviceView())._dragListener.on(
createDragSource(this.container.layoutManager, this.deviceButton, () => createDeviceView()).on(
'dragStart',
hidePaneAdder,
);
@@ -808,7 +810,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createDeviceView());
});
createDragSource(this.container.layoutManager, this.rustMirButton, () => createRustMirView())._dragListener.on(
createDragSource(this.container.layoutManager, this.rustMirButton, () => createRustMirView()).on(
'dragStart',
hidePaneAdder,
);
@@ -820,9 +822,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createRustMirView());
});
createDragSource(this.container.layoutManager, this.haskellCoreButton, () =>
createHaskellCoreView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.haskellCoreButton, () => createHaskellCoreView()).on(
'dragStart',
hidePaneAdder,
);
this.haskellCoreButton.on('click', () => {
const insertPoint =
@@ -831,9 +834,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createHaskellCoreView());
});
createDragSource(this.container.layoutManager, this.haskellStgButton, () =>
createHaskellStgView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.haskellStgButton, () => createHaskellStgView()).on(
'dragStart',
hidePaneAdder,
);
this.haskellStgButton.on('click', () => {
const insertPoint =
@@ -842,9 +846,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createHaskellStgView());
});
createDragSource(this.container.layoutManager, this.haskellCmmButton, () =>
createHaskellCmmView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.haskellCmmButton, () => createHaskellCmmView()).on(
'dragStart',
hidePaneAdder,
);
this.haskellCmmButton.on('click', () => {
const insertPoint =
@@ -853,9 +858,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createHaskellCmmView());
});
createDragSource(this.container.layoutManager, this.rustMacroExpButton, () =>
createRustMacroExpView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.rustMacroExpButton, () => createRustMacroExpView()).on(
'dragStart',
hidePaneAdder,
);
this.rustMacroExpButton.on('click', () => {
const insertPoint =
@@ -864,7 +870,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createRustMacroExpView());
});
createDragSource(this.container.layoutManager, this.rustHirButton, () => createRustHirView())._dragListener.on(
createDragSource(this.container.layoutManager, this.rustHirButton, () => createRustHirView()).on(
'dragStart',
hidePaneAdder,
);
@@ -876,7 +882,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createRustHirView());
});
createDragSource(this.container.layoutManager, this.gccDumpButton, () => createGccDumpView())._dragListener.on(
createDragSource(this.container.layoutManager, this.gccDumpButton, () => createGccDumpView()).on(
'dragStart',
hidePaneAdder,
);
@@ -888,9 +894,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createGccDumpView());
});
createDragSource(this.container.layoutManager, this.gnatDebugTreeButton, () =>
createGnatDebugTreeView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.gnatDebugTreeButton, () => createGnatDebugTreeView()).on(
'dragStart',
hidePaneAdder,
);
this.gnatDebugTreeButton.on('click', () => {
const insertPoint =
@@ -899,9 +906,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createGnatDebugTreeView());
});
createDragSource(this.container.layoutManager, this.gnatDebugButton, () =>
createGnatDebugView(),
)._dragListener.on('dragStart', hidePaneAdder);
createDragSource(this.container.layoutManager, this.gnatDebugButton, () => createGnatDebugView()).on(
'dragStart',
hidePaneAdder,
);
this.gnatDebugButton.on('click', () => {
const insertPoint =
@@ -910,7 +918,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createGnatDebugView());
});
createDragSource(this.container.layoutManager, this.cfgButton, () => createCfgView())._dragListener.on(
createDragSource(this.container.layoutManager, this.cfgButton, () => createCfgView()).on(
'dragStart',
hidePaneAdder,
);
@@ -922,7 +930,7 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
insertPoint.addChild(createCfgView());
});
createDragSource(this.container.layoutManager, this.executorButton, () => createExecutor())._dragListener.on(
createDragSource(this.container.layoutManager, this.executorButton, () => createExecutor()).on(
'dragStart',
hidePaneAdder,
);
@@ -2697,11 +2705,10 @@ export class Compiler extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Co
);
};
this.container.layoutManager
.createDragSource(button, createToolView())
// @ts-ignore
._dragListener.on('dragStart', hideToolDropdown);
createDragSource(this.container.layoutManager, button, () => createToolView()).on(
'dragStart',
hideToolDropdown,
);
button.on('click', () => {
button.prop('disabled', true);
+7 -9
View File
@@ -32,6 +32,7 @@ import _ from 'underscore';
import * as BootstrapUtils from '../bootstrap-utils.js';
import * as colour from '../colour.js';
import * as Components from '../components.js';
import {createDragSource} from '../components.js';
import * as monacoConfig from '../monaco-config.js';
import {options} from '../options.js';
import * as quickFixesHandler from '../quick-fixes-handler.js';
@@ -595,15 +596,12 @@ export class Editor extends MonacoPane<monaco.editor.IStandaloneCodeEditor, Edit
};
const addPaneOpener = (dragSource: JQuery<HTMLElement>, dragConfig) => {
this.container.layoutManager
.createDragSource(dragSource, dragConfig)
// @ts-expect-error: createDragSource returns not void
._dragListener.on('dragStart', () => {
const dropdown = BootstrapUtils.getDropdownInstance(paneAdderDropdown);
if (dropdown) {
dropdown.toggle();
}
});
createDragSource(this.container.layoutManager, dragSource, () => dragConfig()).on('dragStart', () => {
const dropdown = BootstrapUtils.getDropdownInstance(paneAdderDropdown);
if (dropdown) {
dropdown.toggle();
}
});
dragSource.on('click', () => {
const insertPoint =
+1 -1
View File
@@ -447,7 +447,7 @@ export class Tree {
}
private bindClickToOpenPane(dragSource, dragConfig) {
createDragSource(this.container.layoutManager, dragSource, () => dragConfig.bind(this)())._dragListener.on(
createDragSource(this.container.layoutManager, dragSource, () => dragConfig.bind(this)()).on(
'dragStart',
() => {
const dropdown = this.domRoot.find('.add-pane');