Skip to content

Commit

Permalink
Fixup problems in no-setup-in-describe
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 2, 2018
1 parent c3afd4d commit a732864
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
41 changes: 21 additions & 20 deletions lib/rules/no-setup-in-describe.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
'use strict';

var astUtils = require('../util/ast'),
additionalSuiteNames = require('../util/settings').additionalSuiteNames;
const astUtils = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');

const FUNCTION = 1;
const DESCRIBE = 2;
// "Pure" nodes are hooks (like `beforeEach`) or `it` calls
const PURE = 3;

module.exports = function noSetupInDescribe(context) {
var nesting = [],
settings = context.settings,
FUNCTION = 1,
DESCRIBE = 2,
// "Pure" nodes are hooks (like `beforeEach`) or `it` calls
PURE = 3;
const nesting = [];
const settings = context.settings;

function isPureNode(node) {
return astUtils.isHookIdentifier(node) || astUtils.isTestCase(node);
}

function reportCallExpression(callExpression) {
var message = 'Unexpected function call in describe block.';
const message = 'Unexpected function call in describe block.';

context.report({
message: message,
message,
node: callExpression.callee
});
}

function reportMemberExpression(memberExpression) {
var message = 'Unexpected member expression in describe block. ' +
const message = 'Unexpected member expression in describe block. ' +
'Member expressions may call functions via getters.';

context.report({
message: message,
message,
node: memberExpression
});
}
Expand All @@ -49,8 +50,8 @@ module.exports = function noSetupInDescribe(context) {
}

return {
CallExpression: function (node) {
var isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));
CallExpression(node) {
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));
if (isDescribe) {
nesting.push(DESCRIBE);
return;
Expand All @@ -62,35 +63,35 @@ module.exports = function noSetupInDescribe(context) {
handleCallExpressionInDescribe(node);
},

'CallExpression:exit': function (node) {
'CallExpression:exit'(node) {
if (astUtils.isDescribe(node) || nesting.length && isPureNode(node)) {
nesting.pop();
}
},

MemberExpression: function (node) {
MemberExpression(node) {
if (isNestedInDescribeBlock()) {
reportMemberExpression(node);
}
},

FunctionDeclaration: function () {
FunctionDeclaration() {
if (nesting.length) {
nesting.push(FUNCTION);
}
},
'FunctionDeclaration:exit': function () {
'FunctionDeclaration:exit'() {
if (nesting.length) {
nesting.pop();
}
},

ArrowFunctionExpression: function () {
ArrowFunctionExpression() {
if (nesting.length) {
nesting.push(FUNCTION);
}
},
'ArrowFunctionExpression:exit': function () {
'ArrowFunctionExpression:exit'() {
if (nesting.length) {
nesting.pop();
}
Expand Down
36 changes: 18 additions & 18 deletions test/rules/no-setup-in-describe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var RuleTester = require('eslint').RuleTester,
rule = require('../../lib/rules/no-setup-in-describe'),
ruleTester = new RuleTester(),
memberExpressionError = 'Unexpected member expression in describe block. ' +
const RuleTester = require('eslint').RuleTester;
const rule = require('../../lib/rules/no-setup-in-describe');
const ruleTester = new RuleTester();
const memberExpressionError = 'Unexpected member expression in describe block. ' +
'Member expressions may call functions via getters.';

ruleTester.run('no-setup-in-describe', rule, {
Expand Down Expand Up @@ -37,16 +37,16 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'foo("", function () { it(); })',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
}
additionalSuiteNames: [ 'foo' ]
}
}
}, {
code: 'foo("", function () { it("", function () { b(); }); })',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
}
additionalSuiteNames: [ 'foo' ]
}
}
}
],

Expand All @@ -62,8 +62,8 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'foo("", function () { a(); });',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
additionalSuiteNames: [ 'foo' ]
}
},
errors: [ {
message: 'Unexpected function call in describe block.',
Expand All @@ -74,8 +74,8 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'foo("", function () { a[b]; });',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
additionalSuiteNames: [ 'foo' ]
}
},
errors: [ {
message: memberExpressionError,
Expand All @@ -86,8 +86,8 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'foo("", function () { a["b"]; });',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
additionalSuiteNames: [ 'foo' ]
}
},
errors: [ {
message: memberExpressionError,
Expand All @@ -106,8 +106,8 @@ ruleTester.run('no-setup-in-describe', rule, {
code: 'foo("", function () { a.b; });',
settings: {
mocha: {
additionalSuiteNames: [ 'foo' ]
}
additionalSuiteNames: [ 'foo' ]
}
},
errors: [ {
message: memberExpressionError,
Expand Down

0 comments on commit a732864

Please sign in to comment.