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

[New] no-rename-default: Forbid importing a default export by a different name #3006

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`dynamic-import-chunkname`]: Allow empty chunk name when webpackMode: 'eager' is set; add suggestions to remove name in eager mode ([#3004], thanks [@amsardesai])
- [`no-unused-modules`]: Add `ignoreUnusedTypeExports` option ([#3011], thanks [@silverwind])
- add support for Flat Config ([#3018], thanks [@michaelfaith])
- [`no-rename-default`]: Forbid importing a default export by a different name ([#3006], thanks [@whitneyit])

### Fixed
- [`no-extraneous-dependencies`]: allow wrong path ([#3012], thanks [@chabb])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a
| [no-mutable-exports](docs/rules/no-mutable-exports.md) | Forbid the use of mutable exports with `var` or `let`. | | | | | | |
| [no-named-as-default](docs/rules/no-named-as-default.md) | Forbid use of exported name as identifier of default export. | | ☑️ 🚸 | | | | |
| [no-named-as-default-member](docs/rules/no-named-as-default-member.md) | Forbid use of exported name as property of default export. | | ☑️ 🚸 | | | | |
| [no-rename-default](docs/rules/no-rename-default.md) | Forbid importing a default export by a different name. | | | | | | |
| [no-unused-modules](docs/rules/no-unused-modules.md) | Forbid modules without exports, or exports without matching import in another module. | | | | | | |

### Module systems
Expand Down
29 changes: 29 additions & 0 deletions docs/rules/no-rename-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# import/no-rename-default

<!-- end auto-generated rule header -->

Prohibit importing a default export by another name.

## Rule Details

Given:

```js
// api/get-users.js
export default async function getUsers() {}
```

...this would be valid:

```js
import getUsers from './api/get-users.js';
```

...and the following would be reported:

```js
// Caution: `get-users.js` has a default export `getUsers`.
// This imports `getUsers` as `findUsers`.
// Check if you meant to write `import getUsers from './api/get-users'` instead.
import findUsers from './get-users';
```
1 change: 1 addition & 0 deletions src/exportMap/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export default class ExportMapBuilder {
&& exportMap.namespace.size > 0 // anything is exported
&& !exportMap.namespace.has('default') // and default isn't added already
) {
exportMap.exports.set('default', {});
exportMap.namespace.set('default', {}); // add default export
}

Expand Down
1 change: 1 addition & 0 deletions src/exportMap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class ExportMap {
* @type {Map<string, () => ExportMap>}
*/
this.imports = new Map();
this.exports = new Map();
this.errors = [];
/**
* type {'ambiguous' | 'Module' | 'Script'}
Expand Down
3 changes: 3 additions & 0 deletions src/exportMap/specifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ export default function processSpecifier(specifier, astNode, exportMap, namespac
local = 'default';
break;
case 'ExportNamespaceSpecifier':
exportMap.exports.set(specifier.exported.name, astNode);
exportMap.namespace.set(specifier.exported.name, Object.defineProperty(exportMeta, 'namespace', {
get() { return namespace.resolveImport(nsource); },
}));
return;
case 'ExportAllDeclaration':
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.source.value));
return;
case 'ExportSpecifier':
if (!astNode.source) {
exportMap.exports.set(specifier.exported.name || specifier.exported.value, astNode);
exportMap.namespace.set(specifier.exported.name || specifier.exported.value, namespace.add(exportMeta, specifier.local));
return;
}
Expand Down
25 changes: 16 additions & 9 deletions src/exportMap/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class ImportExportVisitorBuilder {
if (astNode.declaration.type === 'Identifier') {
this.namespace.add(exportMeta, astNode.declaration);
}
this.exportMap.exports.set('default', astNode);
this.exportMap.namespace.set('default', exportMeta);
},
ExportAllDeclaration() {
Expand Down Expand Up @@ -86,13 +87,17 @@ export default class ImportExportVisitorBuilder {
case 'TSInterfaceDeclaration':
case 'TSAbstractClassDeclaration':
case 'TSModuleDeclaration':
this.exportMap.exports.set(astNode.declaration.id.name, astNode);
this.exportMap.namespace.set(astNode.declaration.id.name, captureDoc(this.source, this.docStyleParsers, astNode));
break;
case 'VariableDeclaration':
astNode.declaration.declarations.forEach((d) => {
recursivePatternCapture(
d.id,
(id) => this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode)),
(id) => {
this.exportMap.exports.set(id.name, astNode);
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, d, astNode));
},
);
});
break;
Expand Down Expand Up @@ -126,18 +131,21 @@ export default class ImportExportVisitorBuilder {
));
if (exportedDecls.length === 0) {
// Export is not referencing any local declaration, must be re-exporting
this.exportMap.exports.set('default', astNode);
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, astNode));
return;
}
if (
this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
&& !this.exportMap.namespace.has('default') // and default isn't added already
) {
this.exportMap.exports.set('default', {}); // add default export
this.exportMap.namespace.set('default', {}); // add default export
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
this.exportMap.exports.set(decl.body.id.name, astNode);
this.exportMap.namespace.set(decl.body.id.name, captureDoc(this.source, this.docStyleParsers, decl.body));
} else if (decl.body && decl.body.body) {
decl.body.body.forEach((moduleBlockNode) => {
Expand All @@ -150,20 +158,19 @@ export default class ImportExportVisitorBuilder {
if (!namespaceDecl) {
// TypeScript can check this for us; we needn't
} else if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => this.exportMap.namespace.set(
id.name,
captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode),
)),
);
namespaceDecl.declarations.forEach((d) => recursivePatternCapture(d.id, (id) => {
this.exportMap.exports.set(id.name, astNode);
this.exportMap.namespace.set(id.name, captureDoc(this.source, this.docStyleParsers, decl, namespaceDecl, moduleBlockNode));
}));
} else {
this.exportMap.namespace.set(
namespaceDecl.id.name,
captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
this.exportMap.exports.set(namespaceDecl.id.name, astNode);
this.exportMap.namespace.set(namespaceDecl.id.name, captureDoc(this.source, this.docStyleParsers, moduleBlockNode));
}
});
}
} else {
// Export as default
this.exportMap.exports.set('default', {});
this.exportMap.namespace.set('default', captureDoc(this.source, this.docStyleParsers, decl));
}
});
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const rules = {
'no-named-as-default': require('./rules/no-named-as-default'),
'no-named-as-default-member': require('./rules/no-named-as-default-member'),
'no-anonymous-default-export': require('./rules/no-anonymous-default-export'),
'no-rename-default': require('./rules/no-rename-default'),
'no-unused-modules': require('./rules/no-unused-modules'),

'no-commonjs': require('./rules/no-commonjs'),
Expand Down
Loading
Loading