UNPKG

4.85 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'no-confusing-non-null-assertion',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'Disallow non-null assertion in locations that may be confusing',
30 recommended: false,
31 },
32 fixable: 'code',
33 hasSuggestions: true,
34 messages: {
35 confusingEqual: 'Confusing combinations of non-null assertion and equal test like "a! == b", which looks very similar to not equal "a !== b".',
36 confusingAssign: 'Confusing combinations of non-null assertion and equal test like "a! = b", which looks very similar to not equal "a != b".',
37 notNeedInEqualTest: 'Unnecessary non-null assertion (!) in equal test.',
38 notNeedInAssign: 'Unnecessary non-null assertion (!) in assignment left hand.',
39 wrapUpLeft: 'Wrap up left hand to avoid putting non-null assertion "!" and "=" together.',
40 },
41 schema: [],
42 },
43 defaultOptions: [],
44 create(context) {
45 const sourceCode = context.getSourceCode();
46 return {
47 'BinaryExpression, AssignmentExpression'(node) {
48 function isLeftHandPrimaryExpression(node) {
49 return node.type === utils_1.AST_NODE_TYPES.TSNonNullExpression;
50 }
51 if (node.operator === '==' ||
52 node.operator === '===' ||
53 node.operator === '=') {
54 const isAssign = node.operator === '=';
55 const leftHandFinalToken = sourceCode.getLastToken(node.left);
56 const tokenAfterLeft = sourceCode.getTokenAfter(node.left);
57 if ((leftHandFinalToken === null || leftHandFinalToken === void 0 ? void 0 : leftHandFinalToken.type) === utils_1.AST_TOKEN_TYPES.Punctuator &&
58 (leftHandFinalToken === null || leftHandFinalToken === void 0 ? void 0 : leftHandFinalToken.value) === '!' &&
59 (tokenAfterLeft === null || tokenAfterLeft === void 0 ? void 0 : tokenAfterLeft.value) !== ')') {
60 if (isLeftHandPrimaryExpression(node.left)) {
61 context.report({
62 node,
63 messageId: isAssign ? 'confusingAssign' : 'confusingEqual',
64 suggest: [
65 {
66 messageId: isAssign
67 ? 'notNeedInAssign'
68 : 'notNeedInEqualTest',
69 fix: (fixer) => [
70 fixer.remove(leftHandFinalToken),
71 ],
72 },
73 ],
74 });
75 }
76 else {
77 context.report({
78 node,
79 messageId: isAssign ? 'confusingAssign' : 'confusingEqual',
80 suggest: [
81 {
82 messageId: 'wrapUpLeft',
83 fix: (fixer) => [
84 fixer.insertTextBefore(node.left, '('),
85 fixer.insertTextAfter(node.left, ')'),
86 ],
87 },
88 ],
89 });
90 }
91 }
92 }
93 },
94 };
95 },
96});
97//# sourceMappingURL=no-confusing-non-null-assertion.js.map
\No newline at end of file