Skip to content

Commit e30a66d

Browse files
committed
Add utitlity for stringContains
1 parent aa22c56 commit e30a66d

12 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14012,7 +14012,7 @@ namespace ts {
1401214012
*/
1401314013
function isUnhyphenatedJsxName(name: string | __String) {
1401414014
// - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers
14015-
return (name as string).indexOf("-") < 0;
14015+
return !stringContains(name as string, "-");
1401614016
}
1401714017

1401814018
/**

src/compiler/core.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ namespace ts {
16631663
}
16641664

16651665
export function isUrl(path: string) {
1666-
return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1;
1666+
return path && !isRootedDiskPath(path) && stringContains(path, "://");
16671667
}
16681668

16691669
export function pathIsRelative(path: string): boolean {
@@ -1932,8 +1932,12 @@ namespace ts {
19321932
return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;
19331933
}
19341934

1935+
export function stringContains(str: string, substring: string): boolean {
1936+
return str.indexOf(substring) !== -1;
1937+
}
1938+
19351939
export function hasExtension(fileName: string): boolean {
1936-
return getBaseFileName(fileName).indexOf(".") >= 0;
1940+
return stringContains(getBaseFileName(fileName), ".");
19371941
}
19381942

19391943
export function fileExtensionIs(path: string, extension: string): boolean {

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ namespace ts {
172172

173173
function hasInternalAnnotation(range: CommentRange) {
174174
const comment = currentText.substring(range.pos, range.end);
175-
return comment.indexOf("@internal") >= 0;
175+
return stringContains(comment, "@internal");
176176
}
177177

178178
function stripInternal(node: Node) {

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ namespace ts {
12261226
// check if numeric literal is a decimal literal that was originally written with a dot
12271227
const text = getLiteralTextOfNode(<LiteralExpression>expression);
12281228
return !expression.numericLiteralFlags
1229-
&& text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
1229+
&& !stringContains(text, tokenToString(SyntaxKind.DotToken));
12301230
}
12311231
else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
12321232
// check if constant enum value is integer

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ namespace ts {
10611061
export function getPackageNameFromAtTypesDirectory(mangledName: string): string {
10621062
const withoutAtTypePrefix = removePrefix(mangledName, "@types/");
10631063
if (withoutAtTypePrefix !== mangledName) {
1064-
return withoutAtTypePrefix.indexOf(mangledScopedPackageSeparator) !== -1 ?
1064+
return stringContains(withoutAtTypePrefix, mangledScopedPackageSeparator) ?
10651065
"@" + withoutAtTypePrefix.replace(mangledScopedPackageSeparator, ts.directorySeparator) :
10661066
withoutAtTypePrefix;
10671067
}

src/compiler/resolutionCache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ namespace ts {
324324
let dirPath = getDirectoryPath(failedLookupLocationPath);
325325

326326
// If directory path contains node module, get the most parent node_modules directory for watching
327-
while (dirPath.indexOf("/node_modules/") !== -1) {
327+
while (stringContains(dirPath, "/node_modules/")) {
328328
dir = getDirectoryPath(dir);
329329
dirPath = getDirectoryPath(dirPath);
330330
}
@@ -334,7 +334,6 @@ namespace ts {
334334
return { dir, dirPath };
335335
}
336336

337-
338337
// Use some ancestor of the root directory
339338
if (rootPath !== undefined) {
340339
while (!isInDirectoryPath(dirPath, rootPath)) {

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ namespace ts.server {
12051205
projectRootPath?: NormalizedPath) {
12061206
let searchPath = asNormalizedPath(getDirectoryPath(info.fileName));
12071207

1208-
while (!projectRootPath || searchPath.indexOf(projectRootPath) >= 0) {
1208+
while (!projectRootPath || stringContains(searchPath, projectRootPath)) {
12091209
const canonicalSearchPath = normalizedPathToPath(searchPath, this.currentDirectory, this.toCanonicalFileName);
12101210
const tsconfigFileName = asNormalizedPath(combinePaths(searchPath, "tsconfig.json"));
12111211
let result = action(tsconfigFileName, combinePaths(canonicalSearchPath, "tsconfig.json"));

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ namespace ts.server {
16081608
}
16091609

16101610
// No need to analyze lib.d.ts
1611-
const fileNamesInProject = fileNames.filter(value => value.indexOf("lib.d.ts") < 0);
1611+
const fileNamesInProject = fileNames.filter(value => !stringContains(value, "lib.d.ts"));
16121612
if (fileNamesInProject.length === 0) {
16131613
return;
16141614
}

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts.server.typingsInstaller {
8888
this.npmPath = npmLocation !== undefined ? npmLocation : getDefaultNPMLocation(process.argv[0]);
8989

9090
// If the NPM path contains spaces and isn't wrapped in quotes, do so.
91-
if (this.npmPath.indexOf(" ") !== -1 && this.npmPath[0] !== `"`) {
91+
if (stringContains(this.npmPath, " ") && this.npmPath[0] !== `"`) {
9292
this.npmPath = `"${this.npmPath}"`;
9393
}
9494
if (this.log.isEnabled()) {

src/services/pathCompletions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ namespace ts.Completions.PathCompletions {
195195
const normalizedPrefixDirectory = getDirectoryPath(normalizedPrefix);
196196
const normalizedPrefixBase = getBaseFileName(normalizedPrefix);
197197

198-
const fragmentHasPath = fragment.indexOf(directorySeparator) !== -1;
198+
const fragmentHasPath = stringContains(fragment, directorySeparator);
199199

200200
// Try and expand the prefix to include any path from the fragment so that we can limit the readDirectory call
201201
const expandedPrefixDirectory = fragmentHasPath ? combinePaths(normalizedPrefixDirectory, normalizedPrefixBase + getDirectoryPath(fragment)) : normalizedPrefixDirectory;
@@ -235,7 +235,7 @@ namespace ts.Completions.PathCompletions {
235235

236236
function enumeratePotentialNonRelativeModules(fragment: string, scriptPath: string, options: CompilerOptions, typeChecker: TypeChecker, host: LanguageServiceHost): string[] {
237237
// Check If this is a nested module
238-
const isNestedModule = fragment.indexOf(directorySeparator) !== -1;
238+
const isNestedModule = stringContains(fragment, directorySeparator);
239239
const moduleNameFragment = isNestedModule ? fragment.substr(0, fragment.lastIndexOf(directorySeparator)) : undefined;
240240

241241
// Get modules that the type checker picked up

0 commit comments

Comments
 (0)