Skip to content

Commit

Permalink
add allowComputed option to namespace (fixes #456)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jul 26, 2016
1 parent 2252874 commit d8064da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Added
- `allowComputed` option for [`namespace`] rule. If set to `true`, won't report
computed member references to namespaces. (see [#456])

## [1.12.0] - 2016-07-26
### Added
Expand Down Expand Up @@ -304,6 +307,7 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
[#441]: https://github.com/benmosher/eslint-plugin-import/issues/441
[#423]: https://github.com/benmosher/eslint-plugin-import/issues/423
[#415]: https://github.com/benmosher/eslint-plugin-import/issues/415
Expand Down
18 changes: 18 additions & 0 deletions docs/rules/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ function deepTrouble() {

```

### Options

#### `allowComputed`

Defaults to `false`. When false, will report the following:

```js
/*eslint import/namespace: [2, { allowComputed: false }]*/
import * as a from './a'

function f(x) {
return a[x] // Unable to validate computed reference to imported namespace 'a'.
}
```

When set to `true`, the above computed namespace member reference is allowed, but
still can't be statically analyzed any further.

## Further Reading

- Lee Byron's [ES7] export proposal
Expand Down
33 changes: 29 additions & 4 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@ import Exports from '../core/getExports'
import importDeclaration from '../importDeclaration'
import declaredScope from '../core/declaredScope'

module.exports = function (context) {
exports.meta = {
schema: [
{
'type': 'object',
'properties': {
'allowComputed': {
'description':
'If `false`, will report computed (and thus, un-lintable) references ' +
'to namespace members.',
'type': 'boolean',
'default': false,
},
},
'additionalProperties': false,
},
],
}

exports.create = function namespaceRule(context) {

// read options
const {
allowComputed = false,
} = context.options[0] || {}

const namespaces = new Map()

Expand Down Expand Up @@ -93,9 +116,11 @@ module.exports = function (context) {
dereference.type === 'MemberExpression') {

if (dereference.computed) {
context.report(dereference.property,
'Unable to validate computed reference to imported namespace \'' +
dereference.object.name + '\'.')
if (!allowComputed) {
context.report(dereference.property,
'Unable to validate computed reference to imported namespace \'' +
dereference.object.name + '\'.')
}
return
}

Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ const valid = [
parser: 'babel-eslint',
}),

// #456: optionally ignore computed references
test({
code: `import * as names from './named-exports'; console.log(names['a']);`,
options: [{ allowComputed: true }],
}),

...SYNTAX_CASES,
]

Expand Down

0 comments on commit d8064da

Please sign in to comment.