From d998e97d8c64acf1ed4b8cd508a4589c110bd3aa Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 3 Nov 2017 15:20:35 -0700 Subject: [PATCH] Apply 'prefer-for-of' tslint rule (#19721) --- src/compiler/checker.ts | 3 +-- src/compiler/emitter.ts | 4 ++-- src/compiler/transformers/module/system.ts | 3 +-- src/compiler/tsc.ts | 4 +--- src/harness/compilerRunner.ts | 4 ++-- src/harness/harness.ts | 16 ++++++---------- src/harness/parallel/host.ts | 3 +-- src/harness/projectsRunner.ts | 10 +++++----- src/harness/runner.ts | 4 ++-- src/harness/rwcRunner.ts | 6 ++---- src/harness/sourceMapRecorder.ts | 7 +++---- src/harness/userRunner.ts | 4 ++-- .../fixClassSuperMustPrecedeThisAccess.ts | 4 ++-- src/services/codefixes/helpers.ts | 6 ++---- tslint.json | 1 - 15 files changed, 32 insertions(+), 47 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b540f92b525..2b3bc631ebc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2854,8 +2854,7 @@ namespace ts { function mapToTypeNodes(types: Type[], context: NodeBuilderContext): TypeNode[] { if (some(types)) { const result = []; - for (let i = 0; i < types.length; ++i) { - const type = types[i]; + for (const type of types) { const typeNode = typeToTypeNodeHelper(type, context); if (typeNode) { result.push(typeNode); diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 8c41eda192a..74e5d4d5539 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2661,8 +2661,8 @@ namespace ts { function writeLines(text: string): void { const lines = text.split(/\r\n?|\n/g); const indentation = guessIndentation(lines); - for (let i = 0; i < lines.length; i++) { - const line = indentation ? lines[i].slice(indentation) : lines[i]; + for (const lineText of lines) { + const line = indentation ? lineText.slice(indentation) : lineText; if (line.length) { writeLine(); write(line); diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index a481f0c1d40..e5d638cf04d 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -146,8 +146,7 @@ namespace ts { function collectDependencyGroups(externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[]) { const groupIndices = createMap(); const dependencyGroups: DependencyGroup[] = []; - for (let i = 0; i < externalImports.length; i++) { - const externalImport = externalImports[i]; + for (const externalImport of externalImports) { const externalModuleName = getExternalModuleNameLiteral(externalImport, currentSourceFile, host, resolver, compilerOptions); if (externalModuleName) { const text = externalModuleName.text; diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 827dfa152c7..fe4365de265 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -305,9 +305,7 @@ namespace ts { const optionsDescriptionMap = createMap(); // Map between option.description and list of option.type if it is a kind - for (let i = 0; i < optsList.length; i++) { - const option = optsList[i]; - + for (const option of optsList) { // If an option lacks a description, // it is not officially supported. if (!option.description) { diff --git a/src/harness/compilerRunner.ts b/src/harness/compilerRunner.ts index c58accb657c..30c5e47949f 100644 --- a/src/harness/compilerRunner.ts +++ b/src/harness/compilerRunner.ts @@ -211,8 +211,8 @@ class CompilerBaselineRunner extends RunnerBase { this.emit = false; const opts = this.options.split(","); - for (let i = 0; i < opts.length; i++) { - switch (opts[i]) { + for (const opt of opts) { + switch (opt) { case "emit": this.emit = true; break; diff --git a/src/harness/harness.ts b/src/harness/harness.ts index ada7f06f3b3..e698839168a 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -575,14 +575,13 @@ namespace Harness { function filesInFolder(folder: string): string[] { let paths: string[] = []; - const files = fs.readdirSync(folder); - for (let i = 0; i < files.length; i++) { - const pathToFile = pathModule.join(folder, files[i]); + for (const file of fs.readdirSync(folder)) { + const pathToFile = pathModule.join(folder, file); const stat = fs.statSync(pathToFile); if (options.recursive && stat.isDirectory()) { paths = paths.concat(filesInFolder(pathToFile)); } - else if (stat.isFile() && (!spec || files[i].match(spec))) { + else if (stat.isFile() && (!spec || file.match(spec))) { paths.push(pathToFile); } } @@ -1581,10 +1580,8 @@ namespace Harness { // Preserve legacy behavior if (lastIndexWritten === undefined) { - for (let i = 0; i < codeLines.length; i++) { - const currentCodeLine = codeLines[i]; - typeLines += currentCodeLine + "\r\n"; - typeLines += "No type information for this code."; + for (const codeLine of codeLines) { + typeLines += codeLine + "\r\nNo type information for this code."; } } else { @@ -1870,8 +1867,7 @@ namespace Harness { let currentFileName: any = undefined; let refs: string[] = []; - for (let i = 0; i < lines.length; i++) { - const line = lines[i]; + for (const line of lines) { const testMetaData = optionRegex.exec(line); if (testMetaData) { // Comment line, check for global/file @options and record them diff --git a/src/harness/parallel/host.ts b/src/harness/parallel/host.ts index bf218c1b3a1..40dee0da872 100644 --- a/src/harness/parallel/host.ts +++ b/src/harness/parallel/host.ts @@ -314,8 +314,7 @@ namespace Harness.Parallel.Host { stats.failures = errorResults.length; stats.tests = totalPassing + errorResults.length; stats.duration = duration; - for (let j = 0; j < errorResults.length; j++) { - const failure = errorResults[j]; + for (const failure of errorResults) { failures.push(makeMochaTest(failure)); } if (noColors) { diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index cc68f8625f4..fccbba88ff6 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -140,12 +140,12 @@ class ProjectRunner extends RunnerBase { // Clean up source map data that will be used in baselining if (sourceMapData) { - for (let i = 0; i < sourceMapData.length; i++) { - for (let j = 0; j < sourceMapData[i].sourceMapSources.length; j++) { - sourceMapData[i].sourceMapSources[j] = cleanProjectUrl(sourceMapData[i].sourceMapSources[j]); + for (const data of sourceMapData) { + for (let j = 0; j < data.sourceMapSources.length; j++) { + data.sourceMapSources[j] = cleanProjectUrl(data.sourceMapSources[j]); } - sourceMapData[i].jsSourceMappingURL = cleanProjectUrl(sourceMapData[i].jsSourceMappingURL); - sourceMapData[i].sourceMapSourceRoot = cleanProjectUrl(sourceMapData[i].sourceMapSourceRoot); + data.jsSourceMappingURL = cleanProjectUrl(data.jsSourceMappingURL); + data.sourceMapSourceRoot = cleanProjectUrl(data.sourceMapSourceRoot); } } diff --git a/src/harness/runner.ts b/src/harness/runner.ts index 9b8ed41554e..b538f90bc39 100644 --- a/src/harness/runner.ts +++ b/src/harness/runner.ts @@ -27,8 +27,8 @@ let iterations = 1; function runTests(runners: RunnerBase[]) { for (let i = iterations; i > 0; i--) { - for (let j = 0; j < runners.length; j++) { - runners[j].initializeTests(); + for (const runner of runners) { + runner.initializeTests(); } } } diff --git a/src/harness/rwcRunner.ts b/src/harness/rwcRunner.ts index 21cc38cb8b8..af6e8c95c5c 100644 --- a/src/harness/rwcRunner.ts +++ b/src/harness/rwcRunner.ts @@ -245,10 +245,8 @@ class RWCRunner extends RunnerBase { */ public initializeTests(): void { // Read in and evaluate the test list - const testList = this.tests && this.tests.length ? this.tests : this.enumerateTestFiles(); - - for (let i = 0; i < testList.length; i++) { - this.runTest(testList[i]); + for (const test of this.tests && this.tests.length ? this.tests : this.enumerateTestFiles()) { + this.runTest(test); } } diff --git a/src/harness/sourceMapRecorder.ts b/src/harness/sourceMapRecorder.ts index de068706fbe..606d4e67acd 100644 --- a/src/harness/sourceMapRecorder.ts +++ b/src/harness/sourceMapRecorder.ts @@ -386,9 +386,9 @@ namespace Harness.SourceMapRecorder { if (currentSpan.decodeErrors) { // If there are decode errors, write - for (let i = 0; i < currentSpan.decodeErrors.length; i++) { + for (const decodeError of currentSpan.decodeErrors) { writeSourceMapIndent(prevEmittedCol, markerIds[index]); - sourceMapRecorder.WriteLine(currentSpan.decodeErrors[i]); + sourceMapRecorder.WriteLine(decodeError); } } @@ -442,8 +442,7 @@ namespace Harness.SourceMapRecorder { let prevSourceFile: ts.SourceFile; SourceMapSpanWriter.initializeSourceMapSpanWriter(sourceMapRecorder, sourceMapData, jsFiles[i]); - for (let j = 0; j < sourceMapData.sourceMapDecodedMappings.length; j++) { - const decodedSourceMapping = sourceMapData.sourceMapDecodedMappings[j]; + for (const decodedSourceMapping of sourceMapData.sourceMapDecodedMappings) { const currentSourceFile = program.getSourceFile(sourceMapData.inputSourceFileNames[decodedSourceMapping.sourceIndex]); if (currentSourceFile !== prevSourceFile) { SourceMapSpanWriter.recordNewSourceFileSpan(decodedSourceMapping, currentSourceFile.text); diff --git a/src/harness/userRunner.ts b/src/harness/userRunner.ts index 3802330e10c..9be652aebf8 100644 --- a/src/harness/userRunner.ts +++ b/src/harness/userRunner.ts @@ -18,8 +18,8 @@ class UserCodeRunner extends RunnerBase { const testList = this.tests && this.tests.length ? this.tests : this.enumerateTestFiles(); describe(`${this.kind()} code samples`, () => { - for (let i = 0; i < testList.length; i++) { - this.runTest(testList[i]); + for (const test of testList) { + this.runTest(test); } }); } diff --git a/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts b/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts index bc92cd8e0d1..e5c4266684d 100644 --- a/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts +++ b/src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts @@ -20,8 +20,8 @@ namespace ts.codefix { // i.e. super(this.a), since in that case we won't suggest a fix if (superCall.expression && superCall.expression.kind === SyntaxKind.CallExpression) { const expressionArguments = (superCall.expression).arguments; - for (let i = 0; i < expressionArguments.length; i++) { - if ((expressionArguments[i]).expression === token) { + for (const arg of expressionArguments) { + if ((arg).expression === token) { return undefined; } } diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 685c6832f13..206a3864ac0 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -104,8 +104,7 @@ namespace ts.codefix { } const signatureDeclarations: MethodDeclaration[] = []; - for (let i = 0; i < signatures.length; i++) { - const signature = signatures[i]; + for (const signature of signatures) { const methodDeclaration = signatureToMethodDeclaration(signature, enclosingDeclaration); if (methodDeclaration) { signatureDeclarations.push(methodDeclaration); @@ -194,8 +193,7 @@ namespace ts.codefix { let maxArgsSignature = signatures[0]; let minArgumentCount = signatures[0].minArgumentCount; let someSigHasRestParameter = false; - for (let i = 0; i < signatures.length; i++) { - const sig = signatures[i]; + for (const sig of signatures) { minArgumentCount = Math.min(sig.minArgumentCount, minArgumentCount); if (sig.hasRestParameter) { someSigHasRestParameter = true; diff --git a/tslint.json b/tslint.json index bdd783e2868..299a1049e4c 100644 --- a/tslint.json +++ b/tslint.json @@ -98,7 +98,6 @@ "object-literal-key-quotes": false, "ordered-imports": false, "prefer-conditional-expression": false, - "prefer-for-of": false, "radix": false, "space-before-function-paren": false, "trailing-comma": false,