1 | 'use strict';
|
2 |
|
3 | const {isDeepStrictEqual} = require('node:util');
|
4 | const espurify = require('espurify');
|
5 | const {visitIf} = require('enhance-visitors');
|
6 | const createAvaRule = require('../create-ava-rule');
|
7 | const util = require('../util');
|
8 |
|
9 | const notAllowed = [
|
10 | 'truthy',
|
11 | 'true',
|
12 | 'falsy',
|
13 | 'false',
|
14 | 'is',
|
15 | 'not',
|
16 | 'regex',
|
17 | 'notRegex',
|
18 | 'ifError',
|
19 | ];
|
20 |
|
21 | const assertionCalleeAst = methodName => ({
|
22 | type: 'MemberExpression',
|
23 | object: {
|
24 | type: 'Identifier',
|
25 | name: 't',
|
26 | },
|
27 | property: {
|
28 | type: 'Identifier',
|
29 | name: methodName,
|
30 | },
|
31 | computed: false,
|
32 | });
|
33 |
|
34 | const skippedAssertionCalleeAst = methodName => ({
|
35 | type: 'MemberExpression',
|
36 | object: {
|
37 | type: 'MemberExpression',
|
38 | object: {
|
39 | type: 'Identifier',
|
40 | name: 't',
|
41 | },
|
42 | property: {
|
43 | type: 'Identifier',
|
44 | name: 'skip',
|
45 | },
|
46 | computed: false,
|
47 | },
|
48 | property: {
|
49 | type: 'Identifier',
|
50 | name: methodName,
|
51 | },
|
52 | computed: false,
|
53 | });
|
54 |
|
55 | const isCalleeMatched = (callee, methodName) =>
|
56 | isDeepStrictEqual(callee, assertionCalleeAst(methodName))
|
57 | || isDeepStrictEqual(callee, skippedAssertionCalleeAst(methodName));
|
58 |
|
59 | const create = context => {
|
60 | const ava = createAvaRule();
|
61 |
|
62 | return ava.merge({
|
63 | CallExpression: visitIf([
|
64 | ava.isInTestFile,
|
65 | ava.isInTestNode,
|
66 | ])(node => {
|
67 | const callee = espurify(node.callee);
|
68 |
|
69 | if (callee.type === 'MemberExpression') {
|
70 | for (const methodName of notAllowed) {
|
71 | if (isCalleeMatched(callee, methodName)) {
|
72 | context.report({
|
73 | node,
|
74 | message: 'Only asserts with no power-assert alternative are allowed.',
|
75 | });
|
76 | }
|
77 | }
|
78 | }
|
79 | }),
|
80 | });
|
81 | };
|
82 |
|
83 | module.exports = {
|
84 | create,
|
85 | meta: {
|
86 | type: 'suggestion',
|
87 | docs: {
|
88 | description: 'Enforce the use of the asserts that have no [power-assert](https://github.com/power-assert-js/power-assert) alternative.',
|
89 | url: util.getDocsUrl(__filename),
|
90 | },
|
91 | schema: [],
|
92 | },
|
93 | };
|