Skip to content

Commit 8246af4

Browse files
authored
Merge pull request testing-library#3 from Belco90/feature/support-assignements-and-promise-syntax
feat: support assignments and promise syntax
2 parents 5c787f3 + df735da commit 8246af4

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

lib/rules/await-async-query.js

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,81 @@ module.exports = {
2626
},
2727

2828
create: function(context) {
29+
const testingLibraryQueryUsage = [];
2930
return {
3031
[`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
31-
let hasError = true;
32-
try {
33-
if (VALID_PARENTS.includes(node.parent.parent.type)) {
34-
hasError = false;
35-
}
36-
} catch (e) {
37-
// not necessary to do anything
38-
}
39-
40-
if (hasError) {
41-
context.report({
32+
testingLibraryQueryUsage.push(node);
33+
},
34+
'Program:exit'() {
35+
testingLibraryQueryUsage.forEach(node => {
36+
const variableDeclaratorParent = findParent(
4237
node,
43-
messageId: 'awaitAsyncQuery',
44-
data: {
45-
name: node.name,
46-
},
47-
});
48-
}
38+
parent => parent.type === 'VariableDeclarator'
39+
);
40+
41+
const references =
42+
(variableDeclaratorParent &&
43+
context
44+
.getDeclaredVariables(variableDeclaratorParent)[0]
45+
.references.slice(1)) ||
46+
[];
47+
48+
if (
49+
references.length === 0 &&
50+
!isAwaited(node.parent.parent) &&
51+
!isPromiseResolved(node)
52+
) {
53+
context.report({
54+
node,
55+
messageId: 'awaitAsyncQuery',
56+
data: {
57+
name: node.name,
58+
},
59+
});
60+
} else {
61+
references.forEach(reference => {
62+
const node = reference.identifier;
63+
if (!isAwaited(node.parent) && !isPromiseResolved(node)) {
64+
context.report({
65+
node: reference.identifier,
66+
messageId: 'awaitAsyncQuery',
67+
data: {
68+
name: node.name,
69+
},
70+
});
71+
}
72+
});
73+
}
74+
});
4975
},
5076
};
5177
},
5278
};
79+
80+
function isAwaited(node) {
81+
return VALID_PARENTS.includes(node.type);
82+
}
83+
84+
function isPromiseResolved(node) {
85+
const parent = node.parent;
86+
87+
const hasAThenProperty = node =>
88+
node.type === 'MemberExpression' && node.property.name === 'then';
89+
90+
// findByText("foo").then(...)
91+
if (parent.type === 'CallExpression') {
92+
return hasAThenProperty(parent.parent);
93+
}
94+
95+
// promise.then(...)
96+
return hasAThenProperty(parent);
97+
}
98+
99+
function findParent(node, test) {
100+
if (test(node)) {
101+
return node;
102+
} else if (node.parent) {
103+
return findParent(node.parent, test);
104+
}
105+
return null;
106+
}

tests/lib/rules/await-async-query.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ ruleTester.run('await-async-query', rule, {
2525
}
2626
`,
2727
},
28+
{
29+
code: `() => {
30+
findByText('foo').then(node => {
31+
done()
32+
})
33+
}
34+
`,
35+
},
36+
{
37+
code: `() => {
38+
const promise = findByText('foo')
39+
promise.then(node => done())
40+
}
41+
`,
42+
},
2843
{
2944
code: `async () => {
3045
doSomething()

0 commit comments

Comments
 (0)