Prefer eslint-mocha for linting `.only` in tests
DISABLE_THIRD_PARTY_CHECK=new npm module causes package-lock change
We can get rid of a custom script and use ESLint for these rules.
Additionally this means editors will highlight the issue in the editor.
Change-Id: I552b52e7f07b4e7430b3f685dbc3b5fad36ffff2
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2052167
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
diff --git a/node_modules/eslint-plugin-mocha/lib/rules/no-synchronous-tests.js b/node_modules/eslint-plugin-mocha/lib/rules/no-synchronous-tests.js
new file mode 100644
index 0000000..80174fc
--- /dev/null
+++ b/node_modules/eslint-plugin-mocha/lib/rules/no-synchronous-tests.js
@@ -0,0 +1,90 @@
+'use strict';
+
+const isNil = require('ramda/src/isNil');
+const find = require('ramda/src/find');
+const astUtil = require('../util/ast');
+
+const asyncMethods = [ 'async', 'callback', 'promise' ];
+
+function hasAsyncCallback(functionExpression) {
+ return functionExpression.params.length === 1;
+}
+
+function isAsyncFunction(functionExpression) {
+ return functionExpression.async === true;
+}
+
+function findPromiseReturnStatement(nodes) {
+ return find(function (node) {
+ return node.type === 'ReturnStatement' && node.argument && node.argument.type !== 'Literal';
+ }, nodes);
+}
+
+function doesReturnPromise(functionExpression) {
+ const bodyStatement = functionExpression.body;
+ let returnStatement = null;
+
+ if (bodyStatement.type === 'BlockStatement') {
+ returnStatement = findPromiseReturnStatement(functionExpression.body.body);
+ } else if (bodyStatement.type !== 'Literal') {
+ // allow arrow statements calling a promise with implicit return.
+ returnStatement = bodyStatement;
+ }
+
+ return returnStatement !== null &&
+ typeof returnStatement !== 'undefined';
+}
+
+module.exports = function (context) {
+ const options = context.options[0] || {};
+ const allowedAsyncMethods = isNil(options.allowed) ? asyncMethods : options.allowed;
+
+ function check(node) {
+ if (astUtil.hasParentMochaFunctionCall(node)) {
+ // For each allowed async test method, check if it is used in the test
+ const testAsyncMethods = allowedAsyncMethods.map(function (method) {
+ switch (method) {
+ case 'async':
+ return isAsyncFunction(node);
+
+ case 'callback':
+ return hasAsyncCallback(node);
+
+ default:
+ return doesReturnPromise(node);
+ }
+ });
+
+ // Check that at least one allowed async test method is used in the test
+ const isAsyncTest = testAsyncMethods.some(function (value) {
+ return value === true;
+ });
+
+ if (!isAsyncTest) {
+ context.report(node, 'Unexpected synchronous test.');
+ }
+ }
+ }
+
+ return {
+ FunctionExpression: check,
+ ArrowFunctionExpression: check
+ };
+};
+
+module.exports.schema = [
+ {
+ type: 'object',
+ properties: {
+ allowed: {
+ type: 'array',
+ items: {
+ type: 'string',
+ enum: asyncMethods
+ },
+ minItems: 1,
+ uniqueItems: true
+ }
+ }
+ }
+];