UNPKG

5.33 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-non-null-assertion',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'Disallows non-null assertions using the `!` postfix operator',
30 recommended: 'warn',
31 suggestion: true,
32 },
33 hasSuggestions: true,
34 messages: {
35 noNonNull: 'Forbidden non-null assertion.',
36 suggestOptionalChain: 'Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator.',
37 },
38 schema: [],
39 },
40 defaultOptions: [],
41 create(context) {
42 const sourceCode = context.getSourceCode();
43 return {
44 TSNonNullExpression(node) {
45 var _a, _b;
46 const suggest = [];
47 function convertTokenToOptional(replacement) {
48 return (fixer) => {
49 const operator = sourceCode.getTokenAfter(node.expression, util.isNonNullAssertionPunctuator);
50 if (operator) {
51 return fixer.replaceText(operator, replacement);
52 }
53 return null;
54 };
55 }
56 function removeToken() {
57 return (fixer) => {
58 const operator = sourceCode.getTokenAfter(node.expression, util.isNonNullAssertionPunctuator);
59 if (operator) {
60 return fixer.remove(operator);
61 }
62 return null;
63 };
64 }
65 if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.MemberExpression &&
66 node.parent.object === node) {
67 if (!node.parent.optional) {
68 if (node.parent.computed) {
69 // it is x![y]?.z
70 suggest.push({
71 messageId: 'suggestOptionalChain',
72 fix: convertTokenToOptional('?.'),
73 });
74 }
75 else {
76 // it is x!.y?.z
77 suggest.push({
78 messageId: 'suggestOptionalChain',
79 fix: convertTokenToOptional('?'),
80 });
81 }
82 }
83 else {
84 if (node.parent.computed) {
85 // it is x!?.[y].z
86 suggest.push({
87 messageId: 'suggestOptionalChain',
88 fix: removeToken(),
89 });
90 }
91 else {
92 // it is x!?.y.z
93 suggest.push({
94 messageId: 'suggestOptionalChain',
95 fix: removeToken(),
96 });
97 }
98 }
99 }
100 else if (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.type) === utils_1.AST_NODE_TYPES.CallExpression &&
101 node.parent.callee === node) {
102 if (!node.parent.optional) {
103 // it is x.y?.z!()
104 suggest.push({
105 messageId: 'suggestOptionalChain',
106 fix: convertTokenToOptional('?.'),
107 });
108 }
109 else {
110 // it is x.y.z!?.()
111 suggest.push({
112 messageId: 'suggestOptionalChain',
113 fix: removeToken(),
114 });
115 }
116 }
117 context.report({
118 node,
119 messageId: 'noNonNull',
120 suggest,
121 });
122 },
123 };
124 },
125});
126//# sourceMappingURL=no-non-null-assertion.js.map
\No newline at end of file