noSparseArraysRule.js 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 utils = 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. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-sparse-arrays",
  34. description: "Forbids array literals to contain missing elements.",
  35. rationale: "Missing elements are probably an accidentally duplicated comma.",
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. type: "functionality",
  40. typescriptOnly: false,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING = "Array has a missing element.";
  44. return Rule;
  45. }(Lint.Rules.AbstractRule));
  46. exports.Rule = Rule;
  47. function walk(ctx) {
  48. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  49. if (!utils.isArrayLiteralExpression(node)) {
  50. if (utils.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
  51. // Ignore LHS of assignments.
  52. traverseExpressionsInLHS(node.left, cb);
  53. return cb(node.right);
  54. }
  55. else {
  56. return ts.forEachChild(node, cb);
  57. }
  58. }
  59. for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
  60. var element = _a[_i];
  61. if (utils.isOmittedExpression(element)) {
  62. // Node has an empty range, so just use range starting at `element.pos`.
  63. ctx.addFailureAt(element.pos, 1, Rule.FAILURE_STRING);
  64. }
  65. else {
  66. ts.forEachChild(element, cb);
  67. }
  68. }
  69. });
  70. }
  71. /** Traverse the LHS of an `=` expression, calling `cb` embedded default value, but ignoring binding patterns. */
  72. function traverseExpressionsInLHS(node, cb) {
  73. switch (node.kind) {
  74. case ts.SyntaxKind.ParenthesizedExpression:
  75. traverseExpressionsInLHS(node.expression, cb);
  76. break;
  77. case ts.SyntaxKind.ArrayLiteralExpression:
  78. for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
  79. var e = _a[_i];
  80. traverseExpressionsInLHS(e, cb);
  81. }
  82. break;
  83. case ts.SyntaxKind.ObjectLiteralExpression:
  84. for (var _b = 0, _c = node.properties; _b < _c.length; _b++) {
  85. var o = _c[_b];
  86. traverseExpressionsInLHS(o, cb);
  87. }
  88. break;
  89. case ts.SyntaxKind.BinaryExpression: {
  90. var _d = node, left = _d.left, operatorToken = _d.operatorToken, right = _d.right;
  91. if (operatorToken.kind === ts.SyntaxKind.EqualsToken) {
  92. traverseExpressionsInLHS(left, cb);
  93. cb(right);
  94. }
  95. }
  96. }
  97. }