Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use optional chaining for more method calls #156938

Merged
merged 3 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/vs/base/browser/dom.ts
Expand Up @@ -1149,9 +1149,7 @@ export function removeTabIndexAndUpdateFocus(node: HTMLElement): void {
// in the hierarchy of the parent DOM nodes.
if (document.activeElement === node) {
const parentFocusable = findParentWithAttribute(node.parentElement, 'tabIndex');
if (parentFocusable) {
parentFocusable.focus();
}
parentFocusable?.focus();
}

node.removeAttribute('tabindex');
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/indexedDB.ts
Expand Up @@ -106,9 +106,7 @@ export class IndexedDB {
if (this.pendingTransactions.length) {
this.pendingTransactions.splice(0, this.pendingTransactions.length).forEach(transaction => transaction.abort());
}
if (this.database) {
this.database.close();
}
this.database?.close();
this.database = null;
}

Expand Down
8 changes: 2 additions & 6 deletions src/vs/base/browser/mouseEvent.ts
Expand Up @@ -211,14 +211,10 @@ export class StandardWheelEvent {
}

public preventDefault(): void {
if (this.browserEvent) {
this.browserEvent.preventDefault();
}
this.browserEvent?.preventDefault();
}

public stopPropagation(): void {
if (this.browserEvent) {
this.browserEvent.stopPropagation();
}
this.browserEvent?.stopPropagation();
}
}
8 changes: 2 additions & 6 deletions src/vs/base/browser/ui/actionbar/actionViewItems.ts
Expand Up @@ -439,15 +439,11 @@ export class SelectActionViewItem extends BaseActionViewItem {
}

override focus(): void {
if (this.selectBox) {
this.selectBox.focus();
}
this.selectBox?.focus();
}

override blur(): void {
if (this.selectBox) {
this.selectBox.blur();
}
this.selectBox?.blur();
}

override render(container: HTMLElement): void {
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/centered/centeredViewLayout.ts
Expand Up @@ -159,9 +159,7 @@ export class CenteredViewLayout implements IDisposable {
this.container.removeChild(this.splitView.el);
}
this.splitViewDisposables.clear();
if (this.splitView) {
this.splitView.dispose();
}
this.splitView?.dispose();
this.splitView = undefined;
this.emptyViews = undefined;
this.container.appendChild(this.view.element);
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/dropdown/dropdown.ts
Expand Up @@ -192,9 +192,7 @@ export class Dropdown extends BaseDropdown {
override hide(): void {
super.hide();

if (this.contextViewProvider) {
this.contextViewProvider.hideContextView();
}
this.contextViewProvider?.hideContextView();
}

protected renderContents(container: HTMLElement): IDisposable | null {
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/dropdown/dropdownActionViewItem.ts
Expand Up @@ -155,9 +155,7 @@ export class DropdownMenuActionViewItem extends BaseActionViewItem {
}

show(): void {
if (this.dropdownMenu) {
this.dropdownMenu.show();
}
this.dropdownMenu?.show();
}

protected override updateEnabled(): void {
Expand Down
12 changes: 3 additions & 9 deletions src/vs/base/browser/ui/findinput/replaceInput.ts
Expand Up @@ -353,25 +353,19 @@ export class ReplaceInput extends Widget {
}

public validate(): void {
if (this.inputBox) {
this.inputBox.validate();
}
this.inputBox?.validate();
}

public showMessage(message: InputBoxMessage): void {
this.inputBox?.showMessage(message);
}

public clearMessage(): void {
if (this.inputBox) {
this.inputBox.hideMessage();
}
this.inputBox?.hideMessage();
}

private clearValidation(): void {
if (this.inputBox) {
this.inputBox.hideMessage();
}
this.inputBox?.hideMessage();
}

public set width(newWidth: number) {
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/inputbox/inputBox.ts
Expand Up @@ -621,9 +621,7 @@ export class InputBox extends Widget {

this.message = null;

if (this.actionbar) {
this.actionbar.dispose();
}
this.actionbar?.dispose();

super.dispose();
}
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/list/listPaging.ts
Expand Up @@ -38,9 +38,7 @@ class PagedRenderer<TElement, TTemplateData> implements IListRenderer<number, IT
}

renderElement(index: number, _: number, data: ITemplateData<TTemplateData>, height: number | undefined): void {
if (data.disposable) {
data.disposable.dispose();
}
data.disposable?.dispose();

if (!data.data) {
return;
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/list/listWidget.ts
Expand Up @@ -1528,9 +1528,7 @@ export class List<T> implements ISpliceable<T>, IThemable, IDisposable {
}

triggerTypeNavigation(): void {
if (this.typeNavigationController) {
this.typeNavigationController.trigger();
}
this.typeNavigationController?.trigger();
}

setSelection(indexes: number[], browserEvent?: UIEvent): void {
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/menu/menu.ts
Expand Up @@ -552,9 +552,7 @@ class BaseMenuActionViewItem extends BaseActionViewItem {
override focus(): void {
super.focus();

if (this.item) {
this.item.focus();
}
this.item?.focus();

this.applyStyle();
}
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/menu/menubar.ts
Expand Up @@ -985,9 +985,7 @@ export class MenuBar extends Disposable {
this.focusedMenu.holder.remove();
}

if (this.focusedMenu.widget) {
this.focusedMenu.widget.dispose();
}
this.focusedMenu.widget?.dispose();

this.focusedMenu = { index: this.focusedMenu.index };
}
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/common/cancellation.ts
Expand Up @@ -129,9 +129,7 @@ export class CancellationTokenSource {
if (cancel) {
this.cancel();
}
if (this._parentListener) {
this._parentListener.dispose();
}
this._parentListener?.dispose();
if (!this._token) {
// ensure to initialize with an empty token if we had none
this._token = CancellationToken.None;
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/node/zip.ts
Expand Up @@ -81,9 +81,7 @@ function extractEntry(stream: Readable, fileName: string, mode: number, targetPa
let istream: WriteStream;

token.onCancellationRequested(() => {
if (istream) {
istream.destroy();
}
istream?.destroy();
});

return Promise.resolve(Promises.mkdir(targetDirName, { recursive: true })).then(() => new Promise<void>((c, e) => {
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/parts/ipc/electron-main/ipc.electron.ts
Expand Up @@ -37,9 +37,7 @@ export class Server extends IPCServer {
const id = webContents.id;
const client = Server.Clients.get(id);

if (client) {
client.dispose();
}
client?.dispose();

const onDidClientReconnect = new Emitter<void>();
Server.Clients.set(id, toDisposable(() => onDidClientReconnect.fire()));
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/parts/ipc/node/ipc.cp.ts
Expand Up @@ -241,9 +241,7 @@ export class Client implements IChannelClient, IDisposable {
console.warn('IPC "' + this.options.serverName + '" crashed with exit code ' + code + ' and signal ' + signal);
}

if (this.disposeDelayer) {
this.disposeDelayer.cancel();
}
this.disposeDelayer?.cancel();
this.disposeClient();
this._onDidProcessExit.fire({ code, signal });
});
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/parts/quickinput/browser/quickInput.ts
Expand Up @@ -1610,9 +1610,7 @@ export class QuickInputController extends Disposable {
this.onShowEmitter.fire();
const oldController = this.controller;
this.controller = controller;
if (oldController) {
oldController.didHide();
}
oldController?.didHide();

this.setEnabled(true);
ui.leftActionBar.clear();
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/test/browser/indexedDB.test.ts
Expand Up @@ -16,9 +16,7 @@ flakySuite('IndexedDB', () => {
});

teardown(() => {
if (indexedDB) {
indexedDB.close();
}
indexedDB?.close();
});

test('runInTransaction', async () => {
Expand Down
8 changes: 2 additions & 6 deletions src/vs/code/electron-sandbox/issue/issueReporterMain.ts
Expand Up @@ -151,14 +151,10 @@ export class IssueReporter extends Disposable {
const { fileOnExtension } = this.issueReporterModel.getData();
if (fileOnExtension) {
const issueTitle = document.getElementById('issue-title');
if (issueTitle) {
issueTitle.focus();
}
issueTitle?.focus();
} else {
const issueType = document.getElementById('issue-type');
if (issueType) {
issueType.focus();
}
issueType?.focus();
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/vs/editor/browser/widget/diffEditorWidget.ts
Expand Up @@ -1324,9 +1324,7 @@ export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffE
}

private _setStrategy(newStrategy: DiffEditorWidgetStyle): void {
if (this._strategy) {
this._strategy.dispose();
}
this._strategy?.dispose();

this._strategy = newStrategy;
newStrategy.applyColors(this._themeService.getColorTheme());
Expand Down
8 changes: 2 additions & 6 deletions src/vs/editor/browser/widget/diffReview.ts
Expand Up @@ -846,9 +846,7 @@ class DiffReviewNext extends EditorAction {

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
const diffEditor = findFocusedDiffEditor(accessor);
if (diffEditor) {
diffEditor.diffReviewNext();
}
diffEditor?.diffReviewNext();
}
}

Expand All @@ -869,9 +867,7 @@ class DiffReviewPrev extends EditorAction {

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
const diffEditor = findFocusedDiffEditor(accessor);
if (diffEditor) {
diffEditor.diffReviewPrev();
}
diffEditor?.diffReviewPrev();
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/vs/editor/common/viewModel/viewModelImpl.ts
Expand Up @@ -761,13 +761,9 @@ export class ViewModel extends Disposable implements IViewModel {
const decorations = this.model.getOverviewRulerDecorations();
for (const decoration of decorations) {
const opts1 = <ModelDecorationOverviewRulerOptions>decoration.options.overviewRuler;
if (opts1) {
opts1.invalidateCachedColor();
}
opts1?.invalidateCachedColor();
const opts2 = <ModelDecorationMinimapOptions>decoration.options.minimap;
if (opts2) {
opts2.invalidateCachedColor();
}
opts2?.invalidateCachedColor();
}
}

Expand Down
12 changes: 3 additions & 9 deletions src/vs/editor/contrib/folding/browser/folding.ts
Expand Up @@ -245,27 +245,21 @@ export class FoldingController extends Disposable implements IEditorContribution
this.foldingRegionPromise.cancel();
this.foldingRegionPromise = null;
}
if (this.updateScheduler) {
this.updateScheduler.cancel();
}
this.updateScheduler?.cancel();
this.updateScheduler = null;
this.foldingModel = null;
this.foldingModelPromise = null;
this.hiddenRangeModel = null;
this.cursorChangedScheduler = null;
if (this.rangeProvider) {
this.rangeProvider.dispose();
}
this.rangeProvider?.dispose();
this.rangeProvider = null;
}
});
this.triggerFoldingModelChanged();
}

private onFoldingStrategyChanged() {
if (this.rangeProvider) {
this.rangeProvider.dispose();
}
this.rangeProvider?.dispose();
this.rangeProvider = null;
this.triggerFoldingModelChanged();
}
Expand Down
Expand Up @@ -238,9 +238,7 @@ export abstract class ReferencesController implements IEditorContribution {
}

private _gotoReference(ref: Location): Promise<any> {
if (this._widget) {
this._widget.hide();
}
this._widget?.hide();

this._ignoreModelChangeEvent = true;
const range = Range.lift(ref.range).collapseToStart();
Expand Down
4 changes: 1 addition & 3 deletions src/vs/editor/contrib/hover/browser/contentHover.ts
Expand Up @@ -428,9 +428,7 @@ export class ContentHoverWidget extends Disposable implements IContentWidget {
if (visibleData.stoleFocus) {
this._hover.containerDomNode.focus();
}
if (visibleData.colorPicker) {
visibleData.colorPicker.layout();
}
visibleData.colorPicker?.layout();
}

public hide(): void {
Expand Down
Expand Up @@ -56,9 +56,7 @@ class InPlaceReplaceController implements IEditorContribution {
public run(source: string, up: boolean): Promise<void> | undefined {

// cancel any pending request
if (this.currentRequest) {
this.currentRequest.cancel();
}
this.currentRequest?.cancel();

const editorSelection = this.editor.getSelection();
const model = this.editor.getModel();
Expand Down Expand Up @@ -121,9 +119,7 @@ class InPlaceReplaceController implements IEditorContribution {
}]);

// remove decoration after delay
if (this.decorationRemover) {
this.decorationRemover.cancel();
}
this.decorationRemover?.cancel();
this.decorationRemover = timeout(350);
this.decorationRemover.then(() => this.decorations.clear()).catch(onUnexpectedError);

Expand Down
Expand Up @@ -247,8 +247,6 @@ export class TriggerInlineSuggestionAction extends EditorAction {

public async run(accessor: ServicesAccessor | undefined, editor: ICodeEditor): Promise<void> {
const controller = GhostTextController.get(editor);
if (controller) {
controller.trigger();
}
controller?.trigger();
}
}