forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator-result-done-first.js
More file actions
66 lines (55 loc) · 1.43 KB
/
Copy pathiterator-result-done-first.js
File metadata and controls
66 lines (55 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'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)),
];
},
});
}
},
};
},
};