UNPKG

5.43 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 ts = __importStar(require("typescript"));
23const tsutils = __importStar(require("tsutils"));
24const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
25const util_1 = require("../util");
26const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('dot-notation');
27exports.default = (0, util_1.createRule)({
28 name: 'dot-notation',
29 meta: {
30 type: 'suggestion',
31 docs: {
32 description: 'enforce dot notation whenever possible',
33 recommended: false,
34 extendsBaseRule: true,
35 requiresTypeChecking: true,
36 },
37 schema: [
38 {
39 type: 'object',
40 properties: {
41 allowKeywords: {
42 type: 'boolean',
43 default: true,
44 },
45 allowPattern: {
46 type: 'string',
47 default: '',
48 },
49 allowPrivateClassPropertyAccess: {
50 type: 'boolean',
51 default: false,
52 },
53 allowProtectedClassPropertyAccess: {
54 type: 'boolean',
55 default: false,
56 },
57 allowIndexSignaturePropertyAccess: {
58 type: 'boolean',
59 default: false,
60 },
61 },
62 additionalProperties: false,
63 },
64 ],
65 fixable: baseRule.meta.fixable,
66 hasSuggestions: baseRule.meta.hasSuggestions,
67 messages: baseRule.meta.messages,
68 },
69 defaultOptions: [
70 {
71 allowPrivateClassPropertyAccess: false,
72 allowProtectedClassPropertyAccess: false,
73 allowIndexSignaturePropertyAccess: false,
74 allowKeywords: true,
75 allowPattern: '',
76 },
77 ],
78 create(context, [options]) {
79 var _a;
80 const rules = baseRule.create(context);
81 const { program, esTreeNodeToTSNodeMap } = (0, util_1.getParserServices)(context);
82 const typeChecker = program.getTypeChecker();
83 const allowPrivateClassPropertyAccess = options.allowPrivateClassPropertyAccess;
84 const allowProtectedClassPropertyAccess = options.allowProtectedClassPropertyAccess;
85 const allowIndexSignaturePropertyAccess = ((_a = options.allowIndexSignaturePropertyAccess) !== null && _a !== void 0 ? _a : false) ||
86 tsutils.isCompilerOptionEnabled(program.getCompilerOptions(),
87 // @ts-expect-error - TS is refining the type to never for some reason
88 'noPropertyAccessFromIndexSignature');
89 return {
90 MemberExpression(node) {
91 var _a, _b, _c;
92 if ((allowPrivateClassPropertyAccess ||
93 allowProtectedClassPropertyAccess ||
94 allowIndexSignaturePropertyAccess) &&
95 node.computed) {
96 // for perf reasons - only fetch symbols if we have to
97 const propertySymbol = typeChecker.getSymbolAtLocation(esTreeNodeToTSNodeMap.get(node.property));
98 const modifierKind = (_c = (_b = (_a = propertySymbol === null || propertySymbol === void 0 ? void 0 : propertySymbol.getDeclarations()) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.modifiers) === null || _c === void 0 ? void 0 : _c[0].kind;
99 if ((allowPrivateClassPropertyAccess &&
100 modifierKind == ts.SyntaxKind.PrivateKeyword) ||
101 (allowProtectedClassPropertyAccess &&
102 modifierKind == ts.SyntaxKind.ProtectedKeyword)) {
103 return;
104 }
105 if (propertySymbol === undefined &&
106 allowIndexSignaturePropertyAccess) {
107 const objectType = typeChecker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(node.object));
108 const indexType = objectType
109 .getNonNullableType()
110 .getStringIndexType();
111 if (indexType != undefined) {
112 return;
113 }
114 }
115 }
116 rules.MemberExpression(node);
117 },
118 };
119 },
120});
121//# sourceMappingURL=dot-notation.js.map
\No newline at end of file