4 * Copyright 2018 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var utils_1 = require("../language/utils");
25 var NUMBER_METHODS = new Set(["toExponential", "toFixed", "toPrecision", "toString"]);
26 var IGNORE_JSX_OPTION = "ignore-jsx";
27 var ALLOWED_NUMBERS_OPTION = "allowed-numbers";
28 var Rule = /** @class */ (function (_super) {
29 tslib_1.__extends(Rule, _super);
31 return _super !== null && _super.apply(this, arguments) || this;
33 Rule.FAILURE_STRING = function (num) {
34 return "'magic numbers' are not allowed: " + num;
36 Rule.prototype.apply = function (sourceFile) {
37 return this.applyWithWalker(new NoMagicNumbersWalker(sourceFile, this.ruleName, this.ruleArguments.length > 0
38 ? parseOptions(this.ruleArguments)
39 : parseOptions(Rule.DEFAULT_ALLOWED)));
41 /* tslint:disable:object-literal-sort-keys */
43 ruleName: "no-magic-numbers",
44 description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows the use constant number values outside of variable assignments.\n When no list of allowed values is specified, -1, 0 and 1 are allowed by default."], ["\n Disallows the use constant number values outside of variable assignments.\n When no list of allowed values is specified, -1, 0 and 1 are allowed by default."]))),
45 rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Magic numbers should be avoided as they often lack documentation.\n Forcing them to be stored in variables gives them implicit documentation.\n "], ["\n Magic numbers should be avoided as they often lack documentation.\n Forcing them to be stored in variables gives them implicit documentation.\n "]))),
46 optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n Options may either be a list of numbers to ignore (not consider 'magic'), or an object containing up to two properties:\n * `", "` as the list of numbers to ignore.\n * `", "` to specify that 'magic' numbers should be allowed as JSX attributes."], ["\n Options may either be a list of numbers to ignore (not consider 'magic'), or an object containing up to two properties:\n * \\`", "\\` as the list of numbers to ignore.\n * \\`", "\\` to specify that 'magic' numbers should be allowed as JSX attributes."])), ALLOWED_NUMBERS_OPTION, IGNORE_JSX_OPTION),
55 _a[ALLOWED_NUMBERS_OPTION] = {
58 _a[IGNORE_JSX_OPTION] = {
69 _b[ALLOWED_NUMBERS_OPTION] = [1, 2, 3],
70 _b[IGNORE_JSX_OPTION] = true,
75 typescriptOnly: false,
77 /* tslint:enable:object-literal-sort-keys */
78 Rule.ALLOWED_NODES = new Set([
79 ts.SyntaxKind.ExportAssignment,
80 ts.SyntaxKind.FirstAssignment,
81 ts.SyntaxKind.LastAssignment,
82 ts.SyntaxKind.PropertyAssignment,
83 ts.SyntaxKind.ShorthandPropertyAssignment,
84 ts.SyntaxKind.VariableDeclaration,
85 ts.SyntaxKind.VariableDeclarationList,
86 ts.SyntaxKind.EnumMember,
87 ts.SyntaxKind.PropertyDeclaration,
88 ts.SyntaxKind.Parameter,
90 Rule.DEFAULT_ALLOWED = [-1, 0, 1];
92 }(Lint.Rules.AbstractRule));
94 function parseOptions(options) {
96 var parsedOptions = (_a = {}, _a[ALLOWED_NUMBERS_OPTION] = [], _a);
97 for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
98 var option = options_1[_i];
99 if (typeof option === "number") {
100 parsedOptions[ALLOWED_NUMBERS_OPTION].push(option);
103 if (option.hasOwnProperty(ALLOWED_NUMBERS_OPTION)) {
104 var numberOptions = option[ALLOWED_NUMBERS_OPTION];
105 if (Array.isArray(numberOptions) && numberOptions.length > 0) {
106 numberOptions.forEach(function (num) {
107 return parsedOptions[ALLOWED_NUMBERS_OPTION].push(num);
111 if (option.hasOwnProperty(IGNORE_JSX_OPTION)) {
112 parsedOptions[IGNORE_JSX_OPTION] = option[IGNORE_JSX_OPTION];
115 return parsedOptions;
117 var NoMagicNumbersWalker = /** @class */ (function (_super) {
118 tslib_1.__extends(NoMagicNumbersWalker, _super);
119 function NoMagicNumbersWalker() {
120 return _super !== null && _super.apply(this, arguments) || this;
122 NoMagicNumbersWalker.prototype.walk = function (sourceFile) {
124 var cb = function (node) {
125 if (tsutils_1.isCallExpression(node)) {
126 if (tsutils_1.isIdentifier(node.expression) && node.expression.text === "parseInt") {
127 return node.arguments.length === 0 ? undefined : cb(node.arguments[0]);
129 if (tsutils_1.isPropertyAccessExpression(node.expression) &&
130 NUMBER_METHODS.has(node.expression.name.text)) {
134 if (node.kind === ts.SyntaxKind.NumericLiteral) {
135 return _this.checkNumericLiteral(node, node.text);
137 if (utils_1.isNegativeNumberLiteral(node)) {
138 return _this.checkNumericLiteral(node, "-" + node.operand.text);
140 return ts.forEachChild(node, cb);
142 return ts.forEachChild(sourceFile, cb);
144 NoMagicNumbersWalker.prototype.isAllowedNumber = function (num) {
145 var numberOptions = this.options[ALLOWED_NUMBERS_OPTION];
146 if (numberOptions.length > 0) {
147 return numberOptions.some(function (allowedNum) {
148 /* Using Object.is() to differentiate between pos/neg zero */
149 return Object.is(allowedNum, parseFloat(num));
154 NoMagicNumbersWalker.prototype.checkNumericLiteral = function (node, num) {
155 var shouldIgnoreJsxExpression = node.parent.kind === ts.SyntaxKind.JsxExpression &&
156 this.options[IGNORE_JSX_OPTION];
157 if (!Rule.ALLOWED_NODES.has(node.parent.kind) &&
158 !this.isAllowedNumber(num) &&
159 !shouldIgnoreJsxExpression) {
160 this.addFailureAtNode(node, Rule.FAILURE_STRING(num));
163 return NoMagicNumbersWalker;
164 }(Lint.AbstractWalker));
165 var templateObject_1, templateObject_2, templateObject_3;