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 4 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
12 changes: 8 additions & 4 deletions rules/prefer-export-from.js
Expand Up @@ -109,6 +109,8 @@ function getFixFunction({
const sourceNode = importDeclaration.source;
const sourceValue = sourceNode.value;
const exportDeclaration = exportDeclarations.find(({source}) => source.value === sourceValue);
const isTypeExport = exported.node.parent.exportKind === 'type';
nrgnrg marked this conversation as resolved.
Show resolved Hide resolved
const isTypeExportDeclaration = exportDeclaration && exportDeclaration.exportKind === 'type';
nrgnrg marked this conversation as resolved.
Show resolved Hide resolved

/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
Expand All @@ -122,20 +124,22 @@ function getFixFunction({
? exported.text
: `${imported.text} as ${exported.text}`;
nrgnrg marked this conversation as resolved.
Show resolved Hide resolved

if (exportDeclaration) {
const specifierWithKind = isTypeExport ? `type ${specifier}` : specifier;
nrgnrg marked this conversation as resolved.
Show resolved Hide resolved

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

// `export {} from 'foo';`
if (lastSpecifier) {
yield fixer.insertTextAfter(lastSpecifier, `, ${specifier}`);
yield fixer.insertTextAfter(lastSpecifier, `, ${specifierWithKind}`);
} else {
const openingBraceToken = sourceCode.getFirstToken(exportDeclaration, isOpeningBraceToken);
yield fixer.insertTextAfter(openingBraceToken, specifier);
yield fixer.insertTextAfter(openingBraceToken, specifierWithKind);
}
} else {
yield fixer.insertTextAfter(
program,
`\nexport {${specifier}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
`\nexport {${specifierWithKind}} ${getSourceAndAssertionsText(importDeclaration, sourceCode)}`,
);
}
}
Expand Down
16 changes: 15 additions & 1 deletion test/prefer-export-from.mjs
Expand Up @@ -348,7 +348,21 @@ test.typescript({
export {AceEditor};
`,
],
invalid: [],
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";
`,
},
],
});

// `ignoreUsedVariables`
Expand Down