1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const utils_1 = require("@typescript-eslint/utils");
|
23 | const tsutils = __importStar(require("tsutils"));
|
24 | const util = __importStar(require("../util"));
|
25 | const util_1 = require("../util");
|
26 | exports.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 ) {
|
61 |
|
62 |
|
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 : 2 ;
|
70 | stateCache.set(node, state);
|
71 | if (state === 1 ) {
|
72 | const propertyName = sourceCode.getText(node.property);
|
73 | let messageId = 'unsafeMemberExpression';
|
74 | if (!isNoImplicitThis) {
|
75 |
|
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 |
|
94 | ':not(TSClassImplements, TSInterfaceHeritage) > MemberExpression': checkMemberExpression,
|
95 | 'MemberExpression[computed = true] > *.property'(node) {
|
96 | if (
|
97 |
|
98 | node.type === utils_1.AST_NODE_TYPES.Literal ||
|
99 |
|
100 |
|
101 |
|
102 | node.type === utils_1.AST_NODE_TYPES.UpdateExpression) {
|
103 |
|
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 |
|
\ | No newline at end of file |