add an exception to the for-each-of lint rule for interval iteration#709
Conversation
| /^ in\b/.test(next.contents) && | ||
| // Iterating over an interval is a valid construction where "in" is correct, in either the | ||
| // "inclusive interval from _a_ to _b_" shorthand or the full "interval from _a_ ... to _b_ ..." form. | ||
| !/^ in the (?:inclusive )?interval from\b/.test(next.contents) |
There was a problem hiding this comment.
| !/^ in the (?:inclusive )?interval from\b/.test(next.contents) | |
| !/^ in the (?:inclusive )?interval\b/.test(next.contents) |
No need to mandate the "from"; e.g. one might say "in the interval defined by..." or whatever.
There was a problem hiding this comment.
All of our allowed interval forms use from: https://tc39.es/ecma262/#interval
There was a problem hiding this comment.
I second @bakkot's suggestion to stop after "interval". And if we consider it in scope, also adopting the early-exit pattern from above this loop:
// Find the loop variable (underscore), then check the text after it
for (let i = 1; i < stepSeq.items.length - 1; i++) {
const item = stepSeq.items[i];
if (item.name !== 'underscore') {
continue;
}
const next = stepSeq.items[i + 1];
const over = next.name === 'text'
? next.contents.match(/^ in (the (?:inclusive)? interval )?/)
: undefined;
if (over && !over[1]) {
report({
ruleId,
...offsetToLineAndColumn(algorithmSource, next.location.start.offset + 1),
message: 'expected "of" instead of "in" in "for each"',
});
}
break;
}There was a problem hiding this comment.
All of our allowed interval forms use
from: https://tc39.es/ecma262/#interval
This rule is not intended to enforce a specific style for declaring intervals. You could have such a rule if you really wanted, but I would still want this rule not to enforce it; writing "in the interval defined by..." shouldn't lead to an error complaining about using in instead of of even if there's some other error that complains about some other thing.
gibson042
left a comment
There was a problem hiding this comment.
LGTM even without accepting the suggestion(s).
| /^ in\b/.test(next.contents) && | ||
| // Iterating over an interval is a valid construction where "in" is correct, in either the | ||
| // "inclusive interval from _a_ to _b_" shorthand or the full "interval from _a_ ... to _b_ ..." form. | ||
| !/^ in the (?:inclusive )?interval from\b/.test(next.contents) |
There was a problem hiding this comment.
I second @bakkot's suggestion to stop after "interval". And if we consider it in scope, also adopting the early-exit pattern from above this loop:
// Find the loop variable (underscore), then check the text after it
for (let i = 1; i < stepSeq.items.length - 1; i++) {
const item = stepSeq.items[i];
if (item.name !== 'underscore') {
continue;
}
const next = stepSeq.items[i + 1];
const over = next.name === 'text'
? next.contents.match(/^ in (the (?:inclusive)? interval )?/)
: undefined;
if (over && !over[1]) {
report({
ruleId,
...offsetToLineAndColumn(algorithmSource, next.location.start.offset + 1),
message: 'expected "of" instead of "in" in "for each"',
});
}
break;
}
Fixes #708