Skip to content

Commit

Permalink
Merge pull request #239 from singles/no-namespace-rule
Browse files Browse the repository at this point in the history
Add no-namespace rule
  • Loading branch information
benmosher committed Apr 12, 2016
2 parents de93a27 + 933d110 commit 6293364
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
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
- report resolver errors at the top of the linted file
- add `no-namespace` rule

## [1.4.0] - 2016-03-25
### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ Style guide:
* Report AMD `require` and `define` calls. ([`no-amd`])
* Ensure all imports appear before other statements ([`imports-first`])
* Report repeated import of the same module in multiple places ([`no-duplicates`])
* Report namespace imports ([`no-namespace`])

[`no-commonjs`]: ./docs/rules/no-commonjs.md
[`no-amd`]: ./docs/rules/no-amd.md
[`imports-first`]: ./docs/rules/imports-first.md
[`no-duplicates`]: ./docs/rules/no-duplicates.md
[`no-namespace`]: ./docs/rules/no-namespace.md

Work in progress:

Expand Down
24 changes: 24 additions & 0 deletions docs/rules/no-namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# no-namespace

Reports if namespace import is used.

## Rule Details

Valid:

```js
import defaultExport from './foo'
import { a, b } from './bar'
import defaultExport, { a, b } from './foobar'
```

...whereas here imports will be reported:

```js
import * as foo from 'foo';
import defaultExport, * as foo from 'foo';
```

## When Not To Use It

If you want to use namespaces, you don't want to use this rule.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const rules = {
'named': require('./rules/named'),
'default': require('./rules/default'),
'namespace': require('./rules/namespace'),
'no-namespace': require('./rules/no-namespace'),
'export': require('./rules/export'),

'no-named-as-default': require('./rules/no-named-as-default'),
Expand Down
17 changes: 17 additions & 0 deletions src/rules/no-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @fileoverview Rule to disallow namespace import
* @author Radek Benkel
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------


module.exports = function (context) {
return {
'ImportNamespaceSpecifier': function (node) {
context.report(node, `Unexpected namespace import.`)
},
}
}
44 changes: 44 additions & 0 deletions tests/src/rules/no-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RuleTester } from 'eslint'

const ERROR_MESSAGE = 'Unexpected namespace import.';

const ruleTester = new RuleTester()

ruleTester.run('no-namespace', require('rules/no-namespace'), {
valid: [
{ code: "import { a, b } from 'foo';", parserOptions: { sourceType: 'module' } },
{ code: "import { a, b } from './foo';", parserOptions: { sourceType: 'module' } },
{ code: "import bar from 'bar';", parserOptions: { sourceType: 'module' } },
{ code: "import bar from './bar';", parserOptions: { sourceType: 'module' } }
],

invalid: [
{
code: "import * as foo from 'foo';",
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE
} ],
parserOptions: { sourceType: 'module' }
},
{
code: "import defaultExport, * as foo from 'foo';",
errors: [ {
line: 1,
column: 23,
message: ERROR_MESSAGE
} ],
parserOptions: { sourceType: 'module' }
},
{
code: "import * as foo from './foo';",
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE
} ],
parserOptions: { sourceType: 'module' }
}
]
});

0 comments on commit 6293364

Please sign in to comment.