Skip to content

Commit

Permalink
[New] group-exports: make aggregate module exports valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Bartha authored and ljharb committed Sep 7, 2019
1 parent 726dda5 commit 2e047e6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

### Added
- [`group-exports`]: make aggregate module exports valid ([#1472], thanks [@atikenny])

### Added
- support `parseForESLint` from custom parser ([#1435], thanks [@JounQin])

Expand Down Expand Up @@ -604,6 +607,7 @@ for info on changes for earlier releases.

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

[#1472]: https://github.com/benmosher/eslint-plugin-import/pull/1472
[#1470]: https://github.com/benmosher/eslint-plugin-import/pull/1470
[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435
[#1425]: https://github.com/benmosher/eslint-plugin-import/pull/1425
Expand Down Expand Up @@ -981,3 +985,4 @@ for info on changes for earlier releases.
[@sharmilajesupaul]: https://github.com/sharmilajesupaul
[@lencioni]: https://github.com/lencioni
[@JounQin]: https://github.com/JounQin
[@atikenny]: https://github.com/atikenny
12 changes: 12 additions & 0 deletions docs/rules/group-exports.md
Expand Up @@ -26,6 +26,12 @@ export {
}
```

```js
// Aggregating exports -> ok
export { default as module1 } from 'module-1'
export { default as module2 } from 'module-2'
```

```js
// A single exports assignment -> ok
module.exports = {
Expand Down Expand Up @@ -63,6 +69,12 @@ export const first = true
export const second = true
```

```js
// Aggregating exports from the same module -> not ok!
export { module1 } from 'module-1'
export { module2 } from 'module-1'
```

```js
// Multiple exports assignments -> not ok!
exports.first = true
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
},
"dependencies": {
"array-includes": "^3.0.3",
"array.prototype.flat": "^1.2.1",
"contains-path": "^0.1.0",
"debug": "^2.6.9",
"doctrine": "1.5.0",
Expand Down
21 changes: 20 additions & 1 deletion src/rules/group-exports.js
@@ -1,4 +1,6 @@
import docsUrl from '../docsUrl'
import values from 'object.values'
import flat from 'array.prototype.flat'

const meta = {
type: 'suggestion',
Expand Down Expand Up @@ -46,11 +48,18 @@ function create(context) {
const nodes = {
modules: new Set(),
commonjs: new Set(),
sources: {},
}

return {
ExportNamedDeclaration(node) {
nodes.modules.add(node)
if (!node.source) {
nodes.modules.add(node)
} else if (Array.isArray(nodes.sources[node.source.value])) {
nodes.sources[node.source.value].push(node)
} else {
nodes.sources[node.source.value] = [node]
}
},

AssignmentExpression(node) {
Expand Down Expand Up @@ -86,6 +95,16 @@ function create(context) {
})
}

// Report multiple `aggregated exports` from the same module (ES2015 modules)
flat(values(nodes.sources)
.filter(nodesWithSource => Array.isArray(nodesWithSource) && nodesWithSource.length > 1))
.forEach((node) => {
context.report({
node,
message: errors[node.type],
})
})

// Report multiple `module.exports` assignments (CommonJS)
if (nodes.commonjs.size > 1) {
nodes.commonjs.forEach(node => {
Expand Down
14 changes: 14 additions & 0 deletions tests/src/rules/group-exports.js
Expand Up @@ -45,6 +45,10 @@ ruleTester.run('group-exports', rule, {
// test
export default {}
` }),
test({ code: `
export { default as module1 } from './module-1'
export { default as module2 } from './module-2'
` }),
test({ code: 'module.exports = {} '}),
test({ code: `
module.exports = { test: true,
Expand Down Expand Up @@ -111,6 +115,16 @@ ruleTester.run('group-exports', rule, {
errors.named,
],
}),
test({
code: `
export { method1 } from './module-1'
export { method2 } from './module-1'
`,
errors: [
errors.named,
errors.named,
],
}),
test({
code: `
module.exports = {}
Expand Down

0 comments on commit 2e047e6

Please sign in to comment.