Skip to content

Commit e02175d

Browse files
committed
Solve simple cases for ES6 classes
Related to issue #1
1 parent 3947358 commit e02175d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

rules/no-await-async-call.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,25 @@ module.exports = function(context) {
7272
};
7373
stack.push(frame);
7474
let funcName;
75+
let className;
7576
if (node.type === "FunctionDeclaration") {
7677
funcName = node.id.name;
7778
} else if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
7879
// Try to get function name...
7980
const parentType = node.parent ? node.parent.type : undefined;
8081
switch (parentType) {
82+
case "MethodDefinition":
83+
const methodName = node.parent.key.name;
84+
if (node.parent.static) {
85+
if (node.parent.parent.type === "ClassBody" &&
86+
node.parent.parent.parent.type === "ClassDeclaration") {
87+
className = node.parent.parent.parent.id.name;
88+
}
89+
} else {
90+
className = "this";
91+
}
92+
funcName = methodName;
93+
break;
8194
case "AssignmentExpression":
8295
if (node.parent.operator === "=" && node.parent.left.type === "Identifier") {
8396
funcName = node.parent.left.name;
@@ -92,7 +105,11 @@ module.exports = function(context) {
92105
}
93106

94107
if (funcName) {
95-
setFuncProp(callingFrame, funcName, PROP.ASYNC, node.async || false);
108+
if (className) {
109+
setFuncProp(callingFrame, className + "." + funcName, PROP.ASYNC, node.async || false);
110+
} else {
111+
setFuncProp(callingFrame, funcName, PROP.ASYNC, node.async || false);
112+
}
96113
if (node.async) {
97114
const reportNodeIfAsync = isFuncFailedIfAsync(funcName);
98115
if (reportNodeIfAsync) {
@@ -144,8 +161,17 @@ module.exports = function(context) {
144161
// We have a function call within an async function...
145162
const parentIsAwait = node.parent && node.parent.type === "AwaitExpression";
146163
const parentIsAssign = node.parent && ([ "AssignmentExpression", "VariableDeclarator" ].indexOf(node.parent.type) !== -1);
147-
const calledFunctionName = node.callee.name;
148164
if (assignmentRequired ? parentIsAssign : !parentIsAwait) {
165+
let calledFunctionName = node.callee.name;
166+
if (node.callee.type === "MemberExpression") {
167+
let instName;
168+
if (node.callee.object.type === "ThisExpression") {
169+
instName = "this";
170+
} else if (node.callee.object.type === "Identifier") {
171+
instName = node.callee.object.name;
172+
}
173+
calledFunctionName = instName + "." + node.callee.property.name;
174+
}
149175
// That is called without await...
150176
const calledFuncIsAsync = isFuncAsync(calledFunctionName);
151177
if (calledFuncIsAsync === undefined) {

test/no-await-async-call.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ ruleTester.run("no-await-async-call", rule, {
7979
options: [],
8080
errors
8181
},
82+
{ code: `class A {
83+
static async f() {}
84+
async g() {
85+
A.f();
86+
}
87+
}`,
88+
options: [],
89+
errors
90+
},
8291

8392
// async func f calls async func g without await (g declared before f)
8493
{ code: "async function g() {}; async function f() {let x = g()}", options: ["assignment-required"] , errors: errorsAssignmentRequired },

0 commit comments

Comments
 (0)