Skip to content

Commit

Permalink
Add no-mutable-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 28, 2016
1 parent 06e005d commit 48bdc81
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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

This comment has been minimized.

Copy link
@benmosher

benmosher Apr 28, 2016

Member

Spec allows declaration to be null. IIRC it should be for export { x as y }.

Can you add a valid test case for this and fix if needed?

if (kind === 'var' || kind === 'let') {
context.report(node, `Exporting mutable '${kind}' binding, use const instead.`)
}
},
}
}
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.'],
}),
],
})

0 comments on commit 48bdc81

Please sign in to comment.