question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Prefer `.at()` method for negative index access

See original GitHub issue

https://github.com/tc39/proposal-relative-indexing-method

We cannot do this one yet because ESLint only supports stage 4 features.

What should the rule name be? prefer-at-method-for-negative-index-access is the most correct, but also a mouthful.

Fail

array[array.length - 1];
array.slice(-1)[0];

Pass

array.at(-1);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Apr 26, 2021

I think this rule should also enforce array.at(-2) over array.at(array.length - 2)

I’m going to add this to prefer-negative-index rule.

0reactions
fiskercommented, Jun 3, 2021

Added in https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1331/commits/e58d47e90a117229fc82ff9a9a660b2eaffa6697,

When checkAllIndexAccess set to true:

  • For computed access, only if we are sure it’s a positive integer, use .at()
  • For String.charAt(), always report with suggestion to use .at()
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript: Negative Indexing With the at() Method
The at() method takes a single argument. This argument is the index of the element that you want to access. The method allows...
Read more >
Cybersecurity Risk Management for Investment Advisers ...
unauthorized access to or use of adviser or fund information.23 The proposed cybersecurity risk management rules enumerate certain general ...
Read more >
What happens if you attempt to access an element of an array ...
Thus, it is allowed to define an array index range starting at a negative number. ... Find the index position of each element...
Read more >
Proposal for an .at() method on all the built-in indexables
A TC39 proposal to add an .at() method to all the basic indexable classes (Array, ... to do "negative indexing" of JS Arrays,...
Read more >
A Guide to the Rulemaking Process - Federal Register
How does an agency involve the public in developing a proposed rule? ... provides hyperlinks for easy access to the statutes, executive orders, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found