Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-mutable-exports #290

Closed
wants to merge 11 commits into from
23 changes: 23 additions & 0 deletions docs/rules/no-mutable-exports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# no-mutable-exports

Forbids the use of mutable exports with `var` or `let`.

## Rule Details

Valid:

```js
export const count = 1
export function getCount() {}
```

...whereas here exports will be reported:

```js
export let count = 2
export var count = 3
```

## When Not To Use It

If your environment correctly implements mutable export bindings.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const rules = {
'namespace': require('./rules/namespace'),
'no-namespace': require('./rules/no-namespace'),
'export': require('./rules/export'),
'no-mutable-exports': require('./rules/no-mutable-exports'),
'extensions': require('./rules/extensions'),

'no-named-as-default': require('./rules/no-named-as-default'),
Expand Down
10 changes: 10 additions & 0 deletions src/rules/no-mutable-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function (context) {
return {
'ExportNamedDeclaration': function (node) {
const kind = node.declaration.kind
if (kind === 'var' || kind === 'let') {
context.report(node, `Exporting mutable '${kind}' binding, use const instead.`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, could you wrap const in quotes too?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some odd indentation here, do you mind fixing it?

}
},
}
}
23 changes: 23 additions & 0 deletions tests/src/rules/no-mutable-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {test} from '../utils'
import {RuleTester} from 'eslint'
import rule from 'rules/no-mutable-exports'

const ruleTester = new RuleTester()

ruleTester.run('no-mutable-exports', rule, {
valid: [
test({ code: 'export const count = 1'}),
test({ code: 'export function getCount() {}'}),
test({ code: 'export class Counter {}'}),
],
invalid: [
test({
code: 'export let count = 1',
errors: ['Exporting mutable \'let\' binding, use const instead.'],
}),
test({
code: 'export var count = 1',
errors: ['Exporting mutable \'var\' binding, use const instead.'],
}),
],
})