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

prefer-export-from: Fix TypeScript compatibility #1728

Merged
merged 22 commits into from Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions rules/prefer-export-from.js
Expand Up @@ -26,6 +26,10 @@ const getSpecifierName = node => {
}
};

const isTypeExportKind = node => node.exportKind === 'type' || node.parent.exportKind === 'type';

const isTypeImportKind = node => node.importKind === 'type' || node.parent.importKind === 'type';

function * removeSpecifier(node, fixer, sourceCode) {
const {parent} = node;
const {specifiers} = parent;
Expand Down Expand Up @@ -108,7 +112,16 @@ function getFixFunction({
const importDeclaration = imported.declaration;
const sourceNode = importDeclaration.source;
const sourceValue = sourceNode.value;
const exportDeclaration = exportDeclarations.find(({source}) => source.value === sourceValue);
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 @@ -118,24 +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}`;
nrgnrg marked this conversation as resolved.
Show resolved Hide resolved

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, `, ${specifier}`);
yield fixer.insertTextAfter(lastSpecifier, `, ${specifierText}`);
} else {
const openingBraceToken = sourceCode.getFirstToken(exportDeclaration, isOpeningBraceToken);
yield fixer.insertTextAfter(openingBraceToken, specifier);
yield fixer.insertTextAfter(openingBraceToken, specifierText);
}
} else {
yield fixer.insertTextAfter(
program,
`\nexport {${specifier}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
`\nexport ${isTypeSpecifier ? 'type ' : ''}{${specifierText}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
}
}
Expand All @@ -156,13 +173,15 @@ function getExported(identifier, context, sourceCode) {
node: parent,
name: DEFAULT_SPECIFIER_NAME,
text: 'default',
isType: isTypeExportKind(parent),
};

case 'ExportSpecifier':
return {
node: parent,
name: getSpecifierName(parent.exported),
text: sourceCode.getText(parent.exported),
isType: isTypeExportKind(parent),
};

case 'VariableDeclarator': {
Expand Down Expand Up @@ -212,6 +231,7 @@ function getImported(variable, sourceCode) {
node: specifier,
declaration: specifier.parent,
variable,
isType: isTypeImportKind(specifier),
};

switch (specifier.type) {
Expand Down
195 changes: 194 additions & 1 deletion test/prefer-export-from.mjs
Expand Up @@ -347,8 +347,201 @@ test.typescript({
type AceEditor = import("ace-builds").Ace.Editor;
export {AceEditor};
`,
'export type { bar, foo } from "foo";',
],
invalid: [
{
code: outdent`
import { foo } from "foo";
export { foo };
export type { bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export type { bar } from "foo";
export {foo} from "foo";
`,
},
{
code: outdent`
import { foo } from "foo";
export { foo };
export { type bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export { type bar, foo } from "foo";
`,
},
{
code: outdent`
import { foo } from 'foo';
export { foo };
export type { bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export type { bar } from "foo";
export { baz, foo } from "foo";
`,
},
{
code: outdent`
import { foo } from 'foo';
export { foo };
export { type bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export { type bar, foo } from "foo";
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, foo } from "foo";
`,
},
{
code: outdent`
import { foo } from 'foo';
export { foo };
export { baz } from "foo";
export { type bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export { baz, foo } from "foo";
export { type bar } 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, type foo } from "foo";
`,
},
{
code: outdent`
import type { foo } from 'foo';
export type { foo };
export type { bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export type { bar, foo } from "foo";
export { baz } from "foo";
`,
},
{
code: outdent`
import type { foo } from 'foo';
export type { foo };
export { baz } from "foo";
export type { bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export { baz } from "foo";
export type { bar, foo } from "foo";
`,
},
{
code: outdent`
import type { foo } from 'foo';
export type { foo };
export { type bar } from "foo";
export { baz } from "foo";
`,
errors: 1,
output: outdent`
\n
export { type bar, type foo } from "foo";
export { baz } from "foo";
`,
},
{
code: outdent`
import type { foo } from 'foo';
export type { foo };
export { baz } from "foo";
export { type bar } from "foo";
`,
errors: 1,
output: outdent`
\n
export { baz, type foo } from "foo";
export { type bar } from "foo";
`,
},
{
code: outdent`
import { type foo } from 'foo';
export type { foo };
`,
errors: 1,
output: outdent`
\n
export type {foo} from 'foo';
`,
},
{
code: outdent`
import { foo } from 'foo';
export type { foo };
`,
errors: 1,
output: outdent`
\n
export type {foo} from 'foo';
`,
},
{
code: outdent`
import type { foo } from 'foo';
export { type foo };
`,
errors: 1,
output: outdent`
\n
export type {foo} from 'foo';
`,
},
{
code: outdent`
import type foo from "foo";
export default foo
`,
errors: 1,
output: outdent`
\n
export type {default} from "foo";
`,
},
],
invalid: [],
});

// `ignoreUsedVariables`
Expand Down