UNPKG

5.75 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 tsutils = __importStar(require("tsutils"));
24const util = __importStar(require("../util"));
25const util_1 = require("../util");
26exports.default = util.createRule({
27 name: 'no-unsafe-member-access',
28 meta: {
29 type: 'problem',
30 docs: {
31 description: 'Disallows member access on any typed variables',
32 recommended: 'error',
33 requiresTypeChecking: true,
34 },
35 messages: {
36 unsafeMemberExpression: 'Unsafe member access {{property}} on an `any` value.',
37 unsafeThisMemberExpression: [
38 'Unsafe member access {{property}} on an `any` value. `this` is typed as `any`.',
39 'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
40 ].join('\n'),
41 unsafeComputedMemberAccess: 'Computed name {{property}} resolves to an any value.',
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 const sourceCode = context.getSourceCode();
52 const stateCache = new Map();
53 function checkMemberExpression(node) {
54 const cachedState = stateCache.get(node);
55 if (cachedState) {
56 return cachedState;
57 }
58 if (node.object.type === utils_1.AST_NODE_TYPES.MemberExpression) {
59 const objectState = checkMemberExpression(node.object);
60 if (objectState === 1 /* Unsafe */) {
61 // if the object is unsafe, we know this will be unsafe as well
62 // we don't need to report, as we have already reported on the inner member expr
63 stateCache.set(node, objectState);
64 return objectState;
65 }
66 }
67 const tsNode = esTreeNodeToTSNodeMap.get(node.object);
68 const type = checker.getTypeAtLocation(tsNode);
69 const state = util.isTypeAnyType(type) ? 1 /* Unsafe */ : 2 /* Safe */;
70 stateCache.set(node, state);
71 if (state === 1 /* Unsafe */) {
72 const propertyName = sourceCode.getText(node.property);
73 let messageId = 'unsafeMemberExpression';
74 if (!isNoImplicitThis) {
75 // `this.foo` or `this.foo[bar]`
76 const thisExpression = (0, util_1.getThisExpression)(node);
77 if (thisExpression &&
78 util.isTypeAnyType(util.getConstrainedTypeAtLocation(checker, esTreeNodeToTSNodeMap.get(thisExpression)))) {
79 messageId = 'unsafeThisMemberExpression';
80 }
81 }
82 context.report({
83 node,
84 messageId,
85 data: {
86 property: node.computed ? `[${propertyName}]` : `.${propertyName}`,
87 },
88 });
89 }
90 return state;
91 }
92 return {
93 // ignore MemberExpression if it's parent is TSClassImplements or TSInterfaceHeritage
94 ':not(TSClassImplements, TSInterfaceHeritage) > MemberExpression': checkMemberExpression,
95 'MemberExpression[computed = true] > *.property'(node) {
96 if (
97 // x[1]
98 node.type === utils_1.AST_NODE_TYPES.Literal ||
99 // x[1++] x[++x] etc
100 // FUN FACT - **all** update expressions return type number, regardless of the argument's type,
101 // because JS engines return NaN if there the argument is not a number.
102 node.type === utils_1.AST_NODE_TYPES.UpdateExpression) {
103 // perf optimizations - literals can obviously never be `any`
104 return;
105 }
106 const tsNode = esTreeNodeToTSNodeMap.get(node);
107 const type = checker.getTypeAtLocation(tsNode);
108 if (util.isTypeAnyType(type)) {
109 const propertyName = sourceCode.getText(node);
110 context.report({
111 node,
112 messageId: 'unsafeComputedMemberAccess',
113 data: {
114 property: `[${propertyName}]`,
115 },
116 });
117 }
118 },
119 };
120 },
121});
122//# sourceMappingURL=no-unsafe-member-access.js.map
\No newline at end of file