4 * Copyright 2013 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");
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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk);
32 /* tslint:disable:object-literal-sort-keys */
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.",
39 optionExamples: [true],
40 type: "functionality",
41 typescriptOnly: false,
42 codeExamples: radix_examples_1.codeExamples,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = "Missing radix parameter";
47 }(Lint.Rules.AbstractRule));
49 function isParseInt(expression) {
50 return tsutils_1.isIdentifier(expression) && expression.text === "parseInt";
52 function isPropertyAccessParseInt(expression) {
53 return tsutils_1.isPropertyAccessExpression(expression) && expression.name.text === "parseInt";
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; }));
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;
68 return ts.forEachChild(ctx.sourceFile, function cb(node) {
69 if (tsutils_1.isCallExpression(node) &&
70 node.arguments.length === 1 &&
72 (isParseInt(node.expression) ||
73 // window.parseInt("123") || global.parseInt("123") || Number.parseInt("123")
74 (isPropertyAccessParseInt(node.expression) &&
75 isPropertyAccessOfIdentifier(node.expression, [
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);
86 return ts.forEachChild(node, cb);