.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noDynamicDeleteRule.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 Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     Rule.prototype.apply = function (sourceFile) {
29         return this.applyWithFunction(sourceFile, walk);
30     };
31     Rule.metadata = {
32         description: "Bans usage of the delete operator with computed key expressions.",
33         optionExamples: [true],
34         options: null,
35         optionsDescription: "Not configurable.",
36         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Deleting dynamically computed keys is dangerous and not well optimized.\n\n            Also consider using a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)\n            or [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\n            if you're storing collections of objects.\n            Using `Object`s can cause occasional edge case bugs, such as if a key is named \"hasOwnProperty\".\n        "], ["\n            Deleting dynamically computed keys is dangerous and not well optimized.\n\n            Also consider using a [\\`Map\\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)\n            or [\\`Set\\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\n            if you're storing collections of objects.\n            Using \\`Object\\`s can cause occasional edge case bugs, such as if a key is named \"hasOwnProperty\".\n        "]))),
37         ruleName: "no-dynamic-delete",
38         type: "functionality",
39         typescriptOnly: false,
40     };
41     Rule.FAILURE_STRING = "Do not delete dynamically computed property keys.";
42     return Rule;
43 }(Lint.Rules.AbstractRule));
44 exports.Rule = Rule;
45 function walk(context) {
46     function checkDeleteAccessExpression(node) {
47         if (node === undefined || !tsutils.isElementAccessExpression(node)) {
48             return;
49         }
50         var argumentExpression = node.argumentExpression;
51         if (argumentExpression === undefined || isNecessaryDynamicAccess(argumentExpression)) {
52             return;
53         }
54         var start = argumentExpression.getStart(context.sourceFile) - 1;
55         var width = argumentExpression.getWidth() + 2;
56         var fix;
57         if (tsutils.isPrefixUnaryExpression(argumentExpression)) {
58             var convertedOperand = convertUnaryOperand(argumentExpression);
59             if (convertedOperand !== undefined) {
60                 fix = Lint.Replacement.replaceFromTo(start, start + width, "[" + convertedOperand + "]");
61             }
62         }
63         else if (tsutils.isStringLiteral(argumentExpression)) {
64             fix = Lint.Replacement.replaceFromTo(start, start + width, "." + argumentExpression.text);
65         }
66         context.addFailureAt(start, width, Rule.FAILURE_STRING, fix);
67     }
68     return ts.forEachChild(context.sourceFile, function callback(node) {
69         if (isDeleteExpression(node)) {
70             checkDeleteAccessExpression(node.expression);
71         }
72         return ts.forEachChild(node, callback);
73     });
74 }
75 function convertUnaryOperand(node) {
76     return tsutils.isNumericLiteral(node.operand) ? node.operand.text : undefined;
77 }
78 function isDeleteExpression(node) {
79     return node.kind === ts.SyntaxKind.DeleteExpression;
80 }
81 function isNumberLike(node) {
82     if (tsutils.isPrefixUnaryExpression(node)) {
83         return tsutils.isNumericLiteral(node.operand) && node.operator === ts.SyntaxKind.MinusToken;
84     }
85     return tsutils.isNumericLiteral(node);
86 }
87 function isNecessaryDynamicAccess(argumentExpression) {
88     if (isNumberLike(argumentExpression)) {
89         return true;
90     }
91     return (tsutils.isStringLiteral(argumentExpression) &&
92         !tsutils.isValidPropertyAccess(argumentExpression.text));
93 }
94 var templateObject_1;