Skip to content

Commit

Permalink
Improve the fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 14, 2022
1 parent 48d6aa4 commit 03ba7c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
25 changes: 17 additions & 8 deletions rules/prefer-export-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,16 @@ function getFixFunction({
const importDeclaration = imported.declaration;
const sourceNode = importDeclaration.source;
const sourceValue = sourceNode.value;
const exportDeclaration = exportDeclarations.find(({source, exportKind}) => source.value === sourceValue && exportKind !== 'type');
const isTypeSpecifier = imported.isType || exported.isType;
let exportDeclaration;
if (isTypeSpecifier) {
// Prefer put type specifier into a type export declaration, so we don't need add `type ` before specifier
exportDeclaration = exportDeclarations.find(({source, exportKind}) => source.value === sourceValue && exportKind === 'type');
}

if (!exportDeclaration) {
exportDeclaration = exportDeclarations.find(({source, exportKind}) => source.value === sourceValue && exportKind !== 'type');
}

/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
Expand All @@ -123,26 +131,28 @@ function getFixFunction({
`\nexport * as ${exported.text} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
} else {
const specifier = exported.name === imported.name
let specifierText = exported.name === imported.name
? exported.text
: `${imported.text} as ${exported.text}`;

This comment was marked as duplicate.

Copy link
@fisker

fisker Feb 14, 2022

Author Collaborator

This line seems not tested. The following should cover it.

import {type foo} from 'foo';
export {type foo as bar};
import {type foo} from 'foo';
export {type foo as bar};
export {type bar} from 'foo';

const specifierWithKind = isTypeSpecifier ? `type ${specifier}` : specifier;

if (exportDeclaration) {
const lastSpecifier = exportDeclaration.specifiers[exportDeclaration.specifiers.length - 1];

if (isTypeSpecifier && exportDeclaration.exportKind !== 'type') {
specifierText = `type ${specifierText}`;
}

// `export {} from 'foo';`
if (lastSpecifier) {
yield fixer.insertTextAfter(lastSpecifier, `, ${specifierWithKind}`);
yield fixer.insertTextAfter(lastSpecifier, `, ${specifierText}`);
} else {
const openingBraceToken = sourceCode.getFirstToken(exportDeclaration, isOpeningBraceToken);
yield fixer.insertTextAfter(openingBraceToken, specifierWithKind);
yield fixer.insertTextAfter(openingBraceToken, specifierText);
}
} else {
yield fixer.insertTextAfter(
program,
`\nexport {${specifierWithKind}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
`\nexport ${isTypeSpecifier ? 'type ' : ''}{${specifierText}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
}
}
Expand All @@ -157,7 +167,6 @@ function getFixFunction({

function getExported(identifier, context, sourceCode) {
const {parent} = identifier;
const isType = parent.exportKind === 'type' || (Boolean(parent.parent) && parent.parent.exportKind === 'type');
switch (parent.type) {
case 'ExportDefaultDeclaration':
return {
Expand Down
26 changes: 7 additions & 19 deletions test/prefer-export-from.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ test.typescript({
type AceEditor = import("ace-builds").Ace.Editor;
export {AceEditor};
`,
'export type { bar, foo } from "foo";',
],
invalid: [
{
Expand Down Expand Up @@ -402,19 +403,6 @@ test.typescript({
export { baz } from "foo";
`,
},
{
code: outdent`
import type { foo } from "foo";
export type { foo };
export type { bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export type { bar } from "foo";
export {type foo} from "foo";
`,
},
{
code: outdent`
import type { foo } from "foo";
Expand All @@ -437,8 +425,8 @@ test.typescript({
errors: 1,
output: outdent`
\n
export type { bar } from "foo";
export { baz, type foo } from "foo";
export type { bar, foo } from "foo";
export { baz } from "foo";
`,
},
{
Expand All @@ -463,7 +451,7 @@ test.typescript({
errors: 1,
output: outdent`
\n
export {type foo} from 'foo';
export type {foo} from 'foo';
`,
},
{
Expand All @@ -474,7 +462,7 @@ test.typescript({
errors: 1,
output: outdent`
\n
export {type foo} from 'foo';
export type {foo} from 'foo';
`,
},
{
Expand All @@ -485,7 +473,7 @@ test.typescript({
errors: 1,
output: outdent`
\n
export {type foo} from 'foo';
export type {foo} from 'foo';
`,
},
{
Expand All @@ -496,7 +484,7 @@ test.typescript({
errors: 1,
output: outdent`
\n
export {type default} from "foo";
export type {default} from "foo";
`,
},
],
Expand Down

0 comments on commit 03ba7c6

Please sign in to comment.