Skip to content

Commit e092610

Browse files
committed
undefined is preferred
1 parent 892e734 commit e092610

File tree

6 files changed

+38
-46
lines changed

6 files changed

+38
-46
lines changed

src/authentication/keychain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export type GlobalStateContext = { globalState: vscode.Memento };
3939
const SERVICE_ID = 'vscode-pull-request-github';
4040
export const ALL_HOSTS_KEY = 'keychain::all';
4141

42-
let defaultStorage: vscode.Memento | null = null;
43-
let defaultKeychain: Keytar | null = null;
42+
let defaultStorage: vscode.Memento | undefined = undefined;
43+
let defaultKeychain: Keytar | undefined = undefined;
4444

4545
const didChange = new vscode.EventEmitter<IHostConfiguration>();
4646
export const onDidChange = didChange.event;

src/commands.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { Comment } from './common/comment';
2525
import { PullRequestManager } from './github/pullRequestManager';
2626
import { PullRequestModel } from './github/pullRequestModel';
2727

28-
const _onDidUpdatePR = new vscode.EventEmitter<PullRequest | null>();
29-
export const onDidUpdatePR: vscode.Event<PullRequest | null> = _onDidUpdatePR.event;
28+
const _onDidUpdatePR = new vscode.EventEmitter<PullRequest | undefined>();
29+
export const onDidUpdatePR: vscode.Event<PullRequest | undefined> = _onDidUpdatePR.event;
3030

3131
function ensurePR(prManager: PullRequestManager, pr?: PRNode | PullRequestModel): PullRequestModel {
3232
// If the command is called from the command palette, no arguments are passed.
@@ -65,7 +65,7 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: Pu
6565

6666
context.subscriptions.push(vscode.commands.registerCommand('review.suggestDiff', async (e) => {
6767
try {
68-
if (prManager.activePullRequest === null) {
68+
if (!prManager.activePullRequest) {
6969
return;
7070
}
7171

@@ -224,7 +224,7 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: Pu
224224
return vscode.window.showWarningMessage(`Are you sure you want to close this pull request on GitHub? This will close the pull request without merging.`, 'Yes', 'No').then(async value => {
225225
if (value === 'Yes') {
226226
try {
227-
let newComment: Comment | null = null;
227+
let newComment: Comment | undefined = undefined;
228228
if (message) {
229229
newComment = await prManager.createIssueComment(pullRequest, message);
230230
}
@@ -235,11 +235,11 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: Pu
235235
return newComment;
236236
} catch (e) {
237237
vscode.window.showErrorMessage(`Unable to close pull request. ${formatError(e)}`);
238-
_onDidUpdatePR.fire(null);
238+
_onDidUpdatePR.fire();
239239
}
240240
}
241241

242-
_onDidUpdatePR.fire(null);
242+
_onDidUpdatePR.fire();
243243
});
244244
}));
245245

src/common/diffHunk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
9797
let lineReader = LineReader(diffHunkPatch);
9898

9999
let itr = lineReader.next();
100-
let diffHunk: DiffHunk | null = null;
100+
let diffHunk: DiffHunk | undefined = undefined;
101101
let positionInHunk = -1;
102102
let oldLine = -1;
103103
let newLine = -1;
@@ -107,7 +107,7 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
107107
if (DIFF_HUNK_HEADER.test(line)) {
108108
if (diffHunk) {
109109
yield diffHunk;
110-
diffHunk = null;
110+
diffHunk = undefined;
111111
}
112112

113113
if (positionInHunk === -1) {
@@ -125,7 +125,7 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
125125
diffHunk = new DiffHunk(oriStartLine, oriLen, newStartLine, newLen, positionInHunk);
126126
// @rebornix todo, once we have enough tests, this should be removed.
127127
diffHunk.diffLines.push(new DiffLine(DiffChangeType.Control, -1, -1, positionInHunk, line));
128-
} else if (diffHunk !== null) {
128+
} else if (diffHunk) {
129129
let type = getDiffChangeType(line);
130130

131131
if (type === DiffChangeType.Control) {

src/common/telemetry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Telemetry implements ITelemetry {
1212
private _version: string;
1313
private _telemetry: StatsStore;
1414
constructor(private readonly _context: vscode.ExtensionContext) {
15-
this._version = vscode.extensions.getExtension(EXTENSION_ID).packageJSON.version;
15+
this._version = vscode.extensions.getExtension(EXTENSION_ID)!.packageJSON.version;
1616
const database = new MementoDatabase(this._context, () => this._telemetry.createReport());
1717
this._telemetry = new StatsStore(AppName.VSCode, this._version,
1818
() => '',
@@ -40,7 +40,7 @@ class VSSettings implements ISettings {
4040
this._config = vscode.workspace.getConfiguration('githubPullRequests.telemetry');
4141

4242
const deprecated = vscode.workspace.getConfiguration('telemetry');
43-
const { globalValue, workspaceFolderValue, workspaceValue } = deprecated.inspect(DEPRECATED_CONFIG_SECTION);
43+
const { globalValue, workspaceFolderValue, workspaceValue } = deprecated.inspect(DEPRECATED_CONFIG_SECTION)!;
4444
const values = [
4545
{ target: vscode.ConfigurationTarget.Global, value: globalValue },
4646
{ target: vscode.ConfigurationTarget.WorkspaceFolder, value: workspaceFolderValue },
@@ -56,7 +56,7 @@ class VSSettings implements ISettings {
5656
});
5757
}
5858
}
59-
getItem(key: string): Promise<string> {
59+
getItem(key: string): Promise<string | undefined> {
6060
switch (key) {
6161
case 'last-daily-stats-report':
6262
return Promise.resolve(this._context.globalState.get<string>(`${TELEMETRY_KEY}.last`));

src/common/uri.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ export interface PRUriParams {
3030
status: GitChangeType;
3131
}
3232

33-
export function fromPRUri(uri: Uri): PRUriParams | null {
33+
export function fromPRUri(uri: Uri): PRUriParams | undefined {
3434
try {
3535
return JSON.parse(uri.query) as PRUriParams;
36-
} catch (e) {
37-
return null;
38-
}
36+
} catch (e) { }
3937
}
4038

4139
export interface GitUriOptions {
@@ -86,12 +84,10 @@ export function toFileChangeNodeUri(uri: Uri, hasComments: boolean, status: GitC
8684
});
8785
}
8886

89-
export function fromFileChangeNodeUri(uri: Uri): FileChangeNodeUriParams | null {
87+
export function fromFileChangeNodeUri(uri: Uri): FileChangeNodeUriParams | undefined {
9088
try {
9189
return JSON.parse(uri.query) as FileChangeNodeUriParams;
92-
} catch (e) {
93-
return null;
94-
}
90+
} catch (e) { }
9591
}
9692

9793
export function toPRUri(uri: Uri, pullRequestModel: PullRequestModel, baseCommit: string, headCommit: string, fileName: string, base: boolean, status: GitChangeType): Uri {

src/view/reviewManager.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class ReviewManager implements vscode.DecorationProvider {
239239
private async validateState() {
240240
await this._prManager.updateRepositories();
241241

242-
if (this._repository.state.HEAD === undefined) {
242+
if (!this._repository.state.HEAD) {
243243
this.clear(true);
244244
return;
245245
}
@@ -269,12 +269,12 @@ export class ReviewManager implements vscode.DecorationProvider {
269269
Logger.appendLine(`Review> current branch ${this._repository.state.HEAD.name} is associated with pull request #${matchingPullRequestMetadata.prNumber}`);
270270
this.clear(false);
271271
this._prNumber = matchingPullRequestMetadata.prNumber;
272-
this._lastCommitSha = null;
272+
this._lastCommitSha = undefined;
273273

274274
const { owner, repositoryName } = matchingPullRequestMetadata;
275275
const pr = await this._prManager.resolvePullRequest(owner, repositoryName, matchingPullRequestMetadata.prNumber);
276276
if (!pr) {
277-
this._prNumber = null;
277+
this._prNumber = undefined;
278278
Logger.appendLine('Review> This PR is no longer valid');
279279
return;
280280
}
@@ -334,7 +334,7 @@ export class ReviewManager implements vscode.DecorationProvider {
334334

335335
private async replyToCommentThread(document: vscode.TextDocument, range: vscode.Range, thread: vscode.CommentThread, text: string) {
336336
try {
337-
if (this._prManager.activePullRequest === null) {
337+
if (!this._prManager.activePullRequest) {
338338
throw new Error('Unable to find active pull request');
339339
}
340340
const matchedFile = this.findMatchedFileByUri(document);
@@ -381,11 +381,11 @@ export class ReviewManager implements vscode.DecorationProvider {
381381
const query = uri.query === '' ? undefined : fromReviewUri(uri);
382382
const isBase = query && query.base;
383383

384-
if (matchedFile === null) {
384+
if (!matchedFile) {
385385
throw new Error(`Cannot find document ${uri.toString()}`);
386386
}
387387

388-
if (this._lastCommitSha === null) {
388+
if (!this._lastCommitSha) {
389389
throw new Error('Last commit sha can not be null');
390390
}
391391
// git diff sha -- fileName
@@ -434,7 +434,7 @@ export class ReviewManager implements vscode.DecorationProvider {
434434

435435
private async editComment(document: vscode.TextDocument, comment: vscode.Comment, text: string): Promise<void> {
436436
try {
437-
if (this._prManager.activePullRequest === null) {
437+
if (!this._prManager.activePullRequest) {
438438
throw new Error('Unable to find active pull request');
439439
}
440440

@@ -470,7 +470,7 @@ export class ReviewManager implements vscode.DecorationProvider {
470470

471471
private async deleteComment(document: vscode.TextDocument, comment: vscode.Comment): Promise<void> {
472472
try {
473-
if (this._prManager.activePullRequest === null) {
473+
if (!this._prManager.activePullRequest) {
474474
throw new Error('Unable to find active pull request');
475475
}
476476

@@ -533,7 +533,7 @@ export class ReviewManager implements vscode.DecorationProvider {
533533
const remote = branch.upstream ? branch.upstream.remote : null;
534534
if (!remote) { return; }
535535

536-
if (this._prNumber === null || this._prManager.activePullRequest === null) {
536+
if (this._prNumber === undefined || !this._prManager.activePullRequest) {
537537
return;
538538
}
539539

@@ -1051,7 +1051,7 @@ export class ReviewManager implements vscode.DecorationProvider {
10511051
}
10521052

10531053
private async startDraft(_document: vscode.TextDocument, _token: vscode.CancellationToken): Promise<void> {
1054-
if (this._prManager.activePullRequest === null) {
1054+
if (!this._prManager.activePullRequest) {
10551055
throw new Error('Unable to find active pull request');
10561056
}
10571057

@@ -1065,7 +1065,7 @@ export class ReviewManager implements vscode.DecorationProvider {
10651065
}
10661066

10671067
private async deleteDraft(_document: vscode.TextDocument, _token: vscode.CancellationToken) {
1068-
if (this._prManager.activePullRequest === null) {
1068+
if (!this._prManager.activePullRequest) {
10691069
throw new Error('Unable to find active pull request');
10701070
}
10711071

@@ -1111,7 +1111,7 @@ export class ReviewManager implements vscode.DecorationProvider {
11111111
}
11121112

11131113
private async finishDraft(_document: vscode.TextDocument, _token: vscode.CancellationToken) {
1114-
if (this._prManager.activePullRequest === null) {
1114+
if (!this._prManager.activePullRequest) {
11151115
throw new Error('Unable to find active pull request');
11161116
}
11171117

@@ -1148,7 +1148,7 @@ export class ReviewManager implements vscode.DecorationProvider {
11481148
}
11491149
}
11501150

1151-
private findMatchedFileChange(fileChanges: (GitFileChangeNode | RemoteFileChangeNode)[], uri: vscode.Uri): GitFileChangeNode | null {
1151+
private findMatchedFileChange(fileChanges: (GitFileChangeNode | RemoteFileChangeNode)[], uri: vscode.Uri): GitFileChangeNode | undefined {
11521152
let query = fromReviewUri(uri);
11531153
let matchedFiles = fileChanges.filter(fileChange => {
11541154
if (fileChange instanceof RemoteFileChangeNode) {
@@ -1176,8 +1176,6 @@ export class ReviewManager implements vscode.DecorationProvider {
11761176
if (matchedFiles && matchedFiles.length) {
11771177
return matchedFiles[0] as GitFileChangeNode;
11781178
}
1179-
1180-
return null;
11811179
}
11821180

11831181
public async switch(pr: PullRequestModel): Promise<void> {
@@ -1281,10 +1279,10 @@ export class ReviewManager implements vscode.DecorationProvider {
12811279
});
12821280
}
12831281

1284-
private async getRemote(potentialTargetRemotes: Remote[], placeHolder: string, defaultUpstream?: RemoteQuickPickItem): Promise<RemoteQuickPickItem | null> {
1282+
private async getRemote(potentialTargetRemotes: Remote[], placeHolder: string, defaultUpstream?: RemoteQuickPickItem): Promise<RemoteQuickPickItem | undefined> {
12851283
if (!potentialTargetRemotes.length) {
12861284
vscode.window.showWarningMessage(`No GitHub remotes found. Add a remote and try again.`);
1287-
return null;
1285+
return;
12881286
}
12891287

12901288
if (potentialTargetRemotes.length === 1 && !defaultUpstream) {
@@ -1320,7 +1318,7 @@ export class ReviewManager implements vscode.DecorationProvider {
13201318
});
13211319

13221320
if (!selected) {
1323-
return null;
1321+
return;
13241322
}
13251323

13261324
return selected;
@@ -1360,7 +1358,7 @@ export class ReviewManager implements vscode.DecorationProvider {
13601358
cancellable: false
13611359
}, async (progress) => {
13621360
progress.report({ increment: 10 });
1363-
let HEAD: Branch | null = this._repository.state.HEAD!;
1361+
let HEAD: Branch | undefined = this._repository.state.HEAD!;
13641362
const branchName = HEAD.name;
13651363

13661364
if (!HEAD.upstream) {
@@ -1411,8 +1409,8 @@ export class ReviewManager implements vscode.DecorationProvider {
14111409
}
14121410

14131411
if (quitReviewMode) {
1414-
this._prNumber = null;
1415-
this._prManager.activePullRequest = null;
1412+
this._prNumber = undefined;
1413+
this._prManager.activePullRequest = undefined;
14161414

14171415
if (this._statusBarItem) {
14181416
this._statusBarItem.hide();
@@ -1432,7 +1430,7 @@ export class ReviewManager implements vscode.DecorationProvider {
14321430
}
14331431
}
14341432

1435-
async provideTextDocumentContent(uri: vscode.Uri): Promise<string | null> {
1433+
async provideTextDocumentContent(uri: vscode.Uri): Promise<string | undefined> {
14361434
let { path, commit } = fromReviewUri(uri);
14371435
let changedItems = gitFileChangeNodeFilter(this._localFileChanges)
14381436
.filter(change => change.fileName === path)
@@ -1471,8 +1469,6 @@ export class ReviewManager implements vscode.DecorationProvider {
14711469

14721470
return ret.join('\n');
14731471
}
1474-
1475-
return null;
14761472
}
14771473

14781474
dispose() {

0 commit comments

Comments
 (0)