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-namespace rule #239

Merged
merged 1 commit into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }
}
]
});