.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / numberLiteralFormatRule.js
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         hasFix: true,
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.",
38         options: null,
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        "]))),
41         type: "formatting",
42         typescriptOnly: false,
43     };
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.";
50     return Rule;
51 }(Lint.Rules.AbstractRule));
52 exports.Rule = Rule;
53 function walk(ctx) {
54     var sourceFile = ctx.sourceFile;
55     return ts.forEachChild(sourceFile, function cb(node) {
56         if (tsutils_1.isNumericLiteral(node)) {
57             return check(node);
58         }
59         return ts.forEachChild(node, cb);
60     });
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) {
66             return;
67         }
68         if (text.startsWith("0")) {
69             // Hex/octal/binary number can't have decimal point or exponent, so no other errors possible.
70             switch (text[1]) {
71                 case "x":
72                     // strip "0x"
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()));
76                     }
77                     return;
78                 case "o":
79                 case "b":
80                     return;
81                 case ".":
82                     break;
83                 default:
84                     ctx.addFailureAtNode(node, Rule.FAILURE_STRING_LEADING_0, Lint.Replacement.deleteFromTo(start, start + /^0+/.exec(text)[0].length));
85                     return;
86             }
87         }
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));
98         }
99         if (!num.includes(".")) {
100             return;
101         }
102         if (num.startsWith(".")) {
103             // .1 -> 0.1
104             fail(Rule.FAILURE_STRING_LEADING_DECIMAL, Lint.Replacement.appendText(start, "0"));
105         }
106         else if (num.endsWith(".")) {
107             // 1. -> 1
108             fail(Rule.FAILURE_STRING_TRAILING_DECIMAL, Lint.Replacement.deleteText(start + num.length - 1, 1));
109         }
110         // Allow '10', but not '1.0'
111         if (float.endsWith("0")) {
112             // 1.0 -> 1
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));
116         }
117         function fail(message, fix) {
118             ctx.addFailureAt(node.getStart(sourceFile), num.length, message, fix);
119         }
120     }
121 }
122 var templateObject_1;