Skip to content

Commit 66e755f

Browse files
committed
[Refactor] exports-last: use array.prototype.findlastindex
1 parent a24a03b commit 66e755f

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1717
- [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo])
1818
- [Docs] [`group-exports`]: fix syntax highlighting ([#2699], thanks [@devinrhode2])
1919
- [Docs] [`extensions`]: reference node ESM behavior ([#2748], thanks [@xM8WVqaG])
20+
- [Refactor] [`exports-last`]: use `array.prototype.findlastindex` (thanks [@ljharb])
2021

2122
## [2.27.5] - 2023-01-16
2223

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
},
103103
"dependencies": {
104104
"array-includes": "^3.1.6",
105+
"array.prototype.findlastindex": "^1.2.2",
105106
"array.prototype.flat": "^1.3.1",
106107
"array.prototype.flatmap": "^1.3.1",
107108
"debug": "^3.2.7",

src/rules/exports-last.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import findLastIndex from 'array.prototype.findlastindex';
2+
13
import docsUrl from '../docsUrl';
24

35
function isNonExportStatement({ type }) {
@@ -20,15 +22,10 @@ module.exports = {
2022
create(context) {
2123
return {
2224
Program({ body }) {
23-
const lastNonExportStatementIndex = body.reduce(function findLastIndex(acc, item, index) {
24-
if (isNonExportStatement(item)) {
25-
return index;
26-
}
27-
return acc;
28-
}, -1);
25+
const lastNonExportStatementIndex = findLastIndex(body, isNonExportStatement);
2926

3027
if (lastNonExportStatementIndex !== -1) {
31-
body.slice(0, lastNonExportStatementIndex).forEach(function checkNonExport(node) {
28+
body.slice(0, lastNonExportStatementIndex).forEach((node) => {
3229
if (!isNonExportStatement(node)) {
3330
context.report({
3431
node,

0 commit comments

Comments
 (0)