sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-break-in-nested-loop`

Closed

#1,055 opened on Jan 23, 2021

 (5 comments) (2 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Actually, getting this because of auto-fix for no-array-for-each rule, it fix return in nested .forEach to continue.

It's not clear what to break/continue, when loop is nested.

Fail

for (const foo of bar) {
	for (const baz of foo) {
		// ...
		break;
	}
}
for (const foo of bar) {
	while(foo.pop()) {
		// ...
		continue;
	}
}

A even confusing case

for (const foo of bar) {
	switch (foo) {
		case 1:
			// ...
			break;
		case 2:
			// ...
			continue;
	}
}

Pass

function processFoo() {
	for (const baz of foo) {
		// ...
		break;
	}
}

for (const foo of bar) {
	processFoo(foo)
}

Contributor guide