From a76cfdb60942c68b9cbc6326df698c32f4e592f7 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 5 Nov 2018 10:33:56 +0100 Subject: [PATCH] strict null checks: grid.ts fixes #61542 --- src/tsconfig.strictNullChecks.json | 1 + src/vs/base/browser/ui/grid/grid.ts | 50 +++++++++++-------- .../browser/parts/editor/editorPart.ts | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/tsconfig.strictNullChecks.json b/src/tsconfig.strictNullChecks.json index 33e90845599..a5cbb935516 100644 --- a/src/tsconfig.strictNullChecks.json +++ b/src/tsconfig.strictNullChecks.json @@ -28,6 +28,7 @@ "./vs/base/browser/ui/contextview/contextview.ts", "./vs/base/browser/ui/countBadge/countBadge.ts", "./vs/base/browser/ui/grid/gridview.ts", + "./vs/base/browser/ui/grid/grid.ts", "./vs/base/browser/ui/highlightedlabel/highlightedLabel.ts", "./vs/base/browser/ui/iconLabel/iconLabel.ts", "./vs/base/browser/ui/keybindingLabel/keybindingLabel.ts", diff --git a/src/vs/base/browser/ui/grid/grid.ts b/src/vs/base/browser/ui/grid/grid.ts index f09231bc05f..336329a05e2 100644 --- a/src/vs/base/browser/ui/grid/grid.ts +++ b/src/vs/base/browser/ui/grid/grid.ts @@ -139,10 +139,15 @@ export function getRelativeLocation(rootOrientation: Orientation, location: numb function indexInParent(element: HTMLElement): number { const parentElement = element.parentElement; + + if (!parentElement) { + throw new Error('Invalid grid element'); + } + let el = parentElement.firstElementChild; let index = 0; - while (el !== element && el !== parentElement.lastElementChild) { + while (el !== element && el !== parentElement.lastElementChild && el) { el = el.nextElementSibling; index++; } @@ -157,12 +162,18 @@ function indexInParent(element: HTMLElement): number { * This will break as soon as DOM structures of the Splitview or Gridview change. */ function getGridLocation(element: HTMLElement): number[] { - if (/\bmonaco-grid-view\b/.test(element.parentElement.className)) { + const parentElement = element.parentElement; + + if (!parentElement) { + throw new Error('Invalid grid element'); + } + + if (/\bmonaco-grid-view\b/.test(parentElement.className)) { return []; } - const index = indexInParent(element.parentElement); - const ancestor = element.parentElement.parentElement.parentElement.parentElement; + const index = indexInParent(parentElement); + const ancestor = parentElement.parentElement!.parentElement!.parentElement!; return [...getGridLocation(ancestor), index]; } @@ -193,7 +204,7 @@ export class Grid implements IDisposable { get minimumHeight(): number { return this.gridview.minimumHeight; } get maximumWidth(): number { return this.gridview.maximumWidth; } get maximumHeight(): number { return this.gridview.maximumHeight; } - get onDidChange(): Event<{ width: number; height: number; }> { return this.gridview.onDidChange; } + get onDidChange(): Event<{ width: number; height: number; } | undefined> { return this.gridview.onDidChange; } get element(): HTMLElement { return this.gridview.element; } @@ -368,7 +379,7 @@ export interface ISerializableView extends IView { } export interface IViewDeserializer { - fromJSON(json: object): T; + fromJSON(json: object | null): T; } interface InitialLayoutContext { @@ -379,7 +390,7 @@ interface InitialLayoutContext { export interface ISerializedLeafNode { type: 'leaf'; - data: object; + data: object | null; size: number; } @@ -415,18 +426,15 @@ export class SerializableGrid extends Grid { throw new Error('Invalid JSON'); } - const type = json.type; - const data = json.data; - - if (type === 'branch') { - if (!Array.isArray(data)) { + if (json.type === 'branch') { + if (!Array.isArray(json.data)) { throw new Error('Invalid JSON: \'data\' property of branch must be an array.'); } const children: GridNode[] = []; let offset = 0; - for (const child of data) { + for (const child of json.data) { if (typeof child.size !== 'number') { throw new Error('Invalid JSON: \'size\' property of node must be a number.'); } @@ -441,8 +449,8 @@ export class SerializableGrid extends Grid { return { children, box }; - } else if (type === 'leaf') { - const view = deserializer.fromJSON(data) as T; + } else if (json.type === 'leaf') { + const view = deserializer.fromJSON(json.data) as T; return { view, box }; } @@ -527,13 +535,13 @@ export class SerializableGrid extends Grid { const firstLeaves = node.children.map(c => SerializableGrid.getFirstLeaf(c)); for (let i = 1; i < firstLeaves.length; i++) { - const size = orientation === Orientation.VERTICAL ? firstLeaves[i].box.height : firstLeaves[i].box.width; - this.addView(firstLeaves[i].view, size, referenceView, direction); - referenceView = firstLeaves[i].view; + const size = orientation === Orientation.VERTICAL ? firstLeaves[i]!.box.height : firstLeaves[i]!.box.width; + this.addView(firstLeaves[i]!.view, size, referenceView, direction); + referenceView = firstLeaves[i]!.view; } for (let i = 0; i < node.children.length; i++) { - this.restoreViews(firstLeaves[i].view, orthogonal(orientation), node.children[i]); + this.restoreViews(firstLeaves[i]!.view, orthogonal(orientation), node.children[i]); } } @@ -611,10 +619,10 @@ function getDimensions(node: ISerializedNode, orientation: Orientation): { width if (orientation === Orientation.VERTICAL) { const width = node.size || (childrenDimensions.length === 0 ? undefined : Math.max(...childrenDimensions.map(d => d.width || 0))); - const height = childrenDimensions.length === 0 ? undefined : childrenDimensions.reduce((r, d) => r + d.height, 0); + const height = childrenDimensions.length === 0 ? undefined : childrenDimensions.reduce((r, d) => r + (d.height || 0), 0); return { width, height }; } else { - const width = childrenDimensions.length === 0 ? undefined : childrenDimensions.reduce((r, d) => r + d.width, 0); + const width = childrenDimensions.length === 0 ? undefined : childrenDimensions.reduce((r, d) => r + (d.width || 0), 0); const height = node.size || (childrenDimensions.length === 0 ? undefined : Math.max(...childrenDimensions.map(d => d.height || 0))); return { width, height }; } diff --git a/src/vs/workbench/browser/parts/editor/editorPart.ts b/src/vs/workbench/browser/parts/editor/editorPart.ts index 8b01a98aa17..e94317b65ae 100644 --- a/src/vs/workbench/browser/parts/editor/editorPart.ts +++ b/src/vs/workbench/browser/parts/editor/editorPart.ts @@ -877,7 +877,7 @@ export class EditorPart extends Part implements EditorGroupsServiceImpl, IEditor // Create new const groupViews: IEditorGroupView[] = []; const gridWidget = SerializableGrid.deserialize(serializedGrid, { - fromJSON: (serializedEditorGroup: ISerializedEditorGroup) => { + fromJSON: (serializedEditorGroup: ISerializedEditorGroup | null) => { let groupView: IEditorGroupView; if (reuseGroupViews.length > 0) { groupView = reuseGroupViews.shift(); -- GitLab