UNPKG

2.69 kBJavaScriptView Raw
1'use strict';
2
3const {visitIf} = require('enhance-visitors');
4const util = require('../util');
5const createAvaRule = require('../create-ava-rule');
6
7const MESSAGE_ID = 'no-deep-equal-with-primative';
8
9const buildDeepEqualMessage = (context, node) => {
10 context.report({
11 node,
12 messageId: MESSAGE_ID,
13 data: {
14 callee: node.callee.property.name,
15 },
16 fix: fixer => fixer.replaceText(node.callee.property, 'is'),
17 });
18};
19
20const buildNotDeepEqualMessage = (context, node) => {
21 context.report({
22 node,
23 messageId: MESSAGE_ID,
24 data: {
25 callee: node.callee.property.name,
26 },
27 fix: fixer => fixer.replaceText(node.callee.property, 'not'),
28 });
29};
30
31const create = context => {
32 const ava = createAvaRule();
33
34 const callExpression = 'CallExpression';
35 const deepEqual = '[callee.property.name="deepEqual"]';
36 const notDeepEqual = '[callee.property.name="notDeepEqual"]';
37
38 const argumentsLiteral = ':matches([arguments.0.type="Literal"][arguments.0.regex="undefined"],[arguments.1.type="Literal"][arguments.1.regex="undefined"])';
39 const argumentsUndefined = ':matches([arguments.0.type="Identifier"][arguments.0.name="undefined"],[arguments.1.type="Identifier"][arguments.1.name="undefined"])';
40 const argumentsTemplate = ':matches([arguments.0.type="TemplateLiteral"],[arguments.1.type="TemplateLiteral"])';
41
42 return ava.merge({
43 [`${callExpression}${deepEqual}${argumentsLiteral}`]: visitIf([
44 ava.isInTestFile,
45 ava.isInTestNode,
46 ])(node => {
47 buildDeepEqualMessage(context, node);
48 }),
49 [`${callExpression}${deepEqual}${argumentsUndefined}`]: visitIf([
50 ava.isInTestFile,
51 ava.isInTestNode,
52 ])(node => {
53 buildDeepEqualMessage(context, node);
54 }),
55 [`${callExpression}${deepEqual}${argumentsTemplate}`]: visitIf([
56 ava.isInTestFile,
57 ava.isInTestNode,
58 ])(node => {
59 buildDeepEqualMessage(context, node);
60 }),
61 [`${callExpression}${notDeepEqual}${argumentsLiteral}`]: visitIf([
62 ava.isInTestFile,
63 ava.isInTestNode,
64 ])(node => {
65 buildNotDeepEqualMessage(context, node);
66 }),
67 [`${callExpression}${notDeepEqual}${argumentsUndefined}`]: visitIf([
68 ava.isInTestFile,
69 ava.isInTestNode,
70 ])(node => {
71 buildNotDeepEqualMessage(context, node);
72 }),
73 [`${callExpression}${notDeepEqual}${argumentsTemplate}`]: visitIf([
74 ava.isInTestFile,
75 ava.isInTestNode,
76 ])(node => {
77 buildNotDeepEqualMessage(context, node);
78 }),
79 });
80};
81
82module.exports = {
83 create,
84 meta: {
85 type: 'suggestion',
86 docs: {
87 description: 'Disallow using `deepEqual` with primitives.',
88 url: util.getDocsUrl(__filename),
89 },
90 fixable: 'code',
91 schema: [],
92 messages: {
93 [MESSAGE_ID]: 'Avoid using `{{callee}}` with literal primitives',
94 },
95 },
96};