Skip to content

Commit 3af9e45

Browse files
committed
labels: strict null checks
1 parent 3ba3266 commit 3af9e45

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"./vs/base/common/severity.ts",
5959
"./vs/base/common/stopwatch.ts",
6060
"./vs/base/common/strings.ts",
61+
"./vs/base/common/labels.ts",
6162
"./vs/base/common/types.ts",
6263
"./vs/base/common/uri.ts",
6364
"./vs/base/common/uriIpc.ts",
@@ -134,4 +135,4 @@
134135
"exclude": [
135136
"./typings/require-monaco.d.ts"
136137
]
137-
}
138+
}

src/vs/base/common/labels.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,35 @@ export interface IUserHomeProvider {
2424
/**
2525
* @deprecated use LabelService instead
2626
*/
27-
export function getPathLabel(resource: URI | string, userHomeProvider: IUserHomeProvider, rootProvider?: IWorkspaceFolderProvider): string {
27+
export function getPathLabel(resource: URI | string, userHomeProvider: IUserHomeProvider, rootProvider?: IWorkspaceFolderProvider): string | undefined {
2828
if (!resource) {
29-
return null;
29+
return undefined;
3030
}
3131

3232
if (typeof resource === 'string') {
3333
resource = URI.file(resource);
3434
}
3535

3636
// return early if we can resolve a relative path label from the root
37-
const baseResource = rootProvider ? rootProvider.getWorkspaceFolder(resource) : null;
38-
if (baseResource) {
39-
const hasMultipleRoots = rootProvider.getWorkspace().folders.length > 1;
40-
41-
let pathLabel: string;
42-
if (isEqual(baseResource.uri, resource, !isLinux)) {
43-
pathLabel = ''; // no label if paths are identical
44-
} else {
45-
pathLabel = normalize(ltrim(resource.path.substr(baseResource.uri.path.length), sep), true);
46-
}
37+
if (rootProvider) {
38+
const baseResource = rootProvider.getWorkspaceFolder(resource);
39+
if (baseResource) {
40+
const hasMultipleRoots = rootProvider.getWorkspace().folders.length > 1;
41+
42+
let pathLabel: string;
43+
if (isEqual(baseResource.uri, resource, !isLinux)) {
44+
pathLabel = ''; // no label if paths are identical
45+
} else {
46+
pathLabel = normalize(ltrim(resource.path.substr(baseResource.uri.path.length), sep)!, true);
47+
}
4748

48-
if (hasMultipleRoots) {
49-
const rootName = (baseResource && baseResource.name) ? baseResource.name : pathsBasename(baseResource.uri.fsPath);
50-
pathLabel = pathLabel ? (rootName + ' • ' + pathLabel) : rootName; // always show root basename if there are multiple
51-
}
49+
if (hasMultipleRoots) {
50+
const rootName = (baseResource && baseResource.name) ? baseResource.name : pathsBasename(baseResource.uri.fsPath);
51+
pathLabel = pathLabel ? (rootName + ' • ' + pathLabel) : rootName; // always show root basename if there are multiple
52+
}
5253

53-
return pathLabel;
54+
return pathLabel;
55+
}
5456
}
5557

5658
// return if the resource is neither file:// nor untitled:// and no baseResource was provided
@@ -72,9 +74,9 @@ export function getPathLabel(resource: URI | string, userHomeProvider: IUserHome
7274
return res;
7375
}
7476

75-
export function getBaseLabel(resource: URI | string): string {
77+
export function getBaseLabel(resource: URI | string): string | undefined {
7678
if (!resource) {
77-
return null;
79+
return undefined;
7880
}
7981

8082
if (typeof resource === 'string') {
@@ -92,7 +94,7 @@ export function getBaseLabel(resource: URI | string): string {
9294
}
9395

9496
function hasDriveLetter(path: string): boolean {
95-
return isWindows && path && path[1] === ':';
97+
return !!(isWindows && path && path[1] === ':');
9698
}
9799

98100
export function normalizeDriveLetter(path: string): string {

0 commit comments

Comments
 (0)