Skip to content

Commit

Permalink
Add no-namespace rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Radek Benkel authored and Radek Benkel committed Apr 8, 2016
1 parent de93a27 commit 933d110
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' }
}
]
});

2 comments on commit 933d110

@gajus
Copy link
Contributor

@gajus gajus commented on 933d110 Apr 29, 2016

Choose a reason for hiding this comment

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

What is the reason for wanting this rule?

There are a plethora of cases where there simply is no other way to import all exports other then using asterisk-alias.

@radekbenkel
Copy link
Contributor

Choose a reason for hiding this comment

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

@gajus see discussion below PR #239

Also, rule is optional and it's usage depends on your code style :)

Could you please provide an example of mentioned cases, when you don't have other way? We looked into our code base and realised, that there's was no good reason to use asterisk-alias - hence the PR to enforce that on our side.

Please sign in to comment.