4 * Copyright 2013 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 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24 tslib_1.__extends(Rule, _super);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk);
32 description: "Bans usage of the delete operator with computed key expressions.",
33 optionExamples: [true],
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,
41 Rule.FAILURE_STRING = "Do not delete dynamically computed property keys.";
43 }(Lint.Rules.AbstractRule));
45 function walk(context) {
46 function checkDeleteAccessExpression(node) {
47 if (node === undefined || !tsutils.isElementAccessExpression(node)) {
50 var argumentExpression = node.argumentExpression;
51 if (argumentExpression === undefined || isNecessaryDynamicAccess(argumentExpression)) {
54 var start = argumentExpression.getStart(context.sourceFile) - 1;
55 var width = argumentExpression.getWidth() + 2;
57 if (tsutils.isPrefixUnaryExpression(argumentExpression)) {
58 var convertedOperand = convertUnaryOperand(argumentExpression);
59 if (convertedOperand !== undefined) {
60 fix = Lint.Replacement.replaceFromTo(start, start + width, "[" + convertedOperand + "]");
63 else if (tsutils.isStringLiteral(argumentExpression)) {
64 fix = Lint.Replacement.replaceFromTo(start, start + width, "." + argumentExpression.text);
66 context.addFailureAt(start, width, Rule.FAILURE_STRING, fix);
68 return ts.forEachChild(context.sourceFile, function callback(node) {
69 if (isDeleteExpression(node)) {
70 checkDeleteAccessExpression(node.expression);
72 return ts.forEachChild(node, callback);
75 function convertUnaryOperand(node) {
76 return tsutils.isNumericLiteral(node.operand) ? node.operand.text : undefined;
78 function isDeleteExpression(node) {
79 return node.kind === ts.SyntaxKind.DeleteExpression;
81 function isNumberLike(node) {
82 if (tsutils.isPrefixUnaryExpression(node)) {
83 return tsutils.isNumericLiteral(node.operand) && node.operator === ts.SyntaxKind.MinusToken;
85 return tsutils.isNumericLiteral(node);
87 function isNecessaryDynamicAccess(argumentExpression) {
88 if (isNumberLike(argumentExpression)) {
91 return (tsutils.isStringLiteral(argumentExpression) &&
92 !tsutils.isValidPropertyAccess(argumentExpression.text));