Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(prefer-expect-assertions): use ESQuery selectors #170

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions rules/prefer-expect-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ const isExpectAssertionsOrHasAssertionsCall = expression => {
}
};

const isTestOrItFunction = node => {
return (
node.type === 'CallExpression' &&
node.callee &&
(node.callee.name === 'it' || node.callee.name === 'test')
);
};

const getFunctionFirstLine = functionBody => {
return functionBody[0] && functionBody[0].expression;
};
Expand All @@ -47,14 +39,6 @@ const isFirstLineExprStmt = functionBody => {
return functionBody[0] && functionBody[0].type === 'ExpressionStatement';
};

const getTestFunctionBody = node => {
try {
return node.arguments[1].body.body;
} catch (e) {
return undefined;
}
};

const reportMsg = (context, node) => {
context.report({
message: ruleMsg,
Expand All @@ -70,18 +54,15 @@ module.exports = {
},
create(context) {
return {
CallExpression(node) {
if (isTestOrItFunction(node)) {
const testFuncBody = getTestFunctionBody(node);
if (testFuncBody) {
if (!isFirstLineExprStmt(testFuncBody)) {
reportMsg(context, node);
} else {
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
reportMsg(context, node);
}
}
'CallExpression[callee.name=/^it|test$/][arguments.1.body.body]'(node) {
const testFuncBody = node.arguments[1].body.body;

if (!isFirstLineExprStmt(testFuncBody)) {
reportMsg(context, node);
} else {
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
reportMsg(context, node);
}
}
},
Expand Down