noMagicNumbersRule.js 4.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 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 ts = require("typescript");
  21. var tsutils_1 = require("tsutils");
  22. var Lint = require("../index");
  23. var utils_1 = require("../language/utils");
  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. var allowedNumbers = this.ruleArguments.length > 0 ? this.ruleArguments : Rule.DEFAULT_ALLOWED;
  31. return this.applyWithWalker(new NoMagicNumbersWalker(sourceFile, this.ruleName, new Set(allowedNumbers.map(String))));
  32. };
  33. /* tslint:disable:object-literal-sort-keys */
  34. Rule.metadata = {
  35. ruleName: "no-magic-numbers",
  36. 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."]))),
  37. 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 "]))),
  38. optionsDescription: "A list of allowed numbers.",
  39. options: {
  40. type: "array",
  41. items: {
  42. type: "number",
  43. },
  44. minLength: 1,
  45. },
  46. optionExamples: [true, [true, 1, 2, 3]],
  47. type: "typescript",
  48. typescriptOnly: false,
  49. };
  50. /* tslint:enable:object-literal-sort-keys */
  51. Rule.FAILURE_STRING = "'magic numbers' are not allowed";
  52. Rule.ALLOWED_NODES = new Set([
  53. ts.SyntaxKind.ExportAssignment,
  54. ts.SyntaxKind.FirstAssignment,
  55. ts.SyntaxKind.LastAssignment,
  56. ts.SyntaxKind.PropertyAssignment,
  57. ts.SyntaxKind.ShorthandPropertyAssignment,
  58. ts.SyntaxKind.VariableDeclaration,
  59. ts.SyntaxKind.VariableDeclarationList,
  60. ts.SyntaxKind.EnumMember,
  61. ts.SyntaxKind.PropertyDeclaration,
  62. ts.SyntaxKind.Parameter,
  63. ]);
  64. Rule.DEFAULT_ALLOWED = [-1, 0, 1];
  65. return Rule;
  66. }(Lint.Rules.AbstractRule));
  67. exports.Rule = Rule;
  68. var NoMagicNumbersWalker = /** @class */ (function (_super) {
  69. tslib_1.__extends(NoMagicNumbersWalker, _super);
  70. function NoMagicNumbersWalker() {
  71. return _super !== null && _super.apply(this, arguments) || this;
  72. }
  73. NoMagicNumbersWalker.prototype.walk = function (sourceFile) {
  74. var _this = this;
  75. var cb = function (node) {
  76. if (tsutils_1.isCallExpression(node) && tsutils_1.isIdentifier(node.expression) && node.expression.text === "parseInt") {
  77. return node.arguments.length === 0 ? undefined : cb(node.arguments[0]);
  78. }
  79. if (node.kind === ts.SyntaxKind.NumericLiteral) {
  80. return _this.checkNumericLiteral(node, node.text);
  81. }
  82. if (utils_1.isNegativeNumberLiteral(node)) {
  83. return _this.checkNumericLiteral(node, "-" + node.operand.text);
  84. }
  85. return ts.forEachChild(node, cb);
  86. };
  87. return ts.forEachChild(sourceFile, cb);
  88. };
  89. NoMagicNumbersWalker.prototype.checkNumericLiteral = function (node, num) {
  90. if (!Rule.ALLOWED_NODES.has(node.parent.kind) && !this.options.has(num)) {
  91. this.addFailureAtNode(node, Rule.FAILURE_STRING);
  92. }
  93. };
  94. return NoMagicNumbersWalker;
  95. }(Lint.AbstractWalker));
  96. var templateObject_1, templateObject_2;