diff --git a/Jakefile.js b/Jakefile.js index 275303dda13fd..6cde8ac3c148d 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -1179,7 +1179,6 @@ task("update-sublime", ["local", serverFile], function () { var tslintRuleDir = "scripts/tslint"; var tslintRules = [ "nextLineRule", - "preferConstRule", "booleanTriviaRule", "typeOperatorSpacingRule", "noInOperatorRule", diff --git a/package.json b/package.json index 2de37b712aba6..fdb85ea764b8c 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ "through2": "latest", "travis-fold": "latest", "ts-node": "latest", - "tslint": "4.0.0-dev.3", + "tslint": "next", "typescript": "next" }, "scripts": { diff --git a/scripts/tslint/preferConstRule.ts b/scripts/tslint/preferConstRule.ts deleted file mode 100644 index 28d7446b29049..0000000000000 --- a/scripts/tslint/preferConstRule.ts +++ /dev/null @@ -1,204 +0,0 @@ -import * as Lint from "tslint/lib"; -import * as ts from "typescript"; - -export class Rule extends Lint.Rules.AbstractRule { - public static FAILURE_STRING_FACTORY = (identifier: string) => `Identifier '${identifier}' never appears on the LHS of an assignment - use const instead of let for its declaration.`; - - public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithWalker(new PreferConstWalker(sourceFile, this.getOptions())); - } -} - -function isLet(node: ts.Node) { - return !!(ts.getCombinedNodeFlags(node) & ts.NodeFlags.Let); -} - -function isExported(node: ts.Node) { - return !!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export); -} - -function isAssignmentOperator(token: ts.SyntaxKind): boolean { - return token >= ts.SyntaxKind.FirstAssignment && token <= ts.SyntaxKind.LastAssignment; -} - -function isBindingLiteralExpression(node: ts.Node): node is (ts.ArrayLiteralExpression | ts.ObjectLiteralExpression) { - return (!!node) && (node.kind === ts.SyntaxKind.ObjectLiteralExpression || node.kind === ts.SyntaxKind.ArrayLiteralExpression); -} - -interface DeclarationUsages { - declaration: ts.VariableDeclaration; - usages: number; -} - -class PreferConstWalker extends Lint.RuleWalker { - private inScopeLetDeclarations: ts.MapLike[] = []; - private errors: Lint.RuleFailure[] = []; - private markAssignment(identifier: ts.Identifier) { - const name = identifier.text; - for (let i = this.inScopeLetDeclarations.length - 1; i >= 0; i--) { - const declarations = this.inScopeLetDeclarations[i]; - if (declarations[name]) { - declarations[name].usages++; - break; - } - } - } - - visitSourceFile(node: ts.SourceFile) { - super.visitSourceFile(node); - // Sort errors by position because tslint doesn't - this.errors.sort((a, b) => a.getStartPosition().getPosition() - b.getStartPosition().getPosition()).forEach(e => this.addFailure(e)); - } - - visitBinaryExpression(node: ts.BinaryExpression) { - if (isAssignmentOperator(node.operatorToken.kind)) { - this.visitLeftHandSideExpression(node.left); - } - super.visitBinaryExpression(node); - } - - private visitLeftHandSideExpression(node: ts.Expression) { - while (node.kind === ts.SyntaxKind.ParenthesizedExpression) { - node = (node as ts.ParenthesizedExpression).expression; - } - if (node.kind === ts.SyntaxKind.Identifier) { - this.markAssignment(node as ts.Identifier); - } - else if (isBindingLiteralExpression(node)) { - this.visitBindingLiteralExpression(node as (ts.ArrayLiteralExpression | ts.ObjectLiteralExpression)); - } - } - - private visitBindingLiteralExpression(node: ts.ArrayLiteralExpression | ts.ObjectLiteralExpression) { - if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) { - const pattern = node as ts.ObjectLiteralExpression; - for (const element of pattern.properties) { - const kind = element.kind; - - if (kind === ts.SyntaxKind.ShorthandPropertyAssignment) { - this.markAssignment((element as ts.ShorthandPropertyAssignment).name); - } - else if (kind === ts.SyntaxKind.PropertyAssignment) { - this.visitLeftHandSideExpression((element as ts.PropertyAssignment).initializer); - } - } - } - else if (node.kind === ts.SyntaxKind.ArrayLiteralExpression) { - const pattern = node as ts.ArrayLiteralExpression; - for (const element of pattern.elements) { - this.visitLeftHandSideExpression(element); - } - } - } - - private visitBindingPatternIdentifiers(pattern: ts.BindingPattern) { - for (const element of pattern.elements) { - if (element.kind !== ts.SyntaxKind.BindingElement) { - continue; - } - - const name = (element).name; - if (name.kind === ts.SyntaxKind.Identifier) { - this.markAssignment(name as ts.Identifier); - } - else { - this.visitBindingPatternIdentifiers(name as ts.BindingPattern); - } - } - } - - visitPrefixUnaryExpression(node: ts.PrefixUnaryExpression) { - this.visitAnyUnaryExpression(node); - super.visitPrefixUnaryExpression(node); - } - - visitPostfixUnaryExpression(node: ts.PostfixUnaryExpression) { - this.visitAnyUnaryExpression(node); - super.visitPostfixUnaryExpression(node); - } - - private visitAnyUnaryExpression(node: ts.PrefixUnaryExpression | ts.PostfixUnaryExpression) { - if (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken) { - this.visitLeftHandSideExpression(node.operand); - } - } - - visitModuleDeclaration(node: ts.ModuleDeclaration) { - if (node.body.kind === ts.SyntaxKind.ModuleBlock) { - // For some reason module blocks are left out of the visit block traversal - this.visitBlock(node.body as any as ts.Block); - } - super.visitModuleDeclaration(node); - } - - visitForOfStatement(node: ts.ForOfStatement) { - this.visitAnyForStatement(node); - super.visitForOfStatement(node); - this.popDeclarations(); - } - - visitForInStatement(node: ts.ForInStatement) { - this.visitAnyForStatement(node); - super.visitForInStatement(node); - this.popDeclarations(); - } - - private visitAnyForStatement(node: ts.ForOfStatement | ts.ForInStatement) { - const names: ts.MapLike = {}; - if (isLet(node.initializer)) { - if (node.initializer.kind === ts.SyntaxKind.VariableDeclarationList) { - this.collectLetIdentifiers(node.initializer as ts.VariableDeclarationList, names); - } - } - this.inScopeLetDeclarations.push(names); - } - - private popDeclarations() { - const completed = this.inScopeLetDeclarations.pop(); - for (const name in completed) { - if (Object.hasOwnProperty.call(completed, name)) { - const element = completed[name]; - if (element.usages === 0) { - this.errors.push(this.createFailure(element.declaration.getStart(this.getSourceFile()), element.declaration.getWidth(this.getSourceFile()), Rule.FAILURE_STRING_FACTORY(name))); - } - } - } - } - - visitBlock(node: ts.Block) { - const names: ts.MapLike = {}; - for (const statement of node.statements) { - if (statement.kind === ts.SyntaxKind.VariableStatement) { - this.collectLetIdentifiers((statement as ts.VariableStatement).declarationList, names); - } - } - this.inScopeLetDeclarations.push(names); - super.visitBlock(node); - this.popDeclarations(); - } - - private collectLetIdentifiers(list: ts.VariableDeclarationList, ret: ts.MapLike) { - for (const node of list.declarations) { - if (isLet(node) && !isExported(node)) { - this.collectNameIdentifiers(node, node.name, ret); - } - } - } - - private collectNameIdentifiers(declaration: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.MapLike) { - if (node.kind === ts.SyntaxKind.Identifier) { - table[(node as ts.Identifier).text] = { declaration, usages: 0 }; - } - else { - this.collectBindingPatternIdentifiers(declaration, node as ts.BindingPattern, table); - } - } - - private collectBindingPatternIdentifiers(value: ts.VariableDeclaration, pattern: ts.BindingPattern, table: ts.MapLike) { - for (const element of pattern.elements) { - if (element.kind === ts.SyntaxKind.BindingElement) { - this.collectNameIdentifiers(value, (element).name, table); - } - } - } -} diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index a6f5993f6c88d..a00c2f0efc7fa 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -283,8 +283,8 @@ namespace ts { // Parameters with names are handled at the top of this function. Parameters // without names can only come from JSDocFunctionTypes. Debug.assert(node.parent.kind === SyntaxKind.JSDocFunctionType); - let functionType = node.parent; - let index = indexOf(functionType.parameters, node); + const functionType = node.parent; + const index = indexOf(functionType.parameters, node); return "arg" + index; case SyntaxKind.JSDocTypedefTag: const parentNode = node.parent && node.parent.parent; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5246bbfc7001b..e7e53c9e7acb4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5019,7 +5019,7 @@ namespace ts { // If this is a JSDoc construct signature, then skip the first parameter in the // parameter list. The first parameter represents the return type of the construct // signature. - for (let i = isJSConstructSignature ? 1 : 0, n = declaration.parameters.length; i < n; i++) { + for (let i = isJSConstructSignature ? 1 : 0; i < declaration.parameters.length; i++) { const param = declaration.parameters[i]; let paramSymbol = param.symbol; @@ -5118,7 +5118,7 @@ namespace ts { function getSignaturesOfSymbol(symbol: Symbol): Signature[] { if (!symbol) return emptyArray; const result: Signature[] = []; - for (let i = 0, len = symbol.declarations.length; i < len; i++) { + for (let i = 0; i < symbol.declarations.length; i++) { const node = symbol.declarations[i]; switch (node.kind) { case SyntaxKind.FunctionType: @@ -5768,8 +5768,8 @@ namespace ts { } function isSubtypeOfAny(candidate: Type, types: Type[]): boolean { - for (let i = 0, len = types.length; i < len; i++) { - if (candidate !== types[i] && isTypeSubtypeOf(candidate, types[i])) { + for (const type of types) { + if (candidate !== type && isTypeSubtypeOf(candidate, type)) { return true; } } @@ -7911,7 +7911,7 @@ namespace ts { return Ternary.False; } let result = Ternary.True; - for (let i = 0, len = sourceSignatures.length; i < len; i++) { + for (let i = 0; i < sourceSignatures.length; i++) { const related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo); if (!related) { return Ternary.False; @@ -18044,7 +18044,7 @@ namespace ts { /** Check each type parameter and check that type parameters have no duplicate type parameter declarations */ function checkTypeParameters(typeParameterDeclarations: TypeParameterDeclaration[]) { if (typeParameterDeclarations) { - for (let i = 0, n = typeParameterDeclarations.length; i < n; i++) { + for (let i = 0; i < typeParameterDeclarations.length; i++) { const node = typeParameterDeclarations[i]; checkTypeParameter(node); @@ -18324,7 +18324,7 @@ namespace ts { // TypeScript 1.0 spec (April 2014): // When a generic interface has multiple declarations, all declarations must have identical type parameter // lists, i.e. identical type parameter names with identical constraints in identical order. - for (let i = 0, len = list1.length; i < len; i++) { + for (let i = 0; i < list1.length; i++) { const tp1 = list1[i]; const tp2 = list2[i]; if (tp1.name.text !== tp2.name.text) { @@ -20761,7 +20761,7 @@ namespace ts { case SyntaxKind.PublicKeyword: case SyntaxKind.ProtectedKeyword: case SyntaxKind.PrivateKeyword: - let text = visibilityToString(modifierToFlag(modifier.kind)); + const text = visibilityToString(modifierToFlag(modifier.kind)); if (modifier.kind === SyntaxKind.ProtectedKeyword) { lastProtected = modifier; diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index a4257a610e181..25b5f5416ddf1 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -620,7 +620,7 @@ namespace ts { break; case "boolean": // boolean flag has optional value true, false, others - let optValue = args[i]; + const optValue = args[i]; options[opt.name] = optValue !== "false"; // consume next argument as boolean flag value if (optValue === "false" || optValue === "true") { @@ -778,7 +778,7 @@ namespace ts { break; default: const value = options[name]; - let optionDefinition = optionsNameMap[name.toLowerCase()]; + const optionDefinition = optionsNameMap[name.toLowerCase()]; if (optionDefinition) { const customTypeMap = getCustomTypeMapOfCommandLineOption(optionDefinition); if (!customTypeMap) { diff --git a/src/compiler/core.ts b/src/compiler/core.ts index ceff135957940..2daf5c4e564cf 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -119,7 +119,7 @@ namespace ts { */ export function forEach(array: T[] | undefined, callback: (element: T, index: number) => U | undefined): U | undefined { if (array) { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { const result = callback(array[i], i); if (result) { return result; @@ -143,7 +143,7 @@ namespace ts { */ export function every(array: T[], callback: (element: T, index: number) => boolean): boolean { if (array) { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { if (!callback(array[i], i)) { return false; } @@ -155,7 +155,7 @@ namespace ts { /** Works like Array.prototype.find, returning `undefined` if no element satisfying the predicate is found. */ export function find(array: T[], predicate: (element: T, index: number) => boolean): T | undefined { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { const value = array[i]; if (predicate(value, i)) { return value; @@ -169,7 +169,7 @@ namespace ts { * This is like `forEach`, but never returns undefined. */ export function findMap(array: T[], callback: (element: T, index: number) => U | undefined): U { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { const result = callback(array[i], i); if (result) { return result; @@ -191,7 +191,7 @@ namespace ts { export function indexOf(array: T[], value: T): number { if (array) { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { if (array[i] === value) { return i; } @@ -201,7 +201,7 @@ namespace ts { } export function indexOfAnyCharCode(text: string, charCodes: number[], start?: number): number { - for (let i = start || 0, len = text.length; i < len; i++) { + for (let i = start || 0; i < text.length; i++) { if (contains(charCodes, text.charCodeAt(i))) { return i; } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2f0d6a4d9754e..56dd42a1045af 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1678,8 +1678,8 @@ namespace ts { // Method declarations are not necessarily reusable. An object-literal // may have a method calls "constructor(...)" and we must reparse that // into an actual .ConstructorDeclaration. - let methodDeclaration = node; - let nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier && + const methodDeclaration = node; + const nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier && (methodDeclaration.name).originalKeywordKind === SyntaxKind.ConstructorKeyword; return !nameIsConstructor; @@ -7404,7 +7404,7 @@ namespace ts { if (position >= array.pos && position < array.end) { // position was in this array. Search through this array to see if we find a // viable element. - for (let i = 0, n = array.length; i < n; i++) { + for (let i = 0; i < array.length; i++) { const child = array[i]; if (child) { if (child.pos === position) { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 73976d5d02e73..99160a1e5e6d0 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -40,7 +40,8 @@ namespace ts { return; } - for (let i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + const n = Math.min(commonPathComponents.length, sourcePathComponents.length); + for (let i = 0; i < n; i++) { if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { if (i === 0) { // Failed to find any common path component @@ -695,7 +696,7 @@ namespace ts { } // update fileName -> file mapping - for (let i = 0, len = newSourceFiles.length; i < len; i++) { + for (let i = 0; i < newSourceFiles.length; i++) { filesByName.set(filePaths[i], newSourceFiles[i]); } @@ -952,7 +953,7 @@ namespace ts { } break; case SyntaxKind.HeritageClause: - let heritageClause = node; + const heritageClause = node; if (heritageClause.token === SyntaxKind.ImplementsKeyword) { diagnostics.push(createDiagnosticForNode(node, Diagnostics.implements_clauses_can_only_be_used_in_a_ts_file)); return; @@ -971,7 +972,7 @@ namespace ts { diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file)); return; case SyntaxKind.TypeAssertionExpression: - let typeAssertionExpression = node; + const typeAssertionExpression = node; diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file)); return; } @@ -1170,7 +1171,7 @@ namespace ts { case SyntaxKind.ImportDeclaration: case SyntaxKind.ImportEqualsDeclaration: case SyntaxKind.ExportDeclaration: - let moduleNameExpr = getExternalModuleName(node); + const moduleNameExpr = getExternalModuleName(node); if (!moduleNameExpr || moduleNameExpr.kind !== SyntaxKind.StringLiteral) { break; } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 82302e98e37a8..a2ca60578459b 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -531,7 +531,7 @@ namespace ts { const ch = text.charCodeAt(pos); if ((pos + mergeConflictMarkerLength) < text.length) { - for (let i = 0, n = mergeConflictMarkerLength; i < n; i++) { + for (let i = 0; i < mergeConflictMarkerLength; i++) { if (text.charCodeAt(pos + i) !== ch) { return false; } @@ -643,7 +643,7 @@ namespace ts { pos++; continue; case CharacterCodes.slash: - let nextChar = text.charCodeAt(pos + 1); + const nextChar = text.charCodeAt(pos + 1); let hasTrailingNewLine = false; if (nextChar === CharacterCodes.slash || nextChar === CharacterCodes.asterisk) { const kind = nextChar === CharacterCodes.slash ? SyntaxKind.SingleLineCommentTrivia : SyntaxKind.MultiLineCommentTrivia; @@ -766,7 +766,7 @@ namespace ts { return false; } - for (let i = 1, n = name.length; i < n; i++) { + for (let i = 1; i < name.length; i++) { if (!isIdentifierPart(name.charCodeAt(i), languageVersion)) { return false; } @@ -1563,7 +1563,7 @@ namespace ts { pos++; return token = SyntaxKind.AtToken; case CharacterCodes.backslash: - let cookedChar = peekUnicodeEscape(); + const cookedChar = peekUnicodeEscape(); if (cookedChar >= 0 && isIdentifierStart(cookedChar, languageVersion)) { pos += 6; tokenValue = String.fromCharCode(cookedChar) + scanIdentifierParts(); diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index a1e82a66595b0..8e140375dd028 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -160,7 +160,7 @@ namespace ts { function getNames(collection: any): string[] { const result: string[] = []; - for (let e = new Enumerator(collection); !e.atEnd(); e.moveNext()) { + for (const e = new Enumerator(collection); !e.atEnd(); e.moveNext()) { result.push(e.item().Name); } return result.sort(); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6f491f6708fee..5666451717686 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -550,7 +550,7 @@ namespace ts { let errorNode = node; switch (node.kind) { case SyntaxKind.SourceFile: - let pos = skipTrivia(sourceFile.text, 0, /*stopAfterLineBreak*/ false); + const pos = skipTrivia(sourceFile.text, 0, /*stopAfterLineBreak*/ false); if (pos === sourceFile.text.length) { // file is empty - return span for the beginning of the file return createTextSpan(0, 0); @@ -682,7 +682,7 @@ namespace ts { case SyntaxKind.QualifiedName: case SyntaxKind.PropertyAccessExpression: case SyntaxKind.ThisKeyword: - let parent = node.parent; + const parent = node.parent; if (parent.kind === SyntaxKind.TypeQuery) { return false; } @@ -770,7 +770,7 @@ namespace ts { switch (node.kind) { case SyntaxKind.YieldExpression: visitor(node); - let operand = (node).expression; + const operand = (node).expression; if (operand) { traverse(operand); } @@ -1222,7 +1222,7 @@ namespace ts { case SyntaxKind.NumericLiteral: case SyntaxKind.StringLiteral: case SyntaxKind.ThisKeyword: - let parent = node.parent; + const parent = node.parent; switch (parent.kind) { case SyntaxKind.VariableDeclaration: case SyntaxKind.Parameter: @@ -1244,13 +1244,13 @@ namespace ts { case SyntaxKind.SwitchStatement: return (parent).expression === node; case SyntaxKind.ForStatement: - let forStatement = parent; + const forStatement = parent; return (forStatement.initializer === node && forStatement.initializer.kind !== SyntaxKind.VariableDeclarationList) || forStatement.condition === node || forStatement.incrementor === node; case SyntaxKind.ForInStatement: case SyntaxKind.ForOfStatement: - let forInStatement = parent; + const forInStatement = parent; return (forInStatement.initializer === node && forInStatement.initializer.kind !== SyntaxKind.VariableDeclarationList) || forInStatement.expression === node; case SyntaxKind.TypeAssertionExpression: diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 26a4a6f963dc9..4094fc773ea6c 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -85,7 +85,7 @@ namespace Utils { eval(fileContents); break; case ExecutionEnvironment.Node: - let vm = require("vm"); + const vm = require("vm"); if (nodeContext) { vm.runInNewContext(fileContents, nodeContext, fileName); } @@ -175,9 +175,9 @@ namespace Utils { assert.isFalse(array.end > node.end, "array.end > node.end"); assert.isFalse(array.pos < currentPos, "array.pos < currentPos"); - for (let i = 0, n = array.length; i < n; i++) { - assert.isFalse(array[i].pos < currentPos, "array[i].pos < currentPos"); - currentPos = array[i].end; + for (const item of array) { + assert.isFalse(item.pos < currentPos, "array[i].pos < currentPos"); + currentPos = item.end; } currentPos = array.end; @@ -344,7 +344,7 @@ namespace Utils { assert.equal(array1.length, array2.length, "array1.length !== array2.length"); - for (let i = 0, n = array1.length; i < n; i++) { + for (let i = 0; i < array1.length; i++) { const d1 = array1[i]; const d2 = array2[i]; @@ -400,7 +400,7 @@ namespace Utils { assert.equal(array1.end, array2.end, "array1.end !== array2.end"); assert.equal(array1.length, array2.length, "array1.length !== array2.length"); - for (let i = 0, n = array1.length; i < n; i++) { + for (let i = 0; i < array1.length; i++) { assertStructuralEquals(array1[i], array2[i]); } } diff --git a/src/harness/unittests/incrementalParser.ts b/src/harness/unittests/incrementalParser.ts index 6088e5fe082b5..fbd8a60da9233 100644 --- a/src/harness/unittests/incrementalParser.ts +++ b/src/harness/unittests/incrementalParser.ts @@ -28,7 +28,7 @@ namespace ts { const diagnostics2 = file2.parseDiagnostics; assert.equal(diagnostics1.length, diagnostics2.length, "diagnostics1.length !== diagnostics2.length"); - for (let i = 0, n = diagnostics1.length; i < n; i++) { + for (let i = 0; i < diagnostics1.length; i++) { const d1 = diagnostics1[i]; const d2 = diagnostics2[i]; diff --git a/src/harness/unittests/services/colorization.ts b/src/harness/unittests/services/colorization.ts index 0fe63f4ff07d1..70592ea415110 100644 --- a/src/harness/unittests/services/colorization.ts +++ b/src/harness/unittests/services/colorization.ts @@ -13,8 +13,7 @@ describe("Colorization", function () { function getEntryAtPosition(result: ts.ClassificationResult, position: number) { let entryPosition = 0; - for (let i = 0, n = result.entries.length; i < n; i++) { - const entry = result.entries[i]; + for (const entry of result.entries) { if (entryPosition === position) { return entry; } @@ -43,9 +42,7 @@ describe("Colorization", function () { function testLexicalClassification(text: string, initialEndOfLineState: ts.EndOfLineState, ...expectedEntries: ClassificationEntry[]): void { const result = classifier.getClassificationsForLine(text, initialEndOfLineState, /*syntacticClassifierAbsent*/ false); - for (let i = 0, n = expectedEntries.length; i < n; i++) { - const expectedEntry = expectedEntries[i]; - + for (const expectedEntry of expectedEntries) { if (expectedEntry.classification === undefined) { assert.equal(result.finalLexState, expectedEntry.value, "final endOfLineState does not match expected."); } @@ -352,9 +349,9 @@ describe("Colorization", function () { // Adjusts 'pos' by accounting for the length of each portion of the string, // but only return the last given string function track(...vals: string[]): string { - for (let i = 0, n = vals.length; i < n; i++) { + for (const val of vals) { pos += lastLength; - lastLength = vals[i].length; + lastLength = val.length; } return ts.lastOrUndefined(vals); } diff --git a/src/harness/unittests/services/patternMatcher.ts b/src/harness/unittests/services/patternMatcher.ts index 8a70b38ab5e9e..728636e9af2a6 100644 --- a/src/harness/unittests/services/patternMatcher.ts +++ b/src/harness/unittests/services/patternMatcher.ts @@ -502,7 +502,7 @@ describe("PatternMatcher", function () { function assertArrayEquals(array1: T[], array2: T[]) { assert.equal(array1.length, array2.length); - for (let i = 0, n = array1.length; i < n; i++) { + for (let i = 0; i < array1.length; i++) { assert.equal(array1[i], array2[i]); } } diff --git a/src/harness/unittests/versionCache.ts b/src/harness/unittests/versionCache.ts index 7fb01ee770abb..17a70f59d59e2 100644 --- a/src/harness/unittests/versionCache.ts +++ b/src/harness/unittests/versionCache.ts @@ -307,7 +307,7 @@ and grew 1cm per day`; it("Start pos from line", () => { for (let i = 0; i < iterationCount; i++) { - for (let j = 0, llen = lines.length; j < llen; j++) { + for (let j = 0; j < lines.length; j++) { const lineInfo = lineIndex.lineNumberToInfo(j + 1); const lineIndexOffset = lineInfo.offset; const lineMapOffset = lineMap[j]; diff --git a/src/server/scriptVersionCache.ts b/src/server/scriptVersionCache.ts index e087c3b6dde0b..7f09bcd549b13 100644 --- a/src/server/scriptVersionCache.ts +++ b/src/server/scriptVersionCache.ts @@ -113,7 +113,7 @@ namespace ts.server { if (len > 1) { let insertedNodes = new Array(len - 1); let startNode = leafNode; - for (let i = 1, len = lines.length; i < len; i++) { + for (let i = 1; i < lines.length; i++) { insertedNodes[i - 1] = new LineLeaf(lines[i]); } let pathIndex = this.startPath.length - 2; @@ -341,8 +341,7 @@ namespace ts.server { let snap = this.versions[this.currentVersionToIndex()]; if (this.changes.length > 0) { let snapIndex = snap.index; - for (let i = 0, len = this.changes.length; i < len; i++) { - const change = this.changes[i]; + for (const change of this.changes) { snapIndex = snapIndex.edit(change.pos, change.deleteLen, change.insertedText); } snap = new LineIndexSnapshot(this.currentVersion + 1, this); @@ -366,8 +365,7 @@ namespace ts.server { const textChangeRanges: ts.TextChangeRange[] = []; for (let i = oldVersion + 1; i <= newVersion; i++) { const snap = this.versions[this.versionToIndex(i)]; - for (let j = 0, len = snap.changesSincePreviousVersion.length; j < len; j++) { - const textChange = snap.changesSincePreviousVersion[j]; + for (const textChange of snap.changesSincePreviousVersion) { textChangeRanges[textChangeRanges.length] = textChange.getTextChangeRange(); } } @@ -471,7 +469,7 @@ namespace ts.server { load(lines: string[]) { if (lines.length > 0) { const leaves: LineLeaf[] = []; - for (let i = 0, len = lines.length; i < len; i++) { + for (let i = 0; i < lines.length; i++) { leaves[i] = new LineLeaf(lines[i]); } this.root = LineIndex.buildTreeFromBottom(leaves); @@ -643,8 +641,7 @@ namespace ts.server { updateCounts() { this.totalChars = 0; this.totalLines = 0; - for (let i = 0, len = this.children.length; i < len; i++) { - const child = this.children[i]; + for (const child of this.children) { this.totalChars += child.charCount(); this.totalLines += child.lineCount(); } diff --git a/src/server/server.ts b/src/server/server.ts index a020ef210fec2..e689a2fc7824f 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -409,7 +409,8 @@ namespace ts.server { function parseLoggingEnvironmentString(logEnvStr: string): LogOptions { const logEnv: LogOptions = { logToFile: true }; const args = logEnvStr.split(" "); - for (let i = 0, len = args.length; i < (len - 1); i += 2) { + const len = args.length - 1; + for (let i = 0; i < len; i += 2) { const option = args[i]; const value = args[i + 1]; if (option && value) { diff --git a/src/services/breakpoints.ts b/src/services/breakpoints.ts index 6825ccb6371ff..c751cf3871b62 100644 --- a/src/services/breakpoints.ts +++ b/src/services/breakpoints.ts @@ -560,11 +560,11 @@ namespace ts.BreakpointResolver { function spanInOpenBraceToken(node: Node): TextSpan { switch (node.parent.kind) { case SyntaxKind.EnumDeclaration: - let enumDeclaration = node.parent; + const enumDeclaration = node.parent; return spanInNodeIfStartsOnSameLine(findPrecedingToken(node.pos, sourceFile, node.parent), enumDeclaration.members.length ? enumDeclaration.members[0] : enumDeclaration.getLastToken(sourceFile)); case SyntaxKind.ClassDeclaration: - let classDeclaration = node.parent; + const classDeclaration = node.parent; return spanInNodeIfStartsOnSameLine(findPrecedingToken(node.pos, sourceFile, node.parent), classDeclaration.members.length ? classDeclaration.members[0] : classDeclaration.getLastToken(sourceFile)); case SyntaxKind.CaseBlock: @@ -600,8 +600,8 @@ namespace ts.BreakpointResolver { case SyntaxKind.CaseBlock: // breakpoint in last statement of the last clause - let caseBlock = node.parent; - let lastClause = lastOrUndefined(caseBlock.clauses); + const caseBlock = node.parent; + const lastClause = lastOrUndefined(caseBlock.clauses); if (lastClause) { return spanInNode(lastOrUndefined(lastClause.statements)); } @@ -609,7 +609,7 @@ namespace ts.BreakpointResolver { case SyntaxKind.ObjectBindingPattern: // Breakpoint in last binding element or binding pattern if it contains no elements - let bindingPattern = node.parent; + const bindingPattern = node.parent; return spanInNode(lastOrUndefined(bindingPattern.elements) || bindingPattern); // Default to parent node @@ -627,7 +627,7 @@ namespace ts.BreakpointResolver { switch (node.parent.kind) { case SyntaxKind.ArrayBindingPattern: // Breakpoint in last binding element or binding pattern if it contains no elements - let bindingPattern = node.parent; + const bindingPattern = node.parent; return textSpan(lastOrUndefined(bindingPattern.elements) || bindingPattern); default: diff --git a/src/services/classifier.ts b/src/services/classifier.ts index c22aec6a78655..4f553924b9970 100644 --- a/src/services/classifier.ts +++ b/src/services/classifier.ts @@ -71,7 +71,7 @@ namespace ts { const dense = classifications.spans; let lastEnd = 0; - for (let i = 0, n = dense.length; i < n; i += 3) { + for (let i = 0; i < dense.length; i += 3) { const start = dense[i]; const length = dense[i + 1]; const type = dense[i + 2]; @@ -605,7 +605,7 @@ namespace ts { Debug.assert(classifications.spans.length % 3 === 0); const dense = classifications.spans; const result: ClassifiedSpan[] = []; - for (let i = 0, n = dense.length; i < n; i += 3) { + for (let i = 0; i < dense.length; i += 3) { result.push({ textSpan: createTextSpan(dense[i], dense[i + 1]), classificationType: getClassificationTypeName(dense[i + 2]) @@ -972,9 +972,7 @@ namespace ts { if (decodedTextSpanIntersectsWith(spanStart, spanLength, element.pos, element.getFullWidth())) { checkForClassificationCancellation(cancellationToken, element.kind); - const children = element.getChildren(sourceFile); - for (let i = 0, n = children.length; i < n; i++) { - const child = children[i]; + for (const child of element.getChildren(sourceFile)) { if (!tryClassifyNode(child)) { // Recurse into our child nodes. processElement(child); diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index bcd8cebb017e7..ba47c4208170c 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -950,7 +950,7 @@ namespace ts.formatting { // shift all parts on the delta size const delta = indentation - nonWhitespaceColumnInFirstPart.column; - for (let i = startIndex, len = parts.length; i < len; i++ , startLine++) { + for (let i = startIndex; i < parts.length; i++ , startLine++) { const startLinePos = getStartPositionOfLine(startLine, sourceFile); const nonWhitespaceCharacterAndColumn = i === 0 diff --git a/src/services/jsDoc.ts b/src/services/jsDoc.ts index 0e65d3b264e3b..08a51a63e63af 100644 --- a/src/services/jsDoc.ts +++ b/src/services/jsDoc.ts @@ -76,7 +76,7 @@ namespace ts.JsDoc { */ function forEachUnique(array: T[], callback: (element: T, index: number) => U): U { if (array) { - for (let i = 0, len = array.length; i < len; i++) { + for (let i = 0; i < array.length; i++) { if (indexOf(array, array[i]) === i) { const result = callback(array[i], i); if (result) { @@ -170,7 +170,7 @@ namespace ts.JsDoc { const isJavaScriptFile = hasJavaScriptFileExtension(sourceFile.fileName); let docParams = ""; - for (let i = 0, numParams = parameters.length; i < numParams; i++) { + for (let i = 0; i < parameters.length; i++) { const currentName = parameters[i].name; const paramName = currentName.kind === SyntaxKind.Identifier ? (currentName).text : diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 1cbaa3d640faf..4e5986abc01c5 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -146,7 +146,7 @@ namespace ts.NavigationBar { break; case SyntaxKind.ImportClause: - let importClause = node; + const importClause = node; // Handle default import case e.g.: // import d from "mod"; if (importClause.name) { diff --git a/src/services/patternMatcher.ts b/src/services/patternMatcher.ts index b4f67ca056ddb..ad63809969576 100644 --- a/src/services/patternMatcher.ts +++ b/src/services/patternMatcher.ts @@ -516,7 +516,8 @@ namespace ts { // Assumes 'value' is already lowercase. function indexOfIgnoringCase(string: string, value: string): number { - for (let i = 0, n = string.length - value.length; i <= n; i++) { + const n = string.length - value.length; + for (let i = 0; i <= n; i++) { if (startsWithIgnoringCase(string, value, i)) { return i; } @@ -527,7 +528,7 @@ namespace ts { // Assumes 'value' is already lowercase. function startsWithIgnoringCase(string: string, value: string, start: number): boolean { - for (let i = 0, n = value.length; i < n; i++) { + for (let i = 0; i < value.length; i++) { const ch1 = toLowerCase(string.charCodeAt(i + start)); const ch2 = value.charCodeAt(i); @@ -613,7 +614,7 @@ namespace ts { const result: TextSpan[] = []; let wordStart = 0; - for (let i = 1, n = identifier.length; i < n; i++) { + for (let i = 1; i < identifier.length; i++) { const lastIsDigit = isDigit(identifier.charCodeAt(i - 1)); const currentIsDigit = isDigit(identifier.charCodeAt(i)); diff --git a/src/services/services.ts b/src/services/services.ts index cf6df71d45b4b..4ca983529bee2 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1793,7 +1793,7 @@ namespace ts { } let descriptor: TodoCommentDescriptor = undefined; - for (let i = 0, n = descriptors.length; i < n; i++) { + for (let i = 0; i < descriptors.length; i++) { if (matchArray[i + firstDescriptorCaptureIndex]) { descriptor = descriptors[i]; } diff --git a/src/services/shims.ts b/src/services/shims.ts index 6184f1e2ff373..cf6bceb816c5c 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -1239,7 +1239,7 @@ namespace ts { } public unregisterShim(shim: Shim): void { - for (let i = 0, n = this._shims.length; i < n; i++) { + for (let i = 0; i < this._shims.length; i++) { if (this._shims[i] === shim) { delete this._shims[i]; return; diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 2f52d747e239d..c66ff6a8059b5 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -675,8 +675,7 @@ namespace ts { } // find the child that contains 'position' - for (let i = 0, n = current.getChildCount(sourceFile); i < n; i++) { - const child = current.getChildAt(i); + for (const child of current.getChildren()) { // all jsDocComment nodes were already visited if (isJSDocNode(child)) { continue; @@ -766,7 +765,7 @@ namespace ts { } const children = n.getChildren(); - for (let i = 0, len = children.length; i < len; i++) { + for (let i = 0; i < children.length; i++) { const child = children[i]; // condition 'position < child.end' checks if child node end after the position // in the example below this condition will be false for 'aaaa' and 'bbbb' and true for 'ccc' diff --git a/tests/webTestServer.ts b/tests/webTestServer.ts index 3d23ef3e961a4..9063d909b1596 100644 --- a/tests/webTestServer.ts +++ b/tests/webTestServer.ts @@ -48,7 +48,7 @@ function log(msg: string) { } -let directorySeparator = "/"; +const directorySeparator = "/"; function getRootLength(path: string): number { if (path.charAt(0) === directorySeparator) {