Skip to content

Commit

Permalink
Use some new language features
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Mar 2, 2018
1 parent 3e64442 commit bd5c7de
Show file tree
Hide file tree
Showing 37 changed files with 464 additions and 456 deletions.
14 changes: 7 additions & 7 deletions lib/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var R = require('ramda'),
astUtils = require('../util/ast');
const R = require('ramda');
const astUtils = require('../util/ast');

module.exports = function (context) {
function hasParentMochaFunctionCall(functionExpression) {
Expand All @@ -19,7 +19,7 @@ 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;

return parent.type === 'CallExpression';
}
Expand All @@ -29,10 +29,10 @@ module.exports = function (context) {
}

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);

if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
Expand Down
27 changes: 14 additions & 13 deletions lib/rules/max-top-level-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
* @author Alexander Afanasyev
*/

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;

if (R.isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
Expand All @@ -24,13 +25,13 @@ module.exports = function (context) {
}

return {
CallExpression: function (node) {
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
stack.push(node);
}
},

'CallExpression:exit': function (node) {
'CallExpression:exit'(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
if (stack.length === 1) {
topLevelDescribes.push(node);
Expand All @@ -40,11 +41,11 @@ module.exports = function (context) {
}
},

'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 }.`
});
}
}
Expand Down
30 changes: 15 additions & 15 deletions lib/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'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);

mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);

function matchesMochaTestFunction(object) {
var name = astUtils.getNodeName(object);
const name = astUtils.getNodeName(object);

return mochaTestFunctions.indexOf(name) !== -1;
}
Expand All @@ -34,8 +34,8 @@ module.exports = function (context) {
}

return {
CallExpression: function (node) {
var callee = node.callee;
CallExpression(node) {
const callee = node.callee;

if (callee && isCallToMochasOnlyFunction(callee)) {
context.report({
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/no-global-tests.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

var astUtils = require('../util/ast');
const astUtils = require('../util/ast');

module.exports = function (context) {
function isGlobalScope(scope) {
return scope.type === 'global' || scope.type === 'module';
}

return {
CallExpression: function (node) {
var callee = node.callee,
scope = context.getScope();
CallExpression(node) {
const callee = node.callee;
const scope = context.getScope();

if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
context.report(callee, 'Unexpected global mocha test.');
Expand Down
22 changes: 11 additions & 11 deletions lib/rules/no-hooks-for-single-case.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'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: [],
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) {
if (layer.testCount <= 1) {
layer.hookNodes
Expand All @@ -28,7 +28,7 @@ module.exports = function (context) {
.forEach(function (hookNode) {
context.report({
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`
});
});
}
Expand All @@ -37,11 +37,11 @@ module.exports = function (context) {
}

return {
Program: function (node) {
Program(node) {
layers.push(newDescribeLayer(node));
},

CallExpression: function (node) {
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
layers[layers.length - 1].testCount += 1;
layers.push(newDescribeLayer(node));
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-hooks.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'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`
});
}
}
Expand Down
26 changes: 12 additions & 14 deletions lib/rules/no-identical-title.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

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

function newLayer() {
return {
Expand All @@ -14,7 +14,7 @@ function handlTestCaseTitles(context, titles, node, title) {
if (astUtil.isTestCase(node)) {
if (titles.indexOf(title) !== -1) {
context.report({
node: node,
node,
message: 'Test title is used multiple times in the same test suite.'
});
}
Expand All @@ -23,14 +23,14 @@ function handlTestCaseTitles(context, titles, node, title) {
}

function handlTestSuiteTitles(context, titles, node, title) {
var settings = context.settings;
const settings = context.settings;

if (!astUtil.isDescribe(node, additionalSuiteNames(settings))) {
return;
}
if (titles.indexOf(title) !== -1) {
context.report({
node: node,
node,
message: 'Test suite title is used multiple times.'
});
}
Expand All @@ -42,27 +42,25 @@ function isFirstArgLiteral(node) {
}

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))) {
titleLayers.push(newLayer());
}
if (!isFirstArgLiteral(node)) {
return;
}

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))) {
titleLayers.pop();
}
Expand Down
44 changes: 23 additions & 21 deletions lib/rules/no-mocha-arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
* @author Paul Melnikow
*/

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) {
// When 'async' specified, take care about the keyword.
Expand All @@ -26,37 +25,40 @@ module.exports = function (context) {
}

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') {
// When it((...) => { ... }),
// simply replace '(...) => ' with 'function () '
return fixer.replaceTextRange(
[ 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 }; }`
);
}

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);
}
});
Expand Down
Loading

0 comments on commit bd5c7de

Please sign in to comment.