numberLiteralFormatRule.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. type: "style",
  40. typescriptOnly: false,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING_LEADING_0 = "Number literal should not have a leading '0'.";
  44. Rule.FAILURE_STRING_TRAILING_0 = "Number literal should not have a trailing '0'.";
  45. Rule.FAILURE_STRING_TRAILING_DECIMAL = "Number literal should not end in '.'.";
  46. Rule.FAILURE_STRING_LEADING_DECIMAL = "Number literal should begin with '0.' and not just '.'.";
  47. Rule.FAILURE_STRING_NOT_UPPERCASE = "Hexadecimal number literal should be uppercase.";
  48. return Rule;
  49. }(Lint.Rules.AbstractRule));
  50. exports.Rule = Rule;
  51. function walk(ctx) {
  52. var sourceFile = ctx.sourceFile;
  53. return ts.forEachChild(sourceFile, function cb(node) {
  54. if (tsutils_1.isNumericLiteral(node)) {
  55. return check(node);
  56. }
  57. return ts.forEachChild(node, cb);
  58. });
  59. function check(node) {
  60. // Apparently the number literal '0.0' has a '.text' of '0', so use '.getText()' instead.
  61. var text = node.getText(sourceFile);
  62. if (text.length <= 1) {
  63. return;
  64. }
  65. if (text.startsWith("0")) {
  66. // Hex/octal/binary number can't have decimal point or exponent, so no other errors possible.
  67. switch (text[1]) {
  68. case "x":
  69. if (!utils_1.isUpperCase(text.slice(2))) {
  70. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_NOT_UPPERCASE);
  71. }
  72. return;
  73. case "o":
  74. case "b":
  75. return;
  76. case ".":
  77. break;
  78. default:
  79. ctx.addFailureAtNode(node, Rule.FAILURE_STRING_LEADING_0);
  80. return;
  81. }
  82. }
  83. var _a = text.split(/e/i), num = _a[0], exp = _a[1];
  84. if (exp !== undefined && (exp.startsWith("-0") || exp.startsWith("0"))) {
  85. ctx.addFailureAt(node.getEnd() - exp.length, exp.length, Rule.FAILURE_STRING_LEADING_0);
  86. }
  87. if (!num.includes(".")) {
  88. return;
  89. }
  90. if (num.startsWith(".")) {
  91. fail(Rule.FAILURE_STRING_LEADING_DECIMAL);
  92. }
  93. if (num.endsWith(".")) {
  94. fail(Rule.FAILURE_STRING_TRAILING_DECIMAL);
  95. }
  96. // Allow '10', but not '1.0'
  97. if (num.endsWith("0")) {
  98. fail(Rule.FAILURE_STRING_TRAILING_0);
  99. }
  100. function fail(message) {
  101. ctx.addFailureAt(node.getStart(sourceFile), num.length, message);
  102. }
  103. }
  104. }