Skip to content

Commit

Permalink
refactor: extract isHook to util
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Jul 11, 2019
1 parent e901d03 commit 8f57efc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/rules/no-hooks.js
@@ -1,6 +1,6 @@
'use strict';

const { getDocsUrl } = require('./util');
const { getDocsUrl, isHook } = require('./util');

module.exports = {
meta: {
Expand All @@ -24,21 +24,13 @@ module.exports = {
},
],
create(context) {
const testHookNames = Object.assign(Object.create(null), {
beforeAll: true,
beforeEach: true,
afterAll: true,
afterEach: true,
});

const whitelistedHookNames = (
context.options[0] || { allow: [] }
).allow.reduce((hashMap, value) => {
hashMap[value] = true;
return hashMap;
}, Object.create(null));

const isHook = node => testHookNames[node.callee.name];
const isWhitelisted = node => whitelistedHookNames[node.callee.name];

return {
Expand Down
13 changes: 13 additions & 0 deletions src/rules/util.js
Expand Up @@ -99,6 +99,13 @@ const testCaseNames = Object.assign(Object.create(null), {
xtest: true,
});

const testHookNames = Object.assign(Object.create(null), {
beforeAll: true,
beforeEach: true,
afterAll: true,
afterEach: true,
});

const getNodeName = node => {
function joinNames(a, b) {
return a && b ? `${a}.${b}` : null;
Expand All @@ -119,6 +126,11 @@ const getNodeName = node => {
return null;
};

const isHook = node =>
node &&
node.type === 'CallExpression' &&
testHookNames[getNodeName(node.callee)];

const isTestCase = node =>
node &&
node.type === 'CallExpression' &&
Expand Down Expand Up @@ -224,6 +236,7 @@ module.exports = {
getStringValue,
isDescribe,
isFunction,
isHook,
isTemplateLiteral,
isTestCase,
isString,
Expand Down

0 comments on commit 8f57efc

Please sign in to comment.