a zip code crypto-currency system good for red ONLY

noMagicNumbersRule.js 4.7KB

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