.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / radixRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var radix_examples_1 = require("./code-examples/radix.examples");
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithFunction(sourceFile, walk);
31     };
32     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
34         ruleName: "radix",
35         description: "Requires the radix parameter to be specified when calling `parseInt`.",
36         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt):\n            > Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior.\n            > Different implementations produce different results when a radix is not specified, usually defaulting the value to 10."], ["\n            From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt):\n            > Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior.\n            > Different implementations produce different results when a radix is not specified, usually defaulting the value to 10."]))),
37         optionsDescription: "Not configurable.",
38         options: null,
39         optionExamples: [true],
40         type: "functionality",
41         typescriptOnly: false,
42         codeExamples: radix_examples_1.codeExamples,
43     };
44     /* tslint:enable:object-literal-sort-keys */
45     Rule.FAILURE_STRING = "Missing radix parameter";
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 function isParseInt(expression) {
50     return tsutils_1.isIdentifier(expression) && expression.text === "parseInt";
51 }
52 function isPropertyAccessParseInt(expression) {
53     return tsutils_1.isPropertyAccessExpression(expression) && expression.name.text === "parseInt";
54 }
55 function isPropertyAccessOfIdentifier(expression, identifiers) {
56     return (tsutils_1.isPropertyAccessExpression(expression) &&
57         tsutils_1.isIdentifier(expression.expression) &&
58         identifiers.some(function (identifier) { return expression.expression.text === identifier; }));
59 }
60 function isPropertyAccessOfProperty(expression, identifiers) {
61     return (tsutils_1.isPropertyAccessExpression(expression) &&
62         tsutils_1.isPropertyAccessExpression(expression.expression) &&
63         identifiers.some(function (identifier) {
64             return expression.expression.name.text === identifier;
65         }));
66 }
67 function walk(ctx) {
68     return ts.forEachChild(ctx.sourceFile, function cb(node) {
69         if (tsutils_1.isCallExpression(node) &&
70             node.arguments.length === 1 &&
71             // parseInt("123")
72             (isParseInt(node.expression) ||
73                 // window.parseInt("123") || global.parseInt("123") || Number.parseInt("123")
74                 (isPropertyAccessParseInt(node.expression) &&
75                     isPropertyAccessOfIdentifier(node.expression, [
76                         "global",
77                         "window",
78                         "Number",
79                     ])) ||
80                 // window.Number.parseInt("123") || global.Number.parseInt("123")
81                 (isPropertyAccessParseInt(node.expression) &&
82                     isPropertyAccessOfProperty(node.expression, ["Number"]) &&
83                     isPropertyAccessOfIdentifier(node.expression.expression, ["global", "window"])))) {
84             ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
85         }
86         return ts.forEachChild(node, cb);
87     });
88 }
89 var templateObject_1;