Problem:
Complex index access patterns should be replaced with .at() method
Why is this an issue?
This rule raises an issue when code uses verbose patterns like array[array.length - 1], array.slice(-1)[0], or string.charAt(string.length - 1) to access elements by index.
JavaScript provides the .at() method as a modern, cleaner way to access array and string elements by index. This method is especially useful for negative indexing, where you want to access elements from the end.
Older patterns like array[array.length - 1] or array.slice(-1)[0] are verbose and harder to read. They also have performance implications - methods like slice() create new arrays, which uses extra memory and processing time.
The .at() method accepts both positive and negative indices. Negative indices count from the end, so array.at(-1) gets the last element, array.at(-2) gets the second-to-last, and so on. This makes the code more expressive and easier to understand.
Using .at() also provides consistency across different JavaScript objects - it works the same way on arrays, strings, typed arrays, NodeLists, and other array-like objects.
What is the potential impact?
Using outdated index access patterns makes code harder to read and maintain. Methods like slice() create unnecessary intermediate arrays, which can impact performance in loops or frequently called functions. The verbose syntax also increases the chance of off-by-one errors when calculating indices.
How to fix?
Replace array[array.length - n] with array.at(-n) for cleaner negative indexing.
Non-compliant code example
const lastElement = array[array.length - 1]; // Noncompliant
const fifthFromEnd = array[array.length - 5]; // Noncompliant
Compliant code example
const lastElement = array.at(-1);
const fifthFromEnd = array.at(-5);
Documentation
Related Rules
Problem locations:
packages/ketcher-core/src/application/editor/EditorHistory.ts
packages/ketcher-core/src/application/editor/actions/utils.ts
packages/ketcher-core/src/application/editor/editorSingleton.ts
packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
packages/ketcher-core/src/application/render/draw.ts
packages/ketcher-core/src/application/render/renderers/TransientView/SelectionView.ts
packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceViewModel/SequenceViewModel.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceViewModel/SequenceViewModelChain.ts
packages/ketcher-core/src/application/render/restruct/reatom.ts
packages/ketcher-core/src/application/render/restruct/retext.ts
packages/ketcher-core/src/application/render/view-model/ViewModel.ts
packages/ketcher-core/src/domain/entities/DrawingEntity.ts
packages/ketcher-core/src/domain/entities/LinkerSequenceNode.ts
packages/ketcher-core/src/domain/entities/monomer-chains/BaseSubChain.ts
packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts
packages/ketcher-core/src/domain/entities/snake-layout-model/SnakeLayoutModelChain.ts
packages/ketcher-macromolecules/src/components/modal/Open/Open.tsx
packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts
packages/ketcher-react/src/script/editor/tool/select/select.ts
packages/ketcher-react/src/script/ui/component/form/MeasureInput/measure-input.tsx
packages/ketcher-react/src/script/ui/component/form/colorPicker/ColorPicker.utils.test.ts
Problem:
Complex index access patterns should be replaced with
.at()methodWhy is this an issue?
This rule raises an issue when code uses verbose patterns like
array[array.length - 1],array.slice(-1)[0], orstring.charAt(string.length - 1)to access elements by index.JavaScript provides the
.at()method as a modern, cleaner way to access array and string elements by index. This method is especially useful for negative indexing, where you want to access elements from the end.Older patterns like
array[array.length - 1]orarray.slice(-1)[0]are verbose and harder to read. They also have performance implications - methods likeslice()create new arrays, which uses extra memory and processing time.The
.at()method accepts both positive and negative indices. Negative indices count from the end, so array.at(-1) gets the last element, array.at(-2) gets the second-to-last, and so on. This makes the code more expressive and easier to understand.Using
.at()also provides consistency across different JavaScript objects - it works the same way on arrays, strings, typed arrays, NodeLists, and other array-like objects.What is the potential impact?
Using outdated index access patterns makes code harder to read and maintain. Methods like
slice()create unnecessary intermediate arrays, which can impact performance in loops or frequently called functions. The verbose syntax also increases the chance of off-by-one errors when calculating indices.How to fix?
Replace
array[array.length - n]witharray.at(-n)for cleaner negative indexing.Non-compliant code example
Compliant code example
Documentation
Related Rules
Problem locations:
packages/ketcher-core/src/application/editor/EditorHistory.ts
packages/ketcher-core/src/application/editor/actions/utils.ts
packages/ketcher-core/src/application/editor/editorSingleton.ts
packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
packages/ketcher-core/src/application/render/draw.ts
packages/ketcher-core/src/application/render/renderers/TransientView/SelectionView.ts
packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceViewModel/SequenceViewModel.ts
packages/ketcher-core/src/application/render/renderers/sequence/SequenceViewModel/SequenceViewModelChain.ts
packages/ketcher-core/src/application/render/restruct/reatom.ts
packages/ketcher-core/src/application/render/restruct/retext.ts
packages/ketcher-core/src/application/render/view-model/ViewModel.ts
packages/ketcher-core/src/domain/entities/DrawingEntity.ts
packages/ketcher-core/src/domain/entities/LinkerSequenceNode.ts
packages/ketcher-core/src/domain/entities/monomer-chains/BaseSubChain.ts
packages/ketcher-core/src/domain/entities/monomer-chains/Chain.ts
packages/ketcher-core/src/domain/entities/snake-layout-model/SnakeLayoutModelChain.ts
packages/ketcher-macromolecules/src/components/modal/Open/Open.tsx
packages/ketcher-macromolecules/src/state/rna-builder/rnaBuilderSlice.ts
packages/ketcher-react/src/script/editor/tool/select/select.ts
packages/ketcher-react/src/script/ui/component/form/MeasureInput/measure-input.tsx
packages/ketcher-react/src/script/ui/component/form/colorPicker/ColorPicker.utils.test.ts