node/tools/eslint-rules/iterator-result-done-first.js
Trivikram Kamat 92f48f43fe
tools: enforce iterator result property order
Add a custom ESLint rule requiring iterator result objects to place
`done` before `value`, and update existing lib iterator result objects
to follow the rule.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: openai:gpt-5.5
PR-URL: https://github.com/nodejs/node/pull/63526
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-05-26 17:19:42 +00:00

66 lines
1.4 KiB
JavaScript

'use strict';
const MESSAGE = 'Iterator result objects should place `done` before `value`.';
function getStaticPropertyName(property) {
const { key } = property;
if (!key) {
return;
}
if (key.type === 'Identifier' && !property.computed) {
return key.name;
}
if (key.type === 'Literal') {
return key.value;
}
}
module.exports = {
meta: {
type: 'suggestion',
fixable: 'code',
schema: [],
},
create(context) {
const sourceCode = context.sourceCode;
return {
ObjectExpression(node) {
let doneProperty;
let valueProperty;
for (const property of node.properties) {
if (property.type !== 'Property') {
continue;
}
switch (getStaticPropertyName(property)) {
case 'done':
doneProperty ??= property;
break;
case 'value':
valueProperty ??= property;
break;
}
}
if (doneProperty && valueProperty && valueProperty.range[0] < doneProperty.range[0]) {
context.report({
node: valueProperty,
message: MESSAGE,
fix(fixer) {
return [
fixer.replaceText(valueProperty, sourceCode.getText(doneProperty)),
fixer.replaceText(doneProperty, sourceCode.getText(valueProperty)),
];
},
});
}
},
};
},
};