-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request babel/eslint-plugin-babel#123 from daltones/master
Deprecate rule `no-await-in-loop`
- Loading branch information
Showing
3 changed files
with
24 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,24 @@ | ||
/** | ||
* @fileoverview Rule to disallow uses of await inside of loops. | ||
* @author Nat Mote | ||
*/ | ||
"use strict"; | ||
|
||
// Node types which are considered loops. | ||
var loopTypes = { | ||
'ForStatement': true, | ||
'ForOfStatement': true, | ||
'ForInStatement': true, | ||
'WhileStatement': true, | ||
'DoWhileStatement': true, | ||
}; | ||
var isWarnedForDeprecation = false; | ||
module.exports = { | ||
meta: { | ||
deprecated: true, | ||
schema: [] | ||
}, | ||
create: function() { | ||
return { | ||
Program: function() { | ||
if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) { | ||
return; | ||
} | ||
|
||
// Node types at which we should stop looking for loops. For example, it is fine to declare an async | ||
// function within a loop, and use await inside of that. | ||
var boundaryTypes = { | ||
'FunctionDeclaration': true, | ||
'FunctionExpression': true, | ||
'ArrowFunctionExpression': true, | ||
/* eslint-disable no-console */ | ||
console.log('The babel/no-await-in-loop rule is deprecated. Please ' + | ||
'use the built in no-await-in-loop rule instead.'); | ||
/* eslint-enable no-console */ | ||
isWarnedForDeprecation = true; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
module.exports = function(context) { | ||
return { | ||
AwaitExpression(node) { | ||
var ancestors = context.getAncestors(); | ||
// Reverse so that we can traverse from the deepest node upwards. | ||
ancestors.reverse(); | ||
// Create a set of all the ancestors plus this node so that we can check | ||
// if this use of await appears in the body of the loop as opposed to | ||
// the right-hand side of a for...of, for example. | ||
// | ||
// Implement the set with an Array since there are likely to be very few | ||
// elements. An Object would not be appropriate since the elements are | ||
// not strings. | ||
var ancestorSet = [].concat(ancestors, [node]); | ||
var ancestorSetHas = function(element) { | ||
return ancestorSet.indexOf(element) !== -1; | ||
} | ||
for (var i = 0; i < ancestors.length; i++) { | ||
var ancestor = ancestors[i]; | ||
if (boundaryTypes.hasOwnProperty(ancestor.type)) { | ||
// Short-circuit out if we encounter a boundary type. Loops above | ||
// this do not matter. | ||
return; | ||
} | ||
if (loopTypes.hasOwnProperty(ancestor.type)) { | ||
// Only report if we are actually in the body or another part that gets executed on | ||
// every iteration. | ||
if ( | ||
ancestorSetHas(ancestor.body) || | ||
ancestorSetHas(ancestor.test) || | ||
ancestorSetHas(ancestor.update) | ||
) { | ||
context.report( | ||
node, | ||
'Avoid using await inside a loop. Consider refactoring to use Promise.all. If ' + | ||
'you are sure you want to do this, add `// eslint-disable-line ' + | ||
context.id + '` at the end of this line.' | ||
); | ||
return; | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |
106 changes: 0 additions & 106 deletions
106
eslint/babel-eslint-plugin/tests/rules/no-await-in-loop.js
This file was deleted.
Oops, something went wrong.