-
-
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 d4bed97
Showing
6 changed files
with
284 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,62 @@ | ||
# group-exports | ||
|
||
Reports when multiple named exports or CommonJS assignments are present in a single file and when the default export is not adjacent to the named export. | ||
|
||
**Rationale:** An `export` declaration or `module.exports` assignment can appear anywhere in the code. By requiring a single export declaration along with the default export being placed next to the named export, your exports will remain at one place, making it easier to see what exports a module provides. | ||
|
||
## Rule Details | ||
|
||
This rule warns whenever a single file contains multiple named exports or assignments to `module.exports` (or `exports`) and when the default export is not adjacent to the named export. | ||
|
||
### Valid | ||
|
||
```js | ||
// Default export is adjacent to named export -> ok | ||
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 | ||
// Default export is not adjacent to the named export -> not ok! | ||
export default {} | ||
const first = true | ||
export { first } | ||
``` | ||
|
||
```js | ||
// Multiple exports assignments -> not ok! | ||
exports.first = true | ||
exports.second = true | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not mind having your exports spread across the file, 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,133 @@ | ||
const meta = {} | ||
const errors = { | ||
ExportNamedDeclaration: | ||
'Multiple named export declarations; consolidate all named exports into a single statement', | ||
ExportDefaultDeclaration: | ||
'Default export declaration should be adjacent to named export', | ||
MemberExpression: | ||
'Multiple CommonJS exports; consolidate all exports into a single assignment to ' + | ||
'`module.exports`', | ||
} | ||
|
||
/** | ||
* Check if two nodes are adjacent (only whitespace between them) | ||
* | ||
* The two nodes do not have to be sorted in the order they appear in the code. | ||
* | ||
* @param {Object} opts Options for the check | ||
* @param {Object} opts.context The context of the nodes | ||
* @param {Object} opts.first The first node | ||
* @param {Object} opts.second The second node | ||
* @return {Boolean} | ||
* @private | ||
*/ | ||
function isAdjacent(opts = {}) { | ||
const sourceCode = opts.context.getSourceCode() | ||
|
||
if (sourceCode.getTokensBetween(opts.first, opts.second).length || | ||
sourceCode.getTokensBetween(opts.second, opts.first).length) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Determine how many property accesses precede this node | ||
* | ||
* For example, `module.exports` = 1, `module.exports.something` = 2 and so on. | ||
* | ||
* @param {Object} node The node being visited | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function accessorDepth(node) { | ||
let depth = 0 | ||
|
||
while (node.type === 'MemberExpression') { | ||
depth++ | ||
node = node.parent | ||
} | ||
|
||
return depth | ||
} | ||
|
||
function create(context) { | ||
const exports = { | ||
named: new Set(), | ||
default: null, | ||
last: null, | ||
} | ||
|
||
return { | ||
ExportDefaultDeclaration(node) { | ||
exports.default = node | ||
}, | ||
|
||
ExportNamedDeclaration(node) { | ||
exports.named.add(node) | ||
exports.last = node | ||
}, | ||
|
||
MemberExpression(node) { | ||
if (['MemberExpression', 'AssignmentExpression'].indexOf(node.parent.type) === -1) { | ||
return | ||
} | ||
|
||
// Member expressions on the right side of the assignment do not interest us | ||
if (node.parent.type === 'AssignmentExpression' && node.parent.left !== node) { | ||
return | ||
} | ||
|
||
if (node.object.name === 'module' && node.property.name === 'exports') { | ||
// module.exports.exported.*: assignments this deep should not be considered as exports | ||
if (accessorDepth(node) > 2) { | ||
return | ||
} | ||
|
||
return void exports.named.add(node) | ||
} | ||
|
||
if (node.object.name === 'exports') { | ||
// exports.exported.*: assignments this deep should not be considered as exports | ||
if (accessorDepth(node) > 1) { | ||
return | ||
} | ||
|
||
return void exports.named.add(node) | ||
} | ||
}, | ||
|
||
'Program:exit': function onExit() { | ||
if (exports.named.size > 1) { | ||
for (const node of exports.named) { | ||
context.report({ | ||
node, | ||
message: errors[node.type], | ||
}) | ||
} | ||
} | ||
|
||
// There is exactly one named export and a default export -> check if they are adjacent | ||
if (exports.default && exports.last && exports.named.size === 1) { | ||
const adjacent = isAdjacent({ | ||
context, | ||
first: exports.default, | ||
second: exports.last, | ||
}) | ||
|
||
if (!adjacent) { | ||
context.report({ | ||
node: exports.default, | ||
message: errors[exports.default.type], | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
|
||
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,80 @@ | ||
import { test } from '../utils' | ||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/group-exports' | ||
|
||
const errors = { | ||
named: | ||
'Multiple named export declarations; consolidate all named exports into a single statement', | ||
default: | ||
'Default export declaration should be adjacent to named export', | ||
commonjs: | ||
'Multiple CommonJS exports; consolidate all exports into a single assignment to ' + | ||
'`module.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: 'export default {}\n/* test */\nexport const test = true'}), | ||
test({ code: 'export default {}\n// test\nexport const test = true'}), | ||
test({ code: 'export const test = true\n/* test */\nexport default {}'}), | ||
test({ code: 'export const test = true\n// test\nexport default {}'}), | ||
test({ code: 'module.exports = {} '}), | ||
test({ code: 'module.exports = { test: true,\nanother: false }' }), | ||
test({ code: 'exports.test = true' }), | ||
|
||
test({ code: 'module.exports = {}\nconst test = module.exports' }), | ||
test({ code: 'exports.test = true\nconst test = exports.test' }), | ||
test({ code: 'module.exports = {}\nmodule.exports.too.deep = true' }), | ||
test({ code: 'module.exports = {}\nexports.too.deep = true' }), | ||
], | ||
invalid: [ | ||
test({ | ||
code: [ | ||
'export const test = true', | ||
'export const another = true', | ||
].join('\n'), | ||
errors: [ | ||
errors.named, | ||
errors.named, | ||
], | ||
}), | ||
test({ | ||
code: [ | ||
'module.exports = {}', | ||
'module.exports.test = true', | ||
'module.exports.another = true', | ||
].join('\n'), | ||
errors: [ | ||
errors.commonjs, | ||
errors.commonjs, | ||
errors.commonjs, | ||
], | ||
}), | ||
test({ | ||
code: [ | ||
'module.exports = {}', | ||
'module.exports = {}', | ||
].join('\n'), | ||
errors: [ | ||
errors.commonjs, | ||
errors.commonjs, | ||
], | ||
}), | ||
test({ | ||
code: 'export default {}\nconst test = true\nexport { test }', | ||
errors: [errors.default], | ||
}), | ||
test({ | ||
code: 'const test = true\nexport { test }\nconst another = true\nexport default {}', | ||
errors: [errors.default], | ||
}), | ||
], | ||
}) |