Skip to content

Commit

Permalink
Changes to length === 0 for better safety
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 6, 2018
1 parent c1c8c75 commit 03d14fc
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/commands/diffWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
if (GitService.isUncommitted(args.rhs.sha)) {
rhsSuffix = 'deleted';
}
else if (rhsSuffix === '' && args.rhs.sha === GitService.deletedOrMissingSha) {
else if (rhsSuffix.length === 0 && args.rhs.sha === GitService.deletedOrMissingSha) {
rhsSuffix = 'not in Working Tree';
}
else {
Expand All @@ -147,7 +147,7 @@ export class DiffWithCommand extends ActiveEditorCommand {
}

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

if (args.lhs.title === undefined && (lhs !== undefined || lhsSuffix !== '')) {
if (args.lhs.title === undefined && (lhs !== undefined || lhsSuffix.length !== 0)) {
args.lhs.title = `${paths.basename(args.lhs.uri.fsPath)}${lhsSuffix ? ` (${lhsSuffix})` : ''}`;
}
if (args.rhs.title === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/git/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export abstract class Formatter<TItem = any, TOptions extends IFormatOptions = I
private collapsableWhitespace: number = 0;

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

// NOTE: the collapsable whitespace logic relies on the javascript template evaluation to be left to right
if (options === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/git/fsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class GitFileSystemProvider implements FileSystemProvider, Disposable {

const items = [
...Iterables.map<GitTree, [string, FileType]>(tree, t => [
path !== '' ? Strings.normalizePath(paths.relative(path, t.path)) : t.path,
path != null && path.length !== 0 ? Strings.normalizePath(paths.relative(path, t.path)) : t.path,
typeToFileType(t.type)
])
];
Expand Down
22 changes: 11 additions & 11 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class Git {
) {
strings = { stagedUncommitted: 'Index', uncommitted: 'Working Tree', working: '', ...strings };

if (ref === '') return strings.working;
if (ref == null || ref.length === 0) return strings.working;
if (Git.isUncommitted(ref)) {
if (Git.isStagedUncommitted(ref)) return strings.stagedUncommitted;

Expand Down Expand Up @@ -463,7 +463,7 @@ export class Git {
'--get',
key
);
return data === '' ? undefined : data.trim();
return data.length === 0 ? undefined : data.trim();
}

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

static diff(repoPath: string, fileName: string, ref1?: string, ref2?: string, options: { encoding?: string } = {}) {
Expand Down Expand Up @@ -626,7 +626,7 @@ export class Git {
'--',
fileName
);
return data === '' ? undefined : data.trim();
return data.length === 0 ? undefined : data.trim();
}

static async log_resolve(repoPath: string, fileName: string, ref: string) {
Expand All @@ -640,7 +640,7 @@ export class Git {
'--',
fileName
);
return data === '' ? undefined : data.trim();
return data.length === 0 ? undefined : data.trim();
}

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

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

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

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

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

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

// Matches output of `git branch -vv`
const sha = data.trim();
Expand All @@ -752,7 +752,7 @@ export class Git {
'--short',
'HEAD'
);
return data === '' ? undefined : [data.trim(), undefined];
return data.length === 0 ? undefined : [data.trim(), undefined];
}

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

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

static async show<TOut extends string | Buffer>(
Expand Down
4 changes: 2 additions & 2 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ export class GitService implements Disposable {

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

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

if (ref === '') return strings.working;
if (ref == null || ref.length === 0) return strings.working;
if (ref === GitService.deletedOrMissingSha) return strings.deletedOrMissing;

return Git.isShaLike(ref) || Git.isStagedUncommitted(ref) ? Git.shortenSha(ref, strings) : ref;
Expand Down
2 changes: 1 addition & 1 deletion src/git/models/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class GitBranch {
this.name = name;
}

this.tracking = tracking === '' || tracking == null ? undefined : tracking;
this.tracking = tracking == null || tracking.length === 0 ? undefined : tracking;
this.state = {
ahead: ahead,
behind: behind
Expand Down
4 changes: 2 additions & 2 deletions src/git/models/logCommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export class GitLogCommit extends GitCommit {
status += `${Strings.pluralize('file', added)} added`;
}
if (changed) {
status += `${status === '' ? '' : separator}${Strings.pluralize('file', changed)} changed`;
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', changed)} changed`;
}
if (deleted) {
status += `${status === '' ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
}
return `${prefix}${status}${suffix}`;
}
Expand Down
6 changes: 3 additions & 3 deletions src/git/models/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export class GitStatus {
status += `${Strings.pluralize('file', added)} added`;
}
if (changed) {
status += `${status === '' ? '' : separator}${Strings.pluralize('file', changed)} changed`;
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', changed)} changed`;
}
if (deleted) {
status += `${status === '' ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
}
return `${prefix}${status}${suffix}`;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ export class GitStatus {
status += `${Strings.pluralize('commit', state.behind)} behind`;
}
if (state.ahead) {
status += `${status === '' ? '' : separator}${Strings.pluralize('commit', state.ahead)} ahead`;
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('commit', state.ahead)} ahead`;
}
return `${prefix}${status}${suffix}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/git/parsers/statusParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ export class GitStatusParser {
originalFileName?: string
): GitStatusFile {
let indexStatus = rawStatus[0] !== '.' ? rawStatus[0].trim() : undefined;
if (indexStatus === '' || indexStatus === null) {
if (indexStatus == null || indexStatus.length === 0) {
indexStatus = undefined;
}

let workTreeStatus = undefined;
if (rawStatus.length > 1) {
workTreeStatus = rawStatus[1] !== '.' ? rawStatus[1].trim() : undefined;
if (workTreeStatus === '' || workTreeStatus === null) {
if (workTreeStatus == null || workTreeStatus.length === 0) {
workTreeStatus = undefined;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/system/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export namespace Strings {
fileName: string,
options: { addLeadingSlash?: boolean; stripTrailingSlash?: boolean } = { stripTrailingSlash: true }
) {
if (fileName == null || fileName === '') return fileName;
if (fileName == null || fileName.length === 0) return fileName;

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

Expand Down
6 changes: 3 additions & 3 deletions src/ui/shared/app-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export abstract class App<TBootstrap extends Bootstrap> {
}

let value: string | null | undefined = element.value;
if (value === '') {
if (value == null || value.length === 0) {
value = element.dataset.defaultValue;
if (value === undefined) {
value = null;
Expand Down Expand Up @@ -262,7 +262,7 @@ export abstract class App<TBootstrap extends Bootstrap> {
if (value === undefined) {
value = this.getSettingValue<string | boolean>(lhs) || false;
}
state = rhs !== undefined ? rhs === '' + value : Boolean(value);
state = rhs !== undefined ? rhs === String(value) : Boolean(value);
break;
}
case '!': {
Expand All @@ -271,7 +271,7 @@ export abstract class App<TBootstrap extends Bootstrap> {
if (value === undefined) {
value = this.getSettingValue<string | boolean>(lhs) || false;
}
state = rhs !== undefined ? rhs !== '' + value : !value;
state = rhs !== undefined ? rhs !== String(value) : !value;
break;
}
case '+': {
Expand Down
6 changes: 3 additions & 3 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export class ViewCommands implements Disposable {
placeHolder: `Branch name`,
value: value
} as InputBoxOptions);
if (name === undefined || name === '') return;
if (name === undefined || name.length === 0) return;

this.sendTerminalCommand('branch', `${remoteBranch ? '-t ' : ''}${name} ${node.ref}`, node.repoPath);
}
Expand Down Expand Up @@ -571,15 +571,15 @@ export class ViewCommands implements Disposable {
prompt: `Please provide a tag name (Press 'Enter' to confirm or 'Escape' to cancel)`,
placeHolder: `Tag name`
} as InputBoxOptions);
if (name === undefined || name === '') return;
if (name === undefined || name.length === 0) return;

const message = await window.showInputBox({
prompt: `Please provide an optional message to annotate the tag (Press 'Enter' to confirm or 'Escape' to cancel)`,
placeHolder: `Tag message`
} as InputBoxOptions);
if (message === undefined) return;

const args = `${message !== '' ? `-a -m "${message}" ` : ''}${name} ${node.ref}`;
const args = `${message.length !== 0 ? `-a -m "${message}" ` : ''}${name} ${node.ref}`;
this.sendTerminalCommand('tag', args, node.repoPath);
}

Expand Down

0 comments on commit 03d14fc

Please sign in to comment.