-
Notifications
You must be signed in to change notification settings - Fork 887
Conversation
why not name this the same as eslint and tslint-microsoft-contrib? |
src/rules/noArrayLiteralHoleRule.ts
Outdated
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); |
There was a problem hiding this comment.
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.
@@ -0,0 +1,5 @@ | |||
[1, , 3]; | |||
~ [Array has a missing element.] |
There was a problem hiding this comment.
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
~ [Array has a missing element.] | ||
|
||
// Destructuring allowed. | ||
const [x, , z] = arr; |
There was a problem hiding this comment.
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.
|
||
/** 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) { |
There was a problem hiding this comment.
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
|
||
[foo, , bar] = []; | ||
[{foo: [, bar]}] = []; | ||
[foo = [, 1]] = []; |
There was a problem hiding this comment.
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.
PR checklist
Overview of change:
Added the
no-sparse-arrays
rule, which warns on[1, , 3]
.CHANGELOG.md entry:
[new-rule]
no-sparse-arrays