Skip to content
Closed
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
22 changes: 22 additions & 0 deletions docs/rules/no-namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Valid:
import defaultExport from './foo'
import { a, b } from './bar'
import defaultExport, { a, b } from './foobar'

// { allow: ['foo'] }
import * as foo from 'foo';

// { forbid: ['foo'] }
import * as bar from 'bar';
```

Invalid:
Expand All @@ -25,6 +31,22 @@ import * as foo from 'foo';
import defaultExport, * as foo from 'foo';
```

```js
// { allow: ['foo'] }
import * as bar from 'bar';

// { forbid: ['foo'] }
import * as foo from 'foo';
```

### Options

This rule takes the following options:

`allow`: an array of namespaces the rule allows
Copy link
Member

Choose a reason for hiding this comment

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

i assume this should cover specifiers, not the binding names?

(having the binding name and the specifier be the same in most of the examples make it unclear to which this applies)

Copy link
Author

@el-angel el-angel Mar 9, 2020

Choose a reason for hiding this comment

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

the allow option works as follows:

// { allow: ['myModule', 'React'] }
import * as React from 'react';
import * as myModule from '../module/randomFile.js';


`forbid`: an array of namespaces the rule forbids

## When Not To Use It

If you want to use namespaces, you don't want to use this rule.
49 changes: 48 additions & 1 deletion src/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,30 @@ module.exports = {
url: docsUrl('no-namespace'),
},
fixable: 'code',
schema: [],
schema: [
{
oneOf: [
{
type: "object",
properties: {
allow: {
type: "array"
}
},
additionalProperties: false
},
{
type: "object",
properties: {
forbid: {
type: "array"
}
},
additionalProperties: false
}
]
}
]
},

create: function (context) {
Expand All @@ -31,6 +54,16 @@ module.exports = {
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier)
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers)

const config = context.options[0] || {
allow: [],
};

const [type, list] = Array.isArray(config.forbid)
? ['forbid', config.forbid]
: ['allow', config.allow];

if (!shouldReport(type, list, namespaceVariable.name)) { return; }

context.report({
node,
message: `Unexpected namespace import.`,
Expand Down Expand Up @@ -157,3 +190,17 @@ function generateLocalNames(names, nameConflicts, namespaceName) {
})
return localNames
}

/**
*
* @param {'forbid' | 'allow'} type
* @param {string[]} list
* @param {string} name
*/
function shouldReport(type, list, name) {
if (list.includes(name)) {
return type === 'forbid';
}

return type === 'allow';
}
30 changes: 30 additions & 0 deletions tests/src/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ ruleTester.run('no-namespace', require('rules/no-namespace'), {
{ code: 'import { a, b } from \'./foo\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import bar from \'./bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import * as bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{ allow: ['bar']}] },
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{ forbid: ['bar']}] },
],

invalid: [
Expand Down Expand Up @@ -108,6 +110,34 @@ ruleTester.run('no-namespace', require('rules/no-namespace'), {
message: ERROR_MESSAGE,
} ],
}),
test({
code: 'import * as foo from \'./foo\';',
output: 'import * as foo from \'./foo\';',
options: [
{
forbid: ['foo']
}
],
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE,
} ],
}),
test({
code: 'import * as foo from \'./foo\';',
output: 'import * as foo from \'./foo\';',
options: [
{
allow: ['bar']
}
],
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE,
} ],
}),
...FIX_TESTS,
],
})