Skip to content

Commit c182e75

Browse files
authored
feat: implement no-sparse-arrays (#411)
1 parent e0ac4f3 commit c182e75

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import (
9696
"github.com/web-infra-dev/rslint/internal/rules/no_constant_condition"
9797
"github.com/web-infra-dev/rslint/internal/rules/no_constructor_return"
9898
"github.com/web-infra-dev/rslint/internal/rules/no_debugger"
99+
"github.com/web-infra-dev/rslint/internal/rules/no_sparse_arrays"
99100
)
100101

101102
// RslintConfig represents the top-level configuration array
@@ -461,6 +462,7 @@ func registerAllCoreEslintRules() {
461462
GlobalRuleRegistry.Register("no-constant-condition", no_constant_condition.NoConstantConditionRule)
462463
GlobalRuleRegistry.Register("no-constructor-return", no_constructor_return.NoConstructorReturnRule)
463464
GlobalRuleRegistry.Register("no-debugger", no_debugger.NoDebuggerRule)
465+
GlobalRuleRegistry.Register("no-sparse-arrays", no_sparse_arrays.NoSparseArraysRule)
464466
}
465467

466468
// getAllTypeScriptEslintPluginRules returns all registered rules (for backward compatibility when no config is provided)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package no_sparse_arrays
2+
3+
import (
4+
"github.com/microsoft/typescript-go/shim/ast"
5+
"github.com/web-infra-dev/rslint/internal/rule"
6+
)
7+
8+
// https://eslint.org/docs/latest/rules/no-sparse-arrays
9+
var NoSparseArraysRule = rule.Rule{
10+
Name: "no-sparse-arrays",
11+
Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
12+
return rule.RuleListeners{
13+
ast.KindArrayLiteralExpression: func(node *ast.Node) {
14+
for _, v := range node.AsArrayLiteralExpression().Elements.Nodes {
15+
if v.Kind == ast.KindOmittedExpression {
16+
ctx.ReportNode(node, rule.RuleMessage{
17+
Id: "unexpectedSparseArray",
18+
Description: "Unexpected comma in middle of array.",
19+
})
20+
}
21+
}
22+
},
23+
}
24+
},
25+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package no_sparse_arrays
2+
3+
import (
4+
"testing"
5+
6+
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/fixtures"
7+
"github.com/web-infra-dev/rslint/internal/rule_tester"
8+
)
9+
10+
func TestNoSparseArraysRule(t *testing.T) {
11+
rule_tester.RunRuleTester(
12+
fixtures.GetRootDir(),
13+
"tsconfig.json",
14+
t,
15+
&NoSparseArraysRule,
16+
// Valid cases - ported from ESLint
17+
[]rule_tester.ValidTestCase{
18+
{Code: `var a = [ 1, 2, ]`},
19+
},
20+
// Invalid cases - ported from ESLint
21+
[]rule_tester.InvalidTestCase{
22+
{
23+
Code: `var a = [,];`,
24+
Errors: []rule_tester.InvalidTestCaseError{
25+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
26+
},
27+
},
28+
{
29+
Code: `var a = [ 1,, 2];`,
30+
Errors: []rule_tester.InvalidTestCaseError{
31+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
32+
},
33+
},
34+
// This test case is commented out because it produces a TypeScript compilation error:
35+
// error creating TS program for /tsconfig.json: found 5 syntactic errors. [Invalid character. [\r\n\t/* comment */,\n// comment\n ,]; Invalid character. [\r\n\t/* comment */,\n// comment\n ,]; Invalid character. [\r\n\t/* comment */,\n// comment\n ,]; Invalid character. [\r\n\t/* comment */,\n// comment\n ,]; ']' expected. [\r\n\t/* comment */,\n// comment\n ,];]
36+
//{
37+
// Code: `[\r\n\t/* comment */,\n// comment\n ,];`,
38+
// Errors: []rule_tester.InvalidTestCaseError{
39+
// {MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
40+
// },
41+
//},
42+
{
43+
Code: `[(( [a,] )),,,];`,
44+
Errors: []rule_tester.InvalidTestCaseError{
45+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
46+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
47+
},
48+
},
49+
{
50+
Code: `[,(( [a,] )),,];`,
51+
Errors: []rule_tester.InvalidTestCaseError{
52+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
53+
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
54+
},
55+
},
56+
},
57+
)
58+
}

0 commit comments

Comments
 (0)