numberLiteralFormatRule.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 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 utils_1 = require("../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.applyWithFunction(sourceFile, walk);
  31. };
  32. /* tslint:disable:object-literal-sort-keys */
  33. Rule.metadata = {
  34. ruleName: "number-literal-format",
  35. description: "Checks that decimal literals should begin with '0.' instead of just '.', and should not end with a trailing '0'.",
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Helps keep a consistent style with numeric literals.\n Non-standard literals are more difficult to scan through and can be a symptom of typos.\n "], ["\n Helps keep a consistent style with numeric literals.\n Non-standard literals are more difficult to scan through and can be a symptom of typos.\n "]))),
  40. type: "style",
  41. typescriptOnly: false,
  42. };
  43. /* tslint:enable:object-literal-sort-keys */
  44. Rule.FAILURE_STRING_LEADING_0 = "Number literal should not have a leading '0'.";
  45. Rule.FAILURE_STRING_TRAILING_0 = "Number literal should not have a trailing '0'.";
  46. Rule.FAILURE_STRING_TRAILING_DECIMAL = "Number literal should not end in '.'.";
  47. Rule.FAILURE_STRING_LEADING_DECIMAL = "Number literal should begin with '0.' and not just '.'.";
  48. Rule.FAILURE_STRING_NOT_UPPERCASE = "Hexadecimal number literal should be uppercase.";
  49. return Rule;
  50. }(Lint.Rules.AbstractRule));
  51. exports.Rule = Rule;
  52. function walk(ctx) {
  53. var sourceFile = ctx.sourceFile;
  54. return ts.forEachChild(sourceFile, function cb(node) {
  55. if (tsutils_1.isNumericLiteral(node)) {
  56. return check(node);
  57. }
  58. return ts.forEachChild(node, cb);
  59. });
  60. function check(node) {
  61. // Apparently the number literal '0.0' has a '.text' of '0', so use '.getText()' instead.
  62. var text = node.getText(sourceFile);
  63. if (text.length <= 1) {
  64. return;
  65. }
  66. if (text.startsWith("0")) {
  67. // Hex/octal/binary number can't have decimal point or exponent, so no other errors possible.
  68. switch (text[1]) {
  69. case "x":
  70. if (!utils_1.isUpperCase(text.slice(2))) {
  71. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_NOT_UPPERCASE);
  72. }
  73. return;
  74. case "o":
  75. case "b":
  76. return;
  77. case ".":
  78. break;
  79. default:
  80. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_LEADING_0);
  81. return;
  82. }
  83. }
  84. var _a = text.split(/e/i), num = _a[0], exp = _a[1];
  85. if (exp !== undefined && (exp.startsWith("-0") || exp.startsWith("0"))) {
  86. ctx.addFailureAt(node.getEnd() - exp.length, exp.length, Rule.FAILURE_STRING_LEADING_0);
  87. }
  88. if (!num.includes(".")) {
  89. return;
  90. }
  91. if (num.startsWith(".")) {
  92. fail(Rule.FAILURE_STRING_LEADING_DECIMAL);
  93. }
  94. if (num.endsWith(".")) {
  95. fail(Rule.FAILURE_STRING_TRAILING_DECIMAL);
  96. }
  97. // Allow '10', but not '1.0'
  98. if (num.endsWith("0")) {
  99. fail(Rule.FAILURE_STRING_TRAILING_0);
  100. }
  101. function fail(message) {
  102. ctx.addFailureAt(node.getStart(sourceFile), num.length, message);
  103. }
  104. }
  105. }
  106. var templateObject_1;