Skip to content

Commit

Permalink
Fix: Ignore re-export all in no-duplicate-imports (fixes #12760)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jan 10, 2020
1 parent a1d999c commit 8ed3115
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
26 changes: 24 additions & 2 deletions docs/rules/no-duplicate-imports.md
Expand Up @@ -37,14 +37,16 @@ import something from 'another-module';

This rule takes one optional argument, an object with a single key, `includeExports` which is a `boolean`. It defaults to `false`.

If re-exporting from an imported module, you should add the imports to the `import`-statement, and export that directly, not use `export ... from`.
### includeExports

If the `includeExports` option is set to `true`, this rule will also require that all named re-exports from a single module exist in a single `export` statement.

Example of **incorrect** code for this rule with the `{ "includeExports": true }` option:

```js
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge } from 'module';
export { merge } from 'module';

export { find } from 'module';
```
Expand All @@ -54,6 +56,26 @@ Example of **correct** code for this rule with the `{ "includeExports": true }`
```js
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

export { merge, find } from 'module';
```

Additionally, if re-exporting from an imported module, you should add the imports to the `import`-statement, and export that directly, not use `export ... from`.

Example of additional **incorrect** code for this rule with the `{ "includeExports": true }` option:

```js
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge } from 'module';

export { find } from 'module';
```

Example of additional **correct** code for this rule with the `{ "includeExports": true }` option:

```js
/*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/

import { merge, find } from 'module';

export { find };
Expand Down
1 change: 0 additions & 1 deletion lib/rules/no-duplicate-imports.js
Expand Up @@ -134,7 +134,6 @@ module.exports = {

if (includeExports) {
handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);
}

return handlers;
Expand Down
32 changes: 30 additions & 2 deletions tests/lib/rules/no-duplicate-imports.js
Expand Up @@ -45,6 +45,34 @@ ruleTester.run("no-duplicate-imports", rule, {
{
code: "import { merge } from \"lodash-es\";\nexport { merge as lodashMerge }",
options: [{ includeExports: true }]
},

// ignore `export * from` declarations, they cannot be merged with any other import/export declarations
{
code: "import os from 'os'; export * from 'os';",
options: [{ includeExports: true }]
},
{
code: "export * from 'os'; import { a } from 'os';",
options: [{ includeExports: true }]
},
{
code: "import * as ns from 'os'; export * from 'os';",
options: [{ includeExports: true }]
},
{
code: "export * from 'os'; export { a } from 'os';",
options: [{ includeExports: true }]
},
{
code: "export { a as b } from 'os'; export * from 'os';",
options: [{ includeExports: true }]
},

// this is probably an exporting error, but that isn't a responsibility of this rule
{
code: "export * from 'os'; export * from 'os';",
options: [{ includeExports: true }]
}
],
invalid: [
Expand Down Expand Up @@ -80,9 +108,9 @@ ruleTester.run("no-duplicate-imports", rule, {
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }]
},
{
code: "import os from \"os\";\nexport * from \"os\";",
code: "import os from 'os'; export * from 'os'; export { a } from 'os';",
options: [{ includeExports: true }],
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportAllDeclaration" }]
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }]
}
]
});

0 comments on commit 8ed3115

Please sign in to comment.