Skip to content

Commit 03d14fc

Browse files
committed
Changes to length === 0 for better safety
1 parent c1c8c75 commit 03d14fc

File tree

12 files changed

+33
-33
lines changed

12 files changed

+33
-33
lines changed

src/commands/diffWith.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
135135
if (GitService.isUncommitted(args.rhs.sha)) {
136136
rhsSuffix = 'deleted';
137137
}
138-
else if (rhsSuffix === '' && args.rhs.sha === GitService.deletedOrMissingSha) {
138+
else if (rhsSuffix.length === 0 && args.rhs.sha === GitService.deletedOrMissingSha) {
139139
rhsSuffix = 'not in Working Tree';
140140
}
141141
else {
@@ -147,7 +147,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
147147
}
148148

149149
let lhsSuffix = args.lhs.sha !== GitService.deletedOrMissingSha ? GitService.shortenSha(lhsSha) || '' : '';
150-
if (lhs === undefined && args.rhs.sha === '') {
150+
if (lhs === undefined && args.rhs.sha.length === 0) {
151151
if (rhs !== undefined) {
152152
lhsSuffix = `not in ${lhsSuffix}`;
153153
rhsSuffix = '';
@@ -157,7 +157,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
157157
}
158158
}
159159

160-
if (args.lhs.title === undefined && (lhs !== undefined || lhsSuffix !== '')) {
160+
if (args.lhs.title === undefined && (lhs !== undefined || lhsSuffix.length !== 0)) {
161161
args.lhs.title = `${paths.basename(args.lhs.uri.fsPath)}${lhsSuffix ? ` (${lhsSuffix})` : ''}`;
162162
}
163163
if (args.rhs.title === undefined) {

src/git/formatters/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export abstract class Formatter<TItem = any, TOptions extends IFormatOptions = I
4141
private collapsableWhitespace: number = 0;
4242

4343
protected _padOrTruncate(s: string, options: Strings.ITokenOptions | undefined) {
44-
if (s === '') return s;
44+
if (s == null || s.length === 0) return s;
4545

4646
// NOTE: the collapsable whitespace logic relies on the javascript template evaluation to be left to right
4747
if (options === undefined) {

src/git/fsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class GitFileSystemProvider implements FileSystemProvider, Disposable {
7171

7272
const items = [
7373
...Iterables.map<GitTree, [string, FileType]>(tree, t => [
74-
path !== '' ? Strings.normalizePath(paths.relative(path, t.path)) : t.path,
74+
path != null && path.length !== 0 ? Strings.normalizePath(paths.relative(path, t.path)) : t.path,
7575
typeToFileType(t.type)
7676
])
7777
];

src/git/git.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class Git {
262262
) {
263263
strings = { stagedUncommitted: 'Index', uncommitted: 'Working Tree', working: '', ...strings };
264264

265-
if (ref === '') return strings.working;
265+
if (ref == null || ref.length === 0) return strings.working;
266266
if (Git.isUncommitted(ref)) {
267267
if (Git.isStagedUncommitted(ref)) return strings.stagedUncommitted;
268268

@@ -463,7 +463,7 @@ export class Git {
463463
'--get',
464464
key
465465
);
466-
return data === '' ? undefined : data.trim();
466+
return data.length === 0 ? undefined : data.trim();
467467
}
468468

469469
static async config_getRegex(pattern: string, repoPath?: string, options: { local?: boolean } = {}) {
@@ -473,7 +473,7 @@ export class Git {
473473
'--get-regex',
474474
pattern
475475
);
476-
return data === '' ? undefined : data.trim();
476+
return data.length === 0 ? undefined : data.trim();
477477
}
478478

479479
static diff(repoPath: string, fileName: string, ref1?: string, ref2?: string, options: { encoding?: string } = {}) {
@@ -626,7 +626,7 @@ export class Git {
626626
'--',
627627
fileName
628628
);
629-
return data === '' ? undefined : data.trim();
629+
return data.length === 0 ? undefined : data.trim();
630630
}
631631

632632
static async log_resolve(repoPath: string, fileName: string, ref: string) {
@@ -640,7 +640,7 @@ export class Git {
640640
'--',
641641
fileName
642642
);
643-
return data === '' ? undefined : data.trim();
643+
return data.length === 0 ? undefined : data.trim();
644644
}
645645

646646
static log_search(repoPath: string, search: string[] = emptyArray, options: { maxCount?: number } = {}) {
@@ -671,7 +671,7 @@ export class Git {
671671
}
672672

673673
const data = await git<string>({ cwd: repoPath, errors: GitErrorHandling.Ignore }, ...params, '--', fileName);
674-
return data === '' ? undefined : data.trim();
674+
return data.length === 0 ? undefined : data.trim();
675675
}
676676

677677
static async ls_tree(repoPath: string, ref: string, options: { fileName?: string } = {}) {
@@ -683,7 +683,7 @@ export class Git {
683683
params.push('-lrt', ref, '--');
684684
}
685685
const data = await git<string>({ cwd: repoPath, errors: GitErrorHandling.Ignore }, ...params);
686-
return data === '' ? undefined : data.trim();
686+
return data.length === 0 ? undefined : data.trim();
687687
}
688688

689689
static merge_base(repoPath: string, ref1: string, ref2: string, options: { forkPoint?: boolean } = {}) {
@@ -709,7 +709,7 @@ export class Git {
709709

710710
static async revparse(repoPath: string, ref: string): Promise<string | undefined> {
711711
const data = await git<string>({ cwd: repoPath, errors: GitErrorHandling.Ignore }, 'rev-parse', ref);
712-
return data === '' ? undefined : data.trim();
712+
return data.length === 0 ? undefined : data.trim();
713713
}
714714

715715
static async revparse_currentBranch(repoPath: string): Promise<[string, string | undefined] | undefined> {
@@ -734,7 +734,7 @@ export class Git {
734734
'--format=%H',
735735
'--'
736736
);
737-
if (data === '') return undefined;
737+
if (data.length === 0) return undefined;
738738

739739
// Matches output of `git branch -vv`
740740
const sha = data.trim();
@@ -752,7 +752,7 @@ export class Git {
752752
'--short',
753753
'HEAD'
754754
);
755-
return data === '' ? undefined : [data.trim(), undefined];
755+
return data.length === 0 ? undefined : [data.trim(), undefined];
756756
}
757757

758758
defaultExceptionHandler(ex, opts, ...params);
@@ -762,7 +762,7 @@ export class Git {
762762

763763
static async revparse_toplevel(cwd: string): Promise<string | undefined> {
764764
const data = await git<string>({ cwd: cwd, errors: GitErrorHandling.Ignore }, 'rev-parse', '--show-toplevel');
765-
return data === '' ? undefined : data.trim();
765+
return data.length === 0 ? undefined : data.trim();
766766
}
767767

768768
static async show<TOut extends string | Buffer>(

src/git/gitService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ export class GitService implements Disposable {
10811081

10821082
const data = await Git.branch(repoPath, { all: true });
10831083
// If we don't get any data, assume the repo doesn't have any commits yet so check if we have a current branch
1084-
if (data === '') {
1084+
if (data == null || data.length === 0) {
10851085
const current = await this.getBranch(repoPath);
10861086
branches = current !== undefined ? [current] : [];
10871087
}
@@ -2176,7 +2176,7 @@ export class GitService implements Disposable {
21762176

21772177
strings = { deletedOrMissing: '(deleted)', working: '', ...strings };
21782178

2179-
if (ref === '') return strings.working;
2179+
if (ref == null || ref.length === 0) return strings.working;
21802180
if (ref === GitService.deletedOrMissingSha) return strings.deletedOrMissing;
21812181

21822182
return Git.isShaLike(ref) || Git.isStagedUncommitted(ref) ? Git.shortenSha(ref, strings) : ref;

src/git/models/branch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class GitBranch {
3838
this.name = name;
3939
}
4040

41-
this.tracking = tracking === '' || tracking == null ? undefined : tracking;
41+
this.tracking = tracking == null || tracking.length === 0 ? undefined : tracking;
4242
this.state = {
4343
ahead: ahead,
4444
behind: behind

src/git/models/logCommit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ export class GitLogCommit extends GitCommit {
116116
status += `${Strings.pluralize('file', added)} added`;
117117
}
118118
if (changed) {
119-
status += `${status === '' ? '' : separator}${Strings.pluralize('file', changed)} changed`;
119+
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', changed)} changed`;
120120
}
121121
if (deleted) {
122-
status += `${status === '' ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
122+
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
123123
}
124124
return `${prefix}${status}${suffix}`;
125125
}

src/git/models/status.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ export class GitStatus {
8787
status += `${Strings.pluralize('file', added)} added`;
8888
}
8989
if (changed) {
90-
status += `${status === '' ? '' : separator}${Strings.pluralize('file', changed)} changed`;
90+
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', changed)} changed`;
9191
}
9292
if (deleted) {
93-
status += `${status === '' ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
93+
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
9494
}
9595
return `${prefix}${status}${suffix}`;
9696
}
@@ -124,7 +124,7 @@ export class GitStatus {
124124
status += `${Strings.pluralize('commit', state.behind)} behind`;
125125
}
126126
if (state.ahead) {
127-
status += `${status === '' ? '' : separator}${Strings.pluralize('commit', state.ahead)} ahead`;
127+
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('commit', state.ahead)} ahead`;
128128
}
129129
return `${prefix}${status}${suffix}`;
130130
}

src/git/parsers/statusParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ export class GitStatusParser {
124124
originalFileName?: string
125125
): GitStatusFile {
126126
let indexStatus = rawStatus[0] !== '.' ? rawStatus[0].trim() : undefined;
127-
if (indexStatus === '' || indexStatus === null) {
127+
if (indexStatus == null || indexStatus.length === 0) {
128128
indexStatus = undefined;
129129
}
130130

131131
let workTreeStatus = undefined;
132132
if (rawStatus.length > 1) {
133133
workTreeStatus = rawStatus[1] !== '.' ? rawStatus[1].trim() : undefined;
134-
if (workTreeStatus === '' || workTreeStatus === null) {
134+
if (workTreeStatus == null || workTreeStatus.length === 0) {
135135
workTreeStatus = undefined;
136136
}
137137
}

src/system/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export namespace Strings {
8484
fileName: string,
8585
options: { addLeadingSlash?: boolean; stripTrailingSlash?: boolean } = { stripTrailingSlash: true }
8686
) {
87-
if (fileName == null || fileName === '') return fileName;
87+
if (fileName == null || fileName.length === 0) return fileName;
8888

8989
let normalized = fileName.replace(pathNormalizeRegex, '/');
9090

0 commit comments

Comments
 (0)