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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Added
- [`newline-after-import`], new rule. ([#245], thanks [@singles])
- [`no-mutable-exports`], new rule. ([#290], thanks [@josh])

## resolvers/webpack/0.2.4 - 2016-04-29
### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
[`no-named-as-default-member`]: ./docs/rules/no-named-as-default-member.md
[`no-deprecated`]: ./docs/rules/no-deprecated.md
[`no-extraneous-dependencies`]: ./docs/rules/no-extraneous-dependencies.md
[`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md

**Module systems:**

Expand Down
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
45 changes: 45 additions & 0 deletions src/rules/no-mutable-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = function (context) {
function checkDeclaration(node) {
const {kind} = node
if (kind === 'var' || kind === 'let') {
context.report(node, `Exporting mutable '${kind}' binding, use 'const' instead.`)
}
}

function checkDeclarationsInScope({variables}, name) {
for (let variable of variables) {
if (variable.name === name) {
for (let def of variable.defs) {
if (def.type === 'Variable') {
checkDeclaration(def.parent)
}
}
}
}
}

function handleExportDefault(node) {
const scope = context.getScope()

if (node.declaration.name) {
checkDeclarationsInScope(scope, node.declaration.name)
}
}

function handleExportNamed(node) {
const scope = context.getScope()

if (node.declaration) {
checkDeclaration(node.declaration)
} else {
for (let specifier of node.specifiers) {
checkDeclarationsInScope(scope, specifier.local.name)
}
}
}

return {
'ExportDefaultDeclaration': handleExportDefault,
'ExportNamedDeclaration': handleExportNamed,
}
}
62 changes: 62 additions & 0 deletions tests/src/rules/no-mutable-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 {}'}),
test({ code: 'export default count = 1'}),
test({ code: 'export default function getCount() {}'}),
test({ code: 'export default class Counter {}'}),
test({ code: 'const count = 1\nexport { count }'}),
test({ code: 'const count = 1\nexport { count as counter }'}),
test({ code: 'const count = 1\nexport default count'}),
test({ code: 'const count = 1\nexport { count as default }'}),
test({ code: 'function getCount() {}\nexport { getCount }'}),
test({ code: 'function getCount() {}\nexport { getCount as getCounter }'}),
test({ code: 'function getCount() {}\nexport default getCount'}),
test({ code: 'function getCount() {}\nexport { getCount as default }'}),
test({ code: 'class Counter {}\nexport { Counter }'}),
test({ code: 'class Counter {}\nexport { Counter as Count }'}),
test({ code: 'class Counter {}\nexport default Counter'}),
test({ code: 'class Counter {}\nexport { Counter as default }'}),
],
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.'],
}),
test({
code: 'let count = 1\nexport { count }',
errors: ['Exporting mutable \'let\' binding, use \'const\' instead.'],
}),
test({
code: 'var count = 1\nexport { count }',
errors: ['Exporting mutable \'var\' binding, use \'const\' instead.'],
}),
test({
code: 'let count = 1\nexport { count as counter }',
errors: ['Exporting mutable \'let\' binding, use \'const\' instead.'],
}),
test({
code: 'var count = 1\nexport { count as counter }',
errors: ['Exporting mutable \'var\' binding, use \'const\' instead.'],
}),
test({
code: 'let count = 1\nexport default count',
errors: ['Exporting mutable \'let\' binding, use \'const\' instead.'],
}),
test({
code: 'var count = 1\nexport default count',
errors: ['Exporting mutable \'var\' binding, use \'const\' instead.'],
}),
],
})