From e30a66d22f913b824427fd1323dfd82af20c8a76 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 10 Oct 2017 17:08:47 -0700 Subject: [PATCH] Add utitlity for stringContains --- src/compiler/checker.ts | 2 +- src/compiler/core.ts | 8 ++++++-- src/compiler/declarationEmitter.ts | 2 +- src/compiler/emitter.ts | 2 +- src/compiler/moduleNameResolver.ts | 2 +- src/compiler/resolutionCache.ts | 3 +-- src/server/editorServices.ts | 2 +- src/server/session.ts | 2 +- src/server/typingsInstaller/nodeTypingsInstaller.ts | 2 +- src/services/pathCompletions.ts | 4 ++-- src/services/refactors/extractSymbol.ts | 2 +- src/services/services.ts | 2 +- 12 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d47a77a7440..ab61d91ef15 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14012,7 +14012,7 @@ namespace ts { */ function isUnhyphenatedJsxName(name: string | __String) { // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers - return (name as string).indexOf("-") < 0; + return !stringContains(name as string, "-"); } /** diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 45a4a04b6ab..442a262bbe2 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1663,7 +1663,7 @@ namespace ts { } export function isUrl(path: string) { - return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; + return path && !isRootedDiskPath(path) && stringContains(path, "://"); } export function pathIsRelative(path: string): boolean { @@ -1932,8 +1932,12 @@ namespace ts { return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos; } + export function stringContains(str: string, substring: string): boolean { + return str.indexOf(substring) !== -1; + } + export function hasExtension(fileName: string): boolean { - return getBaseFileName(fileName).indexOf(".") >= 0; + return stringContains(getBaseFileName(fileName), "."); } export function fileExtensionIs(path: string, extension: string): boolean { diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 3de20915029..48b97b048e9 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -172,7 +172,7 @@ namespace ts { function hasInternalAnnotation(range: CommentRange) { const comment = currentText.substring(range.pos, range.end); - return comment.indexOf("@internal") >= 0; + return stringContains(comment, "@internal"); } function stripInternal(node: Node) { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 8083b3841c8..da6c0e0fca8 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1226,7 +1226,7 @@ namespace ts { // check if numeric literal is a decimal literal that was originally written with a dot const text = getLiteralTextOfNode(expression); return !expression.numericLiteralFlags - && text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0; + && !stringContains(text, tokenToString(SyntaxKind.DotToken)); } else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) { // check if constant enum value is integer diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 560beb39557..ac83dd41311 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -1061,7 +1061,7 @@ namespace ts { export function getPackageNameFromAtTypesDirectory(mangledName: string): string { const withoutAtTypePrefix = removePrefix(mangledName, "@types/"); if (withoutAtTypePrefix !== mangledName) { - return withoutAtTypePrefix.indexOf(mangledScopedPackageSeparator) !== -1 ? + return stringContains(withoutAtTypePrefix, mangledScopedPackageSeparator) ? "@" + withoutAtTypePrefix.replace(mangledScopedPackageSeparator, ts.directorySeparator) : withoutAtTypePrefix; } diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index 25545c0efbc..3f3955b294f 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -324,7 +324,7 @@ namespace ts { let dirPath = getDirectoryPath(failedLookupLocationPath); // If directory path contains node module, get the most parent node_modules directory for watching - while (dirPath.indexOf("/node_modules/") !== -1) { + while (stringContains(dirPath, "/node_modules/")) { dir = getDirectoryPath(dir); dirPath = getDirectoryPath(dirPath); } @@ -334,7 +334,6 @@ namespace ts { return { dir, dirPath }; } - // Use some ancestor of the root directory if (rootPath !== undefined) { while (!isInDirectoryPath(dirPath, rootPath)) { diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 2916fb60c57..c683ff0080a 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1205,7 +1205,7 @@ namespace ts.server { projectRootPath?: NormalizedPath) { let searchPath = asNormalizedPath(getDirectoryPath(info.fileName)); - while (!projectRootPath || searchPath.indexOf(projectRootPath) >= 0) { + while (!projectRootPath || stringContains(searchPath, projectRootPath)) { const canonicalSearchPath = normalizedPathToPath(searchPath, this.currentDirectory, this.toCanonicalFileName); const tsconfigFileName = asNormalizedPath(combinePaths(searchPath, "tsconfig.json")); let result = action(tsconfigFileName, combinePaths(canonicalSearchPath, "tsconfig.json")); diff --git a/src/server/session.ts b/src/server/session.ts index 57dac7ec799..df9c77b9005 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1608,7 +1608,7 @@ namespace ts.server { } // No need to analyze lib.d.ts - const fileNamesInProject = fileNames.filter(value => value.indexOf("lib.d.ts") < 0); + const fileNamesInProject = fileNames.filter(value => !stringContains(value, "lib.d.ts")); if (fileNamesInProject.length === 0) { return; } diff --git a/src/server/typingsInstaller/nodeTypingsInstaller.ts b/src/server/typingsInstaller/nodeTypingsInstaller.ts index 6a31a114d23..f5d9b866376 100644 --- a/src/server/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/server/typingsInstaller/nodeTypingsInstaller.ts @@ -88,7 +88,7 @@ namespace ts.server.typingsInstaller { this.npmPath = npmLocation !== undefined ? npmLocation : getDefaultNPMLocation(process.argv[0]); // If the NPM path contains spaces and isn't wrapped in quotes, do so. - if (this.npmPath.indexOf(" ") !== -1 && this.npmPath[0] !== `"`) { + if (stringContains(this.npmPath, " ") && this.npmPath[0] !== `"`) { this.npmPath = `"${this.npmPath}"`; } if (this.log.isEnabled()) { diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index c41ed798b1d..e3bf9deac89 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -195,7 +195,7 @@ namespace ts.Completions.PathCompletions { const normalizedPrefixDirectory = getDirectoryPath(normalizedPrefix); const normalizedPrefixBase = getBaseFileName(normalizedPrefix); - const fragmentHasPath = fragment.indexOf(directorySeparator) !== -1; + const fragmentHasPath = stringContains(fragment, directorySeparator); // Try and expand the prefix to include any path from the fragment so that we can limit the readDirectory call const expandedPrefixDirectory = fragmentHasPath ? combinePaths(normalizedPrefixDirectory, normalizedPrefixBase + getDirectoryPath(fragment)) : normalizedPrefixDirectory; @@ -235,7 +235,7 @@ namespace ts.Completions.PathCompletions { function enumeratePotentialNonRelativeModules(fragment: string, scriptPath: string, options: CompilerOptions, typeChecker: TypeChecker, host: LanguageServiceHost): string[] { // Check If this is a nested module - const isNestedModule = fragment.indexOf(directorySeparator) !== -1; + const isNestedModule = stringContains(fragment, directorySeparator); const moduleNameFragment = isNestedModule ? fragment.substr(0, fragment.lastIndexOf(directorySeparator)) : undefined; // Get modules that the type checker picked up diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index c16a290a30f..124a1f720bd 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -660,7 +660,7 @@ namespace ts.refactor.extractSymbol { function getUniqueName(baseName: string, fileText: string): string { let nameText = baseName; - for (let i = 1; fileText.indexOf(nameText) !== -1; i++) { + for (let i = 1; stringContains(fileText, nameText); i++) { nameText = `${baseName}_${i}`; } return nameText; diff --git a/src/services/services.ts b/src/services/services.ts index bc733a4e2fe..6bdc96d8b4d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1952,7 +1952,7 @@ namespace ts { function isNodeModulesFile(path: string): boolean { const node_modulesFolderName = "/node_modules/"; - return path.indexOf(node_modulesFolderName) !== -1; + return stringContains(path, node_modulesFolderName); } }