Skip to content

Commit bba59c4

Browse files
Arkadii Berezkinljharb
Arkadii Berezkin
authored andcommittedJun 2, 2021
[New] no-namespace: Add ignore option
Closes #1916. Closes #1903.
1 parent 54d86c8 commit bba59c4

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed
 

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
99
### Added
1010
- [`no-dynamic-require`]: add option `esmodule` ([#1223], thanks [@vikr01])
1111
- [`named`]: add `commonjs` option ([#1222], thanks [@vikr01])
12+
- [`no-namespace`]: Add `ignore` option ([#2112], thanks [@aberezkin])
1213

1314
### Fixed
1415
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])
@@ -822,6 +823,7 @@ for info on changes for earlier releases.
822823
[#2146]: https://github.com/benmosher/eslint-plugin-import/pull/2146
823824
[#2138]: https://github.com/benmosher/eslint-plugin-import/pull/2138
824825
[#2121]: https://github.com/benmosher/eslint-plugin-import/pull/2121
826+
[#2112]: https://github.com/benmosher/eslint-plugin-import/pull/2112
825827
[#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099
826828
[#2097]: https://github.com/benmosher/eslint-plugin-import/pull/2097
827829
[#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090
@@ -1252,6 +1254,7 @@ for info on changes for earlier releases.
12521254
[@1pete]: https://github.com/1pete
12531255
[@3nuc]: https://github.com/3nuc
12541256
[@aamulumi]: https://github.com/aamulumi
1257+
[@aberezkin]: https://github.com/aberezkin
12551258
[@adamborowski]: https://github.com/adamborowski
12561259
[@adjerbetian]: https://github.com/adjerbetian
12571260
[@ai]: https://github.com/ai

‎docs/rules/no-namespace.md

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Enforce a convention of not using namespace (a.k.a. "wildcard" `*`) imports.
55
+(fixable) The `--fix` option on the [command line] automatically fixes problems reported by this rule, provided that the namespace object is only used for direct member access, e.g. `namespace.a`.
66
The `--fix` functionality for this rule requires ESLint 5 or newer.
77

8+
### Options
9+
10+
This rule supports the following options:
11+
12+
- `ignore`: array of glob strings for modules that should be ignored by the rule.
13+
814
## Rule Details
915

1016
Valid:
@@ -15,6 +21,11 @@ import { a, b } from './bar'
1521
import defaultExport, { a, b } from './foobar'
1622
```
1723

24+
```js
25+
/* eslint import/no-namespace: ["error", {ignore: ['*.ext']] */
Has a conversation. Original line has a conversation.
26+
import * as bar from './ignored-module.ext';
27+
```
28+
1829
Invalid:
1930

2031
```js

‎src/rules/no-namespace.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @author Radek Benkel
44
*/
55

6+
import minimatch from 'minimatch';
67
import docsUrl from '../docsUrl';
78

89
//------------------------------------------------------------------------------
@@ -17,16 +18,32 @@ module.exports = {
1718
url: docsUrl('no-namespace'),
1819
},
1920
fixable: 'code',
20-
schema: [],
21+
schema: [{
22+
type: 'object',
23+
properties: {
24+
ignore: {
25+
type: 'array',
26+
items: {
27+
type: 'string',
28+
},
29+
uniqueItems: true,
30+
},
31+
},
32+
}],
2133
},
2234

2335
create: function (context) {
36+
const firstOption = context.options[0] || {};
37+
const ignoreGlobs = firstOption.ignore;
38+
2439
return {
25-
'ImportNamespaceSpecifier': function (node) {
40+
ImportNamespaceSpecifier(node) {
41+
if (ignoreGlobs && ignoreGlobs.find(glob => minimatch(node.parent.source.value, glob, { matchBase: true }))) {
42+
return;
43+
}
44+
2645
const scopeVariables = context.getScope().variables;
27-
const namespaceVariable = scopeVariables.find((variable) =>
28-
variable.defs[0].node === node
29-
);
46+
const namespaceVariable = scopeVariables.find((variable) => variable.defs[0].node === node);
3047
const namespaceReferences = namespaceVariable.references;
3148
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier);
3249
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers);
@@ -63,11 +80,11 @@ module.exports = {
6380
);
6481

6582
// Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
66-
const namedImportSpecifiers = importNames.map((importName) =>
83+
const namedImportSpecifiers = importNames.map((importName) => (
6784
importName === importLocalNames[importName]
6885
? importName
6986
: `${importName} as ${importLocalNames[importName]}`
70-
);
87+
));
7188
fixes.push(fixer.replaceText(node, `{ ${namedImportSpecifiers.join(', ')} }`));
7289

7390
// Pass 2: Replace references to the namespace with references to the named imports

‎tests/src/rules/no-namespace.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ ruleTester.run('no-namespace', require('rules/no-namespace'), {
7878
{ code: 'import { a, b } from \'./foo\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
7979
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
8080
{ code: 'import bar from \'./bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
81+
{ code: 'import * as bar from \'./ignored-module.ext\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{ ignore: ['*.ext'] }] },
8182
],
8283

8384
invalid: [

0 commit comments

Comments
 (0)
Please sign in to comment.