Skip to content

linter: unicorn/prefer-at with checkAllIndexes does not error on index 0 #23498

Description

@draperunner

What version of Oxlint are you using?

1.69.0

What command did you run?

oxlint .

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

// Replace this with your actual config file contents.
// It can be helpful to keep it minimal, if possible, but please ensure that it actually reproduces the issue you're reporting.
{
  "categories": {
    "correctness": "off"
  },
  "plugins": ["unicorn"],
  "rules": {
    "unicorn/prefer-at": ["error", { "checkAllIndexAccess": true }],
  }
}

What happened?

Since checkAllIndexAccess is set to true, I expected errors when accessing the first element of arrays, i.e. index zero. The following does NOT fail:

const item = array[0] // no complaints from unicorn/prefer-at

If the index is greater, it fails. Like for array[1].

The documentation for the checkAllIndexAccess option says array[0] should fail, so therefore I believe this is unexpected behavior:

Check all index access, not just special patterns like array.length - 1. When enabled, array[0], array[1], etc. will also be flagged.
https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-at.html#checkallindexaccess

Inspecting the linter source code, there seems to be some unresolved TODOs from #17632 and the test for accessing the zero index is commented out:

// ("array[0]", Some(serde_json::json!([{ "checkAllIndexAccess": true }]))),

I think the index !=0 check looks suspicious and might be the reason that this specific index is not causing errors:

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions