Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loops containing await #8

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions lib/no-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ module.exports = function (context) {
context.report(node, 'loops are not allowed', { identifier: node.name });
}

var loopsContainAwait = []

function enterLoop() {
loopsContainAwait.unshift(false)
}

function exitLoop(node) {
if (!loopsContainAwait.shift()) reportLoopPresence(node)
}

function foundAwait() {
for (var i = 0; i < loopsContainAwait.length; i++) {
loopsContainAwait[i] = true
}
}

return {
ForStatement: reportLoopPresence,
ForInStatement: reportLoopPresence,
WhileStatement: reportLoopPresence,
DoWhileStatement: reportLoopPresence,
ForOfStatement: reportLoopPresence
ForStatement: enterLoop,
'ForStatement:exit': exitLoop,
ForInStatement: enterLoop,
'ForInStatement:exit': exitLoop,
WhileStatement: enterLoop,
'WhileStatement:exit': exitLoop,
DoWhileStatement: enterLoop,
'DoWhileStatement:exit': exitLoop,
ForOfStatement: enterLoop,
'ForOfStatement:exit': exitLoop,
AwaitExpression: foundAwait
};
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"homepage": "https://github.com/buildo/eslint-plugin-no-loops#readme",
"devDependencies": {
"babel-eslint": "^6.1.0",
"eslint": "^2.0.0"
"babel-eslint": "8",
"eslint": "5"
},
"peerDependencies": {
"eslint": ">=2.0.0"
Expand Down
30 changes: 29 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
var rule = require('../lib/no-loops.js');
var RuleTester = require('eslint').RuleTester;

var ruleTester = new RuleTester();
var ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
ruleTester.run('no-loops', rule, {
valid: [
{
code: '[1, 2, 3].map(function (i) { console.log(i); });'
},
{
code: 'async function ok() { for (var i = 0; i < n; i++) { await x[i]() } }'
},
{
code: 'const ok = async () => { for (const x of y) { for (const z of y) { await z() } } }'
},
{
code: 'async function ok() { for (const x of y) { await x() } }'
},
{
code: 'async function ok() { while (true) { await x() } }'
},
{
code: 'async function ok() { do { await x() } while (true) }'
},
{
code: 'const ok = async () => { for (const x of y) { for (const z in y) { await z() } } }'
}
],

Expand All @@ -32,6 +50,16 @@ ruleTester.run('no-loops', rule, {
code: 'for (i of [1, 2, 3]) { console.log(i) }',
parser: 'babel-eslint',
errors: [ { message: 'loops are not allowed' } ]
},
{
code: 'async function bad() { while (true) { x() } }',
parser: 'babel-eslint',
errors: [ { message: 'loops are not allowed' } ]
},
{
code: 'const ok = async () => { for (const x of y) { await x(); for (const z in y) { z() } } }',
parser: 'babel-eslint',
errors: [ { message: 'loops are not allowed' } ]
}
]
});