Skip to content

Commit 1ef28f7

Browse files
committed
Use ReadonlyArray for IContextMenuDelegate and IActionProvider and DropdownMenu
These classes do not (and should not) modify the input actions array. Use `ReadonlyArray` to enforce this
1 parent 06dedef commit 1ef28f7

5 files changed

Lines changed: 10 additions & 11 deletions

File tree

src/vs/base/browser/contextmenu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class ContextSubMenu extends SubmenuAction {
2424

2525
export interface IContextMenuDelegate {
2626
getAnchor(): HTMLElement | { x: number; y: number; width?: number; height?: number; };
27-
getActions(): Array<IAction | ContextSubMenu>;
27+
getActions(): ReadonlyArray<IAction | ContextSubMenu>;
2828
getActionViewItem?(action: IAction): IActionViewItem | undefined;
2929
getActionsContext?(event?: IContextMenuEvent): any;
3030
getKeyBinding?(action: IAction): ResolvedKeybinding | undefined;

src/vs/base/browser/ui/actionbar/actionbar.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
1616
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
1717
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
1818
import { Event, Emitter } from 'vs/base/common/event';
19-
import { asArray } from 'vs/base/common/arrays';
2019

2120
export interface IActionViewItem {
2221
actionRunner: IActionRunner;
@@ -596,8 +595,8 @@ export class ActionBar extends Disposable implements IActionRunner {
596595
return this.domNode;
597596
}
598597

599-
push(arg: IAction | IAction[], options: IActionOptions = {}): void {
600-
const actions: IAction[] = asArray(arg);
598+
push(arg: IAction | ReadonlyArray<IAction>, options: IActionOptions = {}): void {
599+
const actions: ReadonlyArray<IAction> = Array.isArray(arg) ? arg : [arg];
601600

602601
let index = types.isNumber(options.index) ? options.index : null;
603602

src/vs/base/browser/ui/dropdown/dropdown.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,20 @@ export interface IContextMenuProvider {
192192
}
193193

194194
export interface IActionProvider {
195-
getActions(): IAction[];
195+
getActions(): ReadonlyArray<IAction>;
196196
}
197197

198198
export interface IDropdownMenuOptions extends IBaseDropdownOptions {
199199
contextMenuProvider: IContextMenuProvider;
200-
actions?: IAction[];
200+
actions?: ReadonlyArray<IAction>;
201201
actionProvider?: IActionProvider;
202202
menuClassName?: string;
203203
}
204204

205205
export class DropdownMenu extends BaseDropdown {
206206
private _contextMenuProvider: IContextMenuProvider;
207207
private _menuOptions: IMenuOptions;
208-
private _actions: IAction[];
208+
private _actions: ReadonlyArray<IAction>;
209209
private actionProvider?: IActionProvider;
210210
private menuClassName: string;
211211

@@ -226,15 +226,15 @@ export class DropdownMenu extends BaseDropdown {
226226
return this._menuOptions;
227227
}
228228

229-
private get actions(): IAction[] {
229+
private get actions(): ReadonlyArray<IAction> {
230230
if (this.actionProvider) {
231231
return this.actionProvider.getActions();
232232
}
233233

234234
return this._actions;
235235
}
236236

237-
private set actions(actions: IAction[]) {
237+
private set actions(actions: ReadonlyArray<IAction>) {
238238
this._actions = actions;
239239
}
240240

src/vs/base/browser/ui/menu/menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Menu extends ActionBar {
7878

7979
private readonly _onScroll: Emitter<void>;
8080

81-
constructor(container: HTMLElement, actions: IAction[], options: IMenuOptions = {}) {
81+
constructor(container: HTMLElement, actions: ReadonlyArray<IAction>, options: IMenuOptions = {}) {
8282
addClass(container, 'monaco-menu-container');
8383
container.setAttribute('role', 'presentation');
8484
const menuElement = document.createElement('div');

src/vs/workbench/services/contextmenu/electron-browser/contextmenuService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class NativeContextMenuService extends Disposable implements IContextMenuService
115115
}
116116
}
117117

118-
private createMenu(delegate: IContextMenuDelegate, entries: Array<IAction | ContextSubMenu>, onHide: () => void): IContextMenuItem[] {
118+
private createMenu(delegate: IContextMenuDelegate, entries: ReadonlyArray<IAction | ContextSubMenu>, onHide: () => void): IContextMenuItem[] {
119119
const actionRunner = delegate.actionRunner || new ActionRunner();
120120

121121
return entries.map(entry => this.createMenuItem(delegate, entry, actionRunner, onHide));

0 commit comments

Comments
 (0)