| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|---|
| 9 |
|
|---|
| 10 | var _utils = require("./utils");
|
|---|
| 11 |
|
|---|
| 12 | /*
|
|---|
| 13 | * This implementation is adapted from eslint-plugin-jasmine.
|
|---|
| 14 | * MIT license, Remco Haszing.
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Checks if node names returned by getNodeName matches any of the given star patterns
|
|---|
| 19 | * Pattern examples:
|
|---|
| 20 | * request.*.expect
|
|---|
| 21 | * request.**.expect
|
|---|
| 22 | * request.**.expect*
|
|---|
| 23 | */
|
|---|
| 24 | function matchesAssertFunctionName(nodeName, patterns) {
|
|---|
| 25 | return patterns.some(p => new RegExp(`^${p.split('.').map(x => {
|
|---|
| 26 | if (x === '**') return '[a-z\\.]*';
|
|---|
| 27 | return x.replace(/\*/gu, '[a-z]*');
|
|---|
| 28 | }).join('\\.')}(\\.|$)`, 'ui').test(nodeName));
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | var _default = (0, _utils.createRule)({
|
|---|
| 32 | name: __filename,
|
|---|
| 33 | meta: {
|
|---|
| 34 | docs: {
|
|---|
| 35 | category: 'Best Practices',
|
|---|
| 36 | description: 'Enforce assertion to be made in a test body',
|
|---|
| 37 | recommended: 'warn'
|
|---|
| 38 | },
|
|---|
| 39 | messages: {
|
|---|
| 40 | noAssertions: 'Test has no assertions'
|
|---|
| 41 | },
|
|---|
| 42 | schema: [{
|
|---|
| 43 | type: 'object',
|
|---|
| 44 | properties: {
|
|---|
| 45 | assertFunctionNames: {
|
|---|
| 46 | type: 'array',
|
|---|
| 47 | items: [{
|
|---|
| 48 | type: 'string'
|
|---|
| 49 | }]
|
|---|
| 50 | },
|
|---|
| 51 | additionalTestBlockFunctions: {
|
|---|
| 52 | type: 'array',
|
|---|
| 53 | items: {
|
|---|
| 54 | type: 'string'
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | },
|
|---|
| 58 | additionalProperties: false
|
|---|
| 59 | }],
|
|---|
| 60 | type: 'suggestion'
|
|---|
| 61 | },
|
|---|
| 62 | defaultOptions: [{
|
|---|
| 63 | assertFunctionNames: ['expect'],
|
|---|
| 64 | additionalTestBlockFunctions: []
|
|---|
| 65 | }],
|
|---|
| 66 |
|
|---|
| 67 | create(context, [{
|
|---|
| 68 | assertFunctionNames = ['expect'],
|
|---|
| 69 | additionalTestBlockFunctions = []
|
|---|
| 70 | }]) {
|
|---|
| 71 | const unchecked = [];
|
|---|
| 72 |
|
|---|
| 73 | function checkCallExpressionUsed(nodes) {
|
|---|
| 74 | for (const node of nodes) {
|
|---|
| 75 | const index = node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? unchecked.indexOf(node) : -1;
|
|---|
| 76 |
|
|---|
| 77 | if (node.type === _experimentalUtils.AST_NODE_TYPES.FunctionDeclaration) {
|
|---|
| 78 | const declaredVariables = context.getDeclaredVariables(node);
|
|---|
| 79 | const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
|
|---|
| 80 | checkCallExpressionUsed(testCallExpressions);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | if (index !== -1) {
|
|---|
| 84 | unchecked.splice(index, 1);
|
|---|
| 85 | break;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | return {
|
|---|
| 91 | CallExpression(node) {
|
|---|
| 92 | var _getNodeName;
|
|---|
| 93 |
|
|---|
| 94 | const name = (_getNodeName = (0, _utils.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 ? _getNodeName : '';
|
|---|
| 95 |
|
|---|
| 96 | if ((0, _utils.isTestCaseCall)(node) || additionalTestBlockFunctions.includes(name)) {
|
|---|
| 97 | if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'todo')) {
|
|---|
| 98 | return;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | unchecked.push(node);
|
|---|
| 102 | } else if (matchesAssertFunctionName(name, assertFunctionNames)) {
|
|---|
| 103 | // Return early in case of nested `it` statements.
|
|---|
| 104 | checkCallExpressionUsed(context.getAncestors());
|
|---|
| 105 | }
|
|---|
| 106 | },
|
|---|
| 107 |
|
|---|
| 108 | 'Program:exit'() {
|
|---|
| 109 | unchecked.forEach(node => context.report({
|
|---|
| 110 | messageId: 'noAssertions',
|
|---|
| 111 | node
|
|---|
| 112 | }));
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | };
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | });
|
|---|
| 119 |
|
|---|
| 120 | exports.default = _default; |
|---|