noDynamicDeleteRule.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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)
  77. ? node.operand.text
  78. : undefined;
  79. }
  80. function isDeleteExpression(node) {
  81. return node.kind === ts.SyntaxKind.DeleteExpression;
  82. }
  83. function isNumberLike(node) {
  84. if (tsutils.isPrefixUnaryExpression(node)) {
  85. return tsutils.isNumericLiteral(node.operand) && node.operator === ts.SyntaxKind.MinusToken;
  86. }
  87. return tsutils.isNumericLiteral(node);
  88. }
  89. function isNecessaryDynamicAccess(argumentExpression) {
  90. if (isNumberLike(argumentExpression)) {
  91. return true;
  92. }
  93. return tsutils.isStringLiteral(argumentExpression) && !tsutils.isValidPropertyAccess(argumentExpression.text);
  94. }
  95. var templateObject_1;