Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add no-sparse-arrays rule #2407

Merged
merged 3 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/rules/noSparseArraysRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as utils from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-sparse-arrays",
description: "Forbids array literals to contain missing elements.",
rationale: "Missing elements are probably an accidentally duplicated comma.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Array has a missing element.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}

function walk(ctx: Lint.WalkContext<void>): void {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (!utils.isArrayLiteralExpression(node)) {
if (utils.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
// Ignore LHS of assignments.
traverseExpressionsInLHS(node.left, cb);
return cb(node.right);
} else {
return ts.forEachChild(node, cb);
}
}

for (const element of node.elements) {
if (utils.isOmittedExpression(element)) {
// Node has an empty range, so just use range starting at `element.pos`.
ctx.addFailureAt(element.pos, 1, Rule.FAILURE_STRING);
} else {
ts.forEachChild(element, cb);
}
}
});
}

/** Traverse the LHS of an `=` expression, calling `cb` embedded default value, but ignoring binding patterns. */
function traverseExpressionsInLHS(node: ts.Node, cb: (node: ts.Expression) => void): void {
switch (node.kind) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to add a case for ts.SyntaxKind.ParethesizedExpression

case ts.SyntaxKind.ArrayLiteralExpression:
for (const e of (node as ts.ArrayLiteralExpression).elements) {
traverseExpressionsInLHS(e, cb);
}
break;

case ts.SyntaxKind.ObjectLiteralExpression:
for (const o of (node as ts.ObjectLiteralExpression).properties) {
traverseExpressionsInLHS(o, cb);
}
break;

case ts.SyntaxKind.BinaryExpression: {
const { left, operatorToken, right } = node as ts.BinaryExpression;
if (operatorToken.kind === ts.SyntaxKind.EqualsToken) {
traverseExpressionsInLHS(left, cb);
cb(right);
}
break;
}
}
}
2 changes: 2 additions & 0 deletions test/rules/no-sparse-arrays/test.js.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[1, , 3];
~ [Array has a missing element.]
22 changes: 22 additions & 0 deletions test/rules/no-sparse-arrays/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[1, , 3];
~ [0]

// Destructuring allowed.
const [foo, , bar] = [];
const [{foo: [, bar]}] = [];
const [foo = [, 1]] = [];
~ [0]

[foo, , bar] = [];
[{foo: [, bar]}] = [];
[foo = [, 1]] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add another test case like:

[foo = bar([,])] = [];
            ~[0]

I know this works with your current implementation. Just to make sure we won't break it in the future.

~ [0]

[1,,];
~ [0]

[1,,,];
~ [0]
~ [0]

[0]: Array has a missing element.
8 changes: 8 additions & 0 deletions test/rules/no-sparse-arrays/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-sparse-arrays": true
},
"jsRules": {
"no-sparse-arrays": true
}
}