.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noMagicNumbersRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var _a, _b;
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);
30     function Rule() {
31         return _super !== null && _super.apply(this, arguments) || this;
32     }
33     Rule.FAILURE_STRING = function (num) {
34         return "'magic numbers' are not allowed: " + num;
35     };
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)));
40     };
41     /* tslint:disable:object-literal-sort-keys */
42     Rule.metadata = {
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),
47         options: {
48             type: "array",
49             items: {
50                 type: "number",
51             },
52             properties: (_a = {
53                     type: "object"
54                 },
55                 _a[ALLOWED_NUMBERS_OPTION] = {
56                     type: "array",
57                 },
58                 _a[IGNORE_JSX_OPTION] = {
59                     type: "boolean",
60                 },
61                 _a),
62             minLength: 1,
63         },
64         optionExamples: [
65             [true, 1, 2, 3],
66             [
67                 true,
68                 (_b = {},
69                     _b[ALLOWED_NUMBERS_OPTION] = [1, 2, 3],
70                     _b[IGNORE_JSX_OPTION] = true,
71                     _b),
72             ],
73         ],
74         type: "typescript",
75         typescriptOnly: false,
76     };
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,
89     ]);
90     Rule.DEFAULT_ALLOWED = [-1, 0, 1];
91     return Rule;
92 }(Lint.Rules.AbstractRule));
93 exports.Rule = Rule;
94 function parseOptions(options) {
95     var _a;
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);
101             continue;
102         }
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);
108                 });
109             }
110         }
111         if (option.hasOwnProperty(IGNORE_JSX_OPTION)) {
112             parsedOptions[IGNORE_JSX_OPTION] = option[IGNORE_JSX_OPTION];
113         }
114     }
115     return parsedOptions;
116 }
117 var NoMagicNumbersWalker = /** @class */ (function (_super) {
118     tslib_1.__extends(NoMagicNumbersWalker, _super);
119     function NoMagicNumbersWalker() {
120         return _super !== null && _super.apply(this, arguments) || this;
121     }
122     NoMagicNumbersWalker.prototype.walk = function (sourceFile) {
123         var _this = this;
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]);
128                 }
129                 if (tsutils_1.isPropertyAccessExpression(node.expression) &&
130                     NUMBER_METHODS.has(node.expression.name.text)) {
131                     return;
132                 }
133             }
134             if (node.kind === ts.SyntaxKind.NumericLiteral) {
135                 return _this.checkNumericLiteral(node, node.text);
136             }
137             if (utils_1.isNegativeNumberLiteral(node)) {
138                 return _this.checkNumericLiteral(node, "-" + node.operand.text);
139             }
140             return ts.forEachChild(node, cb);
141         };
142         return ts.forEachChild(sourceFile, cb);
143     };
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));
150             });
151         }
152         return false;
153     };
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));
161         }
162     };
163     return NoMagicNumbersWalker;
164 }(Lint.AbstractWalker));
165 var templateObject_1, templateObject_2, templateObject_3;