Skip to content

Commit 2e047e6

Browse files
Attila Barthaljharb
Attila Bartha
authored andcommittedSep 7, 2019
[New] group-exports: make aggregate module exports valid
1 parent 726dda5 commit 2e047e6

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed
 

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## [Unreleased]
77

8+
### Added
9+
- [`group-exports`]: make aggregate module exports valid ([#1472], thanks [@atikenny])
10+
811
### Added
912
- support `parseForESLint` from custom parser ([#1435], thanks [@JounQin])
1013

@@ -604,6 +607,7 @@ for info on changes for earlier releases.
604607

605608
[`memo-parser`]: ./memo-parser/README.md
606609

610+
[#1472]: https://github.com/benmosher/eslint-plugin-import/pull/1472
607611
[#1470]: https://github.com/benmosher/eslint-plugin-import/pull/1470
608612
[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435
609613
[#1425]: https://github.com/benmosher/eslint-plugin-import/pull/1425
@@ -981,3 +985,4 @@ for info on changes for earlier releases.
981985
[@sharmilajesupaul]: https://github.com/sharmilajesupaul
982986
[@lencioni]: https://github.com/lencioni
983987
[@JounQin]: https://github.com/JounQin
988+
[@atikenny]: https://github.com/atikenny

‎docs/rules/group-exports.md

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export {
2626
}
2727
```
2828

29+
```js
30+
// Aggregating exports -> ok
31+
export { default as module1 } from 'module-1'
32+
export { default as module2 } from 'module-2'
33+
```
34+
2935
```js
3036
// A single exports assignment -> ok
3137
module.exports = {
@@ -63,6 +69,12 @@ export const first = true
6369
export const second = true
6470
```
6571

72+
```js
73+
// Aggregating exports from the same module -> not ok!
74+
export { module1 } from 'module-1'
75+
export { module2 } from 'module-1'
76+
```
77+
6678
```js
6779
// Multiple exports assignments -> not ok!
6880
exports.first = true

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
},
8282
"dependencies": {
8383
"array-includes": "^3.0.3",
84+
"array.prototype.flat": "^1.2.1",
8485
"contains-path": "^0.1.0",
8586
"debug": "^2.6.9",
8687
"doctrine": "1.5.0",

‎src/rules/group-exports.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import docsUrl from '../docsUrl'
2+
import values from 'object.values'
3+
import flat from 'array.prototype.flat'
24

35
const meta = {
46
type: 'suggestion',
@@ -46,11 +48,18 @@ function create(context) {
4648
const nodes = {
4749
modules: new Set(),
4850
commonjs: new Set(),
51+
sources: {},
4952
}
5053

5154
return {
5255
ExportNamedDeclaration(node) {
53-
nodes.modules.add(node)
56+
if (!node.source) {
57+
nodes.modules.add(node)
58+
} else if (Array.isArray(nodes.sources[node.source.value])) {
59+
nodes.sources[node.source.value].push(node)
60+
} else {
61+
nodes.sources[node.source.value] = [node]
62+
}
5463
},
5564

5665
AssignmentExpression(node) {
@@ -86,6 +95,16 @@ function create(context) {
8695
})
8796
}
8897

98+
// Report multiple `aggregated exports` from the same module (ES2015 modules)
99+
flat(values(nodes.sources)
100+
.filter(nodesWithSource => Array.isArray(nodesWithSource) && nodesWithSource.length > 1))
101+
.forEach((node) => {
102+
context.report({
103+
node,
104+
message: errors[node.type],
105+
})
106+
})
107+
89108
// Report multiple `module.exports` assignments (CommonJS)
90109
if (nodes.commonjs.size > 1) {
91110
nodes.commonjs.forEach(node => {

‎tests/src/rules/group-exports.js

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ ruleTester.run('group-exports', rule, {
4545
// test
4646
export default {}
4747
` }),
48+
test({ code: `
49+
export { default as module1 } from './module-1'
50+
export { default as module2 } from './module-2'
51+
` }),
4852
test({ code: 'module.exports = {} '}),
4953
test({ code: `
5054
module.exports = { test: true,
@@ -111,6 +115,16 @@ ruleTester.run('group-exports', rule, {
111115
errors.named,
112116
],
113117
}),
118+
test({
119+
code: `
120+
export { method1 } from './module-1'
121+
export { method2 } from './module-1'
122+
`,
123+
errors: [
124+
errors.named,
125+
errors.named,
126+
],
127+
}),
114128
test({
115129
code: `
116130
module.exports = {}

0 commit comments

Comments
 (0)
Please sign in to comment.