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
5 changes: 3 additions & 2 deletions rules/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ function getFixFunction({
/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
if (imported.name === NAMESPACE_SPECIFIER_NAME) {
const maybeType = imported.isTypeImport ? 'type ' : '';
yield fixer.insertTextAfter(
program,
`\nexport * as ${exported.text} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
`\nexport ${maybeType}* as ${exported.text} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
} 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 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
100 changes: 100 additions & 0 deletions test/snapshots/prefer-export-from.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,106 @@ Generated by [AVA](https://avajs.dev).
3 | export {bar} 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 };␊
`

> Output

`␊
1 |␊
2 |␊
3 | export type * as X from 'foo';␊
`

> Error 1/1

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

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

> Input

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

> Output

`␊
1 |␊
2 |␊
3 | export * as X from 'foo';␊
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this also change the behavior, it only expose type before, now it expose values. Maybe we can export type but not remove the import.

`

> Error 1/1

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

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

> Input

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

> Output

`␊
1 |␊
2 |␊
3 | export type * as X from 'foo';␊
`

> Error 1/1

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

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

> Input

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

> Output

`␊
1 |␊
2 |␊
3 | export * as X from 'foo';␊
`

> Error 1/1

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

## 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