diff --git a/rules/prefer-export-from.js b/rules/prefer-export-from.js index a8779c8407..f90c3a863b 100644 --- a/rules/prefer-export-from.js +++ b/rules/prefer-export-from.js @@ -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}`; } @@ -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); } diff --git a/test/prefer-export-from.js b/test/prefer-export-from.js index c5aa7a8f24..e6419d2588 100644 --- a/test/prefer-export-from.js +++ b/test/prefer-export-from.js @@ -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 }; + `, ], }); diff --git a/test/snapshots/prefer-export-from.js.md b/test/snapshots/prefer-export-from.js.md index 951019443d..301101dcf5 100644 --- a/test/snapshots/prefer-export-from.js.md +++ b/test/snapshots/prefer-export-from.js.md @@ -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';␊ ` @@ -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 diff --git a/test/snapshots/prefer-export-from.js.snap b/test/snapshots/prefer-export-from.js.snap index 9677a555af..8ff4d90af6 100644 Binary files a/test/snapshots/prefer-export-from.js.snap and b/test/snapshots/prefer-export-from.js.snap differ