4 * Copyright 2017 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 utils_1 = require("../utils");
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 */
34 ruleName: "number-literal-format",
36 description: "Checks that decimal literals should begin with '0.' instead of just '.', and should not end with a trailing '0'.",
37 optionsDescription: "Not configurable.",
39 optionExamples: [true],
40 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 "]))),
42 typescriptOnly: false,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING_LEADING_0 = "Number literal should not have a leading '0'.";
46 Rule.FAILURE_STRING_TRAILING_0 = "Number literal should not have a trailing '0'.";
47 Rule.FAILURE_STRING_TRAILING_DECIMAL = "Number literal should not end in '.'.";
48 Rule.FAILURE_STRING_LEADING_DECIMAL = "Number literal should begin with '0.' and not just '.'.";
49 Rule.FAILURE_STRING_NOT_UPPERCASE = "Hexadecimal number literal should be uppercase.";
51 }(Lint.Rules.AbstractRule));
54 var sourceFile = ctx.sourceFile;
55 return ts.forEachChild(sourceFile, function cb(node) {
56 if (tsutils_1.isNumericLiteral(node)) {
59 return ts.forEachChild(node, cb);
61 function check(node) {
62 // Apparently the number literal '0.0' has a '.text' of '0', so use '.getText()' instead.
63 var text = node.getText(sourceFile);
64 var start = node.getStart();
65 if (text.length <= 1) {
68 if (text.startsWith("0")) {
69 // Hex/octal/binary number can't have decimal point or exponent, so no other errors possible.
73 var hexNumber = text.slice(2);
74 if (!utils_1.isUpperCase(hexNumber)) {
75 ctx.addFailureAtNode(node, Rule.FAILURE_STRING_NOT_UPPERCASE, Lint.Replacement.replaceNode(node, "0x" + hexNumber.toUpperCase()));
84 ctx.addFailureAtNode(node, Rule.FAILURE_STRING_LEADING_0, Lint.Replacement.deleteFromTo(start, start + /^0+/.exec(text)[0].length));
88 var _a = text.split(/e/i), num = _a[0], _b = _a[1], exp = _b === void 0 ? "" : _b;
89 var _c = num.split("."), integer = _c[0], _d = _c[1], float = _d === void 0 ? "" : _d;
90 var matchedNumeric = /(\.)([1-9]*)(0+)/.exec(num);
91 var _e = Array.isArray(matchedNumeric)
92 ? matchedNumeric.slice(1)
93 : [], _f = _e[0], dot = _f === void 0 ? "" : _f, _g = _e[1], numbers = _g === void 0 ? "" : _g, _h = _e[2], zeroes = _h === void 0 ? "" : _h;
94 if (exp.startsWith("-0") || exp.startsWith("0")) {
95 var expStart = start + num.length + 1; // position of exp part
96 var expNumberStart = /\D/.test(exp.charAt(0)) ? expStart + 1 : expStart; // do not remove "-" or "+"
97 ctx.addFailureAt(node.getEnd() - exp.length, exp.length, Rule.FAILURE_STRING_LEADING_0, Lint.Replacement.deleteFromTo(expNumberStart, expNumberStart + /0+/.exec(exp)[0].length));
99 if (!num.includes(".")) {
102 if (num.startsWith(".")) {
104 fail(Rule.FAILURE_STRING_LEADING_DECIMAL, Lint.Replacement.appendText(start, "0"));
106 else if (num.endsWith(".")) {
108 fail(Rule.FAILURE_STRING_TRAILING_DECIMAL, Lint.Replacement.deleteText(start + num.length - 1, 1));
110 // Allow '10', but not '1.0'
111 if (float.endsWith("0")) {
113 var offset = numbers.length > 0 ? dot.length + numbers.length : 0;
114 var length = (numbers.length > 0 ? 0 : dot.length) + zeroes.length;
115 fail(Rule.FAILURE_STRING_TRAILING_0, Lint.Replacement.deleteText(start + integer.length + offset, length));
117 function fail(message, fix) {
118 ctx.addFailureAt(node.getStart(sourceFile), num.length, message, fix);
122 var templateObject_1;