Skip to content

Commit 0f1c691

Browse files
committed
Refactor exports-last
1 parent d9028c5 commit 0f1c691

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

Diff for: CHANGELOG.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77
### Added
88
- [`no-anonymous-default-export`] rule: report anonymous default exports ([#712], thanks [@duncanbeevers]).
99
- Add new value to `order`'s `newlines-between` option to allow newlines inside import groups ([#627], [#628], thanks [@giodamelio])
10+
- [`exports-last`] lints that export statements are at the end of the file ([#620] + [#632])
1011

1112
### Changed
1213
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])
@@ -35,9 +36,6 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
3536
- Properly report [`newline-after-import`] when next line is a decorator
3637
- Fixed documentation for the default values for the [`order`] rule ([#601])
3738

38-
### Added
39-
- [`exports-last`] lints that export statements are at the end of the file ([#620] + [#632])
40-
4139
## [2.0.1] - 2016-10-06
4240
### Fixed
4341
- Fixed code that relied on removed dependencies. ([#604])

Diff for: docs/rules/exports-last.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# exports-last
22

3-
This rule reports all export declaration which come before any non-export statements.
3+
This rule enforces that all exports are declared at the bottom of the file. This rule will report any export declarations that comes before any non-export statements.
44

55

66
## This will be reported
@@ -26,6 +26,8 @@ const str = 'foo'
2626
## This will not be reported
2727

2828
```JS
29+
const arr = ['bar']
30+
2931
export const bool = true
3032

3133
export default bool

Diff for: src/rules/exports-last.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
const isExportStatement = ({ type }) =>
2-
type === 'ExportDefaultDeclaration'
3-
|| type === 'ExportNamedDeclaration'
4-
|| type === 'ExportAllDeclaration'
1+
function isNonExportStatement({ type }) {
2+
return type !== 'ExportDefaultDeclaration' &&
3+
type !== 'ExportNamedDeclaration' &&
4+
type !== 'ExportAllDeclaration'
5+
}
56

6-
const rule = {
7-
create(context) {
7+
module.exports = {
8+
create: function (context) {
89
return {
9-
Program({ body }) {
10-
const firstExportStatementIndex = body.findIndex(isExportStatement)
10+
Program: function ({ body }) {
11+
const lastNonExportStatementIndex = body.reduce(function findLastIndex(acc, item, index) {
12+
if (isNonExportStatement(item)) {
13+
return index
14+
}
15+
return acc
16+
}, -1)
1117

12-
if (firstExportStatementIndex !== -1) {
13-
body.slice(firstExportStatementIndex).forEach((node) => {
14-
if (!isExportStatement(node)) {
18+
if (lastNonExportStatementIndex !== -1) {
19+
body.slice(0, lastNonExportStatementIndex).forEach(function checkNonExport(node) {
20+
if (!isNonExportStatement(node)) {
1521
context.report({
1622
node,
1723
message: 'Export statements should appear at the end of the file',
@@ -23,5 +29,3 @@ const rule = {
2329
}
2430
},
2531
}
26-
27-
export default rule

Diff for: tests/src/rules/exports-last.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import rule from 'rules/exports-last'
55

66
const ruleTester = new RuleTester()
77

8-
const errors = ['Export statements should appear at the end of the file']
8+
const error = type => ({
9+
ruleId: 'exports-last',
10+
message: 'Export statements should appear at the end of the file',
11+
type
12+
});
913

1014
ruleTester.run('exports-last', rule, {
1115
valid: [
1216
// Empty file
1317
test({
14-
code: '',
18+
code: '// comment',
1519
}),
1620
test({
1721
// No exports
@@ -82,23 +86,23 @@ ruleTester.run('exports-last', rule, {
8286
export default 'bar'
8387
const bar = true
8488
`,
85-
errors,
89+
errors: [error('ExportDefaultDeclaration')],
8690
}),
8791
// Named export before variable declaration
8892
test({
8993
code: `
9094
export const foo = 'bar'
9195
const bar = true
9296
`,
93-
errors,
97+
errors: [error('ExportNamedDeclaration')],
9498
}),
9599
// Export all before variable declaration
96100
test({
97101
code: `
98102
export * from './foo'
99103
const bar = true
100104
`,
101-
errors,
105+
errors: [error('ExportAllDeclaration')],
102106
}),
103107
// Many exports arround variable declaration
104108
test({
@@ -111,7 +115,10 @@ ruleTester.run('exports-last', rule, {
111115
export const even = 'count'
112116
export const how = 'many'
113117
`,
114-
errors,
118+
errors: [
119+
error('ExportDefaultDeclaration'),
120+
error('ExportNamedDeclaration'),
121+
],
115122
}),
116123
],
117124
})

0 commit comments

Comments
 (0)