From 4d9ed4e44590b0a52cbffd456dcb19f2ae3c705e Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 19 Sep 2019 00:30:52 -0400 Subject: [PATCH] Fixes static analysis issues --- src/commands/closeUnchangedFiles.ts | 9 ++++----- src/commands/common.ts | 2 +- src/commands/openChangedFiles.ts | 9 ++++----- src/git/gitService.ts | 16 +++++++--------- src/git/parsers/blameParser.ts | 2 +- src/git/parsers/branchParser.ts | 4 ++-- src/git/parsers/diffParser.ts | 6 +++--- src/git/parsers/logParser.ts | 8 ++++---- src/git/parsers/reflogParser.ts | 4 ++-- src/git/parsers/remoteParser.ts | 4 ++-- src/git/parsers/shortlogParser.ts | 4 ++-- src/git/parsers/tagParser.ts | 6 +++--- src/git/parsers/treeParser.ts | 4 ++-- src/logger.ts | 6 +----- src/system/decorators/memoize.ts | 2 +- src/system/string.ts | 6 ++++-- src/views/nodes/statusFileNode.ts | 2 +- webpack.config.js | 1 - 18 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/commands/closeUnchangedFiles.ts b/src/commands/closeUnchangedFiles.ts index c19d3d1..781b14d 100644 --- a/src/commands/closeUnchangedFiles.ts +++ b/src/commands/closeUnchangedFiles.ts @@ -6,22 +6,21 @@ import { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { Functions } from '../system'; -import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrPrompt } from './common'; +import { Command, command, Commands, getRepoPathOrPrompt } from './common'; export interface CloseUnchangedFilesCommandArgs { uris?: Uri[]; } @command() -export class CloseUnchangedFilesCommand extends ActiveEditorCommand { +export class CloseUnchangedFilesCommand extends Command { private _onEditorChangedFn: ((editor: TextEditor | undefined) => void) | undefined; constructor() { super(Commands.CloseUnchangedFiles); } - async execute(editor?: TextEditor, uri?: Uri, args?: CloseUnchangedFilesCommandArgs) { - uri = getCommandUri(uri, editor); + async execute(args?: CloseUnchangedFilesCommandArgs) { args = { ...args }; try { @@ -46,7 +45,7 @@ export class CloseUnchangedFilesCommand extends ActiveEditorCommand { ) ); - editor = window.activeTextEditor; + let editor = window.activeTextEditor; let count = 0; let loopCount = 0; diff --git a/src/commands/common.ts b/src/commands/common.ts index 7a52dd5..62dc6f2 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -352,7 +352,7 @@ function isScmResourceState(state: any): state is SourceControlResourceState { return (state as SourceControlResourceState).resourceUri != null; } -function isTextEditor(editor: any): editor is TextEditor { +function isTextEditor(editor: any | undefined): editor is TextEditor { if (editor == null) return false; return ( diff --git a/src/commands/openChangedFiles.ts b/src/commands/openChangedFiles.ts index 2060f0c..1a8fd50 100644 --- a/src/commands/openChangedFiles.ts +++ b/src/commands/openChangedFiles.ts @@ -1,24 +1,23 @@ 'use strict'; -import { TextEditor, Uri, window } from 'vscode'; +import { Uri, window } from 'vscode'; import { GlyphChars } from '../constants'; import { Container } from '../container'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { Arrays } from '../system'; -import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrPrompt, openEditor } from './common'; +import { Command, command, Commands, getRepoPathOrPrompt, openEditor } from './common'; export interface OpenChangedFilesCommandArgs { uris?: Uri[]; } @command() -export class OpenChangedFilesCommand extends ActiveEditorCommand { +export class OpenChangedFilesCommand extends Command { constructor() { super(Commands.OpenChangedFiles); } - async execute(editor?: TextEditor, uri?: Uri, args?: OpenChangedFilesCommandArgs) { - uri = getCommandUri(uri, editor); + async execute(args?: OpenChangedFilesCommandArgs) { args = { ...args }; try { diff --git a/src/git/gitService.ts b/src/git/gitService.ts index e39eb5a..459dfc0 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -225,11 +225,10 @@ export class GitService implements Disposable { } private onAnyRepositoryChanged(repo: Repository, reason: RepositoryChange) { - this._trackedCache.clear(); - this._branchesCache.delete(repo.path); this._tagsCache.delete(repo.path); this._tagsWithRefsCache.clear(); + this._trackedCache.clear(); if (reason === RepositoryChange.Config) { this._userMapCache.delete(repo.path); @@ -1150,13 +1149,13 @@ export class GitService implements Disposable { include === 'all' || include === 'branches' ? this.getBranches(repoPath, { ...options, - filter: filterBranches && filterBranches + filter: filterBranches }) : undefined, include === 'all' || include === 'tags' ? this.getTags(repoPath, { ...options, - filter: filterTags && filterTags + filter: filterTags }) : undefined ]); @@ -1266,7 +1265,7 @@ export class GitService implements Disposable { let key: string; let value: string; - let match: RegExpExecArray | null; + let match; do { match = userConfigRegex.exec(data); if (match == null) break; @@ -1274,7 +1273,7 @@ export class GitService implements Disposable { [, key, value] = match; // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 user[key as 'name' | 'email'] = ` ${value}`.substr(1); - } while (match != null); + } while (true); const author = `${user.name} <${user.email}>`; // Check if there is a mailmap for the current user @@ -2594,7 +2593,6 @@ export class GitService implements Disposable { return GitUri.resolveToUri(fileName, repoPath); } - ref = renamedRef; fileName = renamedFile; } while (true); } @@ -2923,7 +2921,7 @@ export class GitService implements Disposable { } } } - } while (match != null); + } while (true); return operations; } @@ -2956,6 +2954,6 @@ export class GitService implements Disposable { } else { values.push(value); } - } while (match != null); + } while (true); } } diff --git a/src/git/parsers/blameParser.ts b/src/git/parsers/blameParser.ts index 05dc5b1..007004c 100644 --- a/src/git/parsers/blameParser.ts +++ b/src/git/parsers/blameParser.ts @@ -43,7 +43,7 @@ export class GitBlameParser { const commits: Map = new Map(); const lines: GitCommitLine[] = []; - let relativeFileName = repoPath && fileName; + let relativeFileName; let entry: BlameEntry | undefined = undefined; let line: string; diff --git a/src/git/parsers/branchParser.ts b/src/git/parsers/branchParser.ts index 4fa9ce5..32b4873 100644 --- a/src/git/parsers/branchParser.ts +++ b/src/git/parsers/branchParser.ts @@ -34,7 +34,7 @@ export class GitBranchParser { let remote; - let match: RegExpExecArray | null; + let match; do { match = branchWithTrackingRegex.exec(data); if (match == null) break; @@ -66,7 +66,7 @@ export class GitBranchParser { Number(behind) || 0 ) ); - } while (match != null); + } while (true); return branches; } diff --git a/src/git/parsers/diffParser.ts b/src/git/parsers/diffParser.ts index e506189..89bd298 100644 --- a/src/git/parsers/diffParser.ts +++ b/src/git/parsers/diffParser.ts @@ -19,7 +19,7 @@ export class GitDiffParser { let currentCount; let hunk; - let match: RegExpExecArray | null; + let match; do { match = unifiedDiffRegex.exec(`${data}\n@@`); if (match == null) break; @@ -43,7 +43,7 @@ export class GitDiffParser { } ) ); - } while (match != null); + } while (true); if (!hunks.length) return undefined; @@ -147,7 +147,7 @@ export class GitDiffParser { ? undefined : ` ${originalFileName}`.substr(1) }); - } while (match != null); + } while (true); return files; } diff --git a/src/git/parsers/logParser.ts b/src/git/parsers/logParser.ts index 5d17115..f211012 100644 --- a/src/git/parsers/logParser.ts +++ b/src/git/parsers/logParser.ts @@ -221,8 +221,8 @@ export class GitLogParser { entry.status = 'R'; } - next = lines.next(); - next = lines.next(); + void lines.next(); + void lines.next(); next = lines.next(); match = diffRangeRegex.exec(next.value); @@ -463,7 +463,7 @@ export class GitLogParser { let file; let renamed; - let match: RegExpExecArray | null; + let match; do { match = logFileSimpleRegex.exec(data); if (match == null) break; @@ -518,7 +518,7 @@ export class GitLogParser { status = status == null || status.length === 0 ? undefined : ` ${status}`.substr(1); break; - } while (match != null); + } while (true); // Ensure the regex state is reset logFileSimpleRenamedFilesRegex.lastIndex = 0; diff --git a/src/git/parsers/reflogParser.ts b/src/git/parsers/reflogParser.ts index 4e8e33b..48350a8 100644 --- a/src/git/parsers/reflogParser.ts +++ b/src/git/parsers/reflogParser.ts @@ -40,7 +40,7 @@ export class GitReflogParser { let record: GitReflogRecord | undefined; let truncated = false; - let match: RegExpExecArray | null; + let match; do { match = reflogRegex.exec(data); if (match == null) break; @@ -105,7 +105,7 @@ export class GitReflogParser { ); recordDate = date; } - } while (match != null); + } while (true); // Ensure the regex state is reset reflogRegex.lastIndex = 0; diff --git a/src/git/parsers/remoteParser.ts b/src/git/parsers/remoteParser.ts index 5832ef3..5aa707b 100644 --- a/src/git/parsers/remoteParser.ts +++ b/src/git/parsers/remoteParser.ts @@ -70,7 +70,7 @@ export class GitRemoteParser { let uniqueness; let remote: GitRemote | undefined; - let match: RegExpExecArray | null; + let match; do { match = remoteRegex.exec(data); if (match == null) break; @@ -105,7 +105,7 @@ export class GitRemoteParser { // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 remote.types.push({ url: url, type: ` ${type}`.substr(1) as GitRemoteType }); } - } while (match != null); + } while (true); return remotes; } diff --git a/src/git/parsers/shortlogParser.ts b/src/git/parsers/shortlogParser.ts index 412131f..1af1fbb 100644 --- a/src/git/parsers/shortlogParser.ts +++ b/src/git/parsers/shortlogParser.ts @@ -15,7 +15,7 @@ export class GitShortLogParser { let name; let email; - let match: RegExpExecArray | null; + let match; do { match = shortlogRegex.exec(data); if (match == null) break; @@ -32,7 +32,7 @@ export class GitShortLogParser { Number(count) || 0 ) ); - } while (match != null); + } while (true); return { repoPath: repoPath, contributors: contributors }; } diff --git a/src/git/parsers/tagParser.ts b/src/git/parsers/tagParser.ts index ad4e2cc..6f057f0 100644 --- a/src/git/parsers/tagParser.ts +++ b/src/git/parsers/tagParser.ts @@ -15,7 +15,7 @@ export class GitTagParser { let name; let annotation; - let match: RegExpExecArray | null; + let match; do { match = tagWithAnnotationRegex.exec(data); if (match == null) break; @@ -32,7 +32,7 @@ export class GitTagParser { annotation == null || annotation.length === 0 ? undefined : ` ${annotation}`.substr(1) ) ); - } while (match != null); + } while (true); return tags; } @@ -61,7 +61,7 @@ export class GitTagParser { ` ${sha}`.substr(1) ) ); - } while (match != null); + } while (true); return tags; } diff --git a/src/git/parsers/treeParser.ts b/src/git/parsers/treeParser.ts index dbb80ae..525d7da 100644 --- a/src/git/parsers/treeParser.ts +++ b/src/git/parsers/treeParser.ts @@ -17,7 +17,7 @@ export class GitTreeParser { let size; let filePath; - let match: RegExpExecArray | null; + let match; do { match = treeRegex.exec(data); if (match == null) break; @@ -33,7 +33,7 @@ export class GitTreeParser { // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 type: (type == null || type.length === 0 ? emptyStr : ` ${type}`.substr(1)) as 'blob' | 'tree' }); - } while (match != null); + } while (true); return trees; } diff --git a/src/logger.ts b/src/logger.ts index 3f50b31..47c2b66 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -83,11 +83,7 @@ export class Logger { if (contextOrMessage === undefined || typeof contextOrMessage === 'string') { message = contextOrMessage; } else { - message = params.shift(); - - if (contextOrMessage !== undefined) { - message = `${contextOrMessage.prefix} ${message || emptyStr}`; - } + message = `${contextOrMessage.prefix} ${params.shift() || emptyStr}`; } if (message === undefined) { diff --git a/src/system/decorators/memoize.ts b/src/system/decorators/memoize.ts index ec29a4d..9c213f4 100644 --- a/src/system/decorators/memoize.ts +++ b/src/system/decorators/memoize.ts @@ -32,7 +32,7 @@ export function memoize any>(resolver?: (...args: Par throw new Error('Not supported'); } - if (!fn || !fnKey) throw new Error('Not supported'); + if (fn == null) throw new Error('Not supported'); const memoizeKey = `$memoize$${key}`; diff --git a/src/system/string.ts b/src/system/string.ts index 5b6a5d9..44955db 100644 --- a/src/system/string.ts +++ b/src/system/string.ts @@ -87,7 +87,7 @@ export namespace Strings { truncateTo: truncateTo == null ? undefined : parseInt(truncateTo, 10) } }); - } while (match != null); + } while (true); return tokens; } @@ -250,7 +250,9 @@ export namespace Strings { return `${s.substring(0, chars)}${ellipsis}`; } - const ansiRegex = /[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))/g; + // Lifted from https://github.com/chalk/ansi-regex + // eslint-disable-next-line no-control-regex + const ansiRegex = /[\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~]))/g; const containsNonAsciiRegex = /[^\x20-\x7F\u00a0\u2026]/; export function getWidth(s: string): number { diff --git a/src/views/nodes/statusFileNode.ts b/src/views/nodes/statusFileNode.ts index ce0179b..ae963cf 100644 --- a/src/views/nodes/statusFileNode.ts +++ b/src/views/nodes/statusFileNode.ts @@ -34,7 +34,7 @@ export class StatusFileNode extends ViewNode { ref = undefined; hasUnstagedChanges = true; } - } else if (hasUnstagedChanges || hasStagedChanges) { + } else if (hasUnstagedChanges) { break; } else { ref = c.sha; diff --git a/webpack.config.js b/webpack.config.js index c0e73cd..32585a0 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,7 +3,6 @@ const fs = require('fs'); const path = require('path'); const glob = require('glob'); -const webpack = require('webpack'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin'); const CircularDependencyPlugin = require('circular-dependency-plugin');