4 * Copyright 2017 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 utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var noSparseArrays_examples_1 = require("./code-examples/noSparseArrays.examples");
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk);
32 /* tslint:disable:object-literal-sort-keys */
34 ruleName: "no-sparse-arrays",
35 description: "Forbids array literals to contain missing elements.",
36 rationale: "Missing elements are probably an accidentally duplicated comma.",
37 optionsDescription: "Not configurable.",
39 optionExamples: [true],
40 type: "functionality",
41 typescriptOnly: false,
42 codeExamples: noSparseArrays_examples_1.codeExamples,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = "Array has a missing element.";
47 }(Lint.Rules.AbstractRule));
50 return ts.forEachChild(ctx.sourceFile, function cb(node) {
51 if (!utils.isArrayLiteralExpression(node)) {
52 if (utils.isBinaryExpression(node) &&
53 node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
54 // Ignore LHS of assignments.
55 traverseExpressionsInLHS(node.left, cb);
56 return cb(node.right);
59 return ts.forEachChild(node, cb);
62 for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
64 if (utils.isOmittedExpression(element)) {
65 // Node has an empty range, so just use range starting at `element.pos`.
66 ctx.addFailureAt(element.pos, 1, Rule.FAILURE_STRING);
69 ts.forEachChild(element, cb);
74 /** Traverse the LHS of an `=` expression, calling `cb` embedded default value, but ignoring binding patterns. */
75 function traverseExpressionsInLHS(node, cb) {
77 case ts.SyntaxKind.ParenthesizedExpression:
78 traverseExpressionsInLHS(node.expression, cb);
80 case ts.SyntaxKind.ArrayLiteralExpression:
81 for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
83 traverseExpressionsInLHS(e, cb);
86 case ts.SyntaxKind.ObjectLiteralExpression:
87 for (var _b = 0, _c = node.properties; _b < _c.length; _b++) {
89 traverseExpressionsInLHS(o, cb);
92 case ts.SyntaxKind.BinaryExpression: {
93 var _d = node, left = _d.left, operatorToken = _d.operatorToken, right = _d.right;
94 if (operatorToken.kind === ts.SyntaxKind.EqualsToken) {
95 traverseExpressionsInLHS(left, cb);