Skip to content

Commit

Permalink
chore: enable prefer-for-of rule internally (#4977)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 15, 2022
1 parent 1867728 commit 8e72bf1
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 43 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -64,6 +64,7 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/unbound-method': 'off',
Expand Down
7 changes: 2 additions & 5 deletions packages/eslint-plugin/src/rules/no-unsafe-argument.ts
Expand Up @@ -176,9 +176,7 @@ export default util.createRule<[], MessageIds>({
return;
}

for (let i = 0; i < node.arguments.length; i += 1) {
const argument = node.arguments[i];

for (const argument of node.arguments) {
switch (argument.type) {
// spreads consume
case AST_NODE_TYPES.SpreadElement: {
Expand All @@ -204,8 +202,7 @@ export default util.createRule<[], MessageIds>({
// foo(...[tuple1, tuple2])
const spreadTypeArguments =
checker.getTypeArguments(spreadArgType);
for (let j = 0; j < spreadTypeArguments.length; j += 1) {
const tupleType = spreadTypeArguments[j];
for (const tupleType of spreadTypeArguments) {
const parameterType = signature.getNextParameterType();
if (parameterType == null) {
continue;
Expand Down
7 changes: 1 addition & 6 deletions packages/eslint-plugin/src/rules/no-unsafe-assignment.ts
Expand Up @@ -164,12 +164,7 @@ export default util.createRule({
);

let didReport = false;
for (
let receiverIndex = 0;
receiverIndex < receiverNode.properties.length;
receiverIndex += 1
) {
const receiverProperty = receiverNode.properties[receiverIndex];
for (const receiverProperty of receiverNode.properties) {
if (receiverProperty.type === AST_NODE_TYPES.RestElement) {
// don't bother checking rest
continue;
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Expand Up @@ -419,9 +419,7 @@ export default util.createRule<Options, MessageIds>({

const unusedVars = collectUnusedVariables();

for (let i = 0, l = unusedVars.length; i < l; ++i) {
const unusedVar = unusedVars[i];

for (const unusedVar of unusedVars) {
// Report the first declaration.
if (unusedVar.defs.length > 0) {
context.report({
Expand Down
12 changes: 2 additions & 10 deletions packages/scope-manager/src/ScopeManager.ts
Expand Up @@ -132,17 +132,9 @@ class ScopeManager {
return scope;
}
}
} else {
for (let i = 0; i < scopes.length; ++i) {
const scope = scopes[i];

if (predicate(scope)) {
return scope;
}
}
return null;
}

return null;
return scopes.find(predicate) ?? null;
}

protected nestScope<T extends Scope>(scope: T): T;
Expand Down
12 changes: 3 additions & 9 deletions packages/scope-manager/src/scope/ScopeBase.ts
Expand Up @@ -81,9 +81,7 @@ function isStrictScope(
}

// Search 'use strict' directive.
for (let i = 0; i < body.body.length; ++i) {
const stmt = body.body[i];

for (const stmt of body.body) {
if (stmt.type !== AST_NODE_TYPES.ExpressionStatement) {
break;
}
Expand Down Expand Up @@ -358,7 +356,7 @@ abstract class ScopeBase<
};

public close(scopeManager: ScopeManager): Scope | null {
let closeRef;
let closeRef: (ref: Reference, scopeManager: ScopeManager) => void;

if (this.shouldStaticallyClose()) {
closeRef = this.#staticCloseRef;
Expand All @@ -370,11 +368,7 @@ abstract class ScopeBase<

// Try Resolving all references in this scope.
assert(this.leftToResolve);
for (let i = 0; i < this.leftToResolve.length; ++i) {
const ref = this.leftToResolve[i];

closeRef(ref, scopeManager);
}
this.leftToResolve.forEach(ref => closeRef(ref, scopeManager));
this.leftToResolve = null;

return this.upper;
Expand Down
5 changes: 1 addition & 4 deletions packages/scope-manager/src/scope/WithScope.ts
Expand Up @@ -22,10 +22,7 @@ class WithScope extends ScopeBase<
return super.close(scopeManager);
}
assert(this.leftToResolve);
for (let i = 0; i < this.leftToResolve.length; ++i) {
const ref = this.leftToResolve[i];
this.delegateToUpperScope(ref);
}
this.leftToResolve.forEach(ref => this.delegateToUpperScope(ref));
this.leftToResolve = null;
return this.upper;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -692,8 +692,7 @@ export class Converter {
* to the result, we remove them from the array, so that they
* are not handled twice.
*/
for (let i = 0; i < modifiers.length; i++) {
const modifier = modifiers[i];
for (const modifier of modifiers) {
switch (modifier.kind) {
/**
* Ignore ExportKeyword and DefaultKeyword, they are handled
Expand Down
3 changes: 1 addition & 2 deletions packages/typescript-estree/src/node-utils.ts
Expand Up @@ -278,8 +278,7 @@ export function getTSNodeAccessibility(
if (!modifiers) {
return null;
}
for (let i = 0; i < modifiers.length; i++) {
const modifier = modifiers[i];
for (const modifier of modifiers) {
switch (modifier.kind) {
case SyntaxKind.PublicKeyword:
return 'public';
Expand Down
3 changes: 1 addition & 2 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Expand Up @@ -240,8 +240,7 @@ export function preprocessBabylonAST(ast: File): any {
* @see https://github.com/babel/babel/blob/381277a/eslint/babel-eslint-parser/src/convert/convertAST.cjs#L81-L102
*/
TemplateLiteral(node: any) {
for (let j = 0; j < node.quasis.length; j++) {
const q = node.quasis[j];
for (const q of node.quasis) {
q.range[0] -= 1;
q.loc.start.column -= 1;
if (q.tail) {
Expand Down

0 comments on commit 8e72bf1

Please sign in to comment.