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 util = __importStar(require("../util"));
|
24 | const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
|
25 | const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-magic-numbers');
|
26 |
|
27 | const schema = util.deepMerge(
|
28 |
|
29 | Array.isArray(baseRule.meta.schema)
|
30 | ? baseRule.meta.schema[0]
|
31 | : baseRule.meta.schema, {
|
32 | properties: {
|
33 | ignoreNumericLiteralTypes: {
|
34 | type: 'boolean',
|
35 | },
|
36 | ignoreEnums: {
|
37 | type: 'boolean',
|
38 | },
|
39 | ignoreReadonlyClassProperties: {
|
40 | type: 'boolean',
|
41 | },
|
42 | },
|
43 | });
|
44 | exports.default = util.createRule({
|
45 | name: 'no-magic-numbers',
|
46 | meta: {
|
47 | type: 'suggestion',
|
48 | docs: {
|
49 | description: 'Disallow magic numbers',
|
50 | recommended: false,
|
51 | extendsBaseRule: true,
|
52 | },
|
53 | schema: [schema],
|
54 | messages: baseRule.meta.messages,
|
55 | },
|
56 | defaultOptions: [
|
57 | {
|
58 | ignore: [],
|
59 | ignoreArrayIndexes: false,
|
60 | enforceConst: false,
|
61 | detectObjects: false,
|
62 | ignoreNumericLiteralTypes: false,
|
63 | ignoreEnums: false,
|
64 | ignoreReadonlyClassProperties: false,
|
65 | },
|
66 | ],
|
67 | create(context, [options]) {
|
68 | const rules = baseRule.create(context);
|
69 | return {
|
70 | Literal(node) {
|
71 | var _a;
|
72 |
|
73 | if (options.ignoreEnums && isParentTSEnumDeclaration(node)) {
|
74 | return;
|
75 | }
|
76 |
|
77 | if (options.ignoreNumericLiteralTypes &&
|
78 | typeof node.value === 'number' &&
|
79 | isTSNumericLiteralType(node)) {
|
80 | return;
|
81 | }
|
82 |
|
83 | if ((typeof node.value === 'number' || typeof node.value === 'bigint') &&
|
84 | isParentTSReadonlyPropertyDefinition(node)) {
|
85 | if (options.ignoreReadonlyClassProperties) {
|
86 | return;
|
87 | }
|
88 | let fullNumberNode = node;
|
89 | let raw = node.raw;
|
90 | if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.UnaryExpression &&
|
91 |
|
92 |
|
93 | node.parent.operator === '-') {
|
94 | fullNumberNode = node.parent;
|
95 | raw = `${node.parent.operator}${node.raw}`;
|
96 | }
|
97 | context.report({
|
98 | messageId: 'noMagic',
|
99 | node: fullNumberNode,
|
100 | data: { raw },
|
101 | });
|
102 | return;
|
103 | }
|
104 |
|
105 | rules.Literal(node);
|
106 | },
|
107 | };
|
108 | },
|
109 | });
|
110 |
|
111 |
|
112 |
|
113 | function getLiteralParent(node) {
|
114 | var _a;
|
115 | if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.UnaryExpression &&
|
116 | ['-', '+'].includes(node.parent.operator)) {
|
117 | return node.parent.parent;
|
118 | }
|
119 | return node.parent;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function isGrandparentTSTypeAliasDeclaration(node) {
|
128 | var _a, _b;
|
129 | return ((_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.type) === utils_1.AST_NODE_TYPES.TSTypeAliasDeclaration;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function isGrandparentTSUnionType(node) {
|
138 | var _a, _b;
|
139 | if (((_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.type) === utils_1.AST_NODE_TYPES.TSUnionType) {
|
140 | return isGrandparentTSTypeAliasDeclaration(node.parent);
|
141 | }
|
142 | return false;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function isParentTSEnumDeclaration(node) {
|
151 | const parent = getLiteralParent(node);
|
152 | return (parent === null || parent === void 0 ? void 0 : parent.type) === utils_1.AST_NODE_TYPES.TSEnumMember;
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function isParentTSLiteralType(node) {
|
161 | var _a;
|
162 | return ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.TSLiteralType;
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function isTSNumericLiteralType(node) {
|
171 | var _a;
|
172 |
|
173 | if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.UnaryExpression &&
|
174 | node.parent.operator === '-') {
|
175 | node = node.parent;
|
176 | }
|
177 |
|
178 | if (!isParentTSLiteralType(node)) {
|
179 | return false;
|
180 | }
|
181 |
|
182 | if (isGrandparentTSTypeAliasDeclaration(node)) {
|
183 | return true;
|
184 | }
|
185 |
|
186 | if (isGrandparentTSUnionType(node)) {
|
187 | return true;
|
188 | }
|
189 | return false;
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function isParentTSReadonlyPropertyDefinition(node) {
|
198 | const parent = getLiteralParent(node);
|
199 | if ((parent === null || parent === void 0 ? void 0 : parent.type) === utils_1.AST_NODE_TYPES.PropertyDefinition && parent.readonly) {
|
200 | return true;
|
201 | }
|
202 | return false;
|
203 | }
|
204 |
|
\ | No newline at end of file |