Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(expect-expect): support MemberExpressions in assertFunctionNames #176

Merged
merged 1 commit into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions rules/__tests__/expect-expect.test.js
Expand Up @@ -23,6 +23,14 @@ ruleTester.run('expect-expect', rule, {
code: 'it("should return undefined",() => expectSaga(mySaga).returns());',
options: [{ assertFunctionNames: ['expectSaga'] }],
},
{
code: [
'test("verifies the function call", () => {',
' td.verify(someFunctionCall())',
'})',
].join('\n'),
options: [{ assertFunctionNames: ['td.verify'] }],
},
],

invalid: [
Expand Down
28 changes: 16 additions & 12 deletions rules/expect-expect.js
Expand Up @@ -6,6 +6,7 @@
*/

const getDocsUrl = require('./util').getDocsUrl;
const getNodeName = require('./util').getNodeName;

module.exports = {
meta: {
Expand All @@ -27,22 +28,25 @@ module.exports = {
},
create(context) {
const unchecked = [];
const assertFunctionNames =
const assertFunctionNames = new Set(
context.options[0] && context.options[0].assertFunctionNames
? context.options[0].assertFunctionNames
: ['expect'];
: ['expect']
);

return {
'CallExpression[callee.name=/^it|test$/]'(node) {
unchecked.push(node);
},
[`CallExpression[callee.name=/^${assertFunctionNames.join('|')}$/]`]() {
// Return early in case of nested `it` statements.
for (const ancestor of context.getAncestors()) {
const index = unchecked.indexOf(ancestor);
if (index !== -1) {
unchecked.splice(index, 1);
break;
CallExpression(node) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the selector query syntax became a little confusing now having to support MemberExpressions, so all CallExpression logic lives in this visitor function now. 😃

const name = getNodeName(node.callee);
if (name === 'it' || name === 'test') {
unchecked.push(node);
} else if (assertFunctionNames.has(name)) {
// Return early in case of nested `it` statements.
for (const ancestor of context.getAncestors()) {
const index = unchecked.indexOf(ancestor);
if (index !== -1) {
unchecked.splice(index, 1);
break;
}
}
}
},
Expand Down
20 changes: 2 additions & 18 deletions rules/no-disabled-tests.js
@@ -1,23 +1,7 @@
'use strict';

const getDocsUrl = require('./util').getDocsUrl;

function getName(node) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the implementation of util/getNodeName with this implementation, which I think handles member expressions a little bit better than the expect-expect implementation.

All tests continue to pass with the refactor too. 👍

function joinNames(a, b) {
return a && b ? `${a}.${b}` : null;
}

switch (node && node.type) {
case 'Identifier':
return node.name;
case 'Literal':
return node.value;
case 'MemberExpression':
return joinNames(getName(node.object), getName(node.property));
}

return null;
}
const getNodeName = require('./util').getNodeName;

function collectReferences(scope) {
const locals = new Set();
Expand Down Expand Up @@ -70,7 +54,7 @@ module.exports = {
});
},
CallExpression(node) {
const functionName = getName(node.callee);
const functionName = getNodeName(node.callee);

switch (functionName) {
case 'describe.skip':
Expand Down
16 changes: 13 additions & 3 deletions rules/util.js
Expand Up @@ -98,10 +98,20 @@ const testCaseNames = Object.assign(Object.create(null), {
});

const getNodeName = node => {
if (node.type === 'MemberExpression') {
return `${node.object.name}.${node.property.name}`;
function joinNames(a, b) {
return a && b ? `${a}.${b}` : null;
}
return node.name;

switch (node && node.type) {
case 'Identifier':
return node.name;
case 'Literal':
return node.value;
case 'MemberExpression':
return joinNames(getNodeName(node.object), getNodeName(node.property));
}

return null;
};

const isTestCase = node =>
Expand Down