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