Skip to content

Commit 2848642

Browse files
committed
Strict null check paths.ts
1 parent 64c551a commit 2848642

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"./vs/base/common/json.ts",
6565
"./vs/base/common/jsonEdit.ts",
6666
"./vs/base/common/jsonFormatter.ts",
67+
"./vs/base/common/paths.ts",
6768
"./vs/base/common/uriIpc.ts",
6869
"./vs/base/node/console.ts",
6970
"./vs/base/node/crypto.ts",
@@ -73,6 +74,7 @@
7374
"./vs/base/node/flow.ts",
7475
"./vs/base/node/id.ts",
7576
"./vs/base/node/paths.ts",
77+
"./vs/base/node/paths.ts",
7678
"./vs/base/node/pfs.ts",
7779
"./vs/base/node/ports.ts",
7880
"./vs/base/node/processes.ts",
@@ -97,6 +99,7 @@
9799
"./vs/base/test/common/json.test.ts",
98100
"./vs/base/test/common/jsonEdit.test.ts",
99101
"./vs/base/test/common/jsonFormatter.test.ts",
102+
"./vs/base/test/common/paths.test.ts",
100103
"./vs/base/test/common/utils.ts",
101104
"./vs/base/test/node/processes/fixtures/fork_large.ts",
102105
"./vs/base/test/node/processes/fixtures/fork.ts",
@@ -115,6 +118,7 @@
115118
"./vs/code/electron-main/keyboard.ts",
116119
"./vs/code/electron-main/sharedProcess.ts",
117120
"./vs/code/electron-main/theme.ts",
121+
"./vs/code/node/paths.ts",
118122
"./vs/code/node/shellEnv.ts",
119123
"./vs/code/node/wait.ts",
120124
"./vs/code/node/windowsFinder.ts",

src/vs/base/common/paths.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ function _isNormal(path: string, win: boolean): boolean {
7272
: !_posixBadPath.test(path);
7373
}
7474

75-
export function normalize(path: string, toOSPath?: boolean): string {
75+
export function normalize(path: undefined, toOSPath?: boolean): undefined;
76+
export function normalize(path: null, toOSPath?: boolean): null;
77+
export function normalize(path: string, toOSPath?: boolean): string;
78+
export function normalize(path: string | null | undefined, toOSPath?: boolean): string | null | undefined {
7679

7780
if (path === null || path === void 0) {
7881
return path;
@@ -288,7 +291,7 @@ export function isUNC(path: string): boolean {
288291
// Reference: https://en.wikipedia.org/wiki/Filename
289292
const INVALID_FILE_CHARS = isWindows ? /[\\/:\*\?"<>\|]/g : /[\\/]/g;
290293
const WINDOWS_FORBIDDEN_NAMES = /^(con|prn|aux|clock\$|nul|lpt[0-9]|com[0-9])$/i;
291-
export function isValidBasename(name: string): boolean {
294+
export function isValidBasename(name: string | null | undefined): boolean {
292295
if (!name || name.length === 0 || /^\s+$/.test(name)) {
293296
return false; // require a name that is not just whitespace
294297
}

src/vs/base/common/strings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function escapeRegExpCharacters(value: string): string {
7878
* @param haystack string to trim
7979
* @param needle the thing to trim (default is a blank)
8080
*/
81-
export function trim(haystack: string, needle: string = ' '): string | undefined {
81+
export function trim(haystack: string, needle: string = ' '): string {
8282
let trimmed = ltrim(haystack, needle);
8383
return rtrim(trimmed, needle);
8484
}

src/vs/code/node/paths.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function doValidatePaths(args: string[], gotoLineMode?: boolean): string[] {
3535
const result = args.map(arg => {
3636
let pathCandidate = String(arg);
3737

38-
let parsedPath: IPathWithLineAndColumn;
38+
let parsedPath: IPathWithLineAndColumn | undefined = undefined;
3939
if (gotoLineMode) {
4040
parsedPath = parseLineAndColumnAware(pathCandidate);
4141
pathCandidate = parsedPath.path;
@@ -52,7 +52,7 @@ function doValidatePaths(args: string[], gotoLineMode?: boolean): string[] {
5252
return null; // do not allow invalid file names
5353
}
5454

55-
if (gotoLineMode) {
55+
if (gotoLineMode && parsedPath) {
5656
parsedPath.path = sanitizedFilePath;
5757

5858
return toPath(parsedPath);
@@ -62,7 +62,7 @@ function doValidatePaths(args: string[], gotoLineMode?: boolean): string[] {
6262
});
6363

6464
const caseInsensitive = platform.isWindows || platform.isMacintosh;
65-
const distinct = arrays.distinct(result, e => e && caseInsensitive ? e.toLowerCase() : e);
65+
const distinct = arrays.distinct(result, e => e && caseInsensitive ? e.toLowerCase() : (e || ''));
6666

6767
return arrays.coalesce(distinct);
6868
}
@@ -98,7 +98,7 @@ export interface IPathWithLineAndColumn {
9898
export function parseLineAndColumnAware(rawPath: string): IPathWithLineAndColumn {
9999
const segments = rawPath.split(':'); // C:\file.txt:<line>:<column>
100100

101-
let path: string;
101+
let path: string | null = null;
102102
let line: number | null = null;
103103
let column: number | null = null;
104104

0 commit comments

Comments
 (0)