Skip to content

Commit da546f5

Browse files
committed
Add null checks in filters and context keys
1 parent 0c403f8 commit da546f5

3 files changed

Lines changed: 44 additions & 42 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"./vs/base/common/errors.ts",
3737
"./vs/base/common/errorsWithActions.ts",
3838
"./vs/base/common/event.ts",
39+
"./vs/base/common/filters.ts",
3940
"./vs/base/common/functional.ts",
4041
"./vs/base/common/glob.ts",
4142
"./vs/base/common/hash.ts",
@@ -105,6 +106,7 @@
105106
"./vs/editor/common/model.ts",
106107
"./vs/editor/common/model/editStack.ts",
107108
"./vs/editor/common/model/intervalTree.ts",
109+
"./vs/editor/common/editorContextKeys.ts",
108110
"./vs/editor/common/model/mirrorTextModel.ts",
109111
"./vs/editor/common/model/pieceTreeTextBuffer/pieceTreeBase.ts",
110112
"./vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBuffer.ts",
@@ -170,6 +172,7 @@
170172
"./vs/platform/quickOpen/common/quickOpen.ts",
171173
"./vs/platform/quickinput/common/quickInput.ts",
172174
"./vs/platform/registry/common/platform.ts",
175+
"./vs/platform/search/common/search.ts",
173176
"./vs/platform/state/common/state.ts",
174177
"./vs/platform/statusbar/common/statusbar.ts",
175178
"./vs/platform/telemetry/common/telemetry.ts",
@@ -211,8 +214,7 @@
211214
"./vs/workbench/services/panel/common/panelService.ts",
212215
"./vs/workbench/services/search/node/search.ts",
213216
"./vs/workbench/services/title/common/titleService.ts",
214-
"./vs/workbench/test/electron-browser/api/mock.ts",
215-
"vs/platform/search/common/search.ts"
217+
"./vs/workbench/test/electron-browser/api/mock.ts"
216218
],
217219
"exclude": [
218220
"./typings/require-monaco.d.ts"

src/vs/base/common/filters.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as strings from 'vs/base/common/strings';
99

1010
export interface IFilter {
1111
// Returns null if word doesn't match.
12-
(word: string, wordToMatchAgainst: string): IMatch[];
12+
(word: string, wordToMatchAgainst: string): IMatch[] | null;
1313
}
1414

1515
export interface IMatch {
@@ -26,7 +26,7 @@ export interface IMatch {
2626
* filter.
2727
*/
2828
export function or(...filter: IFilter[]): IFilter {
29-
return function (word: string, wordToMatchAgainst: string): IMatch[] {
29+
return function (word: string, wordToMatchAgainst: string): IMatch[] | null {
3030
for (let i = 0, len = filter.length; i < len; i++) {
3131
let match = filter[i](word, wordToMatchAgainst);
3232
if (match) {
@@ -42,7 +42,7 @@ export function or(...filter: IFilter[]): IFilter {
4242
export const matchesStrictPrefix: IFilter = _matchesPrefix.bind(undefined, false);
4343
export const matchesPrefix: IFilter = _matchesPrefix.bind(undefined, true);
4444

45-
function _matchesPrefix(ignoreCase: boolean, word: string, wordToMatchAgainst: string): IMatch[] {
45+
function _matchesPrefix(ignoreCase: boolean, word: string, wordToMatchAgainst: string): IMatch[] | null {
4646
if (!wordToMatchAgainst || wordToMatchAgainst.length < word.length) {
4747
return null;
4848
}
@@ -63,7 +63,7 @@ function _matchesPrefix(ignoreCase: boolean, word: string, wordToMatchAgainst: s
6363

6464
// Contiguous Substring
6565

66-
export function matchesContiguousSubString(word: string, wordToMatchAgainst: string): IMatch[] {
66+
export function matchesContiguousSubString(word: string, wordToMatchAgainst: string): IMatch[] | null {
6767
let index = wordToMatchAgainst.toLowerCase().indexOf(word.toLowerCase());
6868
if (index === -1) {
6969
return null;
@@ -74,18 +74,18 @@ export function matchesContiguousSubString(word: string, wordToMatchAgainst: str
7474

7575
// Substring
7676

77-
export function matchesSubString(word: string, wordToMatchAgainst: string): IMatch[] {
77+
export function matchesSubString(word: string, wordToMatchAgainst: string): IMatch[] | null {
7878
return _matchesSubString(word.toLowerCase(), wordToMatchAgainst.toLowerCase(), 0, 0);
7979
}
8080

81-
function _matchesSubString(word: string, wordToMatchAgainst: string, i: number, j: number): IMatch[] {
81+
function _matchesSubString(word: string, wordToMatchAgainst: string, i: number, j: number): IMatch[] | null {
8282
if (i === word.length) {
8383
return [];
8484
} else if (j === wordToMatchAgainst.length) {
8585
return null;
8686
} else {
8787
if (word[i] === wordToMatchAgainst[j]) {
88-
let result: IMatch[] = null;
88+
let result: IMatch[] | null = null;
8989
if (result = _matchesSubString(word, wordToMatchAgainst, i + 1, j + 1)) {
9090
return join({ start: j, end: j + 1 }, result);
9191
}
@@ -144,15 +144,15 @@ function nextAnchor(camelCaseWord: string, start: number): number {
144144
return camelCaseWord.length;
145145
}
146146

147-
function _matchesCamelCase(word: string, camelCaseWord: string, i: number, j: number): IMatch[] {
147+
function _matchesCamelCase(word: string, camelCaseWord: string, i: number, j: number): IMatch[] | null {
148148
if (i === word.length) {
149149
return [];
150150
} else if (j === camelCaseWord.length) {
151151
return null;
152152
} else if (word[i] !== camelCaseWord[j].toLowerCase()) {
153153
return null;
154154
} else {
155-
let result: IMatch[] = null;
155+
let result: IMatch[] | null = null;
156156
let nextUpperIndex = j + 1;
157157
result = _matchesCamelCase(word, camelCaseWord, i + 1, j + 1);
158158
while (!result && (nextUpperIndex = nextAnchor(camelCaseWord, nextUpperIndex)) < camelCaseWord.length) {
@@ -222,7 +222,7 @@ function isCamelCasePattern(word: string): boolean {
222222
}
223223
}
224224

225-
export function matchesCamelCase(word: string, camelCaseWord: string): IMatch[] {
225+
export function matchesCamelCase(word: string, camelCaseWord: string): IMatch[] | null {
226226
if (!camelCaseWord) {
227227
return null;
228228
}
@@ -251,7 +251,7 @@ export function matchesCamelCase(word: string, camelCaseWord: string): IMatch[]
251251
camelCaseWord = camelCaseWord.toLowerCase();
252252
}
253253

254-
let result: IMatch[] = null;
254+
let result: IMatch[] | null = null;
255255
let i = 0;
256256

257257
word = word.toLowerCase();
@@ -267,12 +267,12 @@ export function matchesCamelCase(word: string, camelCaseWord: string): IMatch[]
267267
// Otherwise also matches sub string of the word with beginnings of the words in the target. E.g. "gp" or "g p" will match "Git: Pull"
268268
// Useful in cases where the target is words (e.g. command labels)
269269

270-
export function matchesWords(word: string, target: string, contiguous: boolean = false): IMatch[] {
270+
export function matchesWords(word: string, target: string, contiguous: boolean = false): IMatch[] | null {
271271
if (!target || target.length === 0) {
272272
return null;
273273
}
274274

275-
let result: IMatch[] = null;
275+
let result: IMatch[] | null = null;
276276
let i = 0;
277277

278278
word = word.toLowerCase();
@@ -284,15 +284,15 @@ export function matchesWords(word: string, target: string, contiguous: boolean =
284284
return result;
285285
}
286286

287-
function _matchesWords(word: string, target: string, i: number, j: number, contiguous: boolean): IMatch[] {
287+
function _matchesWords(word: string, target: string, i: number, j: number, contiguous: boolean): IMatch[] | null {
288288
if (i === word.length) {
289289
return [];
290290
} else if (j === target.length) {
291291
return null;
292292
} else if (word[i] !== target[j]) {
293293
return null;
294294
} else {
295-
let result: IMatch[] = null;
295+
let result: IMatch[] | null = null;
296296
let nextWordIndex = j + 1;
297297
result = _matchesWords(word, target, i + 1, j + 1, contiguous);
298298
if (!contiguous) {
@@ -321,7 +321,7 @@ export const fuzzyContiguousFilter = or(matchesPrefix, matchesCamelCase, matches
321321
const fuzzySeparateFilter = or(matchesPrefix, matchesCamelCase, matchesSubString);
322322
const fuzzyRegExpCache = new LRUCache<string, RegExp>(10000); // bounded to 10000 elements
323323

324-
export function matchesFuzzy(word: string, wordToMatchAgainst: string, enableSeparateSubstringMatching = false): IMatch[] {
324+
export function matchesFuzzy(word: string, wordToMatchAgainst: string, enableSeparateSubstringMatching = false): IMatch[] | null {
325325
if (typeof word !== 'string' || typeof wordToMatchAgainst !== 'string') {
326326
return null; // return early for invalid input
327327
}
@@ -334,7 +334,7 @@ export function matchesFuzzy(word: string, wordToMatchAgainst: string, enableSep
334334
}
335335

336336
// RegExp Filter
337-
let match: RegExpExecArray = regexp.exec(wordToMatchAgainst);
337+
let match = regexp.exec(wordToMatchAgainst);
338338
if (match) {
339339
return [{ start: match.index, end: match.index + match[0].length }];
340340
}
@@ -372,7 +372,7 @@ export function createMatches(offsetOrScore: number[] | FuzzyScore): IMatch[] {
372372
} else {
373373
offsets = offsetOrScore as number[];
374374
}
375-
let last: IMatch;
375+
let last: IMatch | undefined;
376376
for (const pos of offsets) {
377377
if (last && last.end === pos) {
378378
last.end += 1;
@@ -466,7 +466,7 @@ export interface FuzzyScorer {
466466
(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore;
467467
}
468468

469-
export function fuzzyScore(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore {
469+
export function fuzzyScore(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore | undefined {
470470

471471
const patternLen = pattern.length > 100 ? 100 : pattern.length;
472472
const wordLen = word.length > 100 ? 100 : word.length;
@@ -711,16 +711,16 @@ class LazyArray {
711711

712712
//#region --- graceful ---
713713

714-
export function fuzzyScoreGracefulAggressive(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore {
714+
export function fuzzyScoreGracefulAggressive(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore | undefined {
715715
return fuzzyScoreWithPermutations(pattern, lowPattern, patternPos, word, lowWord, wordPos, true, firstMatchCanBeWeak);
716716
}
717717

718-
export function fuzzyScoreGraceful(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore {
718+
export function fuzzyScoreGraceful(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, firstMatchCanBeWeak: boolean): FuzzyScore | undefined {
719719
return fuzzyScoreWithPermutations(pattern, lowPattern, patternPos, word, lowWord, wordPos, false, firstMatchCanBeWeak);
720720
}
721721

722-
function fuzzyScoreWithPermutations(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, aggressive: boolean, firstMatchCanBeWeak: boolean): FuzzyScore {
723-
let top: [number, number[]] = fuzzyScore(pattern, lowPattern, patternPos, word, lowWord, wordPos, firstMatchCanBeWeak);
722+
function fuzzyScoreWithPermutations(pattern: string, lowPattern: string, patternPos: number, word: string, lowWord: string, wordPos: number, aggressive: boolean, firstMatchCanBeWeak: boolean): FuzzyScore | undefined {
723+
let top = fuzzyScore(pattern, lowPattern, patternPos, word, lowWord, wordPos, firstMatchCanBeWeak);
724724

725725
if (top && !aggressive) {
726726
// when using the original pattern yield a result we`
@@ -752,7 +752,7 @@ function fuzzyScoreWithPermutations(pattern: string, lowPattern: string, pattern
752752
return top;
753753
}
754754

755-
function nextTypoPermutation(pattern: string, patternPos: number): string {
755+
function nextTypoPermutation(pattern: string, patternPos: number): string | undefined {
756756

757757
if (patternPos + 1 >= pattern.length) {
758758
return undefined;

src/vs/editor/common/editorContextKeys.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ export namespace EditorContextKeys {
2828
export const hasSingleSelection: ContextKeyExpr = hasMultipleSelections.toNegated();
2929
export const tabMovesFocus = new RawContextKey<boolean>('editorTabMovesFocus', false);
3030
export const tabDoesNotMoveFocus: ContextKeyExpr = tabMovesFocus.toNegated();
31-
export const isInEmbeddedEditor = new RawContextKey<boolean>('isInEmbeddedEditor', undefined);
31+
export const isInEmbeddedEditor = new RawContextKey<boolean>('isInEmbeddedEditor', false);
3232
export const canUndo = new RawContextKey<boolean>('canUndo', false);
3333
export const canRedo = new RawContextKey<boolean>('canRedo', false);
3434

3535
// -- mode context keys
36-
export const languageId = new RawContextKey<string>('editorLangId', undefined);
37-
export const hasCompletionItemProvider = new RawContextKey<boolean>('editorHasCompletionItemProvider', undefined);
38-
export const hasCodeActionsProvider = new RawContextKey<boolean>('editorHasCodeActionsProvider', undefined);
39-
export const hasCodeLensProvider = new RawContextKey<boolean>('editorHasCodeLensProvider', undefined);
40-
export const hasDefinitionProvider = new RawContextKey<boolean>('editorHasDefinitionProvider', undefined);
41-
export const hasImplementationProvider = new RawContextKey<boolean>('editorHasImplementationProvider', undefined);
42-
export const hasTypeDefinitionProvider = new RawContextKey<boolean>('editorHasTypeDefinitionProvider', undefined);
43-
export const hasHoverProvider = new RawContextKey<boolean>('editorHasHoverProvider', undefined);
44-
export const hasDocumentHighlightProvider = new RawContextKey<boolean>('editorHasDocumentHighlightProvider', undefined);
45-
export const hasDocumentSymbolProvider = new RawContextKey<boolean>('editorHasDocumentSymbolProvider', undefined);
46-
export const hasReferenceProvider = new RawContextKey<boolean>('editorHasReferenceProvider', undefined);
47-
export const hasRenameProvider = new RawContextKey<boolean>('editorHasRenameProvider', undefined);
48-
export const hasDocumentFormattingProvider = new RawContextKey<boolean>('editorHasDocumentFormattingProvider', undefined);
49-
export const hasDocumentSelectionFormattingProvider = new RawContextKey<boolean>('editorHasDocumentSelectionFormattingProvider', undefined);
50-
export const hasSignatureHelpProvider = new RawContextKey<boolean>('editorHasSignatureHelpProvider', undefined);
36+
export const languageId = new RawContextKey<string>('editorLangId', '');
37+
export const hasCompletionItemProvider = new RawContextKey<boolean>('editorHasCompletionItemProvider', false);
38+
export const hasCodeActionsProvider = new RawContextKey<boolean>('editorHasCodeActionsProvider', false);
39+
export const hasCodeLensProvider = new RawContextKey<boolean>('editorHasCodeLensProvider', false);
40+
export const hasDefinitionProvider = new RawContextKey<boolean>('editorHasDefinitionProvider', false);
41+
export const hasImplementationProvider = new RawContextKey<boolean>('editorHasImplementationProvider', false);
42+
export const hasTypeDefinitionProvider = new RawContextKey<boolean>('editorHasTypeDefinitionProvider', false);
43+
export const hasHoverProvider = new RawContextKey<boolean>('editorHasHoverProvider', false);
44+
export const hasDocumentHighlightProvider = new RawContextKey<boolean>('editorHasDocumentHighlightProvider', false);
45+
export const hasDocumentSymbolProvider = new RawContextKey<boolean>('editorHasDocumentSymbolProvider', false);
46+
export const hasReferenceProvider = new RawContextKey<boolean>('editorHasReferenceProvider', false);
47+
export const hasRenameProvider = new RawContextKey<boolean>('editorHasRenameProvider', false);
48+
export const hasDocumentFormattingProvider = new RawContextKey<boolean>('editorHasDocumentFormattingProvider', false);
49+
export const hasDocumentSelectionFormattingProvider = new RawContextKey<boolean>('editorHasDocumentSelectionFormattingProvider', false);
50+
export const hasSignatureHelpProvider = new RawContextKey<boolean>('editorHasSignatureHelpProvider', false);
5151
}

0 commit comments

Comments
 (0)