Skip to content
Open
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
7 changes: 4 additions & 3 deletions rules/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ function getFixFunction({
if (imported.name === NAMESPACE_SPECIFIER_NAME) {
yield fixer.insertTextAfter(
program,
`\nexport * as ${exported.text} ${getSourceAndAssertionsText(importDeclaration, context)}`,
`\nexport ${shouldExportAsType ? 'type ' : ''}* as ${exported.text} ${getSourceAndAssertionsText(importDeclaration, context)}`,
);
} else {
let specifierText = exported.name === imported.name
? exported.text
: `${imported.text} as ${exported.text}`;

// Add an inline type specifier if the value is a type and the export deceleration is a value deceleration
// Add an inline type specifier if the value is a type and the export declaration is a value declaration
if (shouldExportAsType && (!exportDeclaration || exportDeclaration.exportKind !== 'type')) {
specifierText = `type ${specifierText}`;
}
Expand All @@ -116,7 +116,8 @@ function getFixFunction({
}
}

if (imported.variable.references.length === 1) {
const importHasSideEffects = exported.isTypeExport && !imported.isTypeImport;
if (imported.variable.references.length === 1 && !importHasSideEffects) {
yield removeImportOrExport(imported.node, fixer, context);
}

Expand Down
16 changes: 16 additions & 0 deletions test/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,22 @@ test.snapshot({
export {foo};
export {bar} from './foo.json' assert { type: 'unknown' };
`,
outdent`
import type * as X from 'foo';
export { X };
`,
outdent`
import * as X from 'foo';
export type { X };
`,
outdent`
import type * as X from 'foo';
export type { X };
`,
outdent`
import * as X from 'foo';
export { X };
`,
],
});

Expand Down
94 changes: 93 additions & 1 deletion test/snapshots/prefer-export-from.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^ Use \`export…from\` to re-export \`foo\`.␊
Output:␊
1 |␊
1 | import { foo } from 'foo';
2 |␊
3 | export {type foo} from 'foo';␊
`
Expand Down Expand Up @@ -2158,6 +2158,98 @@ Generated by [AVA](https://avajs.dev).
3 | export {bar, foo} from './foo.json' assert { type: 'unknown' };␊
`

## invalid(26): import type * as X from 'foo'; export { X };

> Input

`␊
1 | import type * as X from 'foo';␊
2 | export { X };␊
`

> Error 1/1

`␊
Message:␊
1 | import type * as X from 'foo';␊
> 2 | export { X };␊
| ^ Use \`export…from\` to re-export \`X\`.␊
Output:␊
1 |␊
2 |␊
3 | export type * as X from 'foo';␊
`

## invalid(27): import * as X from 'foo'; export type { X };

> Input

`␊
1 | import * as X from 'foo';␊
2 | export type { X };␊
`

> Error 1/1

`␊
Message:␊
1 | import * as X from 'foo';␊
> 2 | export type { X };␊
| ^ Use \`export…from\` to re-export \`X\`.␊
Output:␊
1 | import * as X from 'foo';␊
2 |␊
3 | export type * as X from 'foo';␊
`

## invalid(28): import type * as X from 'foo'; export type { X };

> Input

`␊
1 | import type * as X from 'foo';␊
2 | export type { X };␊
`

> Error 1/1

`␊
Message:␊
1 | import type * as X from 'foo';␊
> 2 | export type { X };␊
| ^ Use \`export…from\` to re-export \`X\`.␊
Output:␊
1 |␊
2 |␊
3 | export type * as X from 'foo';␊
`

## invalid(29): import * as X from 'foo'; export { X };

> Input

`␊
1 | import * as X from 'foo';␊
2 | export { X };␊
`

> Error 1/1

`␊
Message:␊
1 | import * as X from 'foo';␊
> 2 | export { X };␊
| ^ Use \`export…from\` to re-export \`X\`.␊
Output:␊
1 |␊
2 |␊
3 | export * as X from 'foo';␊
`

## invalid(1): import json from './foo.json' with { type: 'json' }; export default json;

> Input
Expand Down
Binary file modified test/snapshots/prefer-export-from.js.snap
Binary file not shown.
Loading