-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from lo1tuma/no-exports
New rule no-exports
- Loading branch information
Showing
7 changed files
with
447 additions
and
6 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,39 @@ | ||
# Disallow exports from test files (no-exports) | ||
|
||
Test files should have only one purpose, which is testing a specific unit. Using exports could mean the test file is also used to provide and expose utility or library functionalities, instead those should be moved to separate files. | ||
|
||
## Rule Details | ||
|
||
This rule looks for CommonJS or ESM export statements and flags them as a problem when the same file also contains a use of a mocha function. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
describe(function () { /* ... */ }); | ||
module.exports = 'foo'; | ||
|
||
it('works', function () { /* ... */ }); | ||
exports.foo = 'bar'; | ||
|
||
beforeEach(function () { /* ... */ }); | ||
export default 'foo'; | ||
|
||
afterEach(function () { /* ... */ }); | ||
export const foo = 'bar'; | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
describe(function () { /* ... */ }); | ||
|
||
it('works', function () { /* ... */ }); | ||
|
||
beforeEach(function () { /* ... */ }); | ||
|
||
afterEach(function () { /* ... */ }); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
When you use the [`exports`](https://mochajs.org/#exports) interface it is not recommended to use this rule. |
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,56 @@ | ||
'use strict'; | ||
|
||
const createAstUtils = require('../util/ast'); | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow exports from test files' | ||
}, | ||
messages: { | ||
unexpectedExport: 'Unexpected export from a test file' | ||
}, | ||
type: 'suggestion' | ||
}, | ||
create(context) { | ||
const astUtils = createAstUtils(context.settings); | ||
const exportNodes = []; | ||
let hasTestCase = false; | ||
|
||
function isCommonJsExport(node) { | ||
if (node.type === 'MemberExpression') { | ||
const name = astUtils.getNodeName(node); | ||
|
||
return name === 'module.exports' || name.startsWith('exports.'); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return { | ||
'Program:exit'() { | ||
if (hasTestCase && exportNodes.length > 0) { | ||
for (const node of exportNodes) { | ||
context.report({ node, messageId: 'unexpectedExport' }); | ||
} | ||
} | ||
}, | ||
|
||
CallExpression(node) { | ||
if (astUtils.isMochaFunctionCall(node, context.getScope())) { | ||
hasTestCase = true; | ||
} | ||
}, | ||
|
||
'ExportNamedDeclaration, ExportDefaultDeclaration, ExportAllDeclaration'(node) { | ||
exportNodes.push(node); | ||
}, | ||
|
||
AssignmentExpression(node) { | ||
if (isCommonJsExport(node.left)) { | ||
exportNodes.push(node); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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
Oops, something went wrong.