.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / incrementDecrementRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_ALLOW_POST = "allow-post";
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         var options = {
31             allowPost: this.ruleArguments.indexOf(OPTION_ALLOW_POST) !== -1,
32         };
33         return this.applyWithFunction(sourceFile, walk, options);
34     };
35     Rule.metadata = {
36         description: "Enforces using explicit += 1 or -= 1 operators.",
37         optionExamples: [true, [true, OPTION_ALLOW_POST]],
38         options: {
39             items: {
40                 enum: [OPTION_ALLOW_POST],
41                 type: "string",
42             },
43             maxLength: 1,
44             minLength: 0,
45             type: "array",
46         },
47         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            If no arguments are provided, both pre- and post-unary operators are banned.\n            If `\"", "\"` is provided, post-unary operators will be allowed.\n        "], ["\n            If no arguments are provided, both pre- and post-unary operators are banned.\n            If \\`\"", "\"\\` is provided, post-unary operators will be allowed.\n        "])), OPTION_ALLOW_POST),
48         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            It's easy to type +i or -i instead of --i or ++i, and won't always result in invalid code.\n            Prefer standardizing small arithmetic operations with the explicit += and -= operators.\n        "], ["\n            It's easy to type +i or -i instead of --i or ++i, and won't always result in invalid code.\n            Prefer standardizing small arithmetic operations with the explicit += and -= operators.\n        "]))),
49         ruleName: "increment-decrement",
50         type: "style",
51         typescriptOnly: false,
52     };
53     Rule.FAILURE_STRING_FACTORY = function (newOperatorText) {
54         return "Use an explicit " + newOperatorText + " operator.";
55     };
56     return Rule;
57 }(Lint.Rules.AbstractRule));
58 exports.Rule = Rule;
59 function walk(context) {
60     function createReplacement(node, newOperatorText) {
61         var text = node.operand.getText(context.sourceFile) + " " + newOperatorText;
62         if (node.parent !== undefined && tsutils.isBinaryExpression(node.parent)) {
63             text = "(" + text + ")";
64         }
65         return Lint.Replacement.replaceNode(node, text);
66     }
67     function complainOnNode(node) {
68         var newOperatorText = node.operator === ts.SyntaxKind.PlusPlusToken ? "+= 1" : "-= 1";
69         var replacement;
70         if (tsutils.isPrefixUnaryExpression(node) ||
71             node.parent.kind === ts.SyntaxKind.ExpressionStatement) {
72             replacement = createReplacement(node, newOperatorText);
73         }
74         var failure = Rule.FAILURE_STRING_FACTORY(newOperatorText);
75         context.addFailureAtNode(node, failure, replacement);
76     }
77     function checkPostfixUnaryExpression(node) {
78         if (!context.options.allowPost && isIncrementOrDecrementOperator(node.operator)) {
79             complainOnNode(node);
80         }
81     }
82     function checkPrefixUnaryExpression(node) {
83         if (isIncrementOrDecrementOperator(node.operator)) {
84             complainOnNode(node);
85         }
86     }
87     return ts.forEachChild(context.sourceFile, function callback(node) {
88         if (tsutils.isPostfixUnaryExpression(node)) {
89             checkPostfixUnaryExpression(node);
90         }
91         else if (tsutils.isPrefixUnaryExpression(node)) {
92             checkPrefixUnaryExpression(node);
93         }
94         return ts.forEachChild(node, callback);
95     });
96 }
97 function isIncrementOrDecrementOperator(operator) {
98     return operator === ts.SyntaxKind.PlusPlusToken || operator === ts.SyntaxKind.MinusMinusToken;
99 }
100 var templateObject_1, templateObject_2;