Skip to content

Commit

Permalink
Remove extra not null assertions
Browse files Browse the repository at this point in the history
With microsoft/TypeScript#56908, TS should better preserve type refinements. To help test this out, I made a quick pass through our code to remove type assertions that are no longer needed

Most of these are not impacted by microsoft/TypeScript#56908 but removing them helps TS test changes like this

Not adding an eslint rule for now as it requires whole program intellisense, which is too slow for commit hooks
  • Loading branch information
mjbvz committed Jan 11, 2024
1 parent 0bb69da commit 6085a78
Show file tree
Hide file tree
Showing 179 changed files with 534 additions and 536 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/browser/formattedTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function _renderFormattedText(element: Node, treeNode: IFormatParseTree, actionH

if (child && Array.isArray(treeNode.children)) {
treeNode.children.forEach((nodeChild) => {
_renderFormattedText(child!, nodeChild, actionHandler, renderCodeSegments);
_renderFormattedText(child, nodeChild, actionHandler, renderCodeSegments);
});
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/vs/base/browser/ui/contextview/contextview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ export class ContextView extends Disposable {
return;
}

if (this.delegate!.layout) {
this.delegate!.layout!();
}
this.delegate?.layout?.();

this.doLayout();
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/browser/ui/list/listView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class NativeDragAndDropData implements IDragAndDropData {

function equalsDragFeedback(f1: number[] | undefined, f2: number[] | undefined): boolean {
if (Array.isArray(f1) && Array.isArray(f2)) {
return equals(f1, f2!);
return equals(f1, f2);
}

return f1 === f2;
Expand Down Expand Up @@ -909,7 +909,7 @@ export class ListView<T> implements IListView<T> {
const checked = this.accessibilityProvider.isChecked(item.element);

if (typeof checked === 'boolean') {
item.row!.domNode.setAttribute('aria-checked', String(!!checked));
item.row.domNode.setAttribute('aria-checked', String(!!checked));
} else if (checked) {
const update = (checked: boolean) => item.row!.domNode.setAttribute('aria-checked', String(!!checked));
update(checked.value);
Expand Down
6 changes: 3 additions & 3 deletions src/vs/base/browser/ui/tree/abstractTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ class StickyScrollWidget<T, TFilterData, TRef> implements IDisposable {
const isVisible = !!state && state.count > 0;

// If state has not changed, do nothing
if ((!wasVisible && !isVisible) || (wasVisible && isVisible && this._previousState!.equal(state!))) {
if ((!wasVisible && !isVisible) || (wasVisible && isVisible && this._previousState!.equal(state))) {
return;
}

Expand Down Expand Up @@ -2468,7 +2468,7 @@ export abstract class AbstractTree<T, TFilterData, TRef> implements IDisposable
this.findController = new FindController(this, this.model, this.view, filter!, _options.contextViewProvider, opts);
this.focusNavigationFilter = node => this.findController!.shouldAllowFocus(node);
this.onDidChangeFindOpenState = this.findController.onDidChangeOpenState;
this.disposables.add(this.findController!);
this.disposables.add(this.findController);
this.onDidChangeFindMode = this.findController.onDidChangeMode;
this.onDidChangeFindMatchType = this.findController.onDidChangeMatchType;
} else {
Expand Down Expand Up @@ -2877,7 +2877,7 @@ export abstract class AbstractTree<T, TFilterData, TRef> implements IDisposable
const node = queue.shift()!;

if (node !== root && node.collapsible) {
state.expanded[getId(node.element!)] = node.collapsed ? 0 : 1;
state.expanded[getId(node.element)] = node.collapsed ? 0 : 1;
}

queue.push(...node.children);
Expand Down
20 changes: 10 additions & 10 deletions src/vs/base/browser/ui/tree/asyncDataTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ function asObjectTreeOptions<TInput, T, TFilterData>(options?: IAsyncDataTreeOpt
...options.accessibilityProvider,
getPosInSet: undefined,
getSetSize: undefined,
getRole: options.accessibilityProvider!.getRole ? (el) => {
getRole: options.accessibilityProvider.getRole ? (el) => {
return options.accessibilityProvider!.getRole!(el.element as T);
} : () => 'treeitem',
isChecked: options.accessibilityProvider!.isChecked ? (e) => {
isChecked: options.accessibilityProvider.isChecked ? (e) => {
return !!(options.accessibilityProvider?.isChecked!(e.element as T));
} : undefined,
getAriaLabel(e) {
Expand All @@ -251,8 +251,8 @@ function asObjectTreeOptions<TInput, T, TFilterData>(options?: IAsyncDataTreeOpt
getWidgetAriaLabel() {
return options.accessibilityProvider!.getWidgetAriaLabel();
},
getWidgetRole: options.accessibilityProvider!.getWidgetRole ? () => options.accessibilityProvider!.getWidgetRole!() : () => 'tree',
getAriaLevel: options.accessibilityProvider!.getAriaLevel && (node => {
getWidgetRole: options.accessibilityProvider.getWidgetRole ? () => options.accessibilityProvider!.getWidgetRole!() : () => 'tree',
getAriaLevel: options.accessibilityProvider.getAriaLevel && (node => {
return options.accessibilityProvider!.getAriaLevel!(node.element as T);
}),
getActiveDescendantId: options.accessibilityProvider.getActiveDescendantId && (node => {
Expand Down Expand Up @@ -825,7 +825,7 @@ export class AsyncDataTree<TInput, T, TFilterData = void> implements IDisposable
const treeNode = this.tree.getNode(node);

if (treeNode.collapsed) {
node.hasChildren = !!this.dataSource.hasChildren(node.element!);
node.hasChildren = !!this.dataSource.hasChildren(node.element);
node.stale = true;
return;
}
Expand Down Expand Up @@ -855,7 +855,7 @@ export class AsyncDataTree<TInput, T, TFilterData = void> implements IDisposable
}

private async doRefreshNode(node: IAsyncDataTreeNode<TInput, T>, recursive: boolean, viewStateContext?: IAsyncDataTreeViewStateContext<TInput, T>): Promise<IAsyncDataTreeNode<TInput, T>[]> {
node.hasChildren = !!this.dataSource.hasChildren(node.element!);
node.hasChildren = !!this.dataSource.hasChildren(node.element);

let childrenPromise: Promise<Iterable<T>>;

Expand Down Expand Up @@ -904,7 +904,7 @@ export class AsyncDataTree<TInput, T, TFilterData = void> implements IDisposable
if (result) {
return result;
}
const children = this.dataSource.getChildren(node.element!);
const children = this.dataSource.getChildren(node.element);
if (isIterable(children)) {
return this.processChildren(children);
} else {
Expand Down Expand Up @@ -1033,9 +1033,9 @@ export class AsyncDataTree<TInput, T, TFilterData = void> implements IDisposable
const children = node.children.map(node => this.asTreeElement(node, viewStateContext));
const objectTreeOptions: IObjectTreeSetChildrenOptions<IAsyncDataTreeNode<TInput, T>> | undefined = options && {
...options,
diffIdentityProvider: options!.diffIdentityProvider && {
diffIdentityProvider: options.diffIdentityProvider && {
getId(node: IAsyncDataTreeNode<TInput, T>): { toString(): string } {
return options!.diffIdentityProvider!.getId(node.element as T);
return options.diffIdentityProvider!.getId(node.element as T);
}
}
};
Expand Down Expand Up @@ -1210,7 +1210,7 @@ function asCompressibleObjectTreeOptions<TInput, T, TFilterData>(options?: IComp
keyboardNavigationLabelProvider: objectTreeOptions.keyboardNavigationLabelProvider && {
...objectTreeOptions.keyboardNavigationLabelProvider,
getCompressedNodeKeyboardNavigationLabel(els) {
return options!.keyboardNavigationLabelProvider!.getCompressedNodeKeyboardNavigationLabel(els.map(e => e.element as T));
return options.keyboardNavigationLabelProvider!.getCompressedNodeKeyboardNavigationLabel(els.map(e => e.element as T));
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/common/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function memoize(_target: any, key: string, descriptor: any) {
configurable: false,
enumerable: false,
writable: false,
value: fn!.apply(this, args)
value: fn.apply(this, args)
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/common/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ export abstract class ReferenceCollection<T> {

const { object } = reference;
const dispose = createSingleCallFunction(() => {
if (--reference!.counter === 0) {
this.destroyReferencedObject(key, reference!.object);
if (--reference.counter === 0) {
this.destroyReferencedObject(key, reference.object);
this.references.delete(key);
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/vs/base/common/linkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class LinkedList<E> {

} else if (atTheEnd) {
// push
const oldLast = this._last!;
const oldLast = this._last;
this._last = newNode;
newNode.prev = oldLast;
oldLast.next = newNode;
Expand Down Expand Up @@ -119,12 +119,12 @@ export class LinkedList<E> {

} else if (node.next === Node.Undefined) {
// last
this._last = this._last!.prev!;
this._last = this._last.prev!;
this._last.next = Node.Undefined;

} else if (node.prev === Node.Undefined) {
// first
this._first = this._first!.next!;
this._first = this._first.next!;
this._first.prev = Node.Undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/common/ternarySearchTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ export class TernarySearchTree<K, V> {
const min = this._min(node.right);
if (min.key) {
const { key, value, segment } = min;
this._delete(min.key!, false);
this._delete(min.key, false);
node.key = key;
node.value = value;
node.segment = segment;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/test/browser/ui/menu/menubar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ function validateMenuBarItem(menubar: MenuBar, menubarContainer: HTMLElement, la
const buttonElement = getButtonElementByAriaLabel(menubarContainer, readableLabel);
assert(buttonElement !== null, `Button element not found for ${readableLabel} button.`);

const titleDiv = getTitleDivFromButtonDiv(buttonElement!);
const titleDiv = getTitleDivFromButtonDiv(buttonElement);
assert(titleDiv !== null, `Title div not found for ${readableLabel} button.`);

const mnem = getMnemonicFromTitleDiv(titleDiv!);
const mnem = getMnemonicFromTitleDiv(titleDiv);
assert.strictEqual(mnem, mnemonic, 'Mnemonic not correct');
}

Expand Down
74 changes: 37 additions & 37 deletions src/vs/base/test/common/fuzzyScorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ suite('Fuzzy Scorer', () => {
assert.ok(pathRes.score);
assert.ok(pathRes.descriptionMatch);
assert.ok(pathRes.labelMatch);
assert.strictEqual(pathRes.labelMatch!.length, 1);
assert.strictEqual(pathRes.labelMatch![0].start, 8);
assert.strictEqual(pathRes.labelMatch![0].end, 11);
assert.strictEqual(pathRes.descriptionMatch!.length, 1);
assert.strictEqual(pathRes.descriptionMatch![0].start, 1);
assert.strictEqual(pathRes.descriptionMatch![0].end, 4);
assert.strictEqual(pathRes.labelMatch.length, 1);
assert.strictEqual(pathRes.labelMatch[0].start, 8);
assert.strictEqual(pathRes.labelMatch[0].end, 11);
assert.strictEqual(pathRes.descriptionMatch.length, 1);
assert.strictEqual(pathRes.descriptionMatch[0].start, 1);
assert.strictEqual(pathRes.descriptionMatch[0].end, 4);

// No Match
const noRes = scoreItem(resource, '987', true, ResourceAccessor);
Expand Down Expand Up @@ -232,41 +232,41 @@ suite('Fuzzy Scorer', () => {
const res1 = scoreItem(resource, 'xyz some', true, ResourceAccessor);
assert.ok(res1.score);
assert.strictEqual(res1.labelMatch?.length, 1);
assert.strictEqual(res1.labelMatch![0].start, 0);
assert.strictEqual(res1.labelMatch![0].end, 4);
assert.strictEqual(res1.labelMatch[0].start, 0);
assert.strictEqual(res1.labelMatch[0].end, 4);
assert.strictEqual(res1.descriptionMatch?.length, 1);
assert.strictEqual(res1.descriptionMatch![0].start, 1);
assert.strictEqual(res1.descriptionMatch![0].end, 4);
assert.strictEqual(res1.descriptionMatch[0].start, 1);
assert.strictEqual(res1.descriptionMatch[0].end, 4);

const res2 = scoreItem(resource, 'some xyz', true, ResourceAccessor);
assert.ok(res2.score);
assert.strictEqual(res1.score, res2.score);
assert.strictEqual(res2.labelMatch?.length, 1);
assert.strictEqual(res2.labelMatch![0].start, 0);
assert.strictEqual(res2.labelMatch![0].end, 4);
assert.strictEqual(res2.labelMatch[0].start, 0);
assert.strictEqual(res2.labelMatch[0].end, 4);
assert.strictEqual(res2.descriptionMatch?.length, 1);
assert.strictEqual(res2.descriptionMatch![0].start, 1);
assert.strictEqual(res2.descriptionMatch![0].end, 4);
assert.strictEqual(res2.descriptionMatch[0].start, 1);
assert.strictEqual(res2.descriptionMatch[0].end, 4);

const res3 = scoreItem(resource, 'some xyz file file123', true, ResourceAccessor);
assert.ok(res3.score);
assert.ok(res3.score > res2.score);
assert.strictEqual(res3.labelMatch?.length, 1);
assert.strictEqual(res3.labelMatch![0].start, 0);
assert.strictEqual(res3.labelMatch![0].end, 11);
assert.strictEqual(res3.labelMatch[0].start, 0);
assert.strictEqual(res3.labelMatch[0].end, 11);
assert.strictEqual(res3.descriptionMatch?.length, 1);
assert.strictEqual(res3.descriptionMatch![0].start, 1);
assert.strictEqual(res3.descriptionMatch![0].end, 4);
assert.strictEqual(res3.descriptionMatch[0].start, 1);
assert.strictEqual(res3.descriptionMatch[0].end, 4);

const res4 = scoreItem(resource, 'path z y', true, ResourceAccessor);
assert.ok(res4.score);
assert.ok(res4.score < res2.score);
assert.strictEqual(res4.labelMatch?.length, 0);
assert.strictEqual(res4.descriptionMatch?.length, 2);
assert.strictEqual(res4.descriptionMatch![0].start, 2);
assert.strictEqual(res4.descriptionMatch![0].end, 4);
assert.strictEqual(res4.descriptionMatch![1].start, 10);
assert.strictEqual(res4.descriptionMatch![1].end, 14);
assert.strictEqual(res4.descriptionMatch[0].start, 2);
assert.strictEqual(res4.descriptionMatch[0].end, 4);
assert.strictEqual(res4.descriptionMatch[1].start, 10);
assert.strictEqual(res4.descriptionMatch[1].end, 14);
});

test('scoreItem - multiple with cache yields different results', function () {
Expand Down Expand Up @@ -299,12 +299,12 @@ suite('Fuzzy Scorer', () => {
assert.ok(pathRes.score);
assert.ok(pathRes.descriptionMatch);
assert.ok(pathRes.labelMatch);
assert.strictEqual(pathRes.labelMatch!.length, 1);
assert.strictEqual(pathRes.labelMatch![0].start, 0);
assert.strictEqual(pathRes.labelMatch![0].end, 7);
assert.strictEqual(pathRes.descriptionMatch!.length, 1);
assert.strictEqual(pathRes.descriptionMatch![0].start, 23);
assert.strictEqual(pathRes.descriptionMatch![0].end, 26);
assert.strictEqual(pathRes.labelMatch.length, 1);
assert.strictEqual(pathRes.labelMatch[0].start, 0);
assert.strictEqual(pathRes.labelMatch[0].end, 7);
assert.strictEqual(pathRes.descriptionMatch.length, 1);
assert.strictEqual(pathRes.descriptionMatch[0].start, 23);
assert.strictEqual(pathRes.descriptionMatch[0].end, 26);
});

test('scoreItem - avoid match scattering (bug #36119)', function () {
Expand All @@ -314,9 +314,9 @@ suite('Fuzzy Scorer', () => {
assert.ok(pathRes.score);
assert.ok(pathRes.descriptionMatch);
assert.ok(pathRes.labelMatch);
assert.strictEqual(pathRes.labelMatch!.length, 1);
assert.strictEqual(pathRes.labelMatch![0].start, 0);
assert.strictEqual(pathRes.labelMatch![0].end, 9);
assert.strictEqual(pathRes.labelMatch.length, 1);
assert.strictEqual(pathRes.labelMatch[0].start, 0);
assert.strictEqual(pathRes.labelMatch[0].end, 9);
});

test('scoreItem - prefers more compact matches', function () {
Expand All @@ -328,11 +328,11 @@ suite('Fuzzy Scorer', () => {
assert.ok(res.score);
assert.ok(res.descriptionMatch);
assert.ok(!res.labelMatch!.length);
assert.strictEqual(res.descriptionMatch!.length, 2);
assert.strictEqual(res.descriptionMatch![0].start, 11);
assert.strictEqual(res.descriptionMatch![0].end, 12);
assert.strictEqual(res.descriptionMatch![1].start, 13);
assert.strictEqual(res.descriptionMatch![1].end, 14);
assert.strictEqual(res.descriptionMatch.length, 2);
assert.strictEqual(res.descriptionMatch[0].start, 11);
assert.strictEqual(res.descriptionMatch[0].end, 12);
assert.strictEqual(res.descriptionMatch[1].start, 13);
assert.strictEqual(res.descriptionMatch[1].end, 14);
});

test('scoreItem - proper target offset', function () {
Expand Down Expand Up @@ -1121,7 +1121,7 @@ suite('Fuzzy Scorer', () => {
assert.strictEqual(query.values?.[1].normalized, 'World');
assert.strictEqual(query.values?.[1].normalizedLowercase, 'World'.toLowerCase());

const restoredQuery = pieceToQuery(query.values!);
const restoredQuery = pieceToQuery(query.values);
assert.strictEqual(restoredQuery.original, query.original);
assert.strictEqual(restoredQuery.values?.length, query.values?.length);
assert.strictEqual(restoredQuery.containsPathSeparator, query.containsPathSeparator);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/base/test/common/troubleshooting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function beginTrackingDisposables(): void {
export function endTrackingDisposables(): void {
if (currentTracker) {
setDisposableTracker(null);
console.log(currentTracker!.allDisposables.map(e => `${e[0]}\n${e[1]}`).join('\n\n'));
console.log(currentTracker.allDisposables.map(e => `${e[0]}\n${e[1]}`).join('\n\n'));
currentTracker = null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/code/browser/workbench/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ export class LocalStorageSecretStorageProvider implements ISecretStorageProvider

const authAccount = JSON.stringify({ extensionId: 'vscode.github-authentication', key: 'github.auth' });
record[authAccount] = JSON.stringify(authSessionInfo.scopes.map(scopes => ({
id: authSessionInfo!.id,
id: authSessionInfo.id,
scopes,
accessToken: authSessionInfo!.accessToken
accessToken: authSessionInfo.accessToken
})));

return record;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/editorExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export abstract class EditorAction2 extends Action2 {
logService.debug(`[EditorAction2] NOT running command because its precondition is FALSE`, this.desc.id, this.desc.precondition?.serialize());
return;
}
return this.runEditorCommand(editorAccessor, editor!, ...args);
return this.runEditorCommand(editorAccessor, editor, ...args);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/services/hoverService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class HoverService implements IHoverService {
hover.isLocked = true;
}
hover.onDispose(() => {
const hoverWasFocused = this._currentHover?.domNode && isAncestorOfActiveElement(this._currentHover.domNode!);
const hoverWasFocused = this._currentHover?.domNode && isAncestorOfActiveElement(this._currentHover.domNode);
if (hoverWasFocused) {
// Required to handle cases such as closing the hover with the escape key
this._lastFocusedElementBeforeOpen?.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class ViewOverlayWidgets extends ViewPart {
const fixedOverflowWidgets = this._context.configuration.options.get(EditorOption.fixedOverflowWidgets);
if (fixedOverflowWidgets && widgetData.widget.allowEditorOverflow) {
// top, left are computed relative to the editor and we need them relative to the page
const editorBoundingBox = this._viewDomNodeRect!;
const editorBoundingBox = this._viewDomNodeRect;
domNode.setTop(top + editorBoundingBox.top);
domNode.setLeft(left + editorBoundingBox.left);
domNode.setPosition('fixed');
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ class EditorGoToLocation extends BaseEditorOption<EditorOption.gotoLocation, IGo
}
const input = _input as IGotoLocationOptions;
return {
multiple: stringSet<GoToLocationValues>(input.multiple, this.defaultValue.multiple!, ['peek', 'gotoAndPeek', 'goto']),
multiple: stringSet<GoToLocationValues>(input.multiple, this.defaultValue.multiple, ['peek', 'gotoAndPeek', 'goto']),
multipleDefinitions: input.multipleDefinitions ?? stringSet<GoToLocationValues>(input.multipleDefinitions, 'peek', ['peek', 'gotoAndPeek', 'goto']),
multipleTypeDefinitions: input.multipleTypeDefinitions ?? stringSet<GoToLocationValues>(input.multipleTypeDefinitions, 'peek', ['peek', 'gotoAndPeek', 'goto']),
multipleDeclarations: input.multipleDeclarations ?? stringSet<GoToLocationValues>(input.multipleDeclarations, 'peek', ['peek', 'gotoAndPeek', 'goto']),
Expand Down
Loading

0 comments on commit 6085a78

Please sign in to comment.