eslint-plugin-mocha
Advanced tools
Comparing version 4.12.1 to 5.0.0
@@ -0,1 +1,22 @@ | ||
## 5.0.0 (March 24, 2018) | ||
### Breaking Changes | ||
* Remove support for ESLint versions < 4.0.0 ([#155](https://github.com/lo1tuma/eslint-plugin-mocha/pull/155)) | ||
* Remove support for nodejs 4, 5 and 7 ([#154](https://github.com/lo1tuma/eslint-plugin-mocha/pull/154)) | ||
### Dependency Upgrades | ||
* Update pr-log to the latest version 🚀 ([#159](https://github.com/lo1tuma/eslint-plugin-mocha/pull/159)) | ||
* Update chai to version 4.1.2 ([#151](https://github.com/lo1tuma/eslint-plugin-mocha/pull/151)) | ||
### Code Refactoring | ||
* Use new language features ([#156](https://github.com/lo1tuma/eslint-plugin-mocha/pull/156)) | ||
### Build-Related | ||
* Use nyc instead of istanbul ([#153](https://github.com/lo1tuma/eslint-plugin-mocha/pull/153)) | ||
* Whitelist files instead of using .npmignore ([#152](https://github.com/lo1tuma/eslint-plugin-mocha/pull/152)) | ||
## 4.12.1 (March 3, 2018) | ||
@@ -2,0 +23,0 @@ |
'use strict'; | ||
var R = require('ramda'), | ||
astUtils = require('../util/ast'); | ||
const R = require('ramda'); | ||
const astUtils = require('../util/ast'); | ||
@@ -22,3 +22,3 @@ module.exports = function (context) { | ||
function isReferenceHandled(reference) { | ||
var parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent; | ||
const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent; | ||
@@ -33,6 +33,6 @@ return parent.type === 'CallExpression'; | ||
function checkAsyncMochaFunction(functionExpression) { | ||
var scope = context.getScope(), | ||
callback = functionExpression.params[0], | ||
callbackName = callback.name, | ||
callbackVariable = findParamInScope(callbackName, scope); | ||
const scope = context.getScope(); | ||
const callback = functionExpression.params[0]; | ||
const callbackName = callback.name; | ||
const callbackVariable = findParamInScope(callbackName, scope); | ||
@@ -39,0 +39,0 @@ if (callbackVariable && !hasHandledReferences(callbackVariable.references)) { |
@@ -8,13 +8,14 @@ 'use strict'; | ||
var R = require('ramda'), | ||
astUtil = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames, | ||
defaultSuiteLimit = 1; | ||
const R = require('ramda'); | ||
const astUtil = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
const defaultSuiteLimit = 1; | ||
module.exports = function (context) { | ||
var stack = [], | ||
topLevelDescribes = [], | ||
options = context.options[0] || {}, | ||
settings = context.settings, | ||
suiteLimit; | ||
const stack = []; | ||
const topLevelDescribes = []; | ||
const options = context.options[0] || {}; | ||
const settings = context.settings; | ||
let suiteLimit; | ||
@@ -28,3 +29,3 @@ if (R.isNil(options.limit)) { | ||
return { | ||
CallExpression: function (node) { | ||
CallExpression(node) { | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -35,3 +36,3 @@ stack.push(node); | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -46,7 +47,7 @@ if (stack.length === 1) { | ||
'Program:exit': function () { | ||
'Program:exit'() { | ||
if (topLevelDescribes.length > suiteLimit) { | ||
context.report({ | ||
node: topLevelDescribes[suiteLimit], | ||
message: 'The number of top-level suites is more than ' + suiteLimit + '.' | ||
message: `The number of top-level suites is more than ${ suiteLimit }.` | ||
}); | ||
@@ -53,0 +54,0 @@ } |
'use strict'; | ||
var getAdditionalTestFunctions = require('../util/settings').getAdditionalTestFunctions, | ||
astUtils = require('../util/ast'); | ||
const { getAdditionalTestFunctions } = require('../util/settings'); | ||
const astUtils = require('../util/ast'); | ||
module.exports = function (context) { | ||
var mochaTestFunctions = [ | ||
'it', | ||
'describe', | ||
'suite', | ||
'test', | ||
'context', | ||
'specify' | ||
], | ||
settings = context.settings, | ||
additionalTestFunctions = getAdditionalTestFunctions(settings); | ||
let mochaTestFunctions = [ | ||
'it', | ||
'describe', | ||
'suite', | ||
'test', | ||
'context', | ||
'specify' | ||
]; | ||
const settings = context.settings; | ||
const additionalTestFunctions = getAdditionalTestFunctions(settings); | ||
@@ -21,3 +21,3 @@ mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions); | ||
function matchesMochaTestFunction(object) { | ||
var name = astUtils.getNodeName(object); | ||
const name = astUtils.getNodeName(object); | ||
@@ -38,4 +38,4 @@ return mochaTestFunctions.indexOf(name) !== -1; | ||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee; | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
@@ -42,0 +42,0 @@ if (callee && isCallToMochasOnlyFunction(callee)) { |
'use strict'; | ||
var astUtils = require('../util/ast'); | ||
const astUtils = require('../util/ast'); | ||
@@ -11,5 +11,5 @@ module.exports = function (context) { | ||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee, | ||
scope = context.getScope(); | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
const scope = context.getScope(); | ||
@@ -16,0 +16,0 @@ if (astUtils.isTestCase(node) && isGlobalScope(scope)) { |
'use strict'; | ||
var astUtil = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames; | ||
const astUtil = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
function newDescribeLayer(describeNode) { | ||
return { | ||
describeNode: describeNode, | ||
describeNode, | ||
hookNodes: [], | ||
@@ -15,9 +15,9 @@ testCount: 0 | ||
module.exports = function (context) { | ||
var options = context.options[0] || {}, | ||
allowedHooks = options.allow || [], | ||
settings = context.settings, | ||
layers = []; | ||
const options = context.options[0] || {}; | ||
const allowedHooks = options.allow || []; | ||
const settings = context.settings; | ||
const layers = []; | ||
function popLayer(node) { | ||
var layer = layers[layers.length - 1]; | ||
const layer = layers[layers.length - 1]; | ||
if (layer.describeNode === node) { | ||
@@ -32,3 +32,3 @@ if (layer.testCount <= 1) { | ||
node: hookNode, | ||
message: 'Unexpected use of Mocha `' + hookNode.name + '` hook for a single test case' | ||
message: `Unexpected use of Mocha \`${ hookNode.name }\` hook for a single test case` | ||
}); | ||
@@ -42,7 +42,7 @@ }); | ||
return { | ||
Program: function (node) { | ||
Program(node) { | ||
layers.push(newDescribeLayer(node)); | ||
}, | ||
CallExpression: function (node) { | ||
CallExpression(node) { | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -49,0 +49,0 @@ layers[layers.length - 1].testCount += 1; |
'use strict'; | ||
var astUtil = require('../util/ast'); | ||
const astUtil = require('../util/ast'); | ||
module.exports = function (context) { | ||
return { | ||
CallExpression: function (node) { | ||
CallExpression(node) { | ||
if (astUtil.isHookIdentifier(node.callee)) { | ||
context.report({ | ||
node: node.callee, | ||
message: 'Unexpected use of Mocha `' + node.callee.name + '` hook' | ||
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook` | ||
}); | ||
@@ -13,0 +13,0 @@ } |
'use strict'; | ||
var astUtil = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames; | ||
const astUtil = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
@@ -17,3 +17,3 @@ function newLayer() { | ||
context.report({ | ||
node: node, | ||
node, | ||
message: 'Test title is used multiple times in the same test suite.' | ||
@@ -27,3 +27,3 @@ }); | ||
function handlTestSuiteTitles(context, titles, node, title) { | ||
var settings = context.settings; | ||
const settings = context.settings; | ||
@@ -35,3 +35,3 @@ if (!astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
context.report({ | ||
node: node, | ||
node, | ||
message: 'Test suite title is used multiple times.' | ||
@@ -48,11 +48,9 @@ }); | ||
module.exports = function (context) { | ||
var titleLayers = [ | ||
newLayer() | ||
], | ||
settings = context.settings; | ||
const titleLayers = [ newLayer() ]; | ||
const settings = context.settings; | ||
return { | ||
CallExpression: function (node) { | ||
var currentLayer = titleLayers[titleLayers.length - 1], | ||
title; | ||
CallExpression(node) { | ||
const currentLayer = titleLayers[titleLayers.length - 1]; | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -65,7 +63,7 @@ titleLayers.push(newLayer()); | ||
title = node.arguments[0].value; | ||
const title = node.arguments[0].value; | ||
handlTestCaseTitles(context, currentLayer.testTitles, node, title); | ||
handlTestSuiteTitles(context, currentLayer.describeTitles, node, title); | ||
}, | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -72,0 +70,0 @@ titleLayers.pop(); |
@@ -8,15 +8,14 @@ 'use strict'; | ||
var R = require('ramda'), | ||
astUtils = require('../util/ast'); | ||
const R = require('ramda'); | ||
const astUtils = require('../util/ast'); | ||
module.exports = function (context) { | ||
function fixArrowFunction(fixer, fn) { | ||
var sourceCode = context.getSourceCode(), | ||
paramsLeftParen = sourceCode.getFirstToken(fn), | ||
paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fn.body)), | ||
paramsFullText = | ||
sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]), | ||
functionKeyword = 'function', | ||
bodyText; | ||
const sourceCode = context.getSourceCode(); | ||
function formatFunctionHead(fn) { | ||
const paramsLeftParen = sourceCode.getFirstToken(fn); | ||
const paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fn.body)); | ||
let paramsFullText = sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]); | ||
let functionKeyword = 'function'; | ||
if (fn.async) { | ||
@@ -30,5 +29,9 @@ // When 'async' specified, take care about the keyword. | ||
if (fn.params.length > 0) { | ||
paramsFullText = '(' + sourceCode.text.slice(fn.params[0].start, R.last(fn.params).end) + ')'; | ||
paramsFullText = `(${ sourceCode.text.slice(fn.params[0].start, R.last(fn.params).end) })`; | ||
} | ||
return `${functionKeyword}${paramsFullText} `; | ||
} | ||
function fixArrowFunction(fixer, fn) { | ||
if (fn.body.type === 'BlockStatement') { | ||
@@ -39,10 +42,10 @@ // When it((...) => { ... }), | ||
[ fn.start, fn.body.start ], | ||
functionKeyword + paramsFullText + ' ' | ||
formatFunctionHead(fn) | ||
); | ||
} | ||
bodyText = sourceCode.text.slice(fn.body.range[0], fn.body.range[1]); | ||
const bodyText = sourceCode.text.slice(fn.body.range[0], fn.body.range[1]); | ||
return fixer.replaceTextRange( | ||
[ fn.start, fn.end ], | ||
functionKeyword + paramsFullText + ' { return ' + bodyText + '; }' | ||
`${formatFunctionHead(fn)}{ return ${ bodyText }; }` | ||
); | ||
@@ -52,13 +55,12 @@ } | ||
return { | ||
CallExpression: function (node) { | ||
var name = astUtils.getNodeName(node.callee), | ||
fnArg; | ||
CallExpression(node) { | ||
const name = astUtils.getNodeName(node.callee); | ||
if (astUtils.isMochaFunctionCall(node, context.getScope())) { | ||
fnArg = node.arguments.slice(-1)[0]; | ||
const fnArg = node.arguments.slice(-1)[0]; | ||
if (fnArg && fnArg.type === 'ArrowFunctionExpression') { | ||
context.report({ | ||
node: node, | ||
message: 'Do not pass arrow functions to ' + name + '()', | ||
fix: function (fixer) { | ||
node, | ||
message: `Do not pass arrow functions to ${ name }()`, | ||
fix(fixer) { | ||
return fixArrowFunction(fixer, fnArg); | ||
@@ -65,0 +67,0 @@ } |
'use strict'; | ||
var astUtils = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames; | ||
const astUtils = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
module.exports = function noNestedTests(context) { | ||
var testNestingLevel = 0, | ||
settings = context.settings; | ||
const settings = context.settings; | ||
let testNestingLevel = 0; | ||
function report(callExpression, isTestCase) { | ||
var message = isTestCase ? 'Unexpected test nested within another test.' : | ||
const message = isTestCase ? 'Unexpected test nested within another test.' : | ||
'Unexpected suite nested within a test.'; | ||
context.report({ | ||
message: message, | ||
message, | ||
node: callExpression.callee | ||
@@ -21,4 +21,4 @@ }); | ||
function isNestedTest(isTestCase, isDescribe) { | ||
var isNested = testNestingLevel > 0, | ||
isTest = isTestCase || isDescribe; | ||
const isNested = testNestingLevel > 0; | ||
const isTest = isTestCase || isDescribe; | ||
@@ -29,5 +29,5 @@ return isNested && isTest; | ||
return { | ||
CallExpression: function (node) { | ||
var isTestCase = astUtils.isTestCase(node), | ||
isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); | ||
CallExpression(node) { | ||
const isTestCase = astUtils.isTestCase(node); | ||
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); | ||
@@ -43,3 +43,3 @@ if (isNestedTest(isTestCase, isDescribe)) { | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (astUtils.isTestCase(node)) { | ||
@@ -46,0 +46,0 @@ testNestingLevel -= 1; |
'use strict'; | ||
var astUtils = require('../util/ast'); | ||
const astUtils = require('../util/ast'); | ||
@@ -13,6 +13,6 @@ module.exports = function (context) { | ||
return { | ||
CallExpression: function (node) { | ||
CallExpression(node) { | ||
if (node.callee && isPendingMochaTest(node)) { | ||
context.report({ | ||
node: node, | ||
node, | ||
message: 'Unexpected pending mocha test.' | ||
@@ -19,0 +19,0 @@ }); |
'use strict'; | ||
var R = require('ramda'), | ||
astUtils = require('../util/ast'), | ||
findReturnStatement = R.find(R.propEq('type', 'ReturnStatement')); | ||
const R = require('ramda'); | ||
const astUtils = require('../util/ast'); | ||
const findReturnStatement = R.find(R.propEq('type', 'ReturnStatement')); | ||
function hasParentMochaFunctionCall(functionExpression) { | ||
@@ -27,4 +28,4 @@ return astUtils.isTestCase(functionExpression.parent) || astUtils.isHookCall(functionExpression.parent); | ||
function isReturnOfUndefined(node) { | ||
var argument = node.argument, | ||
isImplicitUndefined = argument === null; | ||
const argument = node.argument; | ||
const isImplicitUndefined = argument === null; | ||
@@ -41,3 +42,3 @@ return isImplicitUndefined || isExplicitUndefined(argument); | ||
function isAllowedReturnStatement(node, doneName) { | ||
var argument = node.argument; | ||
const argument = node.argument; | ||
@@ -52,3 +53,3 @@ if (isReturnOfUndefined(node) || argument.type === 'Literal') { | ||
function reportIfFunctionWithBlock(context, node, doneName) { | ||
var returnStatement = findReturnStatement(node.body.body); | ||
const returnStatement = findReturnStatement(node.body.body); | ||
if (returnStatement && !isAllowedReturnStatement(returnStatement, doneName)) { | ||
@@ -55,0 +56,0 @@ context.report({ |
'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; | ||
@@ -19,6 +20,6 @@ function isPureNode(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 | ||
@@ -29,7 +30,7 @@ }); | ||
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 | ||
@@ -54,4 +55,4 @@ }); | ||
return { | ||
CallExpression: function (node) { | ||
var isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); | ||
CallExpression(node) { | ||
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings)); | ||
if (isDescribe) { | ||
@@ -68,3 +69,3 @@ nesting.push(DESCRIBE); | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (astUtils.isDescribe(node) || nesting.length && isPureNode(node)) { | ||
@@ -75,3 +76,3 @@ nesting.pop(); | ||
MemberExpression: function (node) { | ||
MemberExpression(node) { | ||
if (isNestedInDescribeBlock()) { | ||
@@ -82,3 +83,3 @@ reportMemberExpression(node); | ||
FunctionDeclaration: function () { | ||
FunctionDeclaration() { | ||
if (nesting.length) { | ||
@@ -88,3 +89,3 @@ nesting.push(FUNCTION); | ||
}, | ||
'FunctionDeclaration:exit': function () { | ||
'FunctionDeclaration:exit'() { | ||
if (nesting.length) { | ||
@@ -95,3 +96,3 @@ nesting.pop(); | ||
ArrowFunctionExpression: function () { | ||
ArrowFunctionExpression() { | ||
if (nesting.length) { | ||
@@ -101,3 +102,3 @@ nesting.push(FUNCTION); | ||
}, | ||
'ArrowFunctionExpression:exit': function () { | ||
'ArrowFunctionExpression:exit'() { | ||
if (nesting.length) { | ||
@@ -104,0 +105,0 @@ nesting.pop(); |
'use strict'; | ||
var astUtil = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames; | ||
const astUtil = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
function newDescribeLayer(describeNode) { | ||
return { | ||
describeNode: describeNode, | ||
describeNode, | ||
before: false, | ||
@@ -17,20 +17,20 @@ after: false, | ||
module.exports = function (context) { | ||
var isUsed = [], | ||
settings = context.settings; | ||
const isUsed = []; | ||
const settings = context.settings; | ||
return { | ||
Program: function (node) { | ||
Program(node) { | ||
isUsed.push(newDescribeLayer(node)); | ||
}, | ||
CallExpression: function (node) { | ||
var name = astUtil.getNodeName(node.callee); | ||
CallExpression(node) { | ||
const name = astUtil.getNodeName(node.callee); | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
isUsed.push(newDescribeLayer(node)); | ||
return; | ||
isUsed.push(newDescribeLayer(node)); | ||
return; | ||
} | ||
if (!astUtil.isHookIdentifier(node.callee)) { | ||
return; | ||
return; | ||
} | ||
@@ -41,3 +41,3 @@ | ||
node: node.callee, | ||
message: 'Unexpected use of duplicate Mocha `' + name + '` hook' | ||
message: `Unexpected use of duplicate Mocha \`${ name }\` hook` | ||
}); | ||
@@ -49,5 +49,5 @@ } | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (isUsed[isUsed.length - 1].describeNode === node) { | ||
isUsed.pop(); | ||
isUsed.pop(); | ||
} | ||
@@ -54,0 +54,0 @@ } |
'use strict'; | ||
var getAdditionalTestFunctions = require('../util/settings').getAdditionalTestFunctions, | ||
getAdditionalXFunctions = require('../util/settings').getAdditionalXFunctions, | ||
mochaTestFunctions, | ||
mochaXFunctions; | ||
const { getAdditionalTestFunctions, getAdditionalXFunctions } = require('../util/settings'); | ||
let mochaTestFunctions; | ||
let mochaXFunctions; | ||
function matchesMochaTestFunction(object) { | ||
@@ -23,6 +23,7 @@ return object && mochaTestFunctions.indexOf(object.name) !== -1; | ||
function createSkipAutofixFunction(callee) { | ||
var endRangeOfMemberExpression = callee.range[1], | ||
endRangeOfMemberExpressionObject = callee.object.range[1], | ||
rangeToRemove = [ endRangeOfMemberExpressionObject, endRangeOfMemberExpression ]; | ||
const [ , endRangeOfMemberExpression ] = callee.range; | ||
const [ , endRangeOfMemberExpressionObject ] = callee.object.range; | ||
const rangeToRemove = [ endRangeOfMemberExpressionObject, endRangeOfMemberExpression ]; | ||
return function removeSkipProperty(fixer) { | ||
@@ -34,3 +35,3 @@ return fixer.removeRange(rangeToRemove); | ||
function createXAutofixFunction(callee) { | ||
var rangeToRemove = [ callee.range[0], callee.range[0] + 1 ]; | ||
const rangeToRemove = [ callee.range[0], callee.range[0] + 1 ]; | ||
@@ -51,5 +52,5 @@ return function removeXPrefix(fixer) { | ||
module.exports = function (context) { | ||
var settings = context.settings, | ||
additionalTestFunctions = getAdditionalTestFunctions(settings), | ||
additionalXFunctions = getAdditionalXFunctions(settings); | ||
const settings = context.settings; | ||
const additionalTestFunctions = getAdditionalTestFunctions(settings); | ||
const additionalXFunctions = getAdditionalXFunctions(settings); | ||
@@ -72,4 +73,4 @@ mochaTestFunctions = [ | ||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee; | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
@@ -76,0 +77,0 @@ if (isCallToMochasSkipFunction(callee)) { |
'use strict'; | ||
var R = require('ramda'), | ||
astUtil = require('../util/ast'), | ||
asyncMethods = [ 'async', 'callback', 'promise' ]; | ||
const R = require('ramda'); | ||
const astUtil = require('../util/ast'); | ||
const asyncMethods = [ 'async', 'callback', 'promise' ]; | ||
function hasParentMochaFunctionCall(functionExpression) { | ||
return astUtil.isTestCase(functionExpression.parent) | ||
|| astUtil.isHookIdentifier(functionExpression.parent.callee); | ||
return astUtil.isTestCase(functionExpression.parent) || | ||
astUtil.isHookIdentifier(functionExpression.parent.callee); | ||
} | ||
@@ -27,4 +28,4 @@ | ||
function doesReturnPromise(functionExpression) { | ||
var bodyStatement = functionExpression.body, | ||
returnStatement = null; | ||
const bodyStatement = functionExpression.body; | ||
let returnStatement = null; | ||
@@ -38,26 +39,23 @@ if (bodyStatement.type === 'BlockStatement') { | ||
return returnStatement !== null | ||
&& typeof returnStatement !== 'undefined'; | ||
return returnStatement !== null && | ||
typeof returnStatement !== 'undefined'; | ||
} | ||
module.exports = function (context) { | ||
var options = context.options[0] || {}, | ||
allowedAsyncMethods = R.isNil(options.allowed) ? asyncMethods : options.allowed; | ||
const options = context.options[0] || {}; | ||
const allowedAsyncMethods = R.isNil(options.allowed) ? asyncMethods : options.allowed; | ||
function check(node) { | ||
var testAsyncMethods, | ||
isAsyncTest; | ||
if (hasParentMochaFunctionCall(node)) { | ||
// For each allowed async test method, check if it is used in the test | ||
testAsyncMethods = allowedAsyncMethods.map(function (method) { | ||
const testAsyncMethods = allowedAsyncMethods.map(function (method) { | ||
switch (method) { | ||
case 'async': | ||
return isAsyncFunction(node); | ||
case 'async': | ||
return isAsyncFunction(node); | ||
case 'callback': | ||
return hasAsyncCallback(node); | ||
case 'callback': | ||
return hasAsyncCallback(node); | ||
default: | ||
return doesReturnPromise(node); | ||
default: | ||
return doesReturnPromise(node); | ||
} | ||
@@ -67,3 +65,3 @@ }); | ||
// Check that at least one allowed async test method is used in the test | ||
isAsyncTest = testAsyncMethods.some(function (value) { | ||
const isAsyncTest = testAsyncMethods.some(function (value) { | ||
return value === true; | ||
@@ -70,0 +68,0 @@ }); |
'use strict'; | ||
var astUtil = require('../util/ast'), | ||
additionalSuiteNames = require('../util/settings').additionalSuiteNames; | ||
const astUtil = require('../util/ast'); | ||
const { additionalSuiteNames } = require('../util/settings'); | ||
module.exports = function (context) { | ||
var settings = context.settings, | ||
testSuiteStack = []; | ||
const settings = context.settings; | ||
const testSuiteStack = []; | ||
return { | ||
CallExpression: function (node) { | ||
CallExpression(node) { | ||
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { | ||
@@ -18,3 +18,3 @@ testSuiteStack.push(node); | ||
if (!astUtil.isHookIdentifier(node.callee)) { | ||
return; | ||
return; | ||
} | ||
@@ -25,3 +25,3 @@ | ||
node: node.callee, | ||
message: 'Unexpected use of Mocha `' + node.callee.name + '` hook outside of a test suite' | ||
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook outside of a test suite` | ||
}); | ||
@@ -31,3 +31,3 @@ } | ||
'CallExpression:exit': function (node) { | ||
'CallExpression:exit'(node) { | ||
if (testSuiteStack[testSuiteStack.length - 1] === node) { | ||
@@ -34,0 +34,0 @@ testSuiteStack.pop(); |
@@ -8,8 +8,8 @@ 'use strict'; | ||
var defaultSuiteNames = [ 'describe', 'context', 'suite' ], | ||
astUtils = require('../util/ast'); | ||
const astUtils = require('../util/ast'); | ||
const defaultSuiteNames = [ 'describe', 'context', 'suite' ]; | ||
module.exports = function (context) { | ||
var pattern = new RegExp(context.options[0]), | ||
suiteNames = context.options[1] ? context.options[1] : defaultSuiteNames; | ||
const pattern = new RegExp(context.options[0]); | ||
const suiteNames = context.options[1] ? context.options[1] : defaultSuiteNames; | ||
@@ -21,4 +21,4 @@ function isSuite(node) { | ||
function hasValidSuiteDescription(mochaCallExpression) { | ||
var args = mochaCallExpression.arguments, | ||
description = args[0]; | ||
const args = mochaCallExpression.arguments; | ||
const description = args[0]; | ||
@@ -33,4 +33,4 @@ if (astUtils.isStringLiteral(description)) { | ||
function hasValidOrNoSuiteDescription(mochaCallExpression) { | ||
var args = mochaCallExpression.arguments, | ||
hasNoSuiteDescription = args.length === 0; | ||
const args = mochaCallExpression.arguments; | ||
const hasNoSuiteDescription = args.length === 0; | ||
@@ -41,8 +41,8 @@ return hasNoSuiteDescription || hasValidSuiteDescription(mochaCallExpression); | ||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee; | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
if (isSuite(node)) { | ||
if (!hasValidOrNoSuiteDescription(node)) { | ||
context.report(node, 'Invalid "' + callee.name + '()" description found.'); | ||
context.report(node, `Invalid "${ callee.name }()" description found.`); | ||
} | ||
@@ -49,0 +49,0 @@ } |
@@ -8,8 +8,9 @@ 'use strict'; | ||
var defaultTestNames = [ 'it', 'test', 'specify' ], | ||
astUtils = require('../util/ast'); | ||
const astUtils = require('../util/ast'); | ||
const defaultTestNames = [ 'it', 'test', 'specify' ]; | ||
module.exports = function (context) { | ||
var pattern = context.options[0] ? new RegExp(context.options[0]) : /^should/, | ||
testNames = context.options[1] ? context.options[1] : defaultTestNames; | ||
const pattern = context.options[0] ? new RegExp(context.options[0]) : /^should/; | ||
const testNames = context.options[1] ? context.options[1] : defaultTestNames; | ||
@@ -21,4 +22,4 @@ function isTest(node) { | ||
function hasValidTestDescription(mochaCallExpression) { | ||
var args = mochaCallExpression.arguments, | ||
testDescriptionArgument = args[0]; | ||
const args = mochaCallExpression.arguments; | ||
const testDescriptionArgument = args[0]; | ||
@@ -33,4 +34,4 @@ if (astUtils.isStringLiteral(testDescriptionArgument)) { | ||
function hasValidOrNoTestDescription(mochaCallExpression) { | ||
var args = mochaCallExpression.arguments, | ||
hasNoTestDescription = args.length === 0; | ||
const args = mochaCallExpression.arguments; | ||
const hasNoTestDescription = args.length === 0; | ||
@@ -41,8 +42,8 @@ return hasNoTestDescription || hasValidTestDescription(mochaCallExpression); | ||
return { | ||
CallExpression: function (node) { | ||
var callee = node.callee; | ||
CallExpression(node) { | ||
const callee = node.callee; | ||
if (isTest(node)) { | ||
if (!hasValidOrNoTestDescription(node)) { | ||
context.report(node, 'Invalid "' + callee.name + '()" description found.'); | ||
context.report(node, `Invalid "${ callee.name }()" description found.`); | ||
} | ||
@@ -49,0 +50,0 @@ } |
'use strict'; | ||
var R = require('ramda'), | ||
isDefined = R.complement(R.isNil), | ||
isCallExpression = R.both(isDefined, R.propEq('type', 'CallExpression')), | ||
describeAliases = [ 'describe', 'xdescribe', 'describe.only', 'describe.skip', | ||
'context', 'xcontext', 'context.only', 'context.skip', | ||
'suite', 'xsuite', 'suite.only', 'suite.skip' ], | ||
hooks = [ 'before', 'after', 'beforeEach', 'afterEach' ], | ||
testCaseNames = [ 'it', 'it.only', 'it.skip', 'xit', | ||
'test', 'test.only', 'test.skip', | ||
'specify', 'specify.only', 'specify.skip', 'xspecify' ]; | ||
const R = require('ramda'); | ||
const isDefined = R.complement(R.isNil); | ||
const isCallExpression = R.both(isDefined, R.propEq('type', 'CallExpression')); | ||
const describeAliases = [ | ||
'describe', 'xdescribe', 'describe.only', 'describe.skip', | ||
'context', 'xcontext', 'context.only', 'context.skip', | ||
'suite', 'xsuite', 'suite.only', 'suite.skip' | ||
]; | ||
const hooks = [ 'before', 'after', 'beforeEach', 'afterEach' ]; | ||
const testCaseNames = [ | ||
'it', 'it.only', 'it.skip', 'xit', | ||
'test', 'test.only', 'test.skip', | ||
'specify', 'specify.only', 'specify.skip', 'xspecify' | ||
]; | ||
function getPropertyName(property) { | ||
@@ -20,3 +26,3 @@ return property.name || property.value; | ||
if (node.type === 'MemberExpression') { | ||
return getNodeName(node.object) + '.' + getPropertyName(node.property); | ||
return `${getNodeName(node.object) }.${ getPropertyName(node.property)}`; | ||
} | ||
@@ -27,10 +33,10 @@ return node.name; | ||
function isDescribe(node, additionalSuiteNames) { | ||
return isCallExpression(node) | ||
&& describeAliases.concat(additionalSuiteNames).indexOf(getNodeName(node.callee)) > -1; | ||
return isCallExpression(node) && | ||
describeAliases.concat(additionalSuiteNames).indexOf(getNodeName(node.callee)) > -1; | ||
} | ||
function isHookIdentifier(node) { | ||
return node | ||
&& node.type === 'Identifier' | ||
&& hooks.indexOf(node.name) !== -1; | ||
return node && | ||
node.type === 'Identifier' && | ||
hooks.indexOf(node.name) !== -1; | ||
} | ||
@@ -47,3 +53,3 @@ | ||
function findReference(scope, node) { | ||
var hasSameRangeAsNode = R.pathEq([ 'identifier', 'range' ], node.range); | ||
const hasSameRangeAsNode = R.pathEq([ 'identifier', 'range' ], node.range); | ||
@@ -54,3 +60,3 @@ return R.find(hasSameRangeAsNode, scope.references); | ||
function isShadowed(scope, identifier) { | ||
var reference = findReference(scope, identifier); | ||
const reference = findReference(scope, identifier); | ||
@@ -61,3 +67,3 @@ return reference && reference.resolved && reference.resolved.defs.length > 0; | ||
function isCallToShadowedReference(node, scope) { | ||
var identifier = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee; | ||
const identifier = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee; | ||
@@ -80,10 +86,10 @@ return isShadowed(scope, identifier); | ||
module.exports = { | ||
isDescribe: isDescribe, | ||
isHookIdentifier: isHookIdentifier, | ||
isTestCase: isTestCase, | ||
getPropertyName: getPropertyName, | ||
getNodeName: getNodeName, | ||
isMochaFunctionCall: isMochaFunctionCall, | ||
isHookCall: isHookCall, | ||
isStringLiteral: isStringLiteral | ||
isDescribe, | ||
isHookIdentifier, | ||
isTestCase, | ||
getPropertyName, | ||
getNodeName, | ||
isMochaFunctionCall, | ||
isHookCall, | ||
isStringLiteral | ||
}; |
@@ -6,14 +6,14 @@ /* eslint-env node*/ | ||
function settingFor(propertyName) { | ||
return function (settings) { | ||
var value = settings['mocha/' + propertyName], | ||
mochaSettings = settings.mocha || {}; | ||
return function (settings) { | ||
const value = settings[`mocha/${ propertyName}`]; | ||
const mochaSettings = settings.mocha || {}; | ||
return value || mochaSettings[propertyName] || []; | ||
}; | ||
return value || mochaSettings[propertyName] || []; | ||
}; | ||
} | ||
module.exports = { | ||
getAdditionalTestFunctions: settingFor('additionalTestFunctions'), | ||
additionalSuiteNames: settingFor('additionalSuiteNames'), | ||
getAdditionalXFunctions: settingFor('additionalXFunctions') | ||
getAdditionalTestFunctions: settingFor('additionalTestFunctions'), | ||
additionalSuiteNames: settingFor('additionalSuiteNames'), | ||
getAdditionalXFunctions: settingFor('additionalXFunctions') | ||
}; |
{ | ||
"name": "eslint-plugin-mocha", | ||
"version": "4.12.1", | ||
"version": "5.0.0", | ||
"description": "Eslint rules for mocha.", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"lib/", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"pretest": "eslint .", | ||
"test": "npm run test:unit --coverage && npm run check-coverage", | ||
"test:unit": "istanbul test _mocha test -- --recursive --reporter dot", | ||
"check-coverage": "istanbul check-coverage --statement 100 --branch 100 --function 100 --lines 100", | ||
"test": "npm run test:unit:with-coverage", | ||
"test:unit": "mocha test --recursive --reporter dot", | ||
"test:unit:with-coverage": "nyc npm run test:unit", | ||
"coveralls": "cat ./build/coverage/lcov.info | coveralls", | ||
@@ -18,11 +27,13 @@ "changelog": "pr-log" | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"pr-log": "^2.0.0", | ||
"istanbul": "^0.4.2", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.0.0", | ||
"eslint-config-holidaycheck": "^0.12.1", | ||
"eslint-plugin-node": "^6.0.1", | ||
"mocha": "^5.0.1", | ||
"eslint": "^4.0.0", | ||
"coveralls": "^3.0.0" | ||
"nyc": "^11.4.1", | ||
"pr-log": "^3.0.0" | ||
}, | ||
"peerDependencies": { | ||
"eslint": "^2.0.0 || ^3.0.0 || ^4.0.0" | ||
"eslint": ">= 4.0.0" | ||
}, | ||
@@ -47,3 +58,21 @@ "repository": { | ||
"mocha" | ||
] | ||
], | ||
"nyc": { | ||
"all": true, | ||
"cache": false, | ||
"lines": 100, | ||
"statements": 100, | ||
"functions": 100, | ||
"branches": 100, | ||
"exclude": [ | ||
"build", | ||
"test" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text-summary" | ||
], | ||
"check-coverage": true, | ||
"report-dir": "build" | ||
} | ||
} |
@@ -13,3 +13,3 @@ [![NPM Version](https://img.shields.io/npm/v/eslint-plugin-mocha.svg?style=flat)](https://www.npmjs.org/package/eslint-plugin-mocha) | ||
This plugin requires ESLint `2.0.0` or later. | ||
This plugin requires ESLint `4.0.0` or later. | ||
@@ -16,0 +16,0 @@ `npm install --save-dev eslint-plugin-mocha` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45490
8
25
986