diff --git a/rules/prefer-export-from.js b/rules/prefer-export-from.js index 9b9e05eee5..b73d930281 100644 --- a/rules/prefer-export-from.js +++ b/rules/prefer-export-from.js @@ -26,12 +26,12 @@ const getSpecifierName = node => { } }; -function * removeSpecifier(node, fixer, sourceCode) { - const {parent} = node; - const {specifiers} = parent; +function* removeSpecifier(node, fixer, sourceCode) { + const { parent } = node; + const { specifiers } = parent; if (specifiers.length === 1) { - yield * removeImportOrExport(parent, fixer, sourceCode); + yield* removeImportOrExport(parent, fixer, sourceCode); return; } @@ -68,13 +68,13 @@ function * removeSpecifier(node, fixer, sourceCode) { } } -function * removeImportOrExport(node, fixer, sourceCode) { +function* removeImportOrExport(node, fixer, sourceCode) { switch (node.type) { case 'ImportSpecifier': case 'ExportSpecifier': case 'ImportDefaultSpecifier': case 'ImportNamespaceSpecifier': { - yield * removeSpecifier(node, fixer, sourceCode); + yield* removeSpecifier(node, fixer, sourceCode); return; } @@ -108,11 +108,11 @@ 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 exportDeclaration = exportDeclarations.find(({ source, exportKind }) => source.value === sourceValue && exportKind !== 'type'); const isTypeExport = exported.node.exportKind === 'type' || exported.exportKind === 'type'; /** @param {import('eslint').Rule.RuleFixer} fixer */ - return function * (fixer) { + return function* (fixer) { if (imported.name === NAMESPACE_SPECIFIER_NAME) { yield fixer.insertTextAfter( program, @@ -144,15 +144,15 @@ function getFixFunction({ } if (imported.variable.references.length === 1) { - yield * removeImportOrExport(imported.node, fixer, sourceCode); + yield* removeImportOrExport(imported.node, fixer, sourceCode); } - yield * removeImportOrExport(exported.node, fixer, sourceCode); + yield* removeImportOrExport(exported.node, fixer, sourceCode); }; } function getExported(identifier, context, sourceCode) { - const {parent} = identifier; + const { parent } = identifier; switch (parent.type) { case 'ExportDefaultDeclaration': return { @@ -202,7 +202,7 @@ function isVariableUnused(node, context) { return false; } - const [{identifiers, references}] = variables; + const [{ identifiers, references }] = variables; return identifiers.length === 1 && identifiers[0] === node.id && references.length === 1 @@ -245,14 +245,13 @@ function getImported(variable, sourceCode) { function getExports(imported, context, sourceCode) { const exports = []; - for (const {identifier} of imported.variable.references) { + for (const { identifier } of imported.variable.references) { const exported = getExported(identifier, context, sourceCode); if (!exported) { continue; } - // Console.log("getExports", exported) /* There is no substitution for: @@ -287,7 +286,7 @@ const schema = [ /** @param {import('eslint').Rule.RuleContext} context */ function create(context) { const sourceCode = context.getSourceCode(); - const {ignoreUsedVariables} = {ignoreUsedVariables: false, ...context.options[0]}; + const { ignoreUsedVariables } = { ignoreUsedVariables: false, ...context.options[0] }; const importDeclarations = new Set(); const exportDeclarations = []; @@ -309,7 +308,6 @@ function create(context) { variables = variables.map(variable => { const imported = getImported(variable, sourceCode); - // Console.log("getImported", imported) const exports = getExports(imported, context, sourceCode); return { @@ -321,15 +319,15 @@ function create(context) { if ( ignoreUsedVariables - && variables.some(({variable, exports}) => variable.references.length !== exports.length) + && variables.some(({ variable, exports }) => variable.references.length !== exports.length) ) { continue; } const shouldUseSuggestion = ignoreUsedVariables - && variables.some(({variable}) => variable.references.length === 0); + && variables.some(({ variable }) => variable.references.length === 0); - for (const {imported, exports} of variables) { + for (const { imported, exports } of variables) { for (const exported of exports) { const problem = { node: exported.node, diff --git a/test/prefer-export-from.mjs b/test/prefer-export-from.mjs index 0519ad6ba8..a69ed44862 100644 --- a/test/prefer-export-from.mjs +++ b/test/prefer-export-from.mjs @@ -362,6 +362,46 @@ test.typescript({ 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"; + `, + }, ], });