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 1 commit
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
59 changes: 59 additions & 0 deletions src/rules/noArrayLiteralHoleRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @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-array-literal-hole",
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)) {
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately it's not that easy. You need to exclude array destructuring.
So you basically need test cases like:

[foo, , bar] = [];
[({foo: [, bar]})] = [];
[foo = [, 1]] = []; // should fail

Have a look at the tests for prefer-const, which does almost the same.

} else {
ts.forEachChild(element, cb);
}
}
});
}
2 changes: 2 additions & 0 deletions test/rules/no-array-literal-hole/test.js.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[1, , 3];
~ [Array has a missing element.]
5 changes: 5 additions & 0 deletions test/rules/no-array-literal-hole/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[1, , 3];
~ [Array has a missing element.]
Copy link
Contributor

Choose a reason for hiding this comment

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

add a test with trailing comma and one with multiple trailing commas


// Destructuring allowed.
const [x, , z] = arr;
Copy link
Contributor

Choose a reason for hiding this comment

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

That's only destructuring in declarations (ArrayBindingPattern). Destructuring in expression uses ArrayLiteralExpression, see my other comment above.

8 changes: 8 additions & 0 deletions test/rules/no-array-literal-hole/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-array-literal-hole": true
},
"jsRules": {
"no-array-literal-hole": true
}
}