prefer-to-be.js
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _utils = require("./utils");
const isNullLiteral = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && node.value === null;
/**
* Checks if the given `ParsedEqualityMatcherCall` is a call to one of the equality matchers,
* with a `null` literal as the sole argument.
*/
const isNullEqualityMatcher = matcher => isNullLiteral(getFirstArgument(matcher));
const isFirstArgumentIdentifier = (matcher, name) => (0, _utils.isIdentifier)(getFirstArgument(matcher), name);
const shouldUseToBe = matcher => {
const firstArg = getFirstArgument(matcher);
if (firstArg.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
// regex literals are classed as literals, but they're actually objects
// which means "toBe" will give different results than other matchers
return !('regex' in firstArg);
}
return firstArg.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral;
};
const getFirstArgument = matcher => {
return (0, _utils.followTypeAssertionChain)(matcher.arguments[0]);
};
const reportPreferToBe = (context, whatToBe, matcher, modifier) => {
const modifierNode = (modifier === null || modifier === void 0 ? void 0 : modifier.negation) || (modifier === null || modifier === void 0 ? void 0 : modifier.name) === _utils.ModifierName.not && (modifier === null || modifier === void 0 ? void 0 : modifier.node);
context.report({
messageId: `useToBe${whatToBe}`,
fix(fixer) {
var _matcher$arguments;
const fixes = [fixer.replaceText(matcher.node.property, `toBe${whatToBe}`)];
if ((_matcher$arguments = matcher.arguments) !== null && _matcher$arguments !== void 0 && _matcher$arguments.length && whatToBe !== '') {
fixes.push(fixer.remove(matcher.arguments[0]));
}
if (modifierNode) {
fixes.push(fixer.removeRange([modifierNode.property.range[0] - 1, modifierNode.property.range[1]]));
}
return fixes;
},
node: matcher.node.property
});
};
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `toBe()` for primitive literals',
recommended: false
},
messages: {
useToBe: 'Use `toBe` when expecting primitive literals',
useToBeUndefined: 'Use `toBeUndefined` instead',
useToBeDefined: 'Use `toBeDefined` instead',
useToBeNull: 'Use `toBeNull` instead',
useToBeNaN: 'Use `toBeNaN` instead'
},
fixable: 'code',
type: 'suggestion',
schema: []
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!(0, _utils.isExpectCall)(node)) {
return;
}
const {
matcher,
modifier
} = (0, _utils.parseExpectCall)(node);
if (!matcher) {
return;
}
if (((modifier === null || modifier === void 0 ? void 0 : modifier.name) === _utils.ModifierName.not || modifier !== null && modifier !== void 0 && modifier.negation) && ['toBeUndefined', 'toBeDefined'].includes(matcher.name)) {
reportPreferToBe(context, matcher.name === 'toBeDefined' ? 'Undefined' : 'Defined', matcher, modifier);
return;
}
if (!(0, _utils.isParsedEqualityMatcherCall)(matcher)) {
return;
}
if (isNullEqualityMatcher(matcher)) {
reportPreferToBe(context, 'Null', matcher);
return;
}
if (isFirstArgumentIdentifier(matcher, 'undefined')) {
const name = (modifier === null || modifier === void 0 ? void 0 : modifier.name) === _utils.ModifierName.not || modifier !== null && modifier !== void 0 && modifier.negation ? 'Defined' : 'Undefined';
reportPreferToBe(context, name, matcher, modifier);
return;
}
if (isFirstArgumentIdentifier(matcher, 'NaN')) {
reportPreferToBe(context, 'NaN', matcher);
return;
}
if (shouldUseToBe(matcher) && matcher.name !== _utils.EqualityMatcher.toBe) {
reportPreferToBe(context, '', matcher);
}
}
};
}
});
exports.default = _default;