-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from singles/no-namespace-rule
Add no-namespace rule
- Loading branch information
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
} | ||
] | ||
}); |