.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noSparseArraysRule.js
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 noSparseArrays_examples_1 = require("./code-examples/noSparseArrays.examples");
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithFunction(sourceFile, walk);
31     };
32     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
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.",
38         options: null,
39         optionExamples: [true],
40         type: "functionality",
41         typescriptOnly: false,
42         codeExamples: noSparseArrays_examples_1.codeExamples,
43     };
44     /* tslint:enable:object-literal-sort-keys */
45     Rule.FAILURE_STRING = "Array has a missing element.";
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 function walk(ctx) {
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);
57             }
58             else {
59                 return ts.forEachChild(node, cb);
60             }
61         }
62         for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
63             var element = _a[_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);
67             }
68             else {
69                 ts.forEachChild(element, cb);
70             }
71         }
72     });
73 }
74 /** Traverse the LHS of an `=` expression, calling `cb` embedded default value, but ignoring binding patterns. */
75 function traverseExpressionsInLHS(node, cb) {
76     switch (node.kind) {
77         case ts.SyntaxKind.ParenthesizedExpression:
78             traverseExpressionsInLHS(node.expression, cb);
79             break;
80         case ts.SyntaxKind.ArrayLiteralExpression:
81             for (var _i = 0, _a = node.elements; _i < _a.length; _i++) {
82                 var e = _a[_i];
83                 traverseExpressionsInLHS(e, cb);
84             }
85             break;
86         case ts.SyntaxKind.ObjectLiteralExpression:
87             for (var _b = 0, _c = node.properties; _b < _c.length; _b++) {
88                 var o = _c[_b];
89                 traverseExpressionsInLHS(o, cb);
90             }
91             break;
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);
96                 cb(right);
97             }
98         }
99     }
100 }