Merge pull request #13006 from SaschaNaz/lintfix

Fix latest tslint errors
This commit is contained in:
Mohamed Hegazy
2016-12-26 11:24:25 -08:00
committed by GitHub
29 changed files with 81 additions and 292 deletions
-1
View File
@@ -1179,7 +1179,6 @@ task("update-sublime", ["local", serverFile], function () {
var tslintRuleDir = "scripts/tslint";
var tslintRules = [
"nextLineRule",
"preferConstRule",
"booleanTriviaRule",
"typeOperatorSpacingRule",
"noInOperatorRule",
+1 -1
View File
@@ -75,7 +75,7 @@
"through2": "latest",
"travis-fold": "latest",
"ts-node": "latest",
"tslint": "4.0.0-dev.3",
"tslint": "next",
"typescript": "next"
},
"scripts": {
-204
View File
@@ -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<DeclarationUsages>[] = [];
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 = (<ts.BindingElement>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<DeclarationUsages> = {};
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<DeclarationUsages> = {};
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<DeclarationUsages>) {
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<DeclarationUsages>) {
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<DeclarationUsages>) {
for (const element of pattern.elements) {
if (element.kind === ts.SyntaxKind.BindingElement) {
this.collectNameIdentifiers(value, (<ts.BindingElement>element).name, table);
}
}
}
}
+2 -2
View File
@@ -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 = <JSDocFunctionType>node.parent;
let index = indexOf(functionType.parameters, node);
const functionType = <JSDocFunctionType>node.parent;
const index = indexOf(functionType.parameters, node);
return "arg" + index;
case SyntaxKind.JSDocTypedefTag:
const parentNode = node.parent && node.parent.parent;
+8 -8
View File
@@ -5042,7 +5042,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;
@@ -5141,7 +5141,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:
@@ -5791,8 +5791,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;
}
}
@@ -7945,7 +7945,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;
@@ -18093,7 +18093,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);
@@ -18373,7 +18373,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) {
@@ -20817,7 +20817,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;
+2 -2
View File
@@ -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) {
+6 -6
View File
@@ -119,7 +119,7 @@ namespace ts {
*/
export function forEach<T, U>(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<T>(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<T>(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<T, U>(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<T>(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;
}
+3 -3
View File
@@ -1679,8 +1679,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 = <MethodDeclaration>node;
let nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier &&
const methodDeclaration = <MethodDeclaration>node;
const nameIsConstructor = methodDeclaration.name.kind === SyntaxKind.Identifier &&
(<Identifier>methodDeclaration.name).originalKeywordKind === SyntaxKind.ConstructorKeyword;
return !nameIsConstructor;
@@ -7406,7 +7406,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) {
+6 -5
View File
@@ -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
@@ -700,7 +701,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]);
}
@@ -957,7 +958,7 @@ namespace ts {
}
break;
case SyntaxKind.HeritageClause:
let heritageClause = <HeritageClause>node;
const heritageClause = <HeritageClause>node;
if (heritageClause.token === SyntaxKind.ImplementsKeyword) {
diagnostics.push(createDiagnosticForNode(node, Diagnostics.implements_clauses_can_only_be_used_in_a_ts_file));
return;
@@ -976,7 +977,7 @@ namespace ts {
diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file));
return;
case SyntaxKind.TypeAssertionExpression:
let typeAssertionExpression = <TypeAssertion>node;
const typeAssertionExpression = <TypeAssertion>node;
diagnostics.push(createDiagnosticForNode(typeAssertionExpression.type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file));
return;
}
@@ -1175,7 +1176,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;
}
+4 -4
View File
@@ -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();
+1 -1
View File
@@ -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();
+6 -6
View File
@@ -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;
}
@@ -784,7 +784,7 @@ namespace ts {
switch (node.kind) {
case SyntaxKind.YieldExpression:
visitor(<YieldExpression>node);
let operand = (<YieldExpression>node).expression;
const operand = (<YieldExpression>node).expression;
if (operand) {
traverse(operand);
}
@@ -1247,7 +1247,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:
@@ -1269,13 +1269,13 @@ namespace ts {
case SyntaxKind.SwitchStatement:
return (<ExpressionStatement>parent).expression === node;
case SyntaxKind.ForStatement:
let forStatement = <ForStatement>parent;
const forStatement = <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 = <ForInStatement | ForOfStatement>parent;
const forInStatement = <ForInStatement | ForOfStatement>parent;
return (forInStatement.initializer === node && forInStatement.initializer.kind !== SyntaxKind.VariableDeclarationList) ||
forInStatement.expression === node;
case SyntaxKind.TypeAssertionExpression:
+6 -6
View File
@@ -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]);
}
}
+1 -1
View File
@@ -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];
@@ -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);
}
@@ -502,7 +502,7 @@ describe("PatternMatcher", function () {
function assertArrayEquals<T>(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]);
}
}
+1 -1
View File
@@ -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];
+5 -8
View File
@@ -113,7 +113,7 @@ namespace ts.server {
if (len > 1) {
let insertedNodes = <LineCollection[]>new Array(len - 1);
let startNode = <LineCollection>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();
}
+2 -1
View File
@@ -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) {
+6 -6
View File
@@ -560,11 +560,11 @@ namespace ts.BreakpointResolver {
function spanInOpenBraceToken(node: Node): TextSpan {
switch (node.parent.kind) {
case SyntaxKind.EnumDeclaration:
let enumDeclaration = <EnumDeclaration>node.parent;
const enumDeclaration = <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 = <ClassDeclaration>node.parent;
const classDeclaration = <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 = <CaseBlock>node.parent;
let lastClause = lastOrUndefined(caseBlock.clauses);
const caseBlock = <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 = <BindingPattern>node.parent;
const bindingPattern = <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 = <BindingPattern>node.parent;
const bindingPattern = <BindingPattern>node.parent;
return textSpan(lastOrUndefined(bindingPattern.elements) || bindingPattern);
default:
+3 -5
View File
@@ -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 = <ClassificationType>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);
+1 -1
View File
@@ -964,7 +964,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
+2 -2
View File
@@ -76,7 +76,7 @@ namespace ts.JsDoc {
*/
function forEachUnique<T, U>(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 ?
(<Identifier>currentName).text :
+1 -1
View File
@@ -146,7 +146,7 @@ namespace ts.NavigationBar {
break;
case SyntaxKind.ImportClause:
let importClause = <ImportClause>node;
const importClause = <ImportClause>node;
// Handle default import case e.g.:
// import d from "mod";
if (importClause.name) {
+4 -3
View File
@@ -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));
+1 -1
View File
@@ -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];
}
+1 -1
View File
@@ -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;
+2 -3
View File
@@ -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'
+1 -1
View File
@@ -48,7 +48,7 @@ function log(msg: string) {
}
let directorySeparator = "/";
const directorySeparator = "/";
function getRootLength(path: string): number {
if (path.charAt(0) === directorySeparator) {