-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c975742
commit 14c3150
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# group-exports | ||
|
||
Reports when multiple named exports or CommonJS assignments are present in a single file. | ||
|
||
**Rationale:** An `export` declaration or `module.exports` assignment can appear anywhere in the code. By requiring a single export declaration, your exports will remain at one place, making it easier to see what named exports a module provides. | ||
|
||
## Rule Details | ||
|
||
This rule warns whenever a single file contains multiple named exports or assignments to `module.exports` (or `exports`). | ||
|
||
### Valid | ||
|
||
```js | ||
export default function test() {} | ||
// A single named export -> ok | ||
export const valid = true | ||
``` | ||
|
||
```js | ||
const first = true | ||
const second = true | ||
|
||
// A single named export -> ok | ||
export { | ||
first, | ||
second, | ||
} | ||
``` | ||
|
||
```js | ||
// A single exports assignment -> ok | ||
module.exports = { | ||
first: true, | ||
second: true | ||
} | ||
``` | ||
|
||
### Invalid | ||
|
||
```js | ||
// Multiple named exports -> not ok! | ||
export const first = true | ||
export const second = true | ||
``` | ||
|
||
```js | ||
// Multiple exports assignments -> not ok! | ||
exports.first = true | ||
exports.second = true | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not mind having multiple named exports in a single module, you can safely turn this rule off. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const MULTI_EXPORT = 'Multiple named export declarations' | ||
const MULTI_MODULE_EXPORTS = 'Multiple CommonJS exports' | ||
|
||
const meta = {} | ||
|
||
function create(context) { | ||
const issues = new Map() | ||
|
||
return { | ||
ExportNamedDeclaration(node) { | ||
issues.set(node, MULTI_EXPORT) | ||
}, | ||
|
||
MemberExpression(node) { | ||
if (node.object.name === 'module' && node.property.name === 'exports') { | ||
issues.set(node, MULTI_MODULE_EXPORTS) | ||
} | ||
|
||
if (node.object.name === 'exports') { | ||
issues.set(node, MULTI_MODULE_EXPORTS) | ||
} | ||
}, | ||
|
||
'Program:exit': function onExit() { | ||
if (issues.size > 1) { | ||
for (const [node, message] of issues) { | ||
context.report({ node, message }) | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export default { | ||
meta, | ||
create, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { test } from '../utils' | ||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/group-exports' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('group-exports', rule, { | ||
valid: [ | ||
test({ code: 'export const test = true' }), | ||
test({ code: 'export default {}\nexport const test = true' }), | ||
test({ code: [ | ||
'const first = true', | ||
'const second = true', | ||
'export { first,\nsecond }', | ||
].join('\n') }), | ||
test({ code: 'module.exports = {} '}), | ||
test({ code: 'module.exports = { test: true,\nanother: false }' }), | ||
test({ code: 'exports.test = true' }), | ||
], | ||
invalid: [ | ||
test({ | ||
code: [ | ||
'export const test = true', | ||
'export const another = true', | ||
].join('\n'), | ||
errors: [ | ||
'Multiple named export declarations', | ||
'Multiple named export declarations', | ||
], | ||
}), | ||
test({ | ||
code: [ | ||
'module.exports = {}', | ||
'module.exports.test = true', | ||
'module.exports.another = true', | ||
].join('\n'), | ||
errors: [ | ||
'Multiple CommonJS exports', | ||
'Multiple CommonJS exports', | ||
'Multiple CommonJS exports', | ||
], | ||
}), | ||
test({ | ||
code: [ | ||
'module.exports = {}', | ||
'module.exports = {}', | ||
].join('\n'), | ||
errors: [ | ||
'Multiple CommonJS exports', | ||
'Multiple CommonJS exports', | ||
], | ||
}), | ||
], | ||
}) |