Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import (
"github.com/web-infra-dev/rslint/internal/rules/no_constant_condition"
"github.com/web-infra-dev/rslint/internal/rules/no_constructor_return"
"github.com/web-infra-dev/rslint/internal/rules/no_debugger"
"github.com/web-infra-dev/rslint/internal/rules/no_sparse_arrays"
)

// RslintConfig represents the top-level configuration array
Expand Down Expand Up @@ -459,6 +460,7 @@ func registerAllCoreEslintRules() {
GlobalRuleRegistry.Register("no-constant-condition", no_constant_condition.NoConstantConditionRule)
GlobalRuleRegistry.Register("no-constructor-return", no_constructor_return.NoConstructorReturnRule)
GlobalRuleRegistry.Register("no-debugger", no_debugger.NoDebuggerRule)
GlobalRuleRegistry.Register("no-sparse-arrays", no_sparse_arrays.NoSparseArraysRule)
}

// getAllTypeScriptEslintPluginRules returns all registered rules (for backward compatibility when no config is provided)
Expand Down
25 changes: 25 additions & 0 deletions internal/rules/no_sparse_arrays/no_sparse_arrays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package no_sparse_arrays

import (
"github.com/microsoft/typescript-go/shim/ast"
"github.com/web-infra-dev/rslint/internal/rule"
)

// https://eslint.org/docs/latest/rules/no-sparse-arrays
var NoSparseArraysRule = rule.Rule{
Name: "no-sparse-arrays",
Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
return rule.RuleListeners{
ast.KindArrayLiteralExpression: func(node *ast.Node) {
for _, v := range node.AsArrayLiteralExpression().Elements.Nodes {
if v.Kind == ast.KindOmittedExpression {
ctx.ReportNode(node, rule.RuleMessage{
Id: "unexpectedSparseArray",
Description: "Unexpected comma in middle of array.",
})
}
}
},
}
},
}
58 changes: 58 additions & 0 deletions internal/rules/no_sparse_arrays/no_sparse_arrays_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package no_sparse_arrays

import (
"testing"

"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/fixtures"
"github.com/web-infra-dev/rslint/internal/rule_tester"
)

func TestNoSparseArraysRule(t *testing.T) {
rule_tester.RunRuleTester(
fixtures.GetRootDir(),
"tsconfig.json",
t,
&NoSparseArraysRule,
// Valid cases - ported from ESLint
[]rule_tester.ValidTestCase{
{Code: `var a = [ 1, 2, ]`},
},
// Invalid cases - ported from ESLint
[]rule_tester.InvalidTestCase{
{
Code: `var a = [,];`,
Errors: []rule_tester.InvalidTestCaseError{
{MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
},
},
{
Code: `var a = [ 1,, 2];`,
Errors: []rule_tester.InvalidTestCaseError{
{MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
},
},
// This test case is commented out because it produces a TypeScript compilation error:
// 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 ,];]
//{
// Code: `[\r\n\t/* comment */,\n// comment\n ,];`,
// Errors: []rule_tester.InvalidTestCaseError{
// {MessageId: "unexpectedSparseArray", Line: 1, Column: 9},
// },
//},
{
Code: `[(( [a,] )),,,];`,
Errors: []rule_tester.InvalidTestCaseError{
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
},
},
{
Code: `[,(( [a,] )),,];`,
Errors: []rule_tester.InvalidTestCaseError{
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
{MessageId: "unexpectedSparseArray", Line: 1, Column: 1},
},
},
},
)
}
Loading