diff --git a/static/components.interfaces.ts b/static/components.interfaces.ts index 9722dd977..9cdb45622 100644 --- a/static/components.interfaces.ts +++ b/static/components.interfaces.ts @@ -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 { +export interface ComponentConfig { type: 'component'; componentName: K; componentState: ComponentStateMap[K]; @@ -390,6 +390,11 @@ export interface ComponentConfig; + /** * 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 * 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'; } diff --git a/static/components.ts b/static/components.ts index d7b859912..43deb51db 100644 --- a/static/components.ts +++ b/static/components.ts @@ -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 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( componentName: K, componentState: ComponentStateMap[K], - options?: { - title?: string; - isClosable?: boolean; - reorderEnabled?: boolean; - width?: number; - height?: number; - }, + options?: Pick, ): ComponentConfig { return { type: 'component', @@ -982,13 +963,7 @@ export function createComponentConfig( export function createLayoutItem( type: 'row' | 'column' | 'stack', content: ItemConfig[], - options?: { - isClosable?: boolean; - reorderEnabled?: boolean; - width?: number; - height?: number; - activeItemIndex?: number; - }, + options?: Pick, ): 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( layout: GoldenLayout, element: HTMLElement | JQuery, factory: DragSourceFactory, -): 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; } } diff --git a/static/main.ts b/static/main.ts index b0e28e191..6382082e4 100644 --- a/static/main.ts +++ b/static/main.ts @@ -684,7 +684,7 @@ function start() { } function setupAdd(thing: JQuery, func: () => ComponentConfig) { - 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', diff --git a/static/panes/compiler.ts b/static/panes/compiler.ts index 49e4a2a68..8730e0077 100644 --- a/static/panes/compiler.ts +++ b/static/panes/compiler.ts @@ -688,7 +688,7 @@ export class Compiler extends MonacoPane 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 createOptView())._dragListener.on( + createDragSource(this.container.layoutManager, this.optButton, () => createOptView()).on( 'dragStart', hidePaneAdder, ); @@ -709,9 +709,10 @@ export class Compiler extends MonacoPane - 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 createFlagsView())._dragListener.on( + createDragSource(this.container.layoutManager, this.flagsButton, () => createFlagsView()).on( 'dragStart', () => BootstrapUtils.hideDropdown(popularArgumentsMenu), ); @@ -737,7 +738,7 @@ export class Compiler extends MonacoPane createPpView())._dragListener.on( + createDragSource(this.container.layoutManager, this.ppButton, () => createPpView()).on( 'dragStart', hidePaneAdder, ); @@ -749,7 +750,7 @@ export class Compiler extends MonacoPane createAstView())._dragListener.on( + createDragSource(this.container.layoutManager, this.astButton, () => createAstView()).on( 'dragStart', hidePaneAdder, ); @@ -761,7 +762,7 @@ export class Compiler extends MonacoPane createIrView())._dragListener.on( + createDragSource(this.container.layoutManager, this.irButton, () => createIrView()).on( 'dragStart', hidePaneAdder, ); @@ -773,7 +774,7 @@ export class Compiler extends MonacoPane createClangirView())._dragListener.on( + createDragSource(this.container.layoutManager, this.clangirButton, () => createClangirView()).on( 'dragStart', hidePaneAdder, ); @@ -785,9 +786,10 @@ export class Compiler extends MonacoPane - 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 createDeviceView())._dragListener.on( + createDragSource(this.container.layoutManager, this.deviceButton, () => createDeviceView()).on( 'dragStart', hidePaneAdder, ); @@ -808,7 +810,7 @@ export class Compiler extends MonacoPane createRustMirView())._dragListener.on( + createDragSource(this.container.layoutManager, this.rustMirButton, () => createRustMirView()).on( 'dragStart', hidePaneAdder, ); @@ -820,9 +822,10 @@ export class Compiler extends MonacoPane - 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 - 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 - 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 - 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 createRustHirView())._dragListener.on( + createDragSource(this.container.layoutManager, this.rustHirButton, () => createRustHirView()).on( 'dragStart', hidePaneAdder, ); @@ -876,7 +882,7 @@ export class Compiler extends MonacoPane createGccDumpView())._dragListener.on( + createDragSource(this.container.layoutManager, this.gccDumpButton, () => createGccDumpView()).on( 'dragStart', hidePaneAdder, ); @@ -888,9 +894,10 @@ export class Compiler extends MonacoPane - 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 - 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 createCfgView())._dragListener.on( + createDragSource(this.container.layoutManager, this.cfgButton, () => createCfgView()).on( 'dragStart', hidePaneAdder, ); @@ -922,7 +930,7 @@ export class Compiler extends MonacoPane createExecutor())._dragListener.on( + createDragSource(this.container.layoutManager, this.executorButton, () => createExecutor()).on( 'dragStart', hidePaneAdder, ); @@ -2697,11 +2705,10 @@ export class Compiler extends MonacoPane createToolView()).on( + 'dragStart', + hideToolDropdown, + ); button.on('click', () => { button.prop('disabled', true); diff --git a/static/panes/editor.ts b/static/panes/editor.ts index b6efdb101..c69a37784 100644 --- a/static/panes/editor.ts +++ b/static/panes/editor.ts @@ -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, 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 = diff --git a/static/panes/tree.ts b/static/panes/tree.ts index b35bfbe71..61aa194c8 100644 --- a/static/panes/tree.ts +++ b/static/panes/tree.ts @@ -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');