Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
abbfb27
commit a0f0ee1
Showing
6 changed files
with
156 additions
and
2 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,45 @@ | ||
# node/no-exports-assign | ||
> disallow the assignment to `exports` | ||
To assign to `exports` variable would not work as expected. | ||
|
||
```js | ||
// This assigned object is not exported. | ||
// You need to use `module.exports = { ... }`. | ||
exports = { | ||
foo: 1 | ||
} | ||
``` | ||
|
||
## 📖 Rule Details | ||
|
||
This rule is aimed at disallowing `exports = {}`, but allows `module.exports = exports = {}` to avoid conflict with [node/exports-style](./exports-style.md) rule's `allowBatchAssign` option. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
👍 Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint node/no-exports-assign: error */ | ||
module.exports.foo = 1 | ||
exports.bar = 2 | ||
module.exports = {} | ||
// allows `exports = {}` if along with `module.exports =` | ||
module.exports = exports = {} | ||
exports = module.exports = {} | ||
``` | ||
|
||
👎 Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint node/no-exports-assign: error */ | ||
exports = {} | ||
``` | ||
|
||
|
||
## 🔎 Implementation | ||
|
||
- [Rule source](../../lib/rules/no-exports-assign.js) | ||
- [Test source](../../tests/lib/rules/no-exports-assign.js) |
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,75 @@ | ||
/** | ||
* @author Toru Nagashima <https://github.com/mysticatea> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const { findVariable } = require("eslint-utils") | ||
|
||
function isExports(node, scope) { | ||
let variable = null | ||
|
||
return ( | ||
node != null && | ||
node.type === "Identifier" && | ||
node.name === "exports" && | ||
(variable = findVariable(scope, node)) != null && | ||
variable.scope.type === "global" | ||
) | ||
} | ||
|
||
function isModuleExports(node, scope) { | ||
let variable = null | ||
|
||
return ( | ||
node != null && | ||
node.type === "MemberExpression" && | ||
!node.computed && | ||
node.object.type === "Identifier" && | ||
node.object.name === "module" && | ||
node.property.type === "Identifier" && | ||
node.property.name === "exports" && | ||
(variable = findVariable(scope, node.object)) != null && | ||
variable.scope.type === "global" | ||
) | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow the assignment to `exports`", | ||
category: "Possible Errors", | ||
recommended: false, | ||
url: | ||
"https://github.com/mysticatea/eslint-plugin-node/blob/v9.2.0/docs/rules/no-exports-assign.md", | ||
}, | ||
fixable: null, | ||
messages: { | ||
forbidden: | ||
"Unexpected assignment to 'exports' variable. Use 'module.exports' instead.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
create(context) { | ||
return { | ||
AssignmentExpression(node) { | ||
const scope = context.getScope() | ||
if ( | ||
!isExports(node.left, scope) || | ||
// module.exports = exports = {} | ||
(node.parent.type === "AssignmentExpression" && | ||
node.parent.right === node && | ||
isModuleExports(node.parent.left, scope)) || | ||
// exports = module.exports = {} | ||
(node.right.type === "AssignmentExpression" && | ||
isModuleExports(node.right.left, scope)) | ||
) { | ||
return | ||
} | ||
|
||
context.report({ node, messageId: "forbidden" }) | ||
}, | ||
} | ||
}, | ||
} |
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,31 @@ | ||
/** | ||
* @author Toru Nagashima <https://github.com/mysticatea> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const { RuleTester } = require("eslint") | ||
const rule = require("../../../lib/rules/no-exports-assign.js") | ||
|
||
new RuleTester({ | ||
globals: { | ||
exports: "writable", | ||
module: "readonly", | ||
}, | ||
}).run("no-exports-assign", rule, { | ||
valid: [ | ||
"module.exports.foo = 1", | ||
"exports.bar = 1", | ||
"module.exports = exports = {}", | ||
"exports = module.exports = {}", | ||
"function f(exports) { exports = {} }", | ||
], | ||
invalid: [ | ||
{ | ||
code: "exports = {}", | ||
errors: [ | ||
"Unexpected assignment to 'exports' variable. Use 'module.exports' instead.", | ||
], | ||
}, | ||
], | ||
}) |
Please tell me why I should callback
undefined
instead of a literal?