Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Implement no-assign-exports rule (fixes #6)
- Loading branch information
Casey Visco
committed
Apr 9, 2015
1 parent
8520870
commit 03c7e6d
Showing
6 changed files
with
142 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Disallow assignment to `exports` when using Simplified CommonJS Wrapper (no-assign-exports) | ||
|
||
## Rule Details | ||
|
||
This rule aims to prevent assignment to the `exports` variable. This is generally an error, as assignment should be done to `module.exports` instead. This rule is only in effect when using the Simplified CommonJS Wrapper. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
define(function (require, exports) { | ||
exports = function () { | ||
/* ... */ | ||
}; | ||
}); | ||
|
||
define(function (require, exports) { | ||
exports = { | ||
doSomething: function () { | ||
/* ... */ | ||
} | ||
}; | ||
}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
define(function (require, exports) { | ||
exports.doSomething: function () { | ||
/* ... */ | ||
}; | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
You should probably *not* disable 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,49 @@ | ||
/** | ||
* @fileoverview Disallow assignment to `exports` when using Simplified CommonJS Wrapper | ||
* @author Casey Visco | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var helpers = require("../helpers"); | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
|
||
var isCommonJS = false; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public | ||
//------------------------------------------------------------------------------ | ||
|
||
return { | ||
"CallExpression": function (node) { | ||
isCommonJS = isCommonJS || helpers.isCommonJsDef(node.arguments); | ||
}, | ||
|
||
"CallExpression:exit": function (node) { | ||
if (helpers.isCommonJsDef(node.arguments)) { | ||
isCommonJS = false; | ||
} | ||
}, | ||
|
||
"AssignmentExpression": function (node) { | ||
var isIdentifier = node.left.type === "Identifier", | ||
isExports = node.left.name === "exports"; | ||
|
||
if (isCommonJS && isIdentifier && isExports) { | ||
context.report(node, "Invalid assignment to `exports`."); | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
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,41 @@ | ||
/** | ||
* @fileoverview Tests for `no-assign-exports` rule | ||
* @author Casey Visco <cvisco@gmail.com> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslint = require("eslint"), | ||
ESLintTester = require("eslint-tester"), | ||
fixtures = require("../fixtures"); | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint.linter); | ||
|
||
eslintTester.addRuleTest("lib/rules/no-assign-exports", { | ||
|
||
valid: [ | ||
fixtures.exports.MODIFY_EXPORTS, | ||
fixtures.exports.ASSIGN_MODULE_EXPORTS, | ||
fixtures.exports.NO_CJS_ASSIGN_EXPORTS | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: fixtures.exports.ASSIGN_EXPORTS, | ||
errors: [{ | ||
message: "Invalid assignment to `exports`.", | ||
type: "AssignmentExpression" | ||
}] | ||
} | ||
] | ||
|
||
}); |