Skip to content

Commit a62130c

Browse files
authored
fix(valid-expect-in-promise): support await in arrays (#949)
1 parent 71b7e17 commit a62130c

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/rules/__tests__/valid-expect-in-promise.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ ruleTester.run('valid-expect-in-promise', rule, {
120120
return number + 1;
121121
});
122122
123+
expect([await promise]).toHaveLength(1);
124+
});
125+
`,
126+
dedent`
127+
it('is valid', async () => {
128+
const promise = loadNumber().then(number => {
129+
expect(typeof number).toBe('number');
130+
131+
return number + 1;
132+
});
133+
134+
expect([[await promise]]).toHaveLength(1);
135+
});
136+
`,
137+
dedent`
138+
it('is valid', async () => {
139+
const promise = loadNumber().then(number => {
140+
expect(typeof number).toBe('number');
141+
142+
return number + 1;
143+
});
144+
123145
logValue(await promise);
124146
});
125147
`,

src/rules/valid-expect-in-promise.ts

+31-7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,35 @@ const isPromiseMethodThatUsesValue = (
143143
return isIdentifier(node.argument, name);
144144
};
145145

146+
/**
147+
* Attempts to determine if the runtime value represented by the given `identifier`
148+
* is `await`ed within the given array of elements
149+
*/
150+
const isValueAwaitedInElements = (
151+
name: string,
152+
elements:
153+
| TSESTree.ArrayExpression['elements']
154+
| TSESTree.CallExpression['arguments'],
155+
): boolean => {
156+
for (const element of elements) {
157+
if (
158+
element.type === AST_NODE_TYPES.AwaitExpression &&
159+
isIdentifier(element.argument, name)
160+
) {
161+
return true;
162+
}
163+
164+
if (
165+
element.type === AST_NODE_TYPES.ArrayExpression &&
166+
isValueAwaitedInElements(name, element.elements)
167+
) {
168+
return true;
169+
}
170+
}
171+
172+
return false;
173+
};
174+
146175
/**
147176
* Attempts to determine if the runtime value represented by the given `identifier`
148177
* is `await`ed as an argument along the given call expression
@@ -155,13 +184,8 @@ const isValueAwaitedInArguments = (
155184

156185
while (node) {
157186
if (node.type === AST_NODE_TYPES.CallExpression) {
158-
for (const argument of node.arguments) {
159-
if (
160-
argument.type === AST_NODE_TYPES.AwaitExpression &&
161-
isIdentifier(argument.argument, name)
162-
) {
163-
return true;
164-
}
187+
if (isValueAwaitedInElements(name, node.arguments)) {
188+
return true;
165189
}
166190

167191
node = node.callee;

0 commit comments

Comments
 (0)