UNPKG

3.51 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 tsutils = __importStar(require("tsutils"));
23const util = __importStar(require("../util"));
24const util_1 = require("../util");
25exports.default = util.createRule({
26 name: 'no-unsafe-call',
27 meta: {
28 type: 'problem',
29 docs: {
30 description: 'Disallows calling an any type value',
31 recommended: 'error',
32 requiresTypeChecking: true,
33 },
34 messages: {
35 unsafeCall: 'Unsafe call of an `any` typed value.',
36 unsafeCallThis: [
37 'Unsafe call of an `any` typed value. `this` is typed as `any`.',
38 'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
39 ].join('\n'),
40 unsafeNew: 'Unsafe construction of an any type value.',
41 unsafeTemplateTag: 'Unsafe any typed template tag.',
42 },
43 schema: [],
44 },
45 defaultOptions: [],
46 create(context) {
47 const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context);
48 const checker = program.getTypeChecker();
49 const compilerOptions = program.getCompilerOptions();
50 const isNoImplicitThis = tsutils.isStrictCompilerOptionEnabled(compilerOptions, 'noImplicitThis');
51 function checkCall(node, reportingNode, messageId) {
52 const tsNode = esTreeNodeToTSNodeMap.get(node);
53 const type = util.getConstrainedTypeAtLocation(checker, tsNode);
54 if (util.isTypeAnyType(type)) {
55 if (!isNoImplicitThis) {
56 // `this()` or `this.foo()` or `this.foo[bar]()`
57 const thisExpression = (0, util_1.getThisExpression)(node);
58 if (thisExpression &&
59 util.isTypeAnyType(util.getConstrainedTypeAtLocation(checker, esTreeNodeToTSNodeMap.get(thisExpression)))) {
60 messageId = 'unsafeCallThis';
61 }
62 }
63 context.report({
64 node: reportingNode,
65 messageId: messageId,
66 });
67 }
68 }
69 return {
70 'CallExpression > *.callee'(node) {
71 checkCall(node, node, 'unsafeCall');
72 },
73 NewExpression(node) {
74 checkCall(node.callee, node, 'unsafeNew');
75 },
76 'TaggedTemplateExpression > *.tag'(node) {
77 checkCall(node, node, 'unsafeTemplateTag');
78 },
79 };
80 },
81});
82//# sourceMappingURL=no-unsafe-call.js.map
\No newline at end of file