Skip to content

Commit 3ba3266

Browse files
committed
More strict null sources (#60565)
1 parent e6f2819 commit 3ba3266

File tree

17 files changed

+174
-103
lines changed

17 files changed

+174
-103
lines changed

Diff for: src/tsconfig.strictNullChecks.json

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@
66
},
77
"include": [
88
"./typings",
9+
"./vs/base/browser/browser.ts",
10+
"./vs/base/browser/event.ts",
911
"./vs/base/browser/history.ts",
1012
"./vs/base/browser/iframe.ts",
13+
"./vs/base/browser/keyboardEvent.ts",
14+
"./vs/base/browser/mouseEvent.ts",
1115
"./vs/base/browser/ui/octiconLabel/octiconLabel.mock.ts",
1216
"./vs/base/browser/ui/octiconLabel/octiconLabel.ts",
1317
"./vs/base/browser/ui/scrollbar/scrollbarState.ts",
1418
"./vs/base/common/amd.ts",
19+
"./vs/base/common/arrays.ts",
1520
"./vs/base/common/assert.ts",
21+
"./vs/base/common/async.ts",
22+
"./vs/base/common/cancellation.ts",
1623
"./vs/base/common/charCode.ts",
24+
"./vs/base/common/collections.ts",
1725
"./vs/base/common/color.ts",
1826
"./vs/base/common/comparers.ts",
1927
"./vs/base/common/date.ts",
@@ -23,13 +31,17 @@
2331
"./vs/base/common/errors.ts",
2432
"./vs/base/common/event.ts",
2533
"./vs/base/common/functional.ts",
34+
"./vs/base/common/glob.ts",
35+
"./vs/base/common/hash.ts",
36+
"./vs/base/common/htmlContent.ts",
2637
"./vs/base/common/idGenerator.ts",
2738
"./vs/base/common/iterator.ts",
2839
"./vs/base/common/jsonSchema.ts",
2940
"./vs/base/common/keybindingParser.ts",
3041
"./vs/base/common/keyCodes.ts",
3142
"./vs/base/common/lifecycle.ts",
3243
"./vs/base/common/linkedList.ts",
44+
"./vs/base/common/map.ts",
3345
"./vs/base/common/marshalling.ts",
3446
"./vs/base/common/network.ts",
3547
"./vs/base/common/numbers.ts",
@@ -41,6 +53,8 @@
4153
"./vs/base/common/range.ts",
4254
"./vs/base/common/resources.ts",
4355
"./vs/base/common/scanCode.ts",
56+
"./vs/base/common/scrollable.ts",
57+
"./vs/base/common/sequence.ts",
4458
"./vs/base/common/severity.ts",
4559
"./vs/base/common/stopwatch.ts",
4660
"./vs/base/common/strings.ts",
@@ -57,7 +71,9 @@
5771
"./vs/base/parts/contextmenu/electron-main/contextmenu.ts",
5872
"./vs/base/parts/quickopen/common/quickOpen.ts",
5973
"./vs/base/test/node/uri.test.perf.ts",
74+
"./vs/base/worker/defaultWorkerFactory.ts",
6075
"./vs/base/worker/workerMain.ts",
76+
"./vs/editor/common/config/editorZoom.ts",
6177
"./vs/editor/common/controller/cursorEvents.ts",
6278
"./vs/editor/common/controller/wordCharacterClassifier.ts",
6379
"./vs/editor/common/core/characterClassifier.ts",
@@ -67,12 +83,13 @@
6783
"./vs/editor/common/core/selection.ts",
6884
"./vs/editor/common/core/stringBuilder.ts",
6985
"./vs/editor/common/core/uint.ts",
86+
"./vs/editor/common/model/mirrorTextModel.ts",
7087
"./vs/editor/common/model/textModelEvents.ts",
7188
"./vs/editor/common/view/overviewZoneManager.ts",
7289
"./vs/editor/common/viewLayout/whitespaceComputer.ts",
7390
"./vs/editor/common/viewModel/prefixSumComputer.ts",
74-
"./vs/editor/common/model/mirrorTextModel.ts",
7591
"./vs/editor/contrib/codeAction/codeActionTrigger.ts",
92+
"./vs/editor/contrib/find/findState.ts",
7693
"./vs/editor/contrib/find/replacePattern.ts",
7794
"./vs/editor/contrib/indentation/indentUtils.ts",
7895
"./vs/editor/standalone/common/monarch/monarchCommon.ts",
@@ -81,20 +98,25 @@
8198
"./vs/nls.mock.ts",
8299
"./vs/platform/clipboard/common/clipboardService.ts",
83100
"./vs/platform/clipboard/electron-browser/clipboardService.ts",
101+
"./vs/platform/contextkey/common/contextkey.ts",
102+
"./vs/platform/editor/common/editor.ts",
84103
"./vs/platform/environment/common/environment.ts",
85104
"./vs/platform/extensions/common/extensionHost.ts",
86105
"./vs/platform/extensions/common/extensions.ts",
106+
"./vs/platform/files/common/files.ts",
87107
"./vs/platform/files/node/files.ts",
88108
"./vs/platform/instantiation/common/descriptors.ts",
89109
"./vs/platform/instantiation/common/extensions.ts",
90110
"./vs/platform/instantiation/common/instantiation.ts",
91111
"./vs/platform/instantiation/common/serviceCollection.ts",
92112
"./vs/platform/integrity/common/integrity.ts",
113+
"./vs/platform/markers/common/markers.ts",
93114
"./vs/platform/node/package.ts",
94115
"./vs/platform/node/product.ts",
95116
"./vs/platform/opener/common/opener.ts",
96117
"./vs/platform/registry/common/platform.ts",
97118
"./vs/platform/state/common/state.ts",
119+
"./vs/platform/theme/common/colorRegistry.ts",
98120
"./vs/workbench/api/shared/tasks.ts",
99121
"./vs/workbench/common/extensionHostProtocol.ts",
100122
"./vs/workbench/parts/execution/common/execution.ts",

Diff for: src/vs/base/browser/mouseEvent.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export class StandardMouseEvent implements IMouseEvent {
6666
this.posy = e.pageY;
6767
} else {
6868
// Probably hit by MSGestureEvent
69-
this.posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
70-
this.posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
69+
this.posx = e.clientX + document.body.scrollLeft + document.documentElement!.scrollLeft;
70+
this.posy = e.clientY + document.body.scrollTop + document.documentElement!.scrollTop;
7171
}
7272

7373
// Find the position of the iframe this code is executing in relative to the iframe where the event was captured.

Diff for: src/vs/base/common/arrays.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function _sort<T>(a: T[], compare: Compare<T>, lo: number, hi: number, aux: T[])
138138

139139
export function groupBy<T>(data: T[], compare: (a: T, b: T) => number): T[][] {
140140
const result: T[][] = [];
141-
let currentGroup: T[];
141+
let currentGroup: T[] | undefined = undefined;
142142
for (const element of mergeSort(data.slice(0), compare)) {
143143
if (!currentGroup || compare(currentGroup[0], element) !== 0) {
144144
currentGroup = [element];
@@ -388,7 +388,9 @@ export function firstIndex<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => bo
388388
return -1;
389389
}
390390

391-
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T = null): T {
391+
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T): T;
392+
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean): T | null;
393+
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T | null = null): T | null {
392394
const index = firstIndex(array, fn);
393395
return index < 0 ? notFoundValue : array[index];
394396
}
@@ -404,7 +406,7 @@ export function commonPrefixLength<T>(one: T[], other: T[], equals: (a: T, b: T)
404406
}
405407

406408
export function flatten<T>(arr: T[][]): T[] {
407-
return [].concat(...arr);
409+
return (<T[]>[]).concat(...arr);
408410
}
409411

410412
export function range(to: number): number[];
@@ -481,15 +483,20 @@ export function arrayInsert<T>(target: T[], insertIndex: number, insertArr: T[])
481483
* Uses Fisher-Yates shuffle to shuffle the given array
482484
* @param array
483485
*/
484-
export function shuffle<T>(array: T[], seed?: number): void {
485-
// Seeded random number generator in JS. Modified from:
486-
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
487-
const random = () => {
488-
var x = Math.sin(seed++) * 179426549; // throw away most significant digits and reduce any potential bias
489-
return x - Math.floor(x);
490-
};
491-
492-
const rand = typeof seed === 'number' ? random : Math.random;
486+
export function shuffle<T>(array: T[], _seed?: number): void {
487+
let rand: () => number;
488+
489+
if (typeof _seed === 'number') {
490+
let seed = _seed;
491+
// Seeded random number generator in JS. Modified from:
492+
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
493+
rand = () => {
494+
var x = Math.sin(seed++) * 179426549; // throw away most significant digits and reduce any potential bias
495+
return x - Math.floor(x);
496+
};
497+
} else {
498+
rand = Math.random;
499+
}
493500

494501
for (let i = array.length - 1; i > 0; i -= 1) {
495502
let j = Math.floor(rand() * (i + 1));

Diff for: src/vs/base/common/async.ts

+43-21
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export interface ITask<T> {
9191
*/
9292
export class Throttler {
9393

94-
private activePromise: TPromise;
95-
private queuedPromise: TPromise;
96-
private queuedPromiseFactory: ITask<TPromise>;
94+
private activePromise: TPromise | null;
95+
private queuedPromise: TPromise | null;
96+
private queuedPromiseFactory: ITask<TPromise> | null;
9797

9898
constructor() {
9999
this.activePromise = null;
@@ -109,26 +109,26 @@ export class Throttler {
109109
const onComplete = () => {
110110
this.queuedPromise = null;
111111

112-
const result = this.queue(this.queuedPromiseFactory);
112+
const result = this.queue(this.queuedPromiseFactory!);
113113
this.queuedPromiseFactory = null;
114114

115115
return result;
116116
};
117117

118118
this.queuedPromise = new TPromise(c => {
119-
this.activePromise.then(onComplete, onComplete).then(c);
119+
this.activePromise!.then(onComplete, onComplete).then(c);
120120
});
121121
}
122122

123123
return new TPromise((c, e) => {
124-
this.queuedPromise.then(c, e);
124+
this.queuedPromise!.then(c, e);
125125
});
126126
}
127127

128128
this.activePromise = promiseFactory();
129129

130130
return new TPromise((c, e) => {
131-
this.activePromise.then((result: any) => {
131+
this.activePromise!.then((result: any) => {
132132
this.activePromise = null;
133133
c(result);
134134
}, (err: any) => {
@@ -175,10 +175,10 @@ export class SimpleThrottler {
175175
export class Delayer<T> {
176176

177177
private timeout: any;
178-
private completionPromise: TPromise;
179-
private doResolve: ValueCallback;
178+
private completionPromise: TPromise | null;
179+
private doResolve: ValueCallback | null;
180180
private doReject: (err: any) => void;
181-
private task: ITask<T | TPromise<T>>;
181+
private task: ITask<T | TPromise<T>> | null;
182182

183183
constructor(public defaultDelay: number) {
184184
this.timeout = null;
@@ -198,7 +198,7 @@ export class Delayer<T> {
198198
}).then(() => {
199199
this.completionPromise = null;
200200
this.doResolve = null;
201-
const task = this.task;
201+
const task = this.task!;
202202
this.task = null;
203203

204204
return task();
@@ -207,7 +207,7 @@ export class Delayer<T> {
207207

208208
this.timeout = setTimeout(() => {
209209
this.timeout = null;
210-
this.doResolve(null);
210+
this.doResolve!(null);
211211
}, delay);
212212

213213
return this.completionPromise;
@@ -363,11 +363,11 @@ export function sequence<T>(promiseFactories: ITask<Thenable<T>>[]): Promise<T[]
363363
return Promise.resolve(null).then(thenHandler);
364364
}
365365

366-
export function first<T>(promiseFactories: ITask<Thenable<T>>[], shouldStop: (t: T) => boolean = t => !!t, defaultValue: T = null): Promise<T> {
366+
export function first<T>(promiseFactories: ITask<Thenable<T>>[], shouldStop: (t: T) => boolean = t => !!t, defaultValue: T | null = null): Promise<T | null> {
367367
let index = 0;
368368
const len = promiseFactories.length;
369369

370-
const loop: () => Promise<T> = () => {
370+
const loop: () => Promise<T | null> = () => {
371371
if (index >= len) {
372372
return Promise.resolve(defaultValue);
373373
}
@@ -429,7 +429,7 @@ export class Limiter<T> {
429429

430430
private consume(): void {
431431
while (this.outstandingPromises.length && this.runningPromises < this.maxDegreeOfParalellism) {
432-
const iLimitedTask = this.outstandingPromises.shift();
432+
const iLimitedTask = this.outstandingPromises.shift()!;
433433
this.runningPromises++;
434434

435435
const promise = iLimitedTask.factory();
@@ -567,7 +567,7 @@ export class IntervalTimer extends Disposable {
567567

568568
export class RunOnceScheduler {
569569

570-
protected runner: (...args: any[]) => void;
570+
protected runner: ((...args: any[]) => void) | null;
571571

572572
private timeoutToken: any;
573573
private timeout: number;
@@ -621,7 +621,9 @@ export class RunOnceScheduler {
621621
}
622622

623623
protected doRun(): void {
624-
this.runner();
624+
if (this.runner) {
625+
this.runner();
626+
}
625627
}
626628
}
627629

@@ -644,7 +646,9 @@ export class RunOnceWorker<T> extends RunOnceScheduler {
644646
const units = this.units;
645647
this.units = [];
646648

647-
this.runner(units);
649+
if (this.runner) {
650+
this.runner(units);
651+
}
648652
}
649653

650654
dispose(): void {
@@ -689,12 +693,30 @@ declare function cancelIdleCallback(handle: number): void;
689693
});
690694
runWhenIdle = (runner, timeout = 0) => {
691695
let handle = setTimeout(() => runner(dummyIdle), timeout);
692-
return { dispose() { clearTimeout(handle); handle = undefined; } };
696+
let disposed = false;
697+
return {
698+
dispose() {
699+
if (disposed) {
700+
return;
701+
}
702+
disposed = true;
703+
clearTimeout(handle);
704+
}
705+
};
693706
};
694707
} else {
695708
runWhenIdle = (runner, timeout?) => {
696-
let handle = requestIdleCallback(runner, typeof timeout === 'number' ? { timeout } : undefined);
697-
return { dispose() { cancelIdleCallback(handle); handle = undefined; } };
709+
let handle: number = requestIdleCallback(runner, typeof timeout === 'number' ? { timeout } : undefined);
710+
let disposed = false;
711+
return {
712+
dispose() {
713+
if (disposed) {
714+
return;
715+
}
716+
disposed = true;
717+
cancelIdleCallback(handle);
718+
}
719+
};
698720
};
699721
}
700722
})();

Diff for: src/vs/base/common/cancellation.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export namespace CancellationToken {
5151
class MutableToken implements CancellationToken {
5252

5353
private _isCancelled: boolean = false;
54-
private _emitter: Emitter<any>;
54+
private _emitter: Emitter<any> | null = null;
5555

5656
public cancel() {
5757
if (!this._isCancelled) {
@@ -80,7 +80,7 @@ class MutableToken implements CancellationToken {
8080
public dispose(): void {
8181
if (this._emitter) {
8282
this._emitter.dispose();
83-
this._emitter = undefined;
83+
this._emitter = null;
8484
}
8585
}
8686
}

Diff for: src/vs/base/common/collections.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function size<T>(from: IStringDictionary<T> | INumberDictionary<T>): numb
4545
return count;
4646
}
4747

48-
export function first<T>(from: IStringDictionary<T> | INumberDictionary<T>): T {
48+
export function first<T>(from: IStringDictionary<T> | INumberDictionary<T>): T | undefined {
4949
for (let key in from) {
5050
if (hasOwnProperty.call(from, key)) {
5151
return from[key];

0 commit comments

Comments
 (0)