UNPKG

8.01 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 util = __importStar(require("../util"));
24const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
25const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-magic-numbers');
26// Extend base schema with additional property to ignore TS numeric literal types
27const schema = util.deepMerge(
28// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- https://github.com/microsoft/TypeScript/issues/17002
29Array.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});
44exports.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 // Check if the node is a TypeScript enum declaration
73 if (options.ignoreEnums && isParentTSEnumDeclaration(node)) {
74 return;
75 }
76 // Check TypeScript specific nodes for Numeric Literal
77 if (options.ignoreNumericLiteralTypes &&
78 typeof node.value === 'number' &&
79 isTSNumericLiteralType(node)) {
80 return;
81 }
82 // Check if the node is a readonly class property
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 // the base rule only shows the operator for negative numbers
92 // https://github.com/eslint/eslint/blob/9dfc8501fb1956c90dc11e6377b4cb38a6bea65d/lib/rules/no-magic-numbers.js#L126
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 // Let the base rule deal with the rest
105 rules.Literal(node);
106 },
107 };
108 },
109});
110/**
111 * Gets the true parent of the literal, handling prefixed numbers (-1 / +1)
112 */
113function 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 * Checks if the node grandparent is a Typescript type alias declaration
123 * @param node the node to be validated.
124 * @returns true if the node grandparent is a Typescript type alias declaration
125 * @private
126 */
127function 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 * Checks if the node grandparent is a Typescript union type and its parent is a type alias declaration
133 * @param node the node to be validated.
134 * @returns true if the node grandparent is a Typescript union type and its parent is a type alias declaration
135 * @private
136 */
137function 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 * Checks if the node parent is a Typescript enum member
146 * @param node the node to be validated.
147 * @returns true if the node parent is a Typescript enum member
148 * @private
149 */
150function 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 * Checks if the node parent is a Typescript literal type
156 * @param node the node to be validated.
157 * @returns true if the node parent is a Typescript literal type
158 * @private
159 */
160function 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 * Checks if the node is a valid TypeScript numeric literal type.
166 * @param node the node to be validated.
167 * @returns true if the node is a TypeScript numeric literal type.
168 * @private
169 */
170function isTSNumericLiteralType(node) {
171 var _a;
172 // For negative numbers, use the parent node
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 // If the parent node is not a TSLiteralType, early return
178 if (!isParentTSLiteralType(node)) {
179 return false;
180 }
181 // If the grandparent is a TSTypeAliasDeclaration, ignore
182 if (isGrandparentTSTypeAliasDeclaration(node)) {
183 return true;
184 }
185 // If the grandparent is a TSUnionType and it's parent is a TSTypeAliasDeclaration, ignore
186 if (isGrandparentTSUnionType(node)) {
187 return true;
188 }
189 return false;
190}
191/**
192 * Checks if the node parent is a readonly class property
193 * @param node the node to be validated.
194 * @returns true if the node parent is a readonly class property
195 * @private
196 */
197function 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//# sourceMappingURL=no-magic-numbers.js.map
\No newline at end of file