ecmarkup
Version:
Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.
34 lines (33 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const utils_1 = require("../../utils");
const ruleId = 'for-each-of';
/*
Checks that "For each" loops use "of", not "in".
*/
function default_1(report, step, algorithmSource, parsedSteps) {
const stepSeq = parsedSteps.get(step);
if (stepSeq == null || stepSeq.items.length < 3) {
return;
}
const first = stepSeq.items[0];
if (!(first.name === 'text' && first.contents.startsWith('For each '))) {
return;
}
// 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') {
const next = stepSeq.items[i + 1];
if (next.name === 'text' && /^ in\b/.test(next.contents)) {
report({
ruleId,
...(0, utils_1.offsetToLineAndColumn)(algorithmSource, next.location.start.offset + 1),
message: 'expected "of" instead of "in" in "for each"',
});
}
break;
}
}
}